root/trunk/llmozlib2/llembeddedbrowser.cpp

Revision 17, 12.4 kB (checked in by callum.linden, 1 year ago)

Fix via patch from Nyx for DEV-14335 - fixes crash on Korean versions of Windows when username (and therefore path to profile information) contains Korean characters.
Reviewed by me - fix by Nyx.

  • 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     NS_ConvertUTF8toUTF16 applicationDirUTF16(applicationDir.c_str());
140     NS_ConvertUTF8toUTF16 componentDirUTF16(componentDir.c_str());
141         NS_ConvertUTF8toUTF16 profileDirUTF16(profileDir.c_str());
142
143         nsCOMPtr< nsILocalFile > applicationDirNative;
144         nsresult result = NS_NewLocalFile( applicationDirUTF16, PR_FALSE, getter_AddRefs( applicationDirNative ) );
145         if ( NS_FAILED( result ) )
146         {
147                 setLastError( 0x1000 );
148                 return false;
149         };
150
151         nsCOMPtr< nsILocalFile > componentDirNative;
152         result = NS_NewLocalFile( componentDirUTF16 , PR_FALSE, getter_AddRefs( componentDirNative ) );
153         if ( NS_FAILED( result ) )
154         {
155                 setLastError( 0x1001 );
156                 return false;
157         };
158
159         result = XRE_InitEmbedding( componentDirNative, applicationDirNative, nsnull, nsnull, 0 );
160         if ( NS_FAILED( result ) )
161         {
162                 setLastError( 0x1002 );
163                 return false;
164         };
165
166         nsCOMPtr< nsILocalFile > profileDirNative;
167         result = NS_NewLocalFile( profileDirUTF16 , PR_TRUE, getter_AddRefs( profileDirNative ) );
168         if ( NS_FAILED( result ) )
169         {
170                 setLastError( 0x1007 );
171                 return false;
172         };
173         nsCOMPtr< nsProfileDirServiceProvider > locProvider;
174         NS_NewProfileDirServiceProvider( PR_TRUE, getter_AddRefs( locProvider ) );
175         if ( ! locProvider )
176         {
177                 setLastError( 0x1003 );
178                 XRE_TermEmbedding();
179                 return PR_FALSE;
180         };
181
182         result = locProvider->Register();
183         if ( NS_FAILED( result ) )
184         {
185                 setLastError( 0x1004 );
186                 XRE_TermEmbedding();
187                 return PR_FALSE;
188         };
189
190         result = locProvider->SetProfileDir( profileDirNative );
191         if ( NS_FAILED( result ) )
192         {
193                 setLastError( 0x1005 );
194                 XRE_TermEmbedding();
195                 return PR_FALSE;
196         };
197
198         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
199         if ( pref )
200         {
201                 pref->SetBoolPref( "security.warn_entering_secure", PR_FALSE );
202                 pref->SetBoolPref( "security.warn_entering_weak", PR_FALSE );
203                 pref->SetBoolPref( "security.warn_leaving_secure", PR_FALSE );
204                 pref->SetBoolPref( "security.warn_submit_insecure", PR_FALSE );
205                 pref->SetBoolPref( "network.protocol-handler.warn-external-default", PR_FALSE );
206         }
207         else
208         {
209                 setLastError( 0x1006 );
210         };
211
212         // disable proxy by default
213         enableProxy( false, "", 0 );
214
215     // Originally from Linux version but seems to help other platforms too
216         nsresult rv;
217         nsCOMPtr<nsIAppShell> appShell;
218         NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
219         appShell = do_CreateInstance(kAppShellCID, &rv);
220         if (!appShell)
221         {
222                 setLastError( 0x1008 );
223                 return PR_FALSE;
224         }
225         sAppShell = appShell.get();
226         NS_ADDREF(sAppShell);
227         sAppShell->Create(0, nsnull);
228         sAppShell->Spinup();
229
230         clearLastError();
231
232         return true;
233 }
234
235 ////////////////////////////////////////////////////////////////////////////////
236 //
237 bool LLEmbeddedBrowser::reset()
238 {
239         XRE_TermEmbedding();
240
241         return true;
242 }
243
244 ////////////////////////////////////////////////////////////////////////////////
245 //
246 bool LLEmbeddedBrowser::clearCache()
247 {
248         nsCOMPtr< nsICacheService > cacheService = do_GetService( NS_CACHESERVICE_CONTRACTID );
249         if (! cacheService)
250                 return false;
251
252         cacheService->EvictEntries( nsICache::STORE_ANYWHERE );
253
254         return true;
255 }
256
257 ////////////////////////////////////////////////////////////////////////////////
258 //
259 bool LLEmbeddedBrowser::enableProxy( bool proxyEnabledIn, std::string proxyHostNameIn, int proxyPortIn )
260 {
261         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
262         if ( pref )
263         {
264                 if ( proxyEnabledIn )
265                         pref->SetIntPref( "network.proxy.type", 1 );
266                 else
267                         pref->SetIntPref( "network.proxy.type", 0 );
268
269                 pref->SetCharPref( "network.proxy.ssl", proxyHostNameIn.c_str() );
270                 pref->SetIntPref( "network.proxy.ssl_port", proxyPortIn );
271
272                 pref->SetCharPref( "network.proxy.ftp", proxyHostNameIn.c_str() );
273                 pref->SetIntPref( "network.proxy.ftp_port", proxyPortIn );
274
275                 pref->SetCharPref( "network.proxy.gopher", proxyHostNameIn.c_str() );
276                 pref->SetIntPref( "network.proxy.gopher_port", proxyPortIn );
277
278                 pref->SetCharPref( "network.proxy.http", proxyHostNameIn.c_str() );
279                 pref->SetIntPref( "network.proxy.http_port", proxyPortIn );
280
281                 pref->SetBoolPref( "network.proxy.share_proxy_settings", true );
282
283                 return true;
284         };
285
286         return false;
287 }
288
289 ////////////////////////////////////////////////////////////////////////////////
290 //
291 bool LLEmbeddedBrowser::enableCookies( bool enabledIn )
292 {
293         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
294         if ( pref )
295         {
296                 if ( enabledIn )
297                         pref->SetIntPref( "network.cookie.cookieBehavior", 0 );
298                 else
299                         pref->SetIntPref( "network.cookie.cookieBehavior", 2 );
300
301                 return true;
302         }
303
304         return false;
305 }
306
307 ////////////////////////////////////////////////////////////////////////////////
308 //
309 bool LLEmbeddedBrowser::clearAllCookies()
310 {
311         nsCOMPtr< nsICookieManager > cookieManager = do_GetService( NS_COOKIEMANAGER_CONTRACTID );
312         if ( ! cookieManager )
313                 return false;
314
315         cookieManager->RemoveAll();
316
317         return true;
318 }
319
320 ////////////////////////////////////////////////////////////////////////////////
321 //
322 bool LLEmbeddedBrowser::enablePlugins( bool enabledIn )
323 {
324         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
325         if ( pref )
326         {
327                 if ( enabledIn )
328                 {
329                         pref->SetBoolPref( "plugin.scan.plid.all", PR_TRUE );
330                         pref->SetBoolPref( "xpinstall-enabled", PR_TRUE );
331                 }
332                 else
333                 {
334                         pref->SetBoolPref( "plugin.scan.plid.all", PR_FALSE );
335                         pref->SetBoolPref( "xpinstall-enabled", PR_FALSE );
336                         pref->SetBoolPref( "plugin.scan.4xPluginFolder", PR_FALSE );
337                         pref->SetCharPref( "plugin.scan.Quicktime", "20.0" );
338                         pref->SetCharPref( "plugin.scan.Acrobat", "99.0" );
339                         pref->SetCharPref( "plugin.scan.SunJRE", "99.0" );
340                         pref->SetCharPref( "plugin.scan.WindowsMediaPlayer", "99.0" );
341                 };
342
343                 return true;
344         };
345
346         return false;
347 }
348
349 ////////////////////////////////////////////////////////////////////////////////
350 //
351 void LLEmbeddedBrowser::setBrowserAgentId( std::string idIn )
352 {
353         nsCOMPtr<nsIPref> pref = do_CreateInstance( NS_PREF_CONTRACTID );
354         if ( pref )
355         {
356                 pref->SetCharPref( "general.useragent.extra.* ", idIn.c_str() );
357         };
358 }
359
360 ////////////////////////////////////////////////////////////////////////////////
361 //
362 LLEmbeddedBrowserWindow* LLEmbeddedBrowser::createBrowserWindow( int browserWidthIn, int browserHeightIn )
363 {
364     nsCOMPtr< nsIWebBrowserChrome > chrome;
365
366         LLEmbeddedBrowserWindow* newWin = new LLEmbeddedBrowserWindow();
367         if ( ! newWin )
368         {
369                 return 0;
370         };
371
372         nsIWebBrowserChrome** aNewWindow = getter_AddRefs( chrome );
373
374         CallQueryInterface( NS_STATIC_CAST( nsIWebBrowserChrome*, newWin ), aNewWindow );
375
376         NS_ADDREF( *aNewWindow );
377
378         newWin->SetChromeFlags( nsIWebBrowserChrome::CHROME_ALL );
379
380         nsCOMPtr< nsIWebBrowser > newBrowser;
381
382         newWin->createBrowser( mNativeWindowHandle, browserWidthIn, browserHeightIn, getter_AddRefs( newBrowser ) );
383         if ( ! newBrowser )
384         {
385                 return 0;
386         };
387
388     if ( newWin && chrome )
389     {
390                 newWin->setParent( this );
391         nsCOMPtr< nsIWebBrowser > newBrowser;
392         chrome->GetWebBrowser( getter_AddRefs( newBrowser ) );
393         nsCOMPtr< nsIWebNavigation > webNav( do_QueryInterface ( newBrowser ) );
394                 webNav->LoadURI( NS_ConvertUTF8toUTF16( "about:blank" ).get(), nsIWebNavigation::LOAD_FLAGS_NONE, nsnull, nsnull, nsnull );
395
396                 clearLastError();
397
398                 return newWin;
399     };
400
401         setLastError( 0x2001 );
402         return 0;
403 };
404
405
406 ////////////////////////////////////////////////////////////////////////////////
407 //
408 bool LLEmbeddedBrowser::destroyBrowserWindow( LLEmbeddedBrowserWindow* browserWindowIn )
409 {
410         nsCOMPtr< nsIWebBrowser > webBrowser;
411         nsCOMPtr< nsIWebNavigation > webNavigation;
412
413         browserWindowIn->GetWebBrowser( getter_AddRefs( webBrowser ) );
414         webNavigation = do_QueryInterface( webBrowser );
415         if ( webNavigation )
416         {
417                 webNavigation->Stop( nsIWebNavigation::STOP_ALL );
418         };
419
420         nsCOMPtr< nsIWebBrowser > browser = nsnull;
421         browserWindowIn->GetWebBrowser( getter_AddRefs( browser ) );
422         nsCOMPtr< nsIBaseWindow > browserAsWin = do_QueryInterface( browser );
423         if ( browserAsWin )
424         {
425                 browserAsWin->Destroy();
426         };
427
428
429         browserWindowIn->SetWebBrowser( nsnull );
430
431         NS_RELEASE( browserWindowIn );
432
433         delete browserWindowIn;
434
435         clearLastError();
436
437         return true;
438 }
439
440 // Windows specific switches
441 #ifdef WIN32
442         #pragma warning( 3 : 4291 ) // (no matching operator delete found; memory will not be freed if initialization throws an exception)
443         #pragma warning( 3 : 4265 )     // "class has virtual functions, but destructor is not virtual"
444
445         // #define required by this file for LibXUL/Mozilla code to avoid crashes in their debug code
446         #ifdef _DEBUG
447                 #undef DEBUG
448         #endif
449
450 #endif  // WIN32
Note: See TracBrowser for help on using the browser.