-
Notifications
You must be signed in to change notification settings - Fork 2k
/
nsBrowserInstance.cpp
2072 lines (1737 loc) · 62.6 KB
/
nsBrowserInstance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Pierre Phaneuf <pp@ludusdesign.com>
* Travis Bogard <travis@netscape.com>
*/
// Local Includes
#include "nsBrowserInstance.h"
// Helper Includes
// Interfaces Needed
#include "nsIXULWindow.h"
#include "nsIDocShell.h"
#include "nsIDocShellTreeItem.h"
#include "nsISHistory.h"
#include "nsIWebNavigation.h"
#include "nsIWebProgress.h"
#include "nsIXULBrowserWindow.h"
#include "nsPIDOMWindow.h"
/// Unsorted Includes
#include "nsIWebShell.h"
#include "nsIMarkupDocumentViewer.h"
#include "nsIClipboardCommands.h"
#include "pratom.h"
#include "prprf.h"
#include "nsIComponentManager.h"
#include "nsIFileSpecWithUI.h"
#include "nsIScriptContext.h"
#include "nsIScriptGlobalObject.h"
#include "nsIDOMDocument.h"
#include "nsIDOMXULDocument.h"
#include "nsIDocument.h"
#include "nsIDOMWindow.h"
#include "nsIScriptGlobalObject.h"
#include "nsIWebShell.h"
#include "nsIDocShell.h"
#include "nsIWebShellWindow.h"
#include "nsIWebBrowserChrome.h"
#include "nsCOMPtr.h"
#include "nsXPIDLString.h"
#include "nsIPref.h"
#include "nsIServiceManager.h"
#include "nsIURL.h"
#include "nsIIOService.h"
#include "nsIURL.h"
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
#include "nsIWidget.h"
#include "plevent.h"
#include "nsIAppShell.h"
#include "nsIAppShellService.h"
#include "nsAppShellCIDs.h"
#include "nsIDocumentViewer.h"
#include "nsIDOMHTMLImageElement.h"
#include "nsICmdLineService.h"
#include "nsIGlobalHistory.h"
#include "nsIDOMXULDocument.h"
#include "nsIDOMNodeList.h"
#include "nsIDOMNode.h"
#include "nsIDOMElement.h"
#include "nsIPresContext.h"
#include "nsIPresShell.h"
#include "nsIDocumentLoader.h"
#include "nsIObserverService.h"
#include "nsFileLocations.h"
#include "nsIFileLocator.h"
#include "nsIFileSpec.h"
#include "nsIWalletService.h"
#include "nsCURILoader.h"
#include "nsIContentHandler.h"
#include "nsNetUtil.h"
#include "nsICmdLineHandler.h"
static NS_DEFINE_IID(kIWalletServiceIID, NS_IWALLETSERVICE_IID);
static NS_DEFINE_IID(kWalletServiceCID, NS_WALLETSERVICE_CID);
// Interface for "unknown content type handler" component/service.
#include "nsIUnkContentTypeHandler.h"
// Stuff to implement file download dialog.
#include "nsIDocumentObserver.h"
#include "nsIContent.h"
#include "nsIContentViewerFile.h"
#include "nsINameSpaceManager.h"
#include "nsFileStream.h"
#ifdef DEBUG
#ifndef DEBUG_pavlov
#define FORCE_CHECKIN_GUIDELINES
#endif /* DEBUG_pavlov */
#endif /* DEBUG */
// Stuff to implement find/findnext
#include "nsIFindComponent.h"
#ifdef DEBUG_warren
#include "prlog.h"
#if defined(DEBUG) || defined(FORCE_PR_LOG)
static PRLogModuleInfo* gTimerLog = nsnull;
#endif /* DEBUG || FORCE_PR_LOG */
#endif
// if DEBUG or MOZ_PERF_METRICS are defined, enable the PageCycler
#ifdef DEBUG
#define ENABLE_PAGE_CYCLER
#endif
#ifdef MOZ_PERF_METRICS
#define ENABLE_PAGE_CYCLER
#endif
#include "nsTimeBomb.h"
/* Define Class IDs */
static NS_DEFINE_IID(kAppShellServiceCID, NS_APPSHELL_SERVICE_CID);
static NS_DEFINE_IID(kCmdLineServiceCID, NS_COMMANDLINE_SERVICE_CID);
static NS_DEFINE_IID(kCGlobalHistoryCID, NS_GLOBALHISTORY_CID);
static NS_DEFINE_CID(kCPrefServiceCID, NS_PREF_CID);
static NS_DEFINE_CID(kTimeBombCID, NS_TIMEBOMB_CID);
#ifdef DEBUG
static int APP_DEBUG = 0; // Set to 1 in debugger to turn on debugging.
#else
#define APP_DEBUG 0
#endif
#define PREF_HOMEPAGE_OVERRIDE_URL "startup.homepage_override_url"
#define PREF_HOMEPAGE_OVERRIDE "browser.startup.homepage_override.1"
#define PREF_BROWSER_STARTUP_PAGE "browser.startup.page"
#define PREF_BROWSER_STARTUP_HOMEPAGE "browser.startup.homepage"
static nsresult
FindNamedXULElement(nsIDocShell * aShell, const char *aId, nsCOMPtr<nsIDOMElement> * aResult );
//*****************************************************************************
//*** PageCycler: Object Management
//*****************************************************************************
#ifdef ENABLE_PAGE_CYCLER
#include "nsProxyObjectManager.h"
#include "nsITimer.h"
static void TimesUp(nsITimer *aTimer, void *aClosure);
// Timer callback: called when the timer fires
class PageCycler : public nsIObserver {
public:
NS_DECL_ISUPPORTS
PageCycler(nsBrowserInstance* appCore, char *aTimeoutValue = nsnull)
: mAppCore(appCore), mBuffer(nsnull), mCursor(nsnull), mTimeoutValue(0) {
NS_INIT_REFCNT();
NS_ADDREF(mAppCore);
if (aTimeoutValue){
mTimeoutValue = atol(aTimeoutValue);
}
}
virtual ~PageCycler() {
if (mBuffer) delete[] mBuffer;
NS_RELEASE(mAppCore);
}
nsresult Init(const char* nativePath) {
nsresult rv;
mFile = nativePath;
if (!mFile.IsFile())
return mFile.Error();
nsCOMPtr<nsISupports> in;
rv = NS_NewTypicalInputFileStream(getter_AddRefs(in), mFile);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIInputStream> inStr = do_QueryInterface(in, &rv);
if (NS_FAILED(rv)) return rv;
PRUint32 avail;
rv = inStr->Available(&avail);
if (NS_FAILED(rv)) return rv;
mBuffer = new char[avail + 1];
if (mBuffer == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
PRUint32 amt;
rv = inStr->Read(mBuffer, avail, &amt);
if (NS_FAILED(rv)) return rv;
NS_ASSERTION(amt == avail, "Didn't get the whole file.");
mBuffer[avail] = '\0';
mCursor = mBuffer;
NS_WITH_SERVICE(nsIObserverService, obsServ, NS_OBSERVERSERVICE_PROGID, &rv);
if (NS_FAILED(rv)) return rv;
nsString topic; topic.AssignWithConversion("EndDocumentLoad");
rv = obsServ->AddObserver(this, topic.GetUnicode());
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to add self to observer service");
return rv;
}
nsresult GetNextURL(nsString& result) {
result.AssignWithConversion(mCursor);
PRInt32 pos = result.Find(NS_LINEBREAK);
if (pos > 0) {
result.Truncate(pos);
mLastRequest.Assign(result);
mCursor += pos + NS_LINEBREAK_LEN;
return NS_OK;
}
else if ( !result.IsEmpty() ) {
// no more URLs after this one
mCursor += result.Length(); // Advance cursor to terminating '\0'
mLastRequest.Assign(result);
return NS_OK;
}
else {
// no more URLs, so quit the browser
nsresult rv;
// make sure our timer is stopped first
StopTimer();
NS_WITH_SERVICE(nsIAppShellService, appShellServ, kAppShellServiceCID, &rv);
if(NS_FAILED(rv)) return rv;
NS_WITH_SERVICE(nsIProxyObjectManager, pIProxyObjectManager, nsIProxyObjectManager::GetCID(), &rv);
if(NS_FAILED(rv)) return rv;
nsCOMPtr<nsIAppShellService> appShellProxy;
rv = pIProxyObjectManager->GetProxyObject(NS_UI_THREAD_EVENTQ, NS_GET_IID(nsIAppShellService),
appShellServ, PROXY_ASYNC | PROXY_ALWAYS,
getter_AddRefs(appShellProxy));
(void)appShellProxy->Quit();
return NS_ERROR_FAILURE;
}
}
NS_IMETHOD Observe(nsISupports* aSubject,
const PRUnichar* aTopic,
const PRUnichar* someData) {
nsresult rv = NS_OK;
nsString data(someData);
if (data.Find(mLastRequest) == 0) {
char* dataStr = data.ToNewCString();
printf("########## PageCycler loaded: %s\n", dataStr);
nsCRT::free(dataStr);
nsAutoString url;
rv = GetNextURL(url);
if (NS_SUCCEEDED(rv)) {
// stop previous timer
StopTimer();
if (aSubject == this){
// if the aSubject arg is 'this' then we were called by the Timer
// Stop the current load and move on to the next
mAppCore->Stop();
}
// load the URL
rv = mAppCore->LoadUrl(url.GetUnicode());
// start new timer
StartTimer();
}
}
else {
char* dataStr = data.ToNewCString();
printf("########## PageCycler possible failure for: %s\n", dataStr);
nsCRT::free(dataStr);
}
return rv;
}
const nsAutoString &GetLastRequest( void )
{
return mLastRequest;
}
// StartTimer: if mTimeoutValue is positive, then create a new timer
// that will call the callback fcn 'TimesUp' to keep us cycling
// through the URLs
nsresult StartTimer(void)
{
nsresult rv=NS_OK;
if (mTimeoutValue > 0){
rv=NS_NewTimer(getter_AddRefs(mShutdownTimer));
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create timer for PageCycler...");
if (NS_SUCCEEDED(rv) && mShutdownTimer){
mShutdownTimer->Init(TimesUp, (void *)this, mTimeoutValue*1000);
}
}
return rv;
}
// StopTimer: if there is a timer, cancel it
nsresult StopTimer(void)
{
if (mShutdownTimer){
mShutdownTimer->Cancel();
}
return NS_OK;
}
protected:
nsBrowserInstance* mAppCore;
nsFileSpec mFile;
char* mBuffer;
char* mCursor;
nsAutoString mLastRequest;
nsCOMPtr<nsITimer> mShutdownTimer;
PRUint32 mTimeoutValue;
};
NS_IMPL_ADDREF(PageCycler)
NS_IMPL_RELEASE(PageCycler)
NS_INTERFACE_MAP_BEGIN(PageCycler)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
NS_INTERFACE_MAP_END
// TimesUp: callback for the PageCycler timer: called when we have waited too long
// for the page to finish loading.
//
// The aClosure argument is actually the PageCycler,
// so use it to stop the timer and call the Observe fcn to move on to the next URL
// Note that we pass the PageCycler instance as the aSubject argument to the Observe
// function. This is our indication that the Observe method is being called after a
// timeout, allowing the PageCycler to do any necessary processing before moving on.
void TimesUp(nsITimer *aTimer, void *aClosure)
{
if(aClosure){
char urlBuf[64];
PageCycler *pCycler = (PageCycler *)aClosure;
pCycler->StopTimer();
pCycler->GetLastRequest().ToCString( urlBuf, sizeof(urlBuf), 0 );
fprintf(stderr,"########## PageCycler Timeout on URL: %s\n", urlBuf);
pCycler->Observe( pCycler, nsnull, (pCycler->GetLastRequest()).GetUnicode() );
}
}
#endif //ENABLE_PAGE_CYCLER
//*****************************************************************************
//*** nsBrowserInstance: Object Management
//*****************************************************************************
nsBrowserInstance::nsBrowserInstance() : mIsClosed(PR_FALSE)
{
mContentWindow = nsnull;
mContentScriptContext = nsnull;
mWebShellWin = nsnull;
mDocShell = nsnull;
mDOMWindow = nsnull;
mContentAreaDocShell = nsnull;
mContentAreaDocLoader = nsnull;
NS_INIT_REFCNT();
}
nsBrowserInstance::~nsBrowserInstance()
{
Close();
}
//*****************************************************************************
// nsBrowserInstance: nsISupports
//*****************************************************************************
NS_IMPL_ADDREF(nsBrowserInstance)
NS_IMPL_RELEASE(nsBrowserInstance)
NS_INTERFACE_MAP_BEGIN(nsBrowserInstance)
NS_INTERFACE_MAP_ENTRY(nsIBrowserInstance)
NS_INTERFACE_MAP_ENTRY(nsIDocumentLoaderObserver)
NS_INTERFACE_MAP_ENTRY(nsIURIContentListener)
NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIURIContentListener)
NS_INTERFACE_MAP_END
//*****************************************************************************
// nsBrowserInstance: nsIBrowserInstance
//*****************************************************************************
NS_IMETHODIMP
nsBrowserInstance::Back()
{
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mContentAreaDocShell));
NS_ENSURE_TRUE(webNav, NS_ERROR_FAILURE);
webNav->GoBack();
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::Forward()
{
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mContentAreaDocShell));
NS_ENSURE_TRUE(webNav, NS_ERROR_FAILURE);
webNav->GoForward();
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::GetCanGoBack(PRBool* aCan)
{
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mContentAreaDocShell));
NS_ENSURE_TRUE(webNav, NS_ERROR_FAILURE);
webNav->GetCanGoBack(aCan);
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::GetCanGoForward(PRBool* aCan)
{
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mContentAreaDocShell));
NS_ENSURE_TRUE(webNav, NS_ERROR_FAILURE);
webNav->GetCanGoForward(aCan);
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::Reload(nsLoadFlags flags)
{
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mContentAreaDocShell));
NS_ENSURE_TRUE(webNav, NS_ERROR_FAILURE);
webNav->Reload(nsIWebNavigation::reloadNormal);
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::Stop()
{
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mContentAreaDocShell));
if(webNav)
webNav->Stop();
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::LoadUrl(const PRUnichar * urlToLoad)
{
nsresult rv = NS_OK;
/* Ask nsWebShell to load the URl */
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mContentAreaDocShell));
// Normal browser.
rv = webNav->LoadURI( urlToLoad );
return rv;
}
NS_IMETHODIMP
nsBrowserInstance::LoadInitialPage(void)
{
static PRBool cmdLineURLUsed = PR_FALSE;
char * urlstr = nsnull;
nsresult rv;
if (!cmdLineURLUsed) {
NS_WITH_SERVICE(nsICmdLineService, cmdLineArgs, kCmdLineServiceCID, &rv);
if (NS_FAILED(rv)) {
if (APP_DEBUG) fprintf(stderr, "Could not obtain CmdLine processing service\n");
return NS_ERROR_FAILURE;
}
#ifdef ENABLE_PAGE_CYCLER
// First, check if there's a URL file to load (for testing), and if there
// is, process it instead of anything else.
char* file = 0;
rv = cmdLineArgs->GetCmdLineValue("-f", &file);
if (NS_SUCCEEDED(rv) && file) {
// see if we have a timeout value corresponding to the url-file
char *timeoutVal=nsnull;
rv = cmdLineArgs->GetCmdLineValue("-ftimeout", &timeoutVal);
// cereate the cool PageCycler instance
PageCycler* bb = new PageCycler(this, timeoutVal);
if (bb == nsnull) {
nsCRT::free(file);
nsCRT::free(timeoutVal);
return NS_ERROR_OUT_OF_MEMORY;
}
NS_ADDREF(bb);
rv = bb->Init(file);
nsCRT::free(file);
nsCRT::free(timeoutVal);
if (NS_FAILED(rv)) return rv;
nsAutoString str;
rv = bb->GetNextURL(str);
if (NS_SUCCEEDED(rv)) {
urlstr = str.ToNewCString();
}
NS_RELEASE(bb);
}
else
#endif //ENABLE_PAGE_CYCLER
{
// Examine content URL.
if ( mContentAreaDocShell ) {
nsCOMPtr<nsIURI> uri;
rv = mContentAreaDocShell->GetCurrentURI(getter_AddRefs(uri));
nsXPIDLCString spec;
if (uri)
rv = uri->GetSpec(getter_Copies(spec));
/* Check whether url is valid. Otherwise we compare with
* "about:blank" and there by return from here with out
* loading the command line url or default home page.
*/
if (NS_SUCCEEDED(rv) && nsCRT::strcasecmp(spec, "about:blank") != 0) {
// Something has already been loaded (probably via window.open),
// leave it be.
return NS_OK;
}
}
// Get the URL to load
rv = cmdLineArgs->GetURLToLoad(&urlstr);
}
if (urlstr != nsnull) {
// A url was provided. Load it
if (APP_DEBUG) printf("Got Command line URL to load %s\n", urlstr);
nsString url; url.AssignWithConversion( urlstr );
rv = LoadUrl( url.GetUnicode() );
cmdLineURLUsed = PR_TRUE;
return rv;
}
}
// No URL was provided in the command line. Load the default provided
// in the navigator.xul;
// but first, abort if the window doesn't want a default page loaded
if (mWebShellWin) {
PRBool loadDefault;
mWebShellWin->ShouldLoadDefaultPage(&loadDefault);
if (!loadDefault)
return NS_OK;
}
nsCOMPtr<nsIDOMElement> argsElement;
rv = FindNamedXULElement(mDocShell, "args", &argsElement);
if (!argsElement) {
// Couldn't get the "args" element from the xul file. Load a blank page
if (APP_DEBUG) printf("Couldn't find args element\n");
nsAutoString url; url.AssignWithConversion("about:blank");
rv = LoadUrl( url.GetUnicode() );
return rv;
}
// Load the default page mentioned in the xul file.
nsString value;
argsElement->GetAttribute(NS_ConvertASCIItoUCS2("value"), value);
if (value.Length()) {
rv = LoadUrl(value.GetUnicode());
return rv;
}
if (APP_DEBUG) printf("Quitting LoadInitialPage\n");
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::BackButtonPopup(nsIDOMNode * aParent)
{
if (!mContentAreaDocShell) {
printf("nsBrowserInstance::BackButtonPopup Couldn't get a handle to SessionHistory\n");
return NS_ERROR_FAILURE;
}
PRBool hasChildren=PR_FALSE;
nsresult rv;
// Check if menu has children. If so, remove them.
rv = aParent->HasChildNodes(&hasChildren);
if (NS_SUCCEEDED(rv) && hasChildren) {
rv = ClearHistoryMenus(aParent);
if (!NS_SUCCEEDED(rv))
printf("nsBrowserInstance::BackButtonPopup ERROR While removing old history menu items\n");
} // hasChildren
else {
if (APP_DEBUG) printf("nsBrowserInstance::BackButtonPopup Menu has no children\n");
}
nsCOMPtr<nsISHistory> mSHistory;
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mContentAreaDocShell));
webNav->GetSessionHistory(getter_AddRefs(mSHistory));
NS_ENSURE_TRUE(mSHistory, NS_ERROR_FAILURE);
PRInt32 length=0,i=0, indix = 0;
//Get total length of the Session History
mSHistory->GetCount(&length);
mSHistory->GetIndex(&indix);
//Decide on the # of items in the popup list
if (indix > SHISTORY_POPUP_LIST)
i = indix-SHISTORY_POPUP_LIST;
for (PRInt32 j=indix-1;j>=i;j--) {
PRUnichar *title=nsnull;
nsCOMPtr<nsIURI> uri;
nsXPIDLCString uriSpec;
nsCOMPtr<nsISHEntry> shEntry;
mSHistory->GetEntryAtIndex(j, PR_FALSE, getter_AddRefs(shEntry));
NS_ENSURE_TRUE(shEntry, NS_ERROR_FAILURE);
shEntry->GetURI(getter_AddRefs(uri));
NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
uri->GetSpec(getter_Copies(uriSpec));
shEntry->GetTitle(&title);
nsAutoString histTitle(title);
if (APP_DEBUG)printf("nsBrowserInstance::BackButtonPopup URL = %s, TITLE = %s\n", (const char *) uriSpec, histTitle.ToNewCString());
rv = CreateMenuItem(aParent, j, title);
if (!NS_SUCCEEDED(rv)) {
printf("nsBrowserInstance::BackButtonPopup Error while creating history mene item\n");
}
Recycle(title);
} //for
return NS_OK;
} //BackButtonpopup
NS_IMETHODIMP
nsBrowserInstance::ForwardButtonPopup(nsIDOMNode * aParent)
{
if (!mContentAreaDocShell) {
printf("nsBrowserInstance::ForwardButtonPopup Couldn't get a handle to SessionHistory\n");
return NS_ERROR_FAILURE;
}
PRBool hasChildren=PR_FALSE;
nsresult rv;
// Check if menu has children. If so, remove them.
aParent->HasChildNodes(&hasChildren);
if (hasChildren) {
// Remove all old entries
rv = ClearHistoryMenus(aParent);
if (!NS_SUCCEEDED(rv)) {
printf("nsBrowserInstance::ForwardMenuPopup Error while clearing old history entries\n");
}
} // hasChildren
else {
if (APP_DEBUG) printf("nsBrowserInstance::ForwardButtonPopup Menu has no children\n");
}
nsCOMPtr<nsISHistory> mSHistory;
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mContentAreaDocShell));
webNav->GetSessionHistory(getter_AddRefs(mSHistory));
NS_ENSURE_TRUE(mSHistory, NS_ERROR_FAILURE);
PRInt32 length=0,i=0, indix = 0;
//Get total length of the Session History
mSHistory->GetCount(&length);
mSHistory->GetIndex(&indix);
//Decide on the # of items in the popup list
if ((length-indix) > SHISTORY_POPUP_LIST)
i = indix+SHISTORY_POPUP_LIST;
else
i = length;
for (PRInt32 j=indix+1;j<i;j++) {
PRUnichar *title=nsnull;
nsCOMPtr<nsIURI> uri;
nsXPIDLCString uriSpec;
nsCOMPtr<nsISHEntry> shEntry;
mSHistory->GetEntryAtIndex(j, PR_FALSE, getter_AddRefs(shEntry));
NS_ENSURE_TRUE(shEntry, NS_ERROR_FAILURE);
shEntry->GetURI(getter_AddRefs(uri));
NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
uri->GetSpec(getter_Copies(uriSpec));
shEntry->GetTitle(&title);
nsAutoString histTitle(title);
if (APP_DEBUG)printf("nsBrowserInstance::ForwardButtonPopup URL = %s, TITLE = %s\n", (const char *) uriSpec, histTitle.ToNewCString());
rv = CreateMenuItem(aParent, j, title);
if (!NS_SUCCEEDED(rv)) {
printf("nsBrowserInstance::ForwardButtonPopup Error while creating history mene item\n");
}
Recycle(title);
}
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::GotoHistoryIndex(PRInt32 aIndex)
{
nsCOMPtr<nsIWebShell> webShell(do_QueryInterface(mContentAreaDocShell));
webShell->GoTo(aIndex);
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::WalletPreview(nsIDOMWindow* aWin, nsIDOMWindow* aForm)
{
NS_PRECONDITION(aForm != nsnull, "null ptr");
if (! aForm)
return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIScriptGlobalObject> scriptGlobalObject;
scriptGlobalObject = do_QueryInterface(aForm);
nsCOMPtr<nsIDocShell> docShell;
scriptGlobalObject->GetDocShell(getter_AddRefs(docShell));
nsCOMPtr<nsIPresShell> presShell;
if(docShell)
docShell->GetPresShell(getter_AddRefs(presShell));
nsIWalletService *walletservice;
nsresult res = nsServiceManager::GetService(kWalletServiceCID,
kIWalletServiceIID,
(nsISupports **)&walletservice);
if (NS_SUCCEEDED(res) && (nsnull != walletservice)) {
res = walletservice->WALLET_Prefill(presShell, PR_FALSE);
nsServiceManager::ReleaseService(kWalletServiceCID, walletservice);
if (NS_FAILED(res)) { /* this just means that there was nothing to prefill */
return NS_OK;
}
} else {
return res;
}
nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryInterface(aWin));
NS_ENSURE_TRUE(sgo, NS_ERROR_FAILURE);
nsCOMPtr<nsIScriptContext> scriptContext;
sgo->GetContext(getter_AddRefs(scriptContext));
NS_ENSURE_TRUE(scriptContext, NS_ERROR_FAILURE);
JSContext* jsContext = (JSContext*)scriptContext->GetNativeContext();
NS_ENSURE_TRUE(jsContext, NS_ERROR_FAILURE);
void* mark;
jsval* argv;
argv = JS_PushArguments(jsContext, &mark, "sss", "chrome://communicator/content/wallet/WalletPreview.xul", "_blank",
//"chrome,dialog=yes,modal=yes,all");
"chrome,modal=yes,dialog=yes,all,width=504,height=436");
NS_ENSURE_TRUE(argv, NS_ERROR_FAILURE);
nsCOMPtr<nsIDOMWindow> newWindow;
aWin->OpenDialog(jsContext, argv, 3, getter_AddRefs(newWindow));
JS_PopArguments(jsContext, mark);
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::WalletChangePassword()
{
nsIWalletService *walletservice;
nsresult res;
res = nsServiceManager::GetService(kWalletServiceCID,
kIWalletServiceIID,
(nsISupports **)&walletservice);
if ((NS_OK == res) && (nsnull != walletservice)) {
res = walletservice->WALLET_ChangePassword();
nsServiceManager::ReleaseService(kWalletServiceCID, walletservice);
}
return NS_OK;
}
#include "nsIDOMHTMLDocument.h"
static NS_DEFINE_IID(kIDOMHTMLDocumentIID, NS_IDOMHTMLDOCUMENT_IID);
NS_IMETHODIMP
nsBrowserInstance::WalletQuickFillin(nsIDOMWindow* aWin)
{
NS_PRECONDITION(aWin != nsnull, "null ptr");
if (! aWin)
return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIScriptGlobalObject> scriptGlobalObject;
scriptGlobalObject = do_QueryInterface(aWin);
nsCOMPtr<nsIDocShell> docShell;
scriptGlobalObject->GetDocShell(getter_AddRefs(docShell));
nsCOMPtr<nsIPresShell> presShell;
if(docShell)
docShell->GetPresShell(getter_AddRefs(presShell));
nsIWalletService *walletservice;
nsresult res;
res = nsServiceManager::GetService(kWalletServiceCID,
kIWalletServiceIID,
(nsISupports **)&walletservice);
if ((NS_OK == res) && (nsnull != walletservice)) {
res = walletservice->WALLET_Prefill(presShell, PR_TRUE);
nsServiceManager::ReleaseService(kWalletServiceCID, walletservice);
return NS_OK;
} else {
return res;
}
}
NS_IMETHODIMP
nsBrowserInstance::WalletRequestToCapture(nsIDOMWindow* aWin)
{
NS_PRECONDITION(aWin != nsnull, "null ptr");
if (! aWin)
return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIScriptGlobalObject> scriptGlobalObject;
scriptGlobalObject = do_QueryInterface(aWin);
nsCOMPtr<nsIDocShell> docShell;
scriptGlobalObject->GetDocShell(getter_AddRefs(docShell));
nsCOMPtr<nsIPresShell> presShell;
if(docShell)
docShell->GetPresShell(getter_AddRefs(presShell));
nsIWalletService *walletservice;
nsresult res;
res = nsServiceManager::GetService(kWalletServiceCID,
kIWalletServiceIID,
(nsISupports **)&walletservice);
if ((NS_OK == res) && (nsnull != walletservice)) {
res = walletservice->WALLET_RequestToCapture(presShell);
nsServiceManager::ReleaseService(kWalletServiceCID, walletservice);
return NS_OK;
} else {
return res;
}
}
NS_IMETHODIMP
nsBrowserInstance::Init()
{
nsresult rv = NS_OK;
// register ourselves as a content listener with the uri dispatcher service
rv = NS_OK;
NS_WITH_SERVICE(nsIURILoader, dispatcher, NS_URI_LOADER_PROGID, &rv);
if (NS_SUCCEEDED(rv))
rv = dispatcher->RegisterContentListener(this);
return rv;
}
NS_IMETHODIMP
nsBrowserInstance::SetContentWindow(nsIDOMWindow* aWin)
{
NS_PRECONDITION(aWin != nsnull, "null ptr");
if (! aWin)
return NS_ERROR_NULL_POINTER;
mContentWindow = aWin;
// NS_ADDREF(aWin); WE DO NOT OWN THIS
// we do not own the script context, so don't addref it
nsCOMPtr<nsIScriptGlobalObject> globalObj(do_QueryInterface(aWin));
if(!globalObj)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIScriptContext> scriptContext;
globalObj->GetContext(getter_AddRefs(scriptContext));
mContentScriptContext = scriptContext;
nsCOMPtr<nsIDocShell> docShell;
globalObj->GetDocShell(getter_AddRefs(docShell));
nsCOMPtr<nsIWebShell> webShell(do_QueryInterface(docShell));
if (webShell) {
mContentAreaDocShell = docShell; // Weak reference
docShell->SetDocLoaderObserver((nsIDocumentLoaderObserver *)this);
nsCOMPtr<nsIWebProgress> webProgress(do_QueryInterface(docShell));
webProgress->AddProgressListener(NS_STATIC_CAST(nsIWebProgressListener*, this));
nsCOMPtr<nsISHistory> sessionHistory(do_CreateInstance(NS_SHISTORY_PROGID));
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell));
webNav->SetSessionHistory(sessionHistory);
// Cache the Document Loader for the content area webshell. This is a
// weak reference that is *not* reference counted...
nsCOMPtr<nsIDocumentLoader> docLoader;
webShell->GetDocumentLoader(*getter_AddRefs(docLoader));
mContentAreaDocLoader = docLoader.get();
nsCOMPtr<nsIDocShellTreeItem> docShellAsItem(do_QueryInterface(docShell));
nsXPIDLString name;
docShellAsItem->GetName(getter_Copies(name));
nsAutoString str(name);
if (APP_DEBUG) {
printf("Attaching to Content WebShell [%s]\n", (const char *)nsCAutoString(str));
}
}
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::GetContentDocShell(nsIDocShell** aDocShell)
{
NS_ENSURE_ARG_POINTER(aDocShell);
*aDocShell = mContentAreaDocShell;
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::SetWebShellWindow(nsIDOMWindow* aWin)
{
NS_ENSURE_ARG(aWin);
mDOMWindow = aWin;
nsCOMPtr<nsIScriptGlobalObject> globalObj( do_QueryInterface(aWin) );
if (!globalObj) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIDocShell> docShell;
globalObj->GetDocShell(getter_AddRefs(docShell));
if (docShell) {
mDocShell = docShell.get();
//NS_ADDREF(mDocShell); WE DO NOT OWN THIS
// inform our top level webshell that we are its parent URI content listener...
docShell->SetParentURIContentListener(this);
nsCOMPtr<nsIDocShellTreeItem> docShellAsItem(do_QueryInterface(docShell));
nsXPIDLString name;
docShellAsItem->GetName(getter_Copies(name));
nsAutoString str(name);
if (APP_DEBUG) {
printf("Attaching to WebShellWindow[%s]\n", (const char *)nsCAutoString(str));
}
nsCOMPtr<nsIWebShell> webShell(do_QueryInterface(docShell));
nsCOMPtr<nsIWebShellContainer> webShellContainer;
webShell->GetContainer(*getter_AddRefs(webShellContainer));
if (nsnull != webShellContainer)
{
nsCOMPtr<nsIWebShellWindow> webShellWin;
if (NS_OK == webShellContainer->QueryInterface(NS_GET_IID(nsIWebShellWindow), getter_AddRefs(webShellWin)))
{
mWebShellWin = webShellWin; // WE DO NOT OWN THIS
}
}
}
return NS_OK;
}
NS_IMETHODIMP
nsBrowserInstance::SetTextZoom(float aTextZoom)
{
nsCOMPtr<nsIScriptGlobalObject> globalObj( do_QueryInterface(mContentWindow) );
if (!globalObj) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIDocShell> docShell;
globalObj->GetDocShell(getter_AddRefs(docShell));
if (docShell)
{
nsCOMPtr<nsIContentViewer> childCV;
NS_ENSURE_SUCCESS(docShell->GetContentViewer(getter_AddRefs(childCV)), NS_ERROR_FAILURE);