aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/EphyUtils.cpp
blob: ab6d78e1a884870ecf8f04e40021c1ed63ffbe47 (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
/*
 *  Copyright (C) 2004 Marco Pesenti Gritti
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1, 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 Lesser 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$
 */

#include "mozilla-config.h"
#include "config.h"

#include <gtkmozembed.h>
#include <nsCOMPtr.h>
#include <nsIDOMWindow.h>
#include <nsIEmbeddingSiteWindow.h>
#include <nsIFile.h>
#include <nsIIOService.h>
#include <nsIPrintSettings.h>
#include <nsIServiceManager.h>
#include <nsIURI.h>
#include <nsIWebBrowserChrome.h>
#include <nsIWindowWatcher.h>
#include <nsServiceManagerUtils.h>
#include <nsStringAPI.h>
#include <nsXPCOM.h>
#include <nsIXPConnect.h>

#include "ephy-embed-shell.h"
#include "ephy-embed-single.h"
#include "ephy-file-helpers.h"

#include "EphyUtils.h"

nsresult
EphyUtils::GetIOService (nsIIOService **ioService)
{
    nsresult rv;

    nsCOMPtr<nsIServiceManager> mgr; 
    NS_GetServiceManager (getter_AddRefs (mgr));
    if (!mgr) return NS_ERROR_FAILURE;

    rv = mgr->GetServiceByContractID ("@mozilla.org/network/io-service;1",
                      NS_GET_IID (nsIIOService),
                      (void **)ioService);
    return rv;
}

nsresult
EphyUtils::NewURI (nsIURI **result,
           const nsAString &spec,
           const char *charset,
           nsIURI *baseURI)
{
    nsCString cSpec;
    NS_UTF16ToCString (spec, NS_CSTRING_ENCODING_UTF8, cSpec);

    return NewURI (result, cSpec, charset, baseURI);
}

nsresult
EphyUtils::NewURI (nsIURI **result,
           const nsACString &spec,
           const char *charset,
           nsIURI *baseURI)
{
    nsresult rv;
    nsCOMPtr<nsIIOService> ioService;
    rv = EphyUtils::GetIOService (getter_AddRefs (ioService));
    NS_ENSURE_SUCCESS (rv, rv);

    return ioService->NewURI (spec, charset, baseURI, result);
}

nsresult
EphyUtils::NewFileURI (nsIURI **result,
               nsIFile *spec)
{
    nsresult rv;
    nsCOMPtr<nsIIOService> ioService;
    rv = EphyUtils::GetIOService (getter_AddRefs (ioService));
    NS_ENSURE_SUCCESS (rv, rv);

    return ioService->NewFileURI (spec, result);
}

GtkWidget *
EphyUtils::FindEmbed (nsIDOMWindow *aDOMWindow)
{
    if (!aDOMWindow) return nsnull;

    nsCOMPtr<nsIWindowWatcher> wwatch
        (do_GetService("@mozilla.org/embedcomp/window-watcher;1"));
    NS_ENSURE_TRUE (wwatch, nsnull);

    /* this DOM window may belong to some inner frame, we need
     * to get the topmost DOM window to get the embed
     */
    nsCOMPtr<nsIDOMWindow> topWindow;
    aDOMWindow->GetTop (getter_AddRefs (topWindow));
    if (!topWindow) return nsnull;
    
    nsCOMPtr<nsIWebBrowserChrome> windowChrome;
    wwatch->GetChromeForWindow (topWindow, getter_AddRefs(windowChrome));
    NS_ENSURE_TRUE (windowChrome, nsnull);

    nsCOMPtr<nsIEmbeddingSiteWindow> window (do_QueryInterface(windowChrome));
    NS_ENSURE_TRUE (window, nsnull);

    nsresult rv;
    GtkWidget *mozembed;
    rv = window->GetSiteWindow ((void **)&mozembed);
    NS_ENSURE_SUCCESS (rv, nsnull);

    return mozembed;
}

GtkWidget *
EphyUtils::FindGtkParent (nsIDOMWindow *aDOMWindow)
{
    GtkWidget *embed = FindEmbed (aDOMWindow);
    NS_ENSURE_TRUE (embed, nsnull);

    GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (embed));
    if (!GTK_WIDGET_TOPLEVEL (toplevel)) return nsnull;

    return toplevel;
}

char *
EphyUtils::ConvertUTF16toUTF8 (const PRUnichar *aText,
                               PRInt32 aMaxLength)
{
        if (aText == nsnull) return NULL;

        /* This depends on the assumption that
         * typeof(PRUnichar) == typeof (gunichar2) == uint16,
         * which should be pretty safe.
         */
        glong n_read = 0, n_written = 0;
        char *converted = g_utf16_to_utf8 ((gunichar2*) aText, aMaxLength,
                                            &n_read, &n_written, NULL);
        /* FIXME loop from the end while !g_unichar_isspace (char)? */

        return converted;
}

/* This isn't completely accurate: if you do window.prompt in one window, then
 * call this in another window, it still returns TRUE ! Those are the wonders
 * of recursive mainloops :-(
 */
PRBool
EphyJSUtils::IsCalledFromScript ()
{
    nsresult rv;
    nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));
    NS_ENSURE_SUCCESS (rv, PR_FALSE);
        
    nsCOMPtr<nsIXPCNativeCallContext> ncc;
    rv = xpc->GetCurrentNativeCallContext (getter_AddRefs (ncc));
    NS_ENSURE_SUCCESS(rv, PR_FALSE);

    return nsnull != ncc;
}