2 * Copyright (C) 2000 Harri Porten (porten@kde.org)
3 * Copyright (c) 2000 Daniel Molkentin (molkentin@kde.org)
4 * Copyright (c) 2000 Stefan Schimanski (schimmi@kde.org)
5 * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.
6 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "Navigator.h"
27 #include "CookieJar.h"
28 #include "DOMMimeTypeArray.h"
29 #include "DOMPluginArray.h"
31 #include "ExceptionCode.h"
33 #include "FrameLoader.h"
34 #include "FrameLoaderClient.h"
35 #include "Geolocation.h"
38 #include "MediaStreamFrameController.h"
39 #include "NavigatorUserMediaErrorCallback.h"
40 #include "NavigatorUserMediaSuccessCallback.h"
42 #include "PageGroup.h"
43 #include "PlatformString.h"
44 #include "PluginData.h"
46 #include "StorageNamespace.h"
47 #include <wtf/HashSet.h>
48 #include <wtf/StdLibExtras.h>
52 Navigator::Navigator(Frame* frame)
57 Navigator::~Navigator()
62 void Navigator::resetGeolocation()
65 m_geolocation->reset();
68 void Navigator::disconnectFrame()
71 m_plugins->disconnectFrame();
75 m_mimeTypes->disconnectFrame();
79 m_geolocation->disconnectFrame();
85 // If this function returns true, we need to hide the substring "4." that would otherwise
86 // appear in the appVersion string. This is to avoid problems with old versions of a
87 // library called OpenCube QuickMenu, which as of this writing is still being used on
88 // sites such as nwa.com -- the library thinks Safari is Netscape 4 if we don't do this!
89 static bool shouldHideFourDot(Frame* frame)
91 const String* sourceURL = frame->script()->sourceURL();
94 if (!(sourceURL->endsWith("/dqm_script.js") || sourceURL->endsWith("/dqm_loader.js") || sourceURL->endsWith("/tdqm_loader.js")))
96 Settings* settings = frame->settings();
99 return settings->needsSiteSpecificQuirks();
102 String Navigator::appVersion() const
106 String appVersion = NavigatorBase::appVersion();
107 if (shouldHideFourDot(m_frame))
108 appVersion.replace("4.", "4_");
112 String Navigator::language() const
114 return defaultLanguage();
117 String Navigator::userAgent() const
122 // If the frame is already detached, FrameLoader::userAgent may malfunction, because it calls a client method
123 // that uses frame's WebView (at least, in Mac WebKit).
124 if (!m_frame->page())
127 return m_frame->loader()->userAgent(m_frame->document()->url());
130 DOMPluginArray* Navigator::plugins() const
133 m_plugins = DOMPluginArray::create(m_frame);
134 return m_plugins.get();
137 DOMMimeTypeArray* Navigator::mimeTypes() const
140 m_mimeTypes = DOMMimeTypeArray::create(m_frame);
141 return m_mimeTypes.get();
144 bool Navigator::cookieEnabled() const
149 if (m_frame->page() && !m_frame->page()->cookieEnabled())
152 return cookiesEnabled(m_frame->document());
155 bool Navigator::javaEnabled() const
157 if (!m_frame || !m_frame->settings())
160 return m_frame->settings()->isJavaEnabled();
163 Geolocation* Navigator::geolocation() const
166 m_geolocation = Geolocation::create(m_frame);
167 return m_geolocation.get();
170 #if ENABLE(DOM_STORAGE)
171 void Navigator::getStorageUpdates()
176 Page* page = m_frame->page();
180 StorageNamespace* localStorage = page->group().localStorage();
182 localStorage->unlock();
186 #if ENABLE(REGISTER_PROTOCOL_HANDLER)
187 static HashSet<String>* protocolWhitelist;
189 static void initProtocolHandlerWhitelist()
191 protocolWhitelist = new HashSet<String>;
192 static const char* protocols[] = {
204 for (size_t i = 0; i < WTF_ARRAY_LENGTH(protocols); ++i)
205 protocolWhitelist->add(protocols[i]);
208 static bool verifyCustomHandlerURL(const String& baseURL, const String& url, ExceptionCode& ec)
210 // The specification requires that it is a SYNTAX_ERR if the "%s" token is
212 static const char token[] = "%s";
213 int index = url.find(token);
219 // It is also a SYNTAX_ERR if the custom handler URL, as created by removing
220 // the "%s" token and prepending the base url, does not resolve.
222 newURL.remove(index, WTF_ARRAY_LENGTH(token) - 1);
224 KURL base(ParsedURLString, baseURL);
225 KURL kurl(base, newURL);
227 if (kurl.isEmpty() || !kurl.isValid()) {
235 static bool isProtocolWhitelisted(const String& scheme)
237 if (!protocolWhitelist)
238 initProtocolHandlerWhitelist();
239 return protocolWhitelist->contains(scheme);
242 static bool verifyProtocolHandlerScheme(const String& scheme, ExceptionCode& ec)
244 if (scheme.startsWith("web+")) {
245 if (isValidProtocol(scheme))
251 if (isProtocolWhitelisted(scheme))
257 void Navigator::registerProtocolHandler(const String& scheme, const String& url, const String& title, ExceptionCode& ec)
262 Document* document = m_frame->document();
266 String baseURL = document->baseURL().baseAsString();
268 if (!verifyCustomHandlerURL(baseURL, url, ec))
271 if (!verifyProtocolHandlerScheme(scheme, ec))
274 Page* page = m_frame->page();
278 page->chrome()->registerProtocolHandler(scheme, baseURL, url, m_frame->displayStringModifiedByEncoding(title));
282 #if ENABLE(MEDIA_STREAM)
283 void Navigator::webkitGetUserMedia(const String& options, PassRefPtr<NavigatorUserMediaSuccessCallback> successCallback, PassRefPtr<NavigatorUserMediaErrorCallback> errorCallback, ExceptionCode& ec)
285 if (m_frame && m_frame->mediaStreamFrameController())
286 m_frame->mediaStreamFrameController()->generateStream(options, successCallback, errorCallback, ec);
290 } // namespace WebCore