root/trunk/llmozlib2/llembeddedbrowser.cpp

Revision 8, 12.4 kB (checked in by rob.linden, 2 years ago)

Latest internal snapshot
Last Changed Rev: 80909
Last Changed Date: 2008-02-27 15:08:55 -0800 (Wed, 27 Feb 2008)

  • 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 "llembeddedbrowser.h"
48 #include "llembeddedbrowserwindow.h"
49
50 #ifdef WIN32
51         #pragma warning( disable : 4265 )       // "class has virtual functions, but destructor is not virtual"
52         #pragma warning( disable : 4291 )       // (no matching operator delete found; memory will not be freed if initialization throws an exception)
53 #endif  // WIN32
54
55 #include "nsBuildID.h"
56 #include "nsICacheService.h"
57 #include "nsICookieManager.h"
58 #include "nsIPref.h"
59 #include "nsNetCID.h"
60 #include "nsProfileDirServiceProvider.h"
61 #include "nsXULAppAPI.h"
62 #include "nsIAppShell.h"
63
64 #if defined(LL_LINUX) || defined(LL_DARWIN)
65 #include "nsWidgetsCID.h"
66 static nsIAppShell *sAppShell = nsnull;
67 #endif // defined(LL_LINUX) || defined(LL_DARWIN)
68
69 // singleton pattern - initialization
70 LLEmbeddedBrowser* LLEmbeddedBrowser::sInstance = 0;
71
72 ////////////////////////////////////////////////////////////////////////////////
73 //
74 LLEmbeddedBrowser::LLEmbeddedBrowser() :
75         mErrorNum( 0 ),
76         mNativeWindowHandle( 0 )
77 {
78 }
79
80 ////////////////////////////////////////////////////////////////////////////////
81 //
82 LLEmbeddedBrowser::~LLEmbeddedBrowser()
83 {
84 }
85
86 ////////////////////////////////////////////////////////////////////////////////
87 //
88 LLEmbeddedBrowser* LLEmbeddedBrowser::getInstance()
89 {
90     if ( ! sInstance )
91     {
92         sInstance = new LLEmbeddedBrowser;
93     };
94
95         return sInstance;
96 };
97
98 ////////////////////////////////////////////////////////////////////////////////
99 //
100 void LLEmbeddedBrowser::setLastError( int errorNumIn )
101 {
102         mErrorNum = errorNumIn;
103 }
104
105 ////////////////////////////////////////////////////////////////////////////////
106 //
107 void LLEmbeddedBrowser::clearLastError()
108 {
109         mErrorNum = 0x0000;
110 }
111
112 ////////////////////////////////////////////////////////////////////////////////
113 //
114 int LLEmbeddedBrowser::getLastError()
115 {
116         return mErrorNum;
117 }
118
119 ////////////////////////////////////////////////////////////////////////////////
120 //
121 std::string LLEmbeddedBrowser::getGREVersion()
122 {
123         // take the string directly from Mozilla
124         return std::string( GRE_BUILD_ID );
125 }
126
127 ////////////////////////////////////////////////////////////////////////////////
128 //
129 bool LLEmbeddedBrowser::init( std::string applicationDir,
130                                                                 std::string componentDir,
131                                                                         std::string profileDir,
132                                                                                 void* nativeWindowHandleIn )
133 {
134         mNativeWindowHandle = nativeWindowHandleIn;
135
136         nsCOMPtr< nsILocalFile > applicationDirNative;
137         nsresult result = NS_NewNativeLocalFile( nsCString( applicationDir.c_str() ), PR_FALSE, getter_AddRefs( applicationDirNative ) );
138         if ( NS_FAILED( result ) )
139         {
140                 setLastError( 0x1000 );
141                 return false;
142         };
143
144         nsCOMPtr< nsILocalFile > componentDirNative;
145         result = NS_NewNativeLocalFile( nsCString( componentDir.c_str() ), PR_FALSE, getter_AddRefs( componentDirNative ) );
146         if ( NS_FAILED( result ) )
147         {
148                 setLastError( 0x1001 );
149                 return false;
150         };
151
152         result = XRE_InitEmbedding( componentDirNative, applicationDirNative, nsnull, nsnull, 0 );
153         if ( NS_FAILED( result ) )
154         {
155                 setLastError( 0x1002 );
156                 return false;
157         };
158
159         nsCOMPtr< nsILocalFile > profileDirNative;
160         result = NS_NewNativeLocalFile( nsCString( profileDir.c_str() ), PR_TRUE, getter_AddRefs( profileDirNative ) );
161         if ( NS_FAILED( result ) )
162         {
163                 setLastError( 0x1007 );
164                 return false;
165         };
166         nsCOMPtr< nsProfileDirServiceProvider > locProvider;
167         NS_NewProfileDirServiceProvider( PR_TRUE, getter_AddRefs( locProvider ) );
168         if ( ! locProvider )
169         {
170                 setLastError( 0x1003 );
171                 XRE_TermEmbedding();
172                 return PR_FALSE;
173         };
174
175         result = locProvider->Register();
176         if ( NS_FAILED( result ) )
177         {
178                 setLastError( 0x1004 );
179                 XRE_TermEmbedding();
180                 return PR_FALSE;
181         };
182
183         result = locProvider->SetProfileDir( profileDirNative );
184         if ( NS_FAILED( result ) )
185         {
186                 setLastError( 0x1005 );
187                 XRE_TermEmbedding();
188                 return PR_FALSE;
189         };
190
191         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
192         if ( pref )
193         {
194                 pref->SetBoolPref( "security.warn_entering_secure", PR_FALSE );
195                 pref->SetBoolPref( "security.warn_entering_weak", PR_FALSE );
196                 pref->SetBoolPref( "security.warn_leaving_secure", PR_FALSE );
197                 pref->SetBoolPref( "security.warn_submit_insecure", PR_FALSE );
198                 pref->SetBoolPref( "network.protocol-handler.warn-external-default", PR_FALSE );
199         }
200         else
201         {
202                 setLastError( 0x1006 );
203         };
204
205         // disable proxy by default
206         enableProxy( false, "", 0 );
207
208 #if defined(LL_LINUX) || defined(LL_DARWIN)
209         // Essential on Linux/GTK to add the gecko pump to the GTK event
210         // loop.  Might be harmless/good on other platforms too.
211         nsresult rv;
212         nsCOMPtr<nsIAppShell> appShell;
213         NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
214         appShell = do_CreateInstance(kAppShellCID, &rv);
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         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 bool LLEmbeddedBrowser::destroyBrowserWindow( LLEmbeddedBrowserWindow* browserWindowIn )
404 {
405         nsCOMPtr< nsIWebBrowser > webBrowser;
406         nsCOMPtr< nsIWebNavigation > webNavigation;
407
408         browserWindowIn->GetWebBrowser( getter_AddRefs( webBrowser ) );
409         webNavigation = do_QueryInterface( webBrowser );
410         if ( webNavigation )
411         {
412                 webNavigation->Stop( nsIWebNavigation::STOP_ALL );
413         };
414
415         nsCOMPtr< nsIWebBrowser > browser = nsnull;
416         browserWindowIn->GetWebBrowser( getter_AddRefs( browser ) );
417         nsCOMPtr< nsIBaseWindow > browserAsWin = do_QueryInterface( browser );
418         if ( browserAsWin )
419         {
420                 browserAsWin->Destroy();
421         };
422
423         browserWindowIn->SetWebBrowser( nsnull );
424
425         NS_RELEASE( browserWindowIn );
426
427         delete browserWindowIn;
428
429         clearLastError();
430
431         return true;
432 }
433
434 // Windows specific switches
435 #ifdef WIN32
436         #pragma warning( 3 : 4291 ) // (no matching operator delete found; memory will not be freed if initialization throws an exception)
437         #pragma warning( 3 : 4265 )     // "class has virtual functions, but destructor is not virtual"
438
439         // #define required by this file for LibXUL/Mozilla code to avoid crashes in their debug code
440         #ifdef _DEBUG
441                 #undef DEBUG
442         #endif
443
444 #endif  // WIN32
Note: See TracBrowser for help on using the browser.