root/trunk/llmozlib2/llembeddedbrowser.cpp

Revision 15, 12.3 kB (checked in by callum.linden, 1 year ago)

Updates before new site released. Minor changes to LLMozLib and some new bookmarks for uBrowser.
Reviewed by cricket ball

  • Property svn:eol-style set to native
Line 
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Linden Lab Inc. (http://lindenlab.com) code.
15  *
16  * The Initial Developer of the Original Code is:
17  *   Callum Prentice (callum@ubrowser.com)
18  *
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *  Callum Prentice (callum@ubrowser.com)
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 // Windows specific switches
40 #ifdef WIN32
41         // appears to be required by LibXUL/Mozilla code to avoid crashes in debug versions of their code (undef'd at end of this file)
42         #ifdef _DEBUG
43                 #define DEBUG 1
44         #endif
45 #endif  // WIN32
46
47 #include <iostream>
48
49 #include "llembeddedbrowser.h"
50 #include "llembeddedbrowserwindow.h"
51
52 #ifdef WIN32
53         #pragma warning( disable : 4265 )       // "class has virtual functions, but destructor is not virtual"
54         #pragma warning( disable : 4291 )       // (no matching operator delete found; memory will not be freed if initialization throws an exception)
55 #endif  // WIN32
56
57 #include "nsBuildID.h"
58 #include "nsICacheService.h"
59 #include "nsICookieManager.h"
60 #include "nsIPref.h"
61 #include "nsNetCID.h"
62 #include "nsProfileDirServiceProvider.h"
63 #include "nsXULAppAPI.h"
64 #include "nsIAppShell.h"
65 #include "nsIPromptService.h"
66 #include "time.h"
67 #include "nsWidgetsCID.h"
68 #include "nsNetCID.h"
69
70 static nsIAppShell *sAppShell = nsnull;
71
72 // singleton pattern - initialization
73 LLEmbeddedBrowser* LLEmbeddedBrowser::sInstance = 0;
74
75 ////////////////////////////////////////////////////////////////////////////////
76 //
77 LLEmbeddedBrowser::LLEmbeddedBrowser() :
78         mErrorNum( 0 ),
79         mNativeWindowHandle( 0 )
80 {
81 }
82
83 ////////////////////////////////////////////////////////////////////////////////
84 //
85 LLEmbeddedBrowser::~LLEmbeddedBrowser()
86 {
87 }
88
89 ////////////////////////////////////////////////////////////////////////////////
90 //
91 LLEmbeddedBrowser* LLEmbeddedBrowser::getInstance()
92 {
93     if ( ! sInstance )
94     {
95         sInstance = new LLEmbeddedBrowser;
96     };
97
98         return sInstance;
99 };
100
101 ////////////////////////////////////////////////////////////////////////////////
102 //
103 void LLEmbeddedBrowser::setLastError( int errorNumIn )
104 {
105         mErrorNum = errorNumIn;
106 }
107
108 ////////////////////////////////////////////////////////////////////////////////
109 //
110 void LLEmbeddedBrowser::clearLastError()
111 {
112         mErrorNum = 0x0000;
113 }
114
115 ////////////////////////////////////////////////////////////////////////////////
116 //
117 int LLEmbeddedBrowser::getLastError()
118 {
119         return mErrorNum;
120 }
121
122 ////////////////////////////////////////////////////////////////////////////////
123 //
124 std::string LLEmbeddedBrowser::getGREVersion()
125 {
126         // take the string directly from Mozilla
127         return std::string( GRE_BUILD_ID );
128 }
129
130 ////////////////////////////////////////////////////////////////////////////////
131 //
132 bool LLEmbeddedBrowser::init( std::string applicationDir,
133                                                                 std::string componentDir,
134                                                                         std::string profileDir,
135                                                                                 void* nativeWindowHandleIn )
136 {
137         mNativeWindowHandle = nativeWindowHandleIn;
138
139         nsCOMPtr< nsILocalFile > applicationDirNative;
140         nsresult result = NS_NewNativeLocalFile( nsCString( applicationDir.c_str() ), PR_FALSE, getter_AddRefs( applicationDirNative ) );
141         if ( NS_FAILED( result ) )
142         {
143                 setLastError( 0x1000 );
144                 return false;
145         };
146
147         nsCOMPtr< nsILocalFile > componentDirNative;
148         result = NS_NewNativeLocalFile( nsCString( componentDir.c_str() ), PR_FALSE, getter_AddRefs( componentDirNative ) );
149         if ( NS_FAILED( result ) )
150         {
151                 setLastError( 0x1001 );
152                 return false;
153         };
154
155         result = XRE_InitEmbedding( componentDirNative, applicationDirNative, nsnull, nsnull, 0 );
156         if ( NS_FAILED( result ) )
157         {
158                 setLastError( 0x1002 );
159                 return false;
160         };
161
162         nsCOMPtr< nsILocalFile > profileDirNative;
163         result = NS_NewNativeLocalFile( nsCString( profileDir.c_str() ), PR_TRUE, getter_AddRefs( profileDirNative ) );
164         if ( NS_FAILED( result ) )
165         {
166                 setLastError( 0x1007 );
167                 return false;
168         };
169         nsCOMPtr< nsProfileDirServiceProvider > locProvider;
170         NS_NewProfileDirServiceProvider( PR_TRUE, getter_AddRefs( locProvider ) );
171         if ( ! locProvider )
172         {
173                 setLastError( 0x1003 );
174                 XRE_TermEmbedding();
175                 return PR_FALSE;
176         };
177
178         result = locProvider->Register();
179         if ( NS_FAILED( result ) )
180         {
181                 setLastError( 0x1004 );
182                 XRE_TermEmbedding();
183                 return PR_FALSE;
184         };
185
186         result = locProvider->SetProfileDir( profileDirNative );
187         if ( NS_FAILED( result ) )
188         {
189                 setLastError( 0x1005 );
190                 XRE_TermEmbedding();
191                 return PR_FALSE;
192         };
193
194         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
195         if ( pref )
196         {
197                 pref->SetBoolPref( "security.warn_entering_secure", PR_FALSE );
198                 pref->SetBoolPref( "security.warn_entering_weak", PR_FALSE );
199                 pref->SetBoolPref( "security.warn_leaving_secure", PR_FALSE );
200                 pref->SetBoolPref( "security.warn_submit_insecure", PR_FALSE );
201                 pref->SetBoolPref( "network.protocol-handler.warn-external-default", PR_FALSE );
202         }
203         else
204         {
205                 setLastError( 0x1006 );
206         };
207
208         // disable proxy by default
209         enableProxy( false, "", 0 );
210
211     // Originally from Linux version but seems to help other platforms too
212         nsresult rv;
213         nsCOMPtr<nsIAppShell> appShell;
214         NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
215         appShell = do_CreateInstance(kAppShellCID, &rv);
216         if (!appShell)
217         {
218                 setLastError( 0x1008 );
219                 return PR_FALSE;
220         }
221         sAppShell = appShell.get();
222         NS_ADDREF(sAppShell);
223         sAppShell->Create(0, nsnull);
224         sAppShell->Spinup();
225
226         clearLastError();
227
228         return true;
229 }
230
231 ////////////////////////////////////////////////////////////////////////////////
232 //
233 bool LLEmbeddedBrowser::reset()
234 {
235         XRE_TermEmbedding();
236
237         return true;
238 }
239
240 ////////////////////////////////////////////////////////////////////////////////
241 //
242 bool LLEmbeddedBrowser::clearCache()
243 {
244         nsCOMPtr< nsICacheService > cacheService = do_GetService( NS_CACHESERVICE_CONTRACTID );
245         if (! cacheService)
246                 return false;
247
248         cacheService->EvictEntries( nsICache::STORE_ANYWHERE );
249
250         return true;
251 }
252
253 ////////////////////////////////////////////////////////////////////////////////
254 //
255 bool LLEmbeddedBrowser::enableProxy( bool proxyEnabledIn, std::string proxyHostNameIn, int proxyPortIn )
256 {
257         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
258         if ( pref )
259         {
260                 if ( proxyEnabledIn )
261                         pref->SetIntPref( "network.proxy.type", 1 );
262                 else
263                         pref->SetIntPref( "network.proxy.type", 0 );
264
265                 pref->SetCharPref( "network.proxy.ssl", proxyHostNameIn.c_str() );
266                 pref->SetIntPref( "network.proxy.ssl_port", proxyPortIn );
267
268                 pref->SetCharPref( "network.proxy.ftp", proxyHostNameIn.c_str() );
269                 pref->SetIntPref( "network.proxy.ftp_port", proxyPortIn );
270
271                 pref->SetCharPref( "network.proxy.gopher", proxyHostNameIn.c_str() );
272                 pref->SetIntPref( "network.proxy.gopher_port", proxyPortIn );
273
274                 pref->SetCharPref( "network.proxy.http", proxyHostNameIn.c_str() );
275                 pref->SetIntPref( "network.proxy.http_port", proxyPortIn );
276
277                 pref->SetBoolPref( "network.proxy.share_proxy_settings", true );
278
279                 return true;
280         };
281
282         return false;
283 }
284
285 ////////////////////////////////////////////////////////////////////////////////
286 //
287 bool LLEmbeddedBrowser::enableCookies( bool enabledIn )
288 {
289         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
290         if ( pref )
291         {
292                 if ( enabledIn )
293                         pref->SetIntPref( "network.cookie.cookieBehavior", 0 );
294                 else
295                         pref->SetIntPref( "network.cookie.cookieBehavior", 2 );
296
297                 return true;
298         }
299
300         return false;
301 }
302
303 ////////////////////////////////////////////////////////////////////////////////
304 //
305 bool LLEmbeddedBrowser::clearAllCookies()
306 {
307         nsCOMPtr< nsICookieManager > cookieManager = do_GetService( NS_COOKIEMANAGER_CONTRACTID );
308         if ( ! cookieManager )
309                 return false;
310
311         cookieManager->RemoveAll();
312
313         return true;
314 }
315
316 ////////////////////////////////////////////////////////////////////////////////
317 //
318 bool LLEmbeddedBrowser::enablePlugins( bool enabledIn )
319 {
320         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
321         if ( pref )
322         {
323                 if ( enabledIn )
324                 {
325                         pref->SetBoolPref( "plugin.scan.plid.all", PR_TRUE );
326                         pref->SetBoolPref( "xpinstall-enabled", PR_TRUE );
327                 }
328                 else
329                 {
330                         pref->SetBoolPref( "plugin.scan.plid.all", PR_FALSE );
331                         pref->SetBoolPref( "xpinstall-enabled", PR_FALSE );
332                         pref->SetBoolPref( "plugin.scan.4xPluginFolder", PR_FALSE );
333                         pref->SetCharPref( "plugin.scan.Quicktime", "20.0" );
334                         pref->SetCharPref( "plugin.scan.Acrobat", "99.0" );
335                         pref->SetCharPref( "plugin.scan.SunJRE", "99.0" );
336                         pref->SetCharPref( "plugin.scan.WindowsMediaPlayer", "99.0" );
337                 };
338
339                 return true;
340         };
341
342         return false;
343 }
344
345 ////////////////////////////////////////////////////////////////////////////////
346 //
347 void LLEmbeddedBrowser::setBrowserAgentId( std::string idIn )
348 {
349         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
350         if ( pref )
351         {
352                 pref->SetCharPref( "general.useragent.extra.* ", idIn.c_str() );
353         };
354 }
355
356 ////////////////////////////////////////////////////////////////////////////////
357 //
358 LLEmbeddedBrowserWindow* LLEmbeddedBrowser::createBrowserWindow( int browserWidthIn, int browserHeightIn )
359 {
360     nsCOMPtr< nsIWebBrowserChrome > chrome;
361
362         LLEmbeddedBrowserWindow* newWin = new LLEmbeddedBrowserWindow();
363         if ( ! newWin )
364         {
365                 return 0;
366         };
367
368         nsIWebBrowserChrome** aNewWindow = getter_AddRefs( chrome );
369
370         CallQueryInterface( NS_STATIC_CAST( nsIWebBrowserChrome*, newWin ), aNewWindow );
371
372         NS_ADDREF( *aNewWindow );
373
374         newWin->SetChromeFlags( nsIWebBrowserChrome::CHROME_ALL );
375
376         nsCOMPtr< nsIWebBrowser > newBrowser;
377
378         newWin->createBrowser( mNativeWindowHandle, browserWidthIn, browserHeightIn, getter_AddRefs( newBrowser ) );
379         if ( ! newBrowser )
380         {
381                 return 0;
382         };
383
384     if ( newWin && chrome )
385     {
386                 newWin->setParent( this );
387         nsCOMPtr< nsIWebBrowser > newBrowser;
388         chrome->GetWebBrowser( getter_AddRefs( newBrowser ) );
389         nsCOMPtr< nsIWebNavigation > webNav( do_QueryInterface ( newBrowser ) );
390                 webNav->LoadURI( NS_ConvertUTF8toUTF16( "about:blank" ).get(), nsIWebNavigation::LOAD_FLAGS_NONE, nsnull, nsnull, nsnull );
391
392                 clearLastError();
393
394                 return newWin;
395     };
396
397         setLastError( 0x2001 );
398         return 0;
399 };
400
401
402 ////////////////////////////////////////////////////////////////////////////////
403 //
404 bool LLEmbeddedBrowser::destroyBrowserWindow( LLEmbeddedBrowserWindow* browserWindowIn )
405 {
406         nsCOMPtr< nsIWebBrowser > webBrowser;
407         nsCOMPtr< nsIWebNavigation > webNavigation;
408
409         browserWindowIn->GetWebBrowser( getter_AddRefs( webBrowser ) );
410         webNavigation = do_QueryInterface( webBrowser );
411         if ( webNavigation )
412         {
413                 webNavigation->Stop( nsIWebNavigation::STOP_ALL );
414         };
415
416         nsCOMPtr< nsIWebBrowser > browser = nsnull;
417         browserWindowIn->GetWebBrowser( getter_AddRefs( browser ) );
418         nsCOMPtr< nsIBaseWindow > browserAsWin = do_QueryInterface( browser );
419         if ( browserAsWin )
420         {
421                 browserAsWin->Destroy();
422         };
423
424
425         browserWindowIn->SetWebBrowser( nsnull );
426
427         NS_RELEASE( browserWindowIn );
428
429         delete browserWindowIn;
430
431         clearLastError();
432
433         return true;
434 }
435
436 // Windows specific switches
437 #ifdef WIN32
438         #pragma warning( 3 : 4291 ) // (no matching operator delete found; memory will not be freed if initialization throws an exception)
439         #pragma warning( 3 : 4265 )     // "class has virtual functions, but destructor is not virtual"
440
441         // #define required by this file for LibXUL/Mozilla code to avoid crashes in their debug code
442         #ifdef _DEBUG
443                 #undef DEBUG
444         #endif
445
446 #endif  // WIN32
Note: See TracBrowser for help on using the browser.