1/*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ApplicationCache.h"
28
29#include "ApplicationCacheGroup.h"
30#include "ApplicationCacheResource.h"
31#include "ApplicationCacheStorage.h"
32#include "ResourceRequest.h"
33#include <algorithm>
34#include <stdio.h>
35#include <wtf/text/CString.h>
36
37namespace WebCore {
38
39static inline bool fallbackURLLongerThan(const std::pair<URL, URL>& lhs, const std::pair<URL, URL>& rhs)
40{
41 return lhs.first.string().length() > rhs.first.string().length();
42}
43
44ApplicationCache::ApplicationCache()
45{
46}
47
48ApplicationCache::~ApplicationCache()
49{
50 if (m_group)
51 m_group->cacheDestroyed(*this);
52}
53
54void ApplicationCache::setGroup(ApplicationCacheGroup* group)
55{
56 ASSERT(!m_group || group == m_group);
57 m_group = group;
58}
59
60bool ApplicationCache::isComplete()
61{
62 return m_group && m_group->cacheIsComplete(*this);
63}
64
65void ApplicationCache::setManifestResource(Ref<ApplicationCacheResource>&& manifest)
66{
67 ASSERT(!m_manifest);
68 ASSERT(manifest->type() & ApplicationCacheResource::Manifest);
69
70 m_manifest = manifest.ptr();
71
72 addResource(WTFMove(manifest));
73}
74
75void ApplicationCache::addResource(Ref<ApplicationCacheResource>&& resource)
76{
77 auto& url = resource->url();
78
79 ASSERT(!URL({ }, url).hasFragmentIdentifier());
80 ASSERT(!m_resources.contains(url));
81
82 if (m_storageID) {
83 ASSERT(!resource->storageID());
84 ASSERT(resource->type() & ApplicationCacheResource::Master);
85
86 // Add the resource to the storage.
87 m_group->storage().store(resource.ptr(), this);
88 }
89
90 m_estimatedSizeInStorage += resource->estimatedSizeInStorage();
91
92 m_resources.set(url, WTFMove(resource));
93}
94
95ApplicationCacheResource* ApplicationCache::resourceForURL(const String& url)
96{
97 ASSERT(!URL({ }, url).hasFragmentIdentifier());
98 return m_resources.get(url);
99}
100
101bool ApplicationCache::requestIsHTTPOrHTTPSGet(const ResourceRequest& request)
102{
103 return request.url().protocolIsInHTTPFamily() && equalLettersIgnoringASCIICase(request.httpMethod(), "get");
104}
105
106ApplicationCacheResource* ApplicationCache::resourceForRequest(const ResourceRequest& request)
107{
108 // We only care about HTTP/HTTPS GET requests.
109 if (!requestIsHTTPOrHTTPSGet(request))
110 return nullptr;
111
112 URL url(request.url());
113 url.removeFragmentIdentifier();
114 return resourceForURL(url);
115}
116
117void ApplicationCache::setOnlineWhitelist(const Vector<URL>& onlineWhitelist)
118{
119 ASSERT(m_onlineWhitelist.isEmpty());
120 m_onlineWhitelist = onlineWhitelist;
121}
122
123bool ApplicationCache::isURLInOnlineWhitelist(const URL& url)
124{
125 for (auto& whitelistURL : m_onlineWhitelist) {
126 if (protocolHostAndPortAreEqual(url, whitelistURL) && url.string().startsWith(whitelistURL.string()))
127 return true;
128 }
129 return false;
130}
131
132void ApplicationCache::setFallbackURLs(const FallbackURLVector& fallbackURLs)
133{
134 ASSERT(m_fallbackURLs.isEmpty());
135 m_fallbackURLs = fallbackURLs;
136 // FIXME: What's the right behavior if we have 2 or more identical namespace URLs?
137 std::stable_sort(m_fallbackURLs.begin(), m_fallbackURLs.end(), fallbackURLLongerThan);
138}
139
140bool ApplicationCache::urlMatchesFallbackNamespace(const URL& url, URL* fallbackURL)
141{
142 for (auto& fallback : m_fallbackURLs) {
143 if (protocolHostAndPortAreEqual(url, fallback.first) && url.string().startsWith(fallback.first.string())) {
144 if (fallbackURL)
145 *fallbackURL = fallback.second;
146 return true;
147 }
148 }
149 return false;
150}
151
152void ApplicationCache::clearStorageID()
153{
154 m_storageID = 0;
155
156 for (const auto& resource : m_resources.values())
157 resource->clearStorageID();
158}
159
160#ifndef NDEBUG
161void ApplicationCache::dump()
162{
163 for (const auto& urlAndResource : m_resources) {
164 printf("%s ", urlAndResource.key.utf8().data());
165 ApplicationCacheResource::dumpType(urlAndResource.value->type());
166 }
167}
168#endif
169
170}
171