root/trunk/llmozlib2/llembeddedbrowser.cpp

Revision 3, 12.6 kB (checked in by rob.linden, 2 years ago)

I believe this is the version included in 1.18.6.3
Last Changed Rev: 76367
Last Changed Date: 2007-12-21 09:34:18 -0800 (Fri, 21 Dec 2007)

  • Property svn:eol-style set to native
Line 
1 /**
2  * @file llembeddedbrowser.cpp
3  * @brief LLEmbeddedBrowser implementation.
4  *
5  * $LicenseInfo:firstyear=2006&license=viewergpl$
6  *
7  * Copyright (c) 2006-2007, Linden Research, Inc.
8  *
9  * Second Life Viewer Source Code
10  * The source code in this file ("Source Code") is provided by Linden Lab
11  * to you under the terms of the GNU General Public License, version 2.0
12  * ("GPL"), unless you have obtained a separate licensing agreement
13  * ("Other License"), formally executed by you and Linden Lab.  Terms of
14  * the GPL can be found in doc/GPL-license.txt in this distribution, or
15  * online at http://secondlife.com/developers/opensource/gplv2
16  *
17  * There are special exceptions to the terms and conditions of the GPL as
18  * it is applied to this Source Code. View the full text of the exception
19  * in the file doc/FLOSS-exception.txt in this software distribution, or
20  * online at http://secondlife.com/developers/opensource/flossexception
21  *
22  * By copying, modifying or distributing this software, you acknowledge
23  * that you have read and understood your obligations described above,
24  * and agree to abide by those obligations.
25  *
26  * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
27  * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
28  * COMPLETENESS OR PERFORMANCE.
29  * $/LicenseInfo$
30  */
31
32 // Windows specific switches
33 #ifdef WIN32
34         // appears to be required by LibXUL/Mozilla code to avoid crashes in debug versions of their code (undef'd at end of this file)
35         #ifdef _DEBUG
36                 #define DEBUG 1
37         #endif
38 #endif
39
40 #include "llembeddedbrowser.h"
41 #include "llembeddedbrowserwindow.h"
42
43 #ifdef WIN32
44         #pragma warning( disable : 4265 ) // "class has virtual functions, but destructor is not virtual"
45         #pragma warning( disable : 4291 ) // (no matching operator delete found; memory will not be freed if initialization throws an exception)
46 #endif  // WIN32
47
48 #include "nsBuildID.h"
49 #include "nsNetCID.h"
50 #include "nsCWebBrowser.h"
51 #include "nsGUIEvent.h"
52 #include "nsICacheService.h"
53 #include "nsICookieManager.h"
54 #include "nsICaret.h"
55 #include "nsIComponentRegistrar.h"
56 #include "nsIContent.h"
57 #include "nsIDOMDocument.h"
58 #include "nsIDOMElement.h"
59 #include "nsIDOMWindow.h"
60 #include "nsIDocShell.h"
61 #include "nsIDocShellTreeItem.h"
62 #include "nsIDocument.h"
63 #include "nsIFactory.h"
64 #include "nsIFrame.h"
65 #include "nsIInterfaceRequestorUtils.h"
66 #include "nsIPref.h"
67 #include "nsIPromptService.h"
68 #include "nsIScrollableView.h"
69 #include "nsISelection.h"
70 #include "nsISelectionController.h"
71 #include "nsIWebBrowserChrome.h"
72 #include "nsIWebBrowserChromeFocus.h"
73 #include "nsIWebBrowserFocus.h"
74 #include "nsPresContext.h"
75 #include "nsProfileDirServiceProvider.h"
76 #include "nsXPCOMGlue.h"
77 #include "nsXULAppAPI.h"
78 #include "nsWidgetsCID.h" // for NS_APPSHELL_CID
79 #include "nsIAppShell.h"
80 static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
81
82 // singleton pattern - initialization
83 LLEmbeddedBrowser* LLEmbeddedBrowser::sInstance = 0;
84
85 ////////////////////////////////////////////////////////////////////////////////
86 //
87 LLEmbeddedBrowser::LLEmbeddedBrowser() :
88         mErrorNum( 0 )
89 {
90 }
91
92 ////////////////////////////////////////////////////////////////////////////////
93 //
94 LLEmbeddedBrowser::~LLEmbeddedBrowser()
95 {
96 }
97
98 ////////////////////////////////////////////////////////////////////////////////
99 //
100 LLEmbeddedBrowser* LLEmbeddedBrowser::getInstance()
101 {
102     if ( ! sInstance )
103     {
104         sInstance = new LLEmbeddedBrowser;
105     };
106
107         return sInstance;
108 };
109
110 ////////////////////////////////////////////////////////////////////////////////
111 //
112 void LLEmbeddedBrowser::setLastError( int errorNumIn )
113 {
114         mErrorNum = errorNumIn;
115 }
116
117 ////////////////////////////////////////////////////////////////////////////////
118 //
119 void LLEmbeddedBrowser::clearLastError()
120 {
121         mErrorNum = 0x0000;
122 }
123
124 ////////////////////////////////////////////////////////////////////////////////
125 //
126 int LLEmbeddedBrowser::getLastError()
127 {
128         return mErrorNum;
129 }
130
131 ////////////////////////////////////////////////////////////////////////////////
132 //
133 static nsIAppShell *sAppShell = nsnull;
134
135 bool LLEmbeddedBrowser::init( std::string applicationDir, std::string componentDir, std::string profileDir )
136 {
137         nsCOMPtr< nsILocalFile > applicationDirNative;
138         nsresult result = NS_NewNativeLocalFile( nsCString( applicationDir.c_str() ), PR_FALSE, getter_AddRefs( applicationDirNative ) );
139         if ( NS_FAILED( result ) )
140         {
141                 setLastError( 0x1000 );
142                 return false;
143         };
144
145         nsCOMPtr< nsILocalFile > componentDirNative;
146         result = NS_NewNativeLocalFile( nsCString( componentDir.c_str() ), PR_FALSE, getter_AddRefs( componentDirNative ) );
147         if ( NS_FAILED( result ) )
148         {
149                 setLastError( 0x1001 );
150                 return false;
151         };
152
153         result = XRE_InitEmbedding( componentDirNative, applicationDirNative, nsnull, nsnull, 0 );
154         if ( NS_FAILED( result ) )
155         {
156                 setLastError( 0x1002 );
157                 return false;
158         };
159
160         nsCOMPtr< nsILocalFile > profileDirNative;
161         result = NS_NewNativeLocalFile( nsCString( profileDir.c_str() ), PR_TRUE, getter_AddRefs( profileDirNative ) );
162         if ( NS_FAILED( result ) )
163         {
164                 setLastError( 0x1007 );
165                 return false;
166         };
167         nsCOMPtr< nsProfileDirServiceProvider > locProvider;
168         NS_NewProfileDirServiceProvider( PR_TRUE, getter_AddRefs( locProvider ) );
169         if ( ! locProvider )
170         {
171                 setLastError( 0x1003 );
172                 XRE_TermEmbedding();
173                 return PR_FALSE;
174         };
175
176         result = locProvider->Register();
177         if ( NS_FAILED( result ) )
178         {
179                 setLastError( 0x1004 );
180                 XRE_TermEmbedding();
181                 return PR_FALSE;
182         };
183
184         result = locProvider->SetProfileDir( profileDirNative );
185         if ( NS_FAILED( result ) )
186         {
187                 setLastError( 0x1005 );
188                 XRE_TermEmbedding();
189                 return PR_FALSE;
190         };
191
192         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
193         if ( pref )
194         {
195                 //pref->SetBoolPref( "viewmanager.do_doublebuffering", PR_FALSE );
196                 pref->SetBoolPref( "security.warn_entering_secure", PR_FALSE );
197                 pref->SetBoolPref( "security.warn_entering_weak", PR_FALSE );
198                 pref->SetBoolPref( "security.warn_leaving_secure", PR_FALSE );
199                 pref->SetBoolPref( "security.warn_submit_insecure", PR_FALSE );
200                 pref->SetBoolPref( "network.protocol-handler.warn-external-default", PR_FALSE );
201         }
202         else
203         {
204                 setLastError( 0x1006 );
205         };
206
207         // disable proxy by default
208         enableProxy( false, "", 0 );
209
210 #ifdef LL_LINUX
211         // Essential on Linux/GTK to add the gecko pump to the GTK event
212         // loop.  Might be harmless/good on other platforms too.
213         nsCOMPtr<nsIAppShell> appShell;
214         appShell = do_CreateInstance(kAppShellCID);
215         if (!appShell)
216         {
217                 setLastError( 0x1008 );
218                 return PR_FALSE;
219         }
220         sAppShell = appShell.get();
221         NS_ADDREF(sAppShell);
222         sAppShell->Create(0, nsnull);
223         sAppShell->Spinup();
224 #endif // LL_LINUX
225
226         clearLastError();
227
228         return true;
229 }
230
231 ////////////////////////////////////////////////////////////////////////////////
232 //
233 bool LLEmbeddedBrowser::reset()
234 {
235         if (sAppShell) {
236                 // Shutdown the appshell service.
237                 sAppShell->Spindown();
238                 NS_RELEASE(sAppShell);
239                 sAppShell = NULL;
240         }
241
242         XRE_TermEmbedding();
243
244         return true;
245 }
246
247 ////////////////////////////////////////////////////////////////////////////////
248 //
249 std::string LLEmbeddedBrowser::getGREVersion()
250 {
251         // take the string directly from Mozilla
252         return std::string( GRE_BUILD_ID );
253 }
254
255 ////////////////////////////////////////////////////////////////////////////////
256 //
257 void LLEmbeddedBrowser::setBrowserAgentId( std::string idIn )
258 {
259         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
260         if ( pref )
261         {
262                 pref->SetCharPref( "general.useragent.extra.* ", idIn.c_str() );
263         };
264 }
265
266 ////////////////////////////////////////////////////////////////////////////////
267 //
268 LLEmbeddedBrowserWindow* createWindow(  void* nativeWindowHandleIn, int widthIn, int heightIn, PRUint32 aChromeFlags, nsIWebBrowserChrome* aParent, nsIWebBrowserChrome** aNewWindow )
269 {
270         LLEmbeddedBrowserWindow* chrome = new LLEmbeddedBrowserWindow();
271         if ( ! chrome )
272         {
273                 return 0;
274         };
275
276         CallQueryInterface( NS_STATIC_CAST( nsIWebBrowserChrome*, chrome ), aNewWindow );
277
278         NS_ADDREF( *aNewWindow );
279
280         chrome->SetChromeFlags( aChromeFlags );
281
282         nsCOMPtr< nsIWebBrowser > newBrowser;
283
284         chrome->createBrowser( nativeWindowHandleIn, widthIn, heightIn, getter_AddRefs( newBrowser ) );
285         if ( ! newBrowser )
286         {
287                 return 0;
288         };
289
290         return chrome;
291 }
292
293 ////////////////////////////////////////////////////////////////////////////////
294 //
295 LLEmbeddedBrowserWindow* LLEmbeddedBrowser::createBrowserWindow( void* nativeWindowHandleIn, int browserWidthIn, int browserHeightIn )
296 {
297     nsCOMPtr< nsIWebBrowserChrome > chrome;
298     LLEmbeddedBrowserWindow* newWin = createWindow( nativeWindowHandleIn, browserWidthIn, browserHeightIn, nsIWebBrowserChrome::CHROME_ALL, nsnull, getter_AddRefs( chrome ) );
299     if ( newWin && chrome )
300     {
301         nsCOMPtr< nsIWebBrowser > newBrowser;
302         chrome->GetWebBrowser( getter_AddRefs( newBrowser ) );
303         nsCOMPtr< nsIWebNavigation > webNav( do_QueryInterface ( newBrowser ) );
304         webNav->LoadURI( NS_ConvertUTF8toUTF16( "about:blank" ).get(), nsIWebNavigation::LOAD_FLAGS_NONE, nsnull, nsnull, nsnull );
305
306
307                 clearLastError();
308
309                 return newWin;
310     };
311
312         setLastError( 0x2001 );
313
314         return 0;
315 };
316
317 ////////////////////////////////////////////////////////////////////////////////
318 //
319 bool LLEmbeddedBrowser::destroyBrowserWindow( LLEmbeddedBrowserWindow* browserWindowIn )
320 {
321         nsCOMPtr< nsIWebBrowser > webBrowser;
322         nsCOMPtr< nsIWebNavigation > webNavigation;
323
324         browserWindowIn->GetWebBrowser( getter_AddRefs( webBrowser ) );
325         webNavigation = do_QueryInterface( webBrowser );
326         if ( webNavigation )
327         {
328                 webNavigation->Stop( nsIWebNavigation::STOP_ALL );
329         };
330
331         nsCOMPtr< nsIWebBrowser > browser = nsnull;
332         browserWindowIn->GetWebBrowser( getter_AddRefs( browser ) );
333         nsCOMPtr< nsIBaseWindow > browserAsWin = do_QueryInterface( browser );
334         if ( browserAsWin )
335         {
336                 browserAsWin->Destroy();
337         };
338
339         browserWindowIn->SetWebBrowser( nsnull );
340
341         NS_RELEASE( browserWindowIn );
342
343         delete browserWindowIn;
344
345         clearLastError();
346
347         return true;
348 }
349
350 ////////////////////////////////////////////////////////////////////////////////
351 //
352 bool LLEmbeddedBrowser::clearCache()
353 {
354         nsCOMPtr< nsICacheService > cacheService = do_GetService( NS_CACHESERVICE_CONTRACTID );
355         if (! cacheService)
356                 return false;
357
358         cacheService->EvictEntries( nsICache::STORE_ANYWHERE );
359
360         return true;
361 }
362
363 ///////////////////////////////////////////////////////////////////////////////
364 //
365 bool LLEmbeddedBrowser::enableProxy( bool proxyEnabledIn, std::string proxyHostNameIn, int proxyPortIn )
366 {
367         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
368         if ( pref )
369         {
370                 if ( proxyEnabledIn )
371                         pref->SetIntPref( "network.proxy.type", 1 );
372                 else
373                         pref->SetIntPref( "network.proxy.type", 0 );
374
375                 pref->SetCharPref( "network.proxy.ssl", proxyHostNameIn.c_str() );
376                 pref->SetIntPref( "network.proxy.ssl_port", proxyPortIn );
377
378                 pref->SetCharPref( "network.proxy.ftp", proxyHostNameIn.c_str() );
379                 pref->SetIntPref( "network.proxy.ftp_port", proxyPortIn );
380
381                 pref->SetCharPref( "network.proxy.gopher", proxyHostNameIn.c_str() );
382                 pref->SetIntPref( "network.proxy.gopher_port", proxyPortIn );
383
384                 pref->SetCharPref( "network.proxy.http", proxyHostNameIn.c_str() );
385                 pref->SetIntPref( "network.proxy.http_port", proxyPortIn );
386
387                 pref->SetBoolPref( "network.proxy.share_proxy_settings", true );
388
389                 return true;
390         };
391
392         return false;
393 }
394
395 ////////////////////////////////////////////////////////////////////////////////
396 //
397 bool LLEmbeddedBrowser::enableCookies( bool enabledIn )
398 {
399         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
400         if ( pref )
401         {
402                 if ( enabledIn )
403                         pref->SetIntPref( "network.cookie.cookieBehavior", 0 );
404                 else
405                         pref->SetIntPref( "network.cookie.cookieBehavior", 2 );
406
407                 return true;
408         }
409
410         return false;
411 }
412
413 ////////////////////////////////////////////////////////////////////////////////
414 //
415 bool LLEmbeddedBrowser::clearAllCookies()
416 {
417         nsCOMPtr< nsICookieManager > cookieManager = do_GetService( NS_COOKIEMANAGER_CONTRACTID );
418         if ( ! cookieManager )
419                 return false;
420
421         cookieManager->RemoveAll();
422
423         return true;
424 }
425
426 ////////////////////////////////////////////////////////////////////////////////
427 //
428 bool LLEmbeddedBrowser::enablePlugins( bool enabledIn )
429 {
430         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
431         if ( pref )
432         {
433                 if ( enabledIn )
434                 {
435                         pref->SetBoolPref( "plugin.scan.plid.all", PR_TRUE );
436                         pref->SetBoolPref( "xpinstall-enabled", PR_TRUE );
437                 }
438                 else
439                 {
440                         pref->SetBoolPref( "plugin.scan.plid.all", PR_FALSE );
441                         pref->SetBoolPref( "xpinstall-enabled", PR_FALSE );
442                 };
443
444                 return true;
445         };
446
447         return false;
448 }
449
450 // Windows specific switches
451 #ifdef WIN32
452
453         #pragma warning( 3 : 4265 ) // "class has virtual functions, but destructor is not virtual"
454         #pragma warning( 3 : 4291 ) // (no matching operator delete found; memory will not be freed if initialization throws an exception)
455
456         // #define required by this file for LibXUL/Mozilla code to avoid crashes in their debug code
457         #ifdef _DEBUG
458                 #undef DEBUG
459         #endif
460
461 #endif  // WIN32
Note: See TracBrowser for help on using the browser.