2 * Copyright (C) 2011, Google Inc. All rights reserved.
3 * Copyright (C) 2012, Samsung Electronics. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28 #include "NavigatorContentUtils.h"
30 #if ENABLE(NAVIGATOR_CONTENT_UTILS)
33 #include "ExceptionCode.h"
35 #include "Navigator.h"
37 #include <wtf/HashSet.h>
41 static HashSet<String>* protocolWhitelist;
43 static void initProtocolHandlerWhitelist()
45 protocolWhitelist = new HashSet<String>;
46 #if !PLATFORM(BLACKBERRY)
47 static const char* protocols[] = {
68 for (size_t i = 0; i < WTF_ARRAY_LENGTH(protocols); ++i)
69 protocolWhitelist->add(protocols[i]);
73 static bool verifyCustomHandlerURL(const String& baseURL, const String& url, ExceptionCode& ec)
75 // The specification requires that it is a SYNTAX_ERR if the "%s" token is
77 static const char token[] = "%s";
78 int index = url.find(token);
84 // It is also a SYNTAX_ERR if the custom handler URL, as created by removing
85 // the "%s" token and prepending the base url, does not resolve.
87 newURL.remove(index, WTF_ARRAY_LENGTH(token) - 1);
89 KURL base(ParsedURLString, baseURL);
90 KURL kurl(base, newURL);
92 if (kurl.isEmpty() || !kurl.isValid()) {
100 static bool isProtocolWhitelisted(const String& scheme)
102 if (!protocolWhitelist)
103 initProtocolHandlerWhitelist();
104 return protocolWhitelist->contains(scheme);
107 static bool verifyProtocolHandlerScheme(const String& scheme, ExceptionCode& ec)
109 if (scheme.startsWith("web+")) {
110 if (isValidProtocol(scheme))
116 if (isProtocolWhitelisted(scheme))
122 NavigatorContentUtils* NavigatorContentUtils::from(Page* page)
124 return static_cast<NavigatorContentUtils*>(RefCountedSupplement<Page, NavigatorContentUtils>::from(page, NavigatorContentUtils::supplementName()));
127 NavigatorContentUtils::~NavigatorContentUtils()
131 PassRefPtr<NavigatorContentUtils> NavigatorContentUtils::create(NavigatorContentUtilsClient* client)
133 return adoptRef(new NavigatorContentUtils(client));
136 void NavigatorContentUtils::registerProtocolHandler(Navigator* navigator, const String& scheme, const String& url, const String& title, ExceptionCode& ec)
138 if (!navigator->frame())
141 Document* document = navigator->frame()->document();
145 String baseURL = document->baseURL().baseAsString();
147 if (!verifyCustomHandlerURL(baseURL, url, ec))
150 if (!verifyProtocolHandlerScheme(scheme, ec))
153 NavigatorContentUtils::from(navigator->frame()->page())->client()->registerProtocolHandler(scheme, baseURL, url, navigator->frame()->displayStringModifiedByEncoding(title));
156 #if ENABLE(CUSTOM_SCHEME_HANDLER)
157 static String customHandlersStateString(const NavigatorContentUtilsClient::CustomHandlersState state)
159 DEFINE_STATIC_LOCAL(const String, newHandler, (ASCIILiteral("new")));
160 DEFINE_STATIC_LOCAL(const String, registeredHandler, (ASCIILiteral("registered")));
161 DEFINE_STATIC_LOCAL(const String, declinedHandler, (ASCIILiteral("declined")));
164 case NavigatorContentUtilsClient::CustomHandlersNew:
166 case NavigatorContentUtilsClient::CustomHandlersRegistered:
167 return registeredHandler;
168 case NavigatorContentUtilsClient::CustomHandlersDeclined:
169 return declinedHandler;
172 ASSERT_NOT_REACHED();
176 String NavigatorContentUtils::isProtocolHandlerRegistered(Navigator* navigator, const String& scheme, const String& url, ExceptionCode& ec)
178 DEFINE_STATIC_LOCAL(const String, declined, ("declined"));
180 if (!navigator->frame())
183 Document* document = navigator->frame()->document();
184 String baseURL = document->baseURL().baseAsString();
186 if (!verifyCustomHandlerURL(baseURL, url, ec))
189 if (!verifyProtocolHandlerScheme(scheme, ec))
192 return customHandlersStateString(NavigatorContentUtils::from(navigator->frame()->page())->client()->isProtocolHandlerRegistered(scheme, baseURL, url));
195 void NavigatorContentUtils::unregisterProtocolHandler(Navigator* navigator, const String& scheme, const String& url, ExceptionCode& ec)
197 if (!navigator->frame())
200 Document* document = navigator->frame()->document();
201 String baseURL = document->baseURL().baseAsString();
203 if (!verifyCustomHandlerURL(baseURL, url, ec))
206 if (!verifyProtocolHandlerScheme(scheme, ec))
209 NavigatorContentUtils::from(navigator->frame()->page())->client()->unregisterProtocolHandler(scheme, baseURL, url);
213 const char* NavigatorContentUtils::supplementName()
215 return "NavigatorContentUtils";
218 void provideNavigatorContentUtilsTo(Page* page, NavigatorContentUtilsClient* client)
220 RefCountedSupplement<Page, NavigatorContentUtils>::provideTo(page, NavigatorContentUtils::supplementName(), NavigatorContentUtils::create(client));
223 } // namespace WebCore
225 #endif // ENABLE(NAVIGATOR_CONTENT_UTILS)