aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/EphyBrowser.cpp
blob: 1cc78a29c3a8caccd8d7cb0644d5c4a7c6a99719 (plain) (blame)
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
/*
 *  Copyright (C) 2000-2004 Marco Pesenti Gritti
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "EphyBrowser.h"
#include "EphyUtils.h"
#include "MozillaPrivate.h"
#include "ephy-embed.h"
#include "ephy-string.h"
#include "ephy-debug.h"
#include "print-dialog.h"

#include <gtkmozembed_internal.h>
#include <unistd.h>

#include "nsIInterfaceRequestorUtils.h"
#include "nsIURI.h"
#include "nsISimpleEnumerator.h"

#include "nsIContentViewer.h"
#include "nsIWebBrowserFind.h"
#include "nsIWebBrowserFocus.h"
#include "nsICommandManager.h"
#include "nsIWebBrowserPrint.h"
#include "nsIDocCharset.h"
#include "nsIDocShellTreeItem.h"
#include "nsIDocShellTreeNode.h"
#include "nsIDocShellTreeOwner.h"
#include "nsIWebPageDescriptor.h"
#include "nsISHEntry.h"
#include "nsIHistoryEntry.h"
#include "nsIDOMHTMLDocument.h"
#include "nsIDOMHTMLCollection.h"
#include "nsIDOMHTMLElement.h"
#include "nsIDOMHTMLFormElement.h"
#include "nsIDOMHTMLInputElement.h"
#include "nsIDOMHTMLTextAreaElement.h"
#include "nsIDOMDocument.h"
#include "nsIDOM3Document.h"
#include "nsIDOMEvent.h"
#include "nsIDOMEventTarget.h"
#include "nsIDOMPopupBlockedEvent.h"
#include "nsIDOMNode.h"
#include "nsIDOMElement.h"
#include "nsIDOMWindow2.h"
#include "nsEmbedString.h"
#include "nsMemory.h"
#include "nsIChannel.h"

#ifdef ALLOW_PRIVATE_API
#include "nsIMarkupDocumentViewer.h"
#ifdef HAVE_MOZILLA_PSM
/* not sure about this one: */
#include <nsITransportSecurityInfo.h>
#endif
#endif

static PRUnichar DOMLinkAdded[] = { 'D', 'O', 'M', 'L', 'i', 'n', 'k',
                    'A', 'd', 'd', 'e', 'd', '\0' };
static PRUnichar DOMPopupBlocked[] = { 'D', 'O', 'M', 'P', 'o', 'p',
                       'u', 'p', 'B', 'l', 'o', 'c',
                       'k', 'e', 'd', '\0' };

EphyEventListener::EphyEventListener(void)
: mOwner(nsnull)
{
    LOG ("EphyEventListener ctor (%p)", this)
}

EphyEventListener::~EphyEventListener()
{
    LOG ("EphyEventListener dtor (%p)", this)
}

NS_IMPL_ISUPPORTS1(EphyEventListener, nsIDOMEventListener)

nsresult
EphyEventListener::Init(EphyEmbed *aOwner)
{
    mOwner = aOwner;
    return NS_OK;
}

nsresult
EphyFaviconEventListener::HandleFaviconLink (nsIDOMNode *node)
{
    nsresult result;

    nsCOMPtr<nsIDOMElement> linkElement;
    linkElement = do_QueryInterface (node);
    if (!linkElement) return NS_ERROR_FAILURE;

    PRUnichar relAttr[] = { 'r', 'e', 'l', '\0' };
    nsEmbedString value;
    result = linkElement->GetAttribute (nsEmbedString(relAttr), value);
    if (NS_FAILED (result)) return NS_ERROR_FAILURE;

    nsEmbedCString rel;
    NS_UTF16ToCString (value, NS_CSTRING_ENCODING_UTF8, rel);   

    if (g_ascii_strcasecmp (rel.get(), "SHORTCUT ICON") == 0 ||
        g_ascii_strcasecmp (rel.get(), "ICON") == 0)
    {
        PRUnichar hrefAttr[] = { 'h', 'r', 'e', 'f', '\0' };
        nsEmbedString value;
        result = linkElement->GetAttribute (nsEmbedString (hrefAttr), value);
        if (NS_FAILED (result) || !value.Length()) return NS_ERROR_FAILURE;

        nsEmbedCString link;
        NS_UTF16ToCString (value, NS_CSTRING_ENCODING_UTF8, link);

        nsCOMPtr<nsIDOMDocument> domDoc;
        node->GetOwnerDocument(getter_AddRefs(domDoc));
        NS_ENSURE_TRUE (domDoc, NS_ERROR_FAILURE);

        nsCOMPtr<nsIDOM3Document> doc = do_QueryInterface (domDoc);
        NS_ENSURE_TRUE (doc, NS_ERROR_FAILURE);

        nsEmbedString spec;
        result = doc->GetDocumentURI (spec);
        NS_ENSURE_SUCCESS (result, result);

        nsCOMPtr<nsIURI> uri;
        result = EphyUtils::NewURI (getter_AddRefs(uri), spec);
        NS_ENSURE_SUCCESS (result, result);

        nsEmbedCString favicon_url;
        result = uri->Resolve (link, favicon_url);
        if (NS_FAILED (result)) return NS_ERROR_FAILURE;
        
        char *url = g_strdup (favicon_url.get());
        g_signal_emit_by_name (mOwner, "ge_favicon", url);
        g_free (url);
    }

    return NS_OK;
}   

NS_IMETHODIMP
EphyFaviconEventListener::HandleEvent(nsIDOMEvent* aDOMEvent)
{
    nsCOMPtr<nsIDOMEventTarget> eventTarget;

    aDOMEvent->GetTarget(getter_AddRefs(eventTarget));

    nsCOMPtr<nsIDOMNode> node = do_QueryInterface(eventTarget);
    NS_ENSURE_TRUE (node, NS_ERROR_FAILURE);

    HandleFaviconLink (node);

    return NS_OK;
}

NS_IMETHODIMP
EphyPopupBlockEventListener::HandleEvent (nsIDOMEvent * aDOMEvent)
{
    nsresult rv;

    NS_ENSURE_TRUE (mOwner != NULL, NS_ERROR_FAILURE);

    nsCOMPtr<nsIDOMPopupBlockedEvent> popupEvent =
        do_QueryInterface (aDOMEvent, &rv);
    NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

    nsCOMPtr<nsIURI> popupWindowURI;
    rv = popupEvent->GetPopupWindowURI (getter_AddRefs (popupWindowURI));
    NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);
    NS_ENSURE_SUCCESS (popupWindowURI != NULL, NS_ERROR_FAILURE);

    nsEmbedCString popupWindowURIString;
    rv = popupWindowURI->GetSpec (popupWindowURIString);
    NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

    nsEmbedString popupWindowFeatures;
    rv = popupEvent->GetPopupWindowFeatures (popupWindowFeatures);
    NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

    nsEmbedCString popupWindowFeaturesString;
    NS_UTF16ToCString (popupWindowFeatures,
               NS_CSTRING_ENCODING_UTF8,
               popupWindowFeaturesString);

    g_signal_emit_by_name(mOwner, "ge_popup_blocked",
                  popupWindowURIString.get(),
                  popupWindowFeaturesString.get());

    return NS_OK;
}

EphyBrowser::EphyBrowser ()
: mFaviconEventListener(nsnull)
, mPopupBlockEventListener(nsnull)
, mInitialized(PR_FALSE)
{
}

EphyBrowser::~EphyBrowser ()
{
}

nsresult EphyBrowser::Init (GtkMozEmbed *mozembed)
{
    nsresult rv;

    if (mInitialized) return NS_OK;

    gtk_moz_embed_get_nsIWebBrowser (mozembed,
                     getter_AddRefs(mWebBrowser));
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    mWebBrowser->GetContentDOMWindow (getter_AddRefs (mDOMWindow));
    NS_ENSURE_TRUE (mDOMWindow, NS_ERROR_FAILURE);

    /* This will instantiate an about:blank doc if necessary */
    nsCOMPtr<nsIDOMDocument> domDocument;
    rv = mDOMWindow->GetDocument (getter_AddRefs (domDocument));
    NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

    mFaviconEventListener = new EphyFaviconEventListener();
    if (!mFaviconEventListener) return NS_ERROR_OUT_OF_MEMORY;

    rv = mFaviconEventListener->Init (EPHY_EMBED (mozembed));
    NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

    mPopupBlockEventListener = new EphyPopupBlockEventListener();
    if (!mPopupBlockEventListener) return NS_ERROR_OUT_OF_MEMORY;

    rv = mPopupBlockEventListener->Init (EPHY_EMBED (mozembed));
    NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

    rv = GetListener();
    NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

    mInitialized = PR_TRUE;

    return AttachListeners();
}

nsresult
EphyBrowser::GetListener (void)
{
    if (mEventReceiver) return NS_ERROR_FAILURE;

    nsCOMPtr<nsIDOMWindow> domWindowExternal;
    mWebBrowser->GetContentDOMWindow (getter_AddRefs(domWindowExternal));
  
    nsCOMPtr<nsIDOMWindow2> domWindow (do_QueryInterface (domWindowExternal));
    NS_ENSURE_TRUE (domWindow, NS_ERROR_FAILURE);
    
    nsCOMPtr<nsIDOMEventTarget> rootWindow;
    domWindow->GetWindowRoot (getter_AddRefs(rootWindow));

    mEventReceiver = do_QueryInterface (rootWindow);
    NS_ENSURE_TRUE (mEventReceiver, NS_ERROR_FAILURE);

    return NS_OK;
}

nsresult
EphyBrowser::AttachListeners(void)
{
    nsresult rv;

    NS_ENSURE_TRUE (mEventReceiver, NS_ERROR_FAILURE);

    nsCOMPtr<nsIDOMEventTarget> target;
    target = do_QueryInterface (mEventReceiver);

    rv = target->AddEventListener(nsEmbedString(DOMLinkAdded),
                      mFaviconEventListener, PR_FALSE);
    NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

    rv = target->AddEventListener(nsEmbedString(DOMPopupBlocked),
                      mPopupBlockEventListener, PR_FALSE);
    NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

    return NS_OK;
}

nsresult
EphyBrowser::DetachListeners(void)
{
    nsresult rv;

    if (!mEventReceiver) return NS_OK;

    nsCOMPtr<nsIDOMEventTarget> target;
    target = do_QueryInterface (mEventReceiver);
    NS_ENSURE_TRUE (target, NS_ERROR_FAILURE);

    rv = target->RemoveEventListener(nsEmbedString(DOMLinkAdded),
                     mFaviconEventListener, PR_FALSE);
    NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

    rv = target->RemoveEventListener(nsEmbedString(DOMPopupBlocked),
                     mPopupBlockEventListener, PR_FALSE);

    return NS_OK;
}

nsresult EphyBrowser::Print ()
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIWebBrowserPrint> print(do_GetInterface(mWebBrowser));
    NS_ENSURE_TRUE (print, NS_ERROR_FAILURE);

    return  print->Print (nsnull, nsnull);
}

nsresult EphyBrowser::SetPrintPreviewMode (PRBool previewMode)
{
    nsresult rv;

    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIWebBrowserPrint> print(do_GetInterface(mWebBrowser));
    NS_ENSURE_TRUE (print, NS_ERROR_FAILURE);

    if (previewMode)
    {
        EmbedPrintInfo *info;

        nsCOMPtr<nsIPrintSettings> settings;
        print->GetGlobalPrintSettings (getter_AddRefs(settings));

        info = ephy_print_get_print_info ();
        MozillaCollatePrintSettings (info, settings, TRUE);
        ephy_print_info_free (info);

        rv = print->PrintPreview (nsnull, mDOMWindow, nsnull);
    }
    else
    {
        PRBool isPreview = PR_FALSE;

        rv = print->GetDoingPrintPreview(&isPreview);
        NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);

        if (isPreview == PR_TRUE)
        {
            rv = print->ExitPrintPreview();
        }
    }

    return rv;
}

nsresult EphyBrowser::PrintPreviewNumPages (int *numPages)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIWebBrowserPrint> print(do_GetInterface(mWebBrowser));
    NS_ENSURE_TRUE (print, NS_ERROR_FAILURE);

    return print->GetPrintPreviewNumPages(numPages);
}

nsresult EphyBrowser::PrintPreviewNavigate(PRInt16 navType, PRInt32 pageNum)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIWebBrowserPrint> print(do_GetInterface(mWebBrowser));
    NS_ENSURE_TRUE (print, NS_ERROR_FAILURE);

    return print->PrintPreviewNavigate(navType, pageNum);
}

nsresult EphyBrowser::GetSHistory (nsISHistory **aSHistory)
{
    nsresult result;

    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIWebNavigation> ContentNav = do_QueryInterface (mWebBrowser);
    NS_ENSURE_TRUE (ContentNav, NS_ERROR_FAILURE);

    nsCOMPtr<nsISHistory> SessionHistory;
    result = ContentNav->GetSessionHistory (getter_AddRefs (SessionHistory));
    NS_ENSURE_TRUE (SessionHistory, NS_ERROR_FAILURE);

    *aSHistory = SessionHistory.get();
    NS_IF_ADDREF (*aSHistory);

    return NS_OK;
}

nsresult EphyBrowser::Destroy ()
{
    DetachListeners ();

        mWebBrowser = nsnull;
    mDOMWindow = nsnull;
    mEventReceiver = nsnull;

    mInitialized = PR_FALSE;

    return NS_OK;
}

nsresult EphyBrowser::GoToHistoryIndex (PRInt16 index)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIWebNavigation> ContentNav = do_QueryInterface (mWebBrowser);
    NS_ENSURE_TRUE (ContentNav, NS_ERROR_FAILURE);

    return ContentNav->GotoIndex (index);
}

/* Workaround for broken reload with frames, see mozilla bug
 * http://bugzilla.mozilla.org/show_bug.cgi?id=246392
 */
nsresult EphyBrowser::Reload (PRUint32 flags)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsISHistory> sessionHistory;
    GetSHistory (getter_AddRefs (sessionHistory));

    nsCOMPtr<nsIWebNavigation> webNavigation;
    webNavigation = do_QueryInterface (sessionHistory);

    if (!webNavigation)
    {
        webNavigation = do_QueryInterface (mWebBrowser);
    }
    NS_ENSURE_TRUE (webNavigation, NS_ERROR_FAILURE);

    PRUint32 reloadFlags;
    switch (flags)
    {
        case RELOAD_FORCE:
            reloadFlags = nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
                      nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
            break;
        case RELOAD_ENCODING_CHANGE:
            reloadFlags = nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE;
            break;
        case RELOAD_NORMAL:
        default:
            reloadFlags = 0;
            break;
    }

    return webNavigation->Reload (reloadFlags);
}

nsresult EphyBrowser::SetZoom (float aZoom)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIContentViewer> contentViewer;   
    GetContentViewer (getter_AddRefs(contentViewer));
    NS_ENSURE_TRUE (contentViewer, NS_ERROR_FAILURE);

    nsCOMPtr<nsIMarkupDocumentViewer> mdv = do_QueryInterface(contentViewer);
    NS_ENSURE_TRUE (mdv, NS_ERROR_FAILURE);

    return mdv->SetTextZoom (aZoom);
}

nsresult EphyBrowser::GetContentViewer (nsIContentViewer **aViewer)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIDocShell> ourDocShell(do_GetInterface(mWebBrowser));
    NS_ENSURE_TRUE (ourDocShell, NS_ERROR_FAILURE);

    return ourDocShell->GetContentViewer(aViewer);
}

nsresult EphyBrowser::GetZoom (float *aZoom)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIContentViewer> contentViewer;   
    GetContentViewer (getter_AddRefs(contentViewer));
    NS_ENSURE_TRUE (contentViewer, NS_ERROR_FAILURE);

    nsCOMPtr<nsIMarkupDocumentViewer> mdv = do_QueryInterface(contentViewer);
    NS_ENSURE_TRUE (mdv, NS_ERROR_FAILURE);

    return mdv->GetTextZoom (aZoom);
}

nsresult EphyBrowser::GetDocument (nsIDOMDocument **aDOMDocument)
{
    return mDOMWindow->GetDocument (aDOMDocument);
}

nsresult EphyBrowser::GetTargetDocument (nsIDOMDocument **aDOMDocument)
{
    nsresult result;

    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    /* Use the current target document */
    if (mTargetDocument)
    {
        *aDOMDocument = mTargetDocument.get();

        NS_IF_ADDREF(*aDOMDocument);

        return NS_OK;
    }

    /* Use the focused document */
    nsCOMPtr<nsIWebBrowserFocus> webBrowserFocus;
    webBrowserFocus = do_QueryInterface (mWebBrowser);
    NS_ENSURE_TRUE (webBrowserFocus, NS_ERROR_FAILURE);

    nsCOMPtr<nsIDOMWindow> DOMWindow;
    result = webBrowserFocus->GetFocusedWindow (getter_AddRefs(DOMWindow));
    if (NS_SUCCEEDED(result) && DOMWindow)
    {
        return DOMWindow->GetDocument (aDOMDocument);
    }

    /* Use the main document */
    return mDOMWindow->GetDocument (aDOMDocument);
}

nsresult EphyBrowser::GetSHInfo (PRInt32 *count, PRInt32 *index)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsISHistory> SessionHistory;
    GetSHistory (getter_AddRefs(SessionHistory));
    NS_ENSURE_TRUE (SessionHistory, NS_ERROR_FAILURE);

    SessionHistory->GetCount (count);
    SessionHistory->GetIndex (index);   

    return NS_OK;
}

nsresult EphyBrowser::GetSHTitleAtIndex (PRInt32 index, PRUnichar **title)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsISHistory> SessionHistory;
    GetSHistory (getter_AddRefs(SessionHistory));
    NS_ENSURE_TRUE (SessionHistory, NS_ERROR_FAILURE);

    nsCOMPtr<nsIHistoryEntry> he;
    SessionHistory->GetEntryAtIndex (index, PR_FALSE,
                     getter_AddRefs (he));
    NS_ENSURE_TRUE (he, NS_ERROR_FAILURE);

    nsresult result;
    result = he->GetTitle (title);
    NS_ENSURE_TRUE (NS_SUCCEEDED (result) && title, NS_ERROR_FAILURE);

    return NS_OK;
}

nsresult EphyBrowser::GetSHUrlAtIndex (PRInt32 index, nsACString &url)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsISHistory> SessionHistory;
    GetSHistory (getter_AddRefs(SessionHistory));
    NS_ENSURE_TRUE (SessionHistory, NS_ERROR_FAILURE);

    nsCOMPtr<nsIHistoryEntry> he;
    SessionHistory->GetEntryAtIndex (index, PR_FALSE,
                     getter_AddRefs (he));
    NS_ENSURE_TRUE (he, NS_ERROR_FAILURE);

    nsCOMPtr<nsIURI> uri;
    he->GetURI (getter_AddRefs(uri));
    NS_ENSURE_TRUE (uri, NS_ERROR_FAILURE);

    nsresult result;
    result = uri->GetSpec(url);
    NS_ENSURE_TRUE (NS_SUCCEEDED (result) && url.Length(), NS_ERROR_FAILURE);

    return NS_OK;
}

nsresult EphyBrowser::FindSetProperties (const PRUnichar *search_string,
                             PRBool case_sensitive,
                     PRBool wrap_around)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIWebBrowserFind> finder (do_GetInterface(mWebBrowser));
    NS_ENSURE_TRUE (finder, NS_ERROR_FAILURE);

    finder->SetSearchString (search_string);
    finder->SetMatchCase (case_sensitive);
    finder->SetWrapFind (wrap_around);

    return NS_OK;
}

nsresult EphyBrowser::Find (PRBool backwards,
                PRBool *didFind)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIWebBrowserFind> finder (do_GetInterface(mWebBrowser));
    NS_ENSURE_TRUE (finder, NS_ERROR_FAILURE);

    finder->SetFindBackwards (backwards);

    return finder->FindNext(didFind);
}

nsresult EphyBrowser::GetPageDescriptor(nsISupports **aPageDescriptor)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIDocShell> ds = do_GetInterface (mWebBrowser);

    nsCOMPtr<nsIWebPageDescriptor> wpd = do_QueryInterface (ds);
    NS_ENSURE_TRUE (wpd, NS_ERROR_FAILURE);

    *aPageDescriptor = wpd.get();
    NS_IF_ADDREF (*aPageDescriptor);

    return NS_OK;
}

nsresult EphyBrowser::GetDOMWindow (nsIDOMWindow **aDOMWindow)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    NS_IF_ADDREF (*aDOMWindow = mDOMWindow);

    return NS_OK;
}

nsresult EphyBrowser::GetDocumentUrl (nsACString &url)
{
    nsresult rv;

    NS_ENSURE_TRUE (mDOMWindow, NS_ERROR_FAILURE);

    nsCOMPtr<nsIDOMDocument> DOMDocument;
    mDOMWindow->GetDocument (getter_AddRefs(DOMDocument));
    NS_ENSURE_TRUE (DOMDocument, NS_ERROR_FAILURE);

    nsCOMPtr<nsIDOM3Document> doc = do_QueryInterface(DOMDocument);
    NS_ENSURE_TRUE (doc, NS_ERROR_FAILURE);

    nsEmbedString docURI;
    rv = doc->GetDocumentURI (docURI);
    NS_ENSURE_SUCCESS (rv, rv);

    NS_UTF16ToCString (docURI, NS_CSTRING_ENCODING_UTF8, url);

    return NS_OK;
}

nsresult EphyBrowser::GetTargetDocumentUrl (nsACString &url)
{
    nsresult rv;

    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

        nsCOMPtr<nsIDOMDocument> DOMDocument;
    GetTargetDocument (getter_AddRefs(DOMDocument));
    NS_ENSURE_TRUE (DOMDocument, NS_ERROR_FAILURE);

    nsCOMPtr<nsIDOM3Document> doc = do_QueryInterface(DOMDocument);
    NS_ENSURE_TRUE (doc, NS_ERROR_FAILURE);

    nsEmbedString docURI;
    rv = doc->GetDocumentURI (docURI);
    NS_ENSURE_SUCCESS (rv, rv);

    NS_UTF16ToCString (docURI, NS_CSTRING_ENCODING_UTF8, url);

    return NS_OK;
}

nsresult EphyBrowser::ForceEncoding (const char *encoding) 
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIContentViewer> contentViewer;   
    GetContentViewer (getter_AddRefs(contentViewer));
    NS_ENSURE_TRUE (contentViewer, NS_ERROR_FAILURE);

    nsCOMPtr<nsIMarkupDocumentViewer> mdv = do_QueryInterface(contentViewer);
    NS_ENSURE_TRUE (mdv, NS_ERROR_FAILURE);

    return mdv->SetForceCharacterSet (nsEmbedCString(encoding));
}

nsresult EphyBrowser::GetEncoding (nsACString &encoding)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIDocCharset> docCharset = do_GetInterface (mWebBrowser);
    NS_ENSURE_TRUE (docCharset, NS_ERROR_FAILURE);

    char *charset;
    docCharset->GetCharset (&charset);
    encoding = charset;
    nsMemory::Free (charset);

    return NS_OK;
}

nsresult EphyBrowser::GetForcedEncoding (nsACString &encoding)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIContentViewer> contentViewer;   
    GetContentViewer (getter_AddRefs(contentViewer));
    NS_ENSURE_TRUE (contentViewer, NS_ERROR_FAILURE);

    nsCOMPtr<nsIMarkupDocumentViewer> mdv = do_QueryInterface(contentViewer);
    NS_ENSURE_TRUE (mdv, NS_ERROR_FAILURE);

    nsresult result = mdv->GetForceCharacterSet (encoding);
    NS_ENSURE_SUCCESS (result, NS_ERROR_FAILURE);

    return NS_OK;
}

nsresult EphyBrowser::PushTargetDocument (nsIDOMDocument *domDoc)
{
    mTargetDocument = domDoc;

    return NS_OK;
}

nsresult EphyBrowser::PopTargetDocument ()
{
    mTargetDocument = nsnull;

    return NS_OK;
}

nsresult EphyBrowser::DoCommand (const char *command)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsICommandManager> cmdManager;
    cmdManager = do_GetInterface (mWebBrowser);
    NS_ENSURE_TRUE (cmdManager, NS_ERROR_FAILURE);

    return cmdManager->DoCommand (command, nsnull, nsnull);
}

nsresult EphyBrowser::GetCommandState (const char *command, PRBool *enabled)
{
    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsICommandManager> cmdManager;
    cmdManager = do_GetInterface (mWebBrowser);
    NS_ENSURE_TRUE (cmdManager, NS_ERROR_FAILURE);

    return cmdManager->IsCommandEnabled (command, nsnull, enabled);
}

#define NUM_MODIFIED_TEXTFIELDS_REQUIRED    2

PRBool
EphyBrowser::CompareFormsText (nsAString &aDefaultText, nsAString &aUserText)
{
    if (aDefaultText.Length() != aUserText.Length())
    {
        return FALSE;
    }

    /* Mozilla Bug 218277, 195946 and others */
    const PRUnichar *text = aDefaultText.BeginReading();
    for (PRUint32 i = 0; i < aDefaultText.Length(); i++)
    {
        if (text[i] == 0xa0)
        {
            aDefaultText.Replace (i, 1, ' ');
        }
    }

    return (memcmp (aDefaultText.BeginReading(),
                aUserText.BeginReading(),
                aUserText.Length() * sizeof (PRUnichar)) == 0);
}

nsresult EphyBrowser::GetDocumentHasModifiedForms (nsIDOMDocument *aDomDoc, PRUint32 *aNumTextFields, PRBool *aHasTextArea)
{
    nsCOMPtr<nsIDOMHTMLDocument> htmlDoc = do_QueryInterface(aDomDoc);
    /* it's okay not to be a HTML doc (happens for XUL documents, like about:config) */
    if (!htmlDoc) return NS_OK;

    nsCOMPtr<nsIDOMHTMLCollection> forms;
    htmlDoc->GetForms (getter_AddRefs (forms));
    if (!forms) return NS_OK; /* it's ok not to have any forms */

    PRUint32 formNum;
    forms->GetLength (&formNum);

    /* check all forms */
    for (PRUint32 formIndex = 0; formIndex < formNum; formIndex++)
    {
        nsCOMPtr<nsIDOMNode> formNode;
        forms->Item (formIndex, getter_AddRefs (formNode));
        if (!formNode) continue;

        nsCOMPtr<nsIDOMHTMLFormElement> formElement = do_QueryInterface (formNode);
        if (!formElement) continue;

        nsCOMPtr<nsIDOMHTMLCollection> formElements;
        formElement->GetElements (getter_AddRefs (formElements));
        if (!formElements) continue;

        PRUint32 elementNum;
        formElements->GetLength (&elementNum);

        /* check all input elements in the form for user input */
        for (PRUint32 elementIndex = 0; elementIndex < elementNum; elementIndex++)
        {
            nsCOMPtr<nsIDOMNode> domNode;
            formElements->Item (elementIndex, getter_AddRefs (domNode));
            if (!domNode) continue;

            nsCOMPtr<nsIDOMHTMLTextAreaElement> areaElement = do_QueryInterface (domNode);
            if (areaElement)
            {
                nsEmbedString defaultText, userText;
                areaElement->GetDefaultValue (defaultText);
                areaElement->GetValue (userText);

                if (!CompareFormsText (defaultText, userText))
                {
                    *aHasTextArea = PR_TRUE;
                    return NS_OK;
                }

                continue;
            }

            nsCOMPtr<nsIDOMHTMLInputElement> inputElement = do_QueryInterface(domNode);
            if (!inputElement) continue;
    
            nsEmbedString type;
            inputElement->GetType(type);

            nsEmbedCString cType;
            NS_UTF16ToCString (type, NS_CSTRING_ENCODING_UTF8, cType);

            if (g_ascii_strcasecmp (cType.get(), "text") == 0)
            {
                nsEmbedString defaultText, userText;
                PRInt32 max_length;
                inputElement->GetDefaultValue (defaultText);
                inputElement->GetValue (userText);
                inputElement->GetMaxLength (&max_length);

                /* There are forms for which defaultValue is longer than
                 * userValue. Mozilla consider this not a bug [see WONTFIXed
                 * bug 232057], but we need to check for this here.
                 */
                if (defaultText.Length() > (PRUint32)max_length)
                {
                    defaultText.Cut (max_length, PR_UINT32_MAX);
                }

                if (!CompareFormsText (defaultText, userText))
                {
                    (*aNumTextFields)++;
                    if (*aNumTextFields >= NUM_MODIFIED_TEXTFIELDS_REQUIRED)
                    {
                        return NS_OK;
                    }
                }
            }
        }
    }

    return NS_OK;
}

nsresult EphyBrowser::GetHasModifiedForms (PRBool *modified)
{
    *modified = PR_FALSE;

    NS_ENSURE_TRUE (mWebBrowser, NS_ERROR_FAILURE);

    nsCOMPtr<nsIDocShell> rootDocShell = do_GetInterface (mWebBrowser);
    NS_ENSURE_TRUE (rootDocShell, NS_ERROR_FAILURE);

    nsCOMPtr<nsISimpleEnumerator> enumerator;
    rootDocShell->GetDocShellEnumerator(nsIDocShellTreeItem::typeContent,
                        nsIDocShell::ENUMERATE_FORWARDS,
                        getter_AddRefs(enumerator));
    NS_ENSURE_TRUE (enumerator, NS_ERROR_FAILURE);

    PRBool hasMore;
    PRBool hasTextArea = PR_FALSE;
    PRUint32 numTextFields = 0;
    while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore)
    {
        nsCOMPtr<nsISupports> element;
        enumerator->GetNext (getter_AddRefs(element));
        if (!element) continue;

        nsCOMPtr<nsIDocShell> docShell = do_QueryInterface (element);
        if (!docShell) continue;

        nsCOMPtr<nsIContentViewer> contentViewer;
        docShell->GetContentViewer (getter_AddRefs(contentViewer));
        if (!contentViewer) continue;

        nsCOMPtr<nsIDOMDocument> domDoc;
        contentViewer->GetDOMDocument (getter_AddRefs (domDoc));

        nsresult result;
        result = GetDocumentHasModifiedForms (domDoc, &numTextFields, &hasTextArea);
        if (NS_SUCCEEDED (result) &&
            (numTextFields >= NUM_MODIFIED_TEXTFIELDS_REQUIRED || hasTextArea))
        {
            *modified = PR_TRUE;
            break;
        }
    }

    return NS_OK;
}

nsresult
EphyBrowser::SetSecurityInfo (nsIRequest *aRequest)
{
#ifdef HAVE_MOZILLA_PSM
    /* clear previous security info */
    mSecurityInfo = nsnull;

    nsCOMPtr<nsIChannel> channel (do_QueryInterface (aRequest));
    NS_ENSURE_TRUE (channel, NS_ERROR_FAILURE);

    channel->GetSecurityInfo (getter_AddRefs (mSecurityInfo));

    return NS_OK;
#else
    return NS_ERROR_NOT_IMPLEMENTED;
#endif
}

nsresult
EphyBrowser::GetSecurityDescription (nsACString &aDescription)
{
#ifdef HAVE_MOZILLA_PSM
    if (!mSecurityInfo) return NS_ERROR_FAILURE;

    nsCOMPtr<nsITransportSecurityInfo> tsInfo (do_QueryInterface (mSecurityInfo));
    NS_ENSURE_TRUE (tsInfo, NS_ERROR_FAILURE);

    nsresult rv;
    PRUnichar *tooltip = nsnull;
    rv = tsInfo->GetShortSecurityDescription (&tooltip);
    NS_ENSURE_TRUE (NS_SUCCEEDED (rv) && tooltip, NS_ERROR_FAILURE);

    NS_UTF16ToCString (nsEmbedString (tooltip),
               NS_CSTRING_ENCODING_UTF8, aDescription);
    if (tooltip) nsMemory::Free (tooltip);

    return NS_OK;
#else
    return NS_ERROR_NOT_IMPLEMENTED;
#endif
}