1/*
2 * Copyright (C) 2012 Motorola Mobility Inc.
3 * Copyright (C) 2013 Google Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "PublicURLManager.h"
29#include <wtf/URL.h>
30#include "URLRegistry.h"
31#include <wtf/text/StringHash.h>
32
33namespace WebCore {
34
35std::unique_ptr<PublicURLManager> PublicURLManager::create(ScriptExecutionContext* context)
36{
37 auto publicURLManager = std::make_unique<PublicURLManager>(context);
38 publicURLManager->suspendIfNeeded();
39 return publicURLManager;
40}
41
42PublicURLManager::PublicURLManager(ScriptExecutionContext* context)
43 : ActiveDOMObject(context)
44 , m_isStopped(false)
45{
46}
47
48void PublicURLManager::registerURL(SecurityOrigin* origin, const URL& url, URLRegistrable& registrable)
49{
50 if (m_isStopped)
51 return;
52
53 RegistryURLMap::iterator found = m_registryToURL.add(&registrable.registry(), URLSet()).iterator;
54 found->key->registerURL(origin, url, registrable);
55 found->value.add(url.string());
56}
57
58void PublicURLManager::revoke(const URL& url)
59{
60 for (auto& registry : m_registryToURL) {
61 if (registry.value.contains(url.string())) {
62 registry.key->unregisterURL(url);
63 registry.value.remove(url.string());
64 break;
65 }
66 }
67}
68
69void PublicURLManager::stop()
70{
71 if (m_isStopped)
72 return;
73
74 m_isStopped = true;
75 for (auto& registry : m_registryToURL) {
76 for (auto& url : registry.value)
77 registry.key->unregisterURL(URL({ }, url));
78 }
79
80 m_registryToURL.clear();
81}
82
83bool PublicURLManager::canSuspendForDocumentSuspension() const
84{
85 // Suspending an PublicURLManager is safe as it does not cause any JS to be executed.
86 return true;
87}
88
89const char* PublicURLManager::activeDOMObjectName() const
90{
91 return "PublicURLManager";
92}
93
94} // namespace WebCore
95