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"
36 #include "PointerLock.h"
40 #include "PageGroup.h"
41 #include "PlatformString.h"
42 #include "PluginData.h"
44 #include "StorageNamespace.h"
45 #include <wtf/HashSet.h>
46 #include <wtf/StdLibExtras.h>
49 #include "GamepadList.h"
53 #if ENABLE(MEDIA_STREAM)
54 #include "NavigatorUserMediaErrorCallback.h"
55 #include "NavigatorUserMediaSuccessCallback.h"
56 #include "UserMediaRequest.h"
61 Navigator::Navigator(Frame* frame)
66 Navigator::~Navigator()
71 void Navigator::resetGeolocation()
74 m_geolocation->reset();
77 void Navigator::disconnectFrame()
80 m_plugins->disconnectFrame();
84 m_mimeTypes->disconnectFrame();
88 m_geolocation->disconnectFrame();
94 // If this function returns true, we need to hide the substring "4." that would otherwise
95 // appear in the appVersion string. This is to avoid problems with old versions of a
96 // library called OpenCube QuickMenu, which as of this writing is still being used on
97 // sites such as nwa.com -- the library thinks Safari is Netscape 4 if we don't do this!
98 static bool shouldHideFourDot(Frame* frame)
100 const String* sourceURL = frame->script()->sourceURL();
103 if (!(sourceURL->endsWith("/dqm_script.js") || sourceURL->endsWith("/dqm_loader.js") || sourceURL->endsWith("/tdqm_loader.js")))
105 Settings* settings = frame->settings();
108 return settings->needsSiteSpecificQuirks();
111 String Navigator::appVersion() const
115 String appVersion = NavigatorBase::appVersion();
116 if (shouldHideFourDot(m_frame))
117 appVersion.replace("4.", "4_");
121 String Navigator::language() const
123 return defaultLanguage();
126 String Navigator::userAgent() const
131 // If the frame is already detached, FrameLoader::userAgent may malfunction, because it calls a client method
132 // that uses frame's WebView (at least, in Mac WebKit).
133 if (!m_frame->page())
136 return m_frame->loader()->userAgent(m_frame->document()->url());
139 DOMPluginArray* Navigator::plugins() const
142 m_plugins = DOMPluginArray::create(m_frame);
143 return m_plugins.get();
146 DOMMimeTypeArray* Navigator::mimeTypes() const
149 m_mimeTypes = DOMMimeTypeArray::create(m_frame);
150 return m_mimeTypes.get();
153 bool Navigator::cookieEnabled() const
158 if (m_frame->page() && !m_frame->page()->cookieEnabled())
161 return cookiesEnabled(m_frame->document());
164 bool Navigator::javaEnabled() const
166 if (!m_frame || !m_frame->settings())
169 return m_frame->settings()->isJavaEnabled();
172 Geolocation* Navigator::geolocation() const
175 m_geolocation = Geolocation::create(m_frame);
176 return m_geolocation.get();
179 #if ENABLE(POINTER_LOCK)
180 PointerLock* Navigator::webkitPointer() const
183 m_pointer = PointerLock::create();
184 return m_pointer.get();
188 void Navigator::getStorageUpdates()
190 // FIXME: Remove this method or rename to yieldForStorageUpdates.
193 #if ENABLE(REGISTER_PROTOCOL_HANDLER)
194 static HashSet<String>* protocolWhitelist;
196 static void initProtocolHandlerWhitelist()
198 protocolWhitelist = new HashSet<String>;
199 static const char* protocols[] = {
211 for (size_t i = 0; i < WTF_ARRAY_LENGTH(protocols); ++i)
212 protocolWhitelist->add(protocols[i]);
215 static bool verifyCustomHandlerURL(const String& baseURL, const String& url, ExceptionCode& ec)
217 // The specification requires that it is a SYNTAX_ERR if the "%s" token is
219 static const char token[] = "%s";
220 int index = url.find(token);
226 // It is also a SYNTAX_ERR if the custom handler URL, as created by removing
227 // the "%s" token and prepending the base url, does not resolve.
229 newURL.remove(index, WTF_ARRAY_LENGTH(token) - 1);
231 KURL base(ParsedURLString, baseURL);
232 KURL kurl(base, newURL);
234 if (kurl.isEmpty() || !kurl.isValid()) {
242 static bool isProtocolWhitelisted(const String& scheme)
244 if (!protocolWhitelist)
245 initProtocolHandlerWhitelist();
246 return protocolWhitelist->contains(scheme);
249 static bool verifyProtocolHandlerScheme(const String& scheme, ExceptionCode& ec)
251 if (scheme.startsWith("web+")) {
252 if (isValidProtocol(scheme))
258 if (isProtocolWhitelisted(scheme))
264 void Navigator::registerProtocolHandler(const String& scheme, const String& url, const String& title, ExceptionCode& ec)
269 Document* document = m_frame->document();
273 String baseURL = document->baseURL().baseAsString();
275 if (!verifyCustomHandlerURL(baseURL, url, ec))
278 if (!verifyProtocolHandlerScheme(scheme, ec))
281 Page* page = m_frame->page();
285 page->chrome()->registerProtocolHandler(scheme, baseURL, url, m_frame->displayStringModifiedByEncoding(title));
289 #if ENABLE(MEDIA_STREAM)
290 void Navigator::webkitGetUserMedia(const String& options, PassRefPtr<NavigatorUserMediaSuccessCallback> successCallback, PassRefPtr<NavigatorUserMediaErrorCallback> errorCallback, ExceptionCode& ec)
292 if (!successCallback)
298 Page* page = m_frame->page();
302 RefPtr<UserMediaRequest> request = UserMediaRequest::create(m_frame->document(), page->userMediaClient(), options, successCallback, errorCallback);
304 ec = NOT_SUPPORTED_ERR;
313 GamepadList* Navigator::webkitGamepads()
316 m_gamepads = GamepadList::create();
317 sampleGamepads(m_gamepads.get());
318 return m_gamepads.get();
322 } // namespace WebCore