Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: content/browser/service_worker/service_worker_cache_listener.cc

Issue 411973004: Stitch CacheStorage messages to implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cache_storage_stitch
Patch Set: nits Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/service_worker/service_worker_cache_listener.h" 5 #include "content/browser/service_worker/service_worker_cache_listener.h"
6 6
7 #include "base/bind.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "content/browser/service_worker/service_worker_cache_storage_manager.h"
10 #include "content/browser/service_worker/service_worker_context_core.h"
7 #include "content/browser/service_worker/service_worker_version.h" 11 #include "content/browser/service_worker/service_worker_version.h"
8 #include "content/common/service_worker/service_worker_messages.h" 12 #include "content/common/service_worker/service_worker_messages.h"
9 #include "third_party/WebKit/public/platform/WebServiceWorkerCacheError.h" 13 #include "third_party/WebKit/public/platform/WebServiceWorkerCacheError.h"
10 14
11 namespace content { 15 namespace content {
12 16
13 ServiceWorkerStoresListener::ServiceWorkerStoresListener( 17 using blink::WebServiceWorkerCacheError;
14 ServiceWorkerVersion* version) : version_(version) {}
15 18
16 ServiceWorkerStoresListener::~ServiceWorkerStoresListener() {} 19 namespace {
17 20
18 bool ServiceWorkerStoresListener::OnMessageReceived( 21 WebServiceWorkerCacheError ToWebServiceWorkerCacheError(
22 content::ServiceWorkerCacheStorage::CacheStorageError err) {
michaeln 2014/08/12 03:59:23 nit: i think content:: not needed here and below
jkarlin 2014/08/12 15:17:48 Done.
23 switch (err) {
24 case content::ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR:
25 NOTREACHED();
26 return WebServiceWorkerCacheError::
27 WebServiceWorkerCacheErrorNotImplemented;
28 case content::ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NOT_IMPLEMENTED
29 :
30 return WebServiceWorkerCacheError::
31 WebServiceWorkerCacheErrorNotImplemented;
32 case content::ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NOT_FOUND:
33 return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound;
34 case content::ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_EXISTS:
35 return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorExists;
36 case content::ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_STORAGE:
37 // TODO(jkarlin): Changethis to CACHE_STORAGE_ERROR_STORAGE once that's
38 // added.
39 return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound;
40 case content::ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_EMPTY_KEY:
41 // TODO(jkarlin): Update this to CACHE_STORAGE_ERROR_EMPTY_KEY once that's
42 // added.
43 return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound;
44 }
45 NOTREACHED();
46 return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotImplemented;
47 }
48
49 } // namespace
50
51 ServiceWorkerCacheListener::ServiceWorkerCacheListener(
52 ServiceWorkerVersion* version,
53 base::WeakPtr<ServiceWorkerContextCore> context)
54 : version_(version), context_(context), weak_factory_(this) {
55 }
56
57 ServiceWorkerCacheListener::~ServiceWorkerCacheListener() {
58 }
59
60 bool ServiceWorkerCacheListener::OnMessageReceived(
19 const IPC::Message& message) { 61 const IPC::Message& message) {
20 bool handled = true; 62 bool handled = true;
21 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerStoresListener, message) 63 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerCacheListener, message)
22 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageGet, 64 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageGet,
23 OnCacheStorageGet) 65 OnCacheStorageGet)
24 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageHas, 66 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageHas,
25 OnCacheStorageGet) 67 OnCacheStorageGet)
26 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageCreate, 68 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageCreate,
27 OnCacheStorageCreate) 69 OnCacheStorageCreate)
28 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageDelete, 70 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageDelete,
29 OnCacheStorageDelete) 71 OnCacheStorageDelete)
30 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageKeys, 72 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageKeys,
31 OnCacheStorageKeys) 73 OnCacheStorageKeys)
32 IPC_MESSAGE_UNHANDLED(handled = false) 74 IPC_MESSAGE_UNHANDLED(handled = false)
33 IPC_END_MESSAGE_MAP() 75 IPC_END_MESSAGE_MAP()
34 76
35 return handled; 77 return handled;
36 } 78 }
37 79
38 void ServiceWorkerStoresListener::OnCacheStorageGet( 80 void ServiceWorkerCacheListener::OnCacheStorageGet(
39 int request_id, 81 int request_id,
40 const base::string16& fetch_store_name) { 82 const base::string16& cache_name) {
41 // TODO(gavinp,jkarlin): Implement this method. 83 context_->cache_manager()->GetCache(
42 Send(ServiceWorkerMsg_CacheStorageGetError( 84 version_->scope().GetOrigin(),
43 request_id, 85 base::UTF16ToUTF8(cache_name),
44 blink::WebServiceWorkerCacheErrorNotImplemented)); 86 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageGetCallback,
87 weak_factory_.GetWeakPtr(),
88 request_id));
45 } 89 }
46 90
47 void ServiceWorkerStoresListener::OnCacheStorageHas( 91 void ServiceWorkerCacheListener::OnCacheStorageHas(
48 int request_id, 92 int request_id,
49 const base::string16& fetch_store_name) { 93 const base::string16& cache_name) {
50 // TODO(gavinp,jkarlin): Implement this method. 94 context_->cache_manager()->HasCache(
51 Send(ServiceWorkerMsg_CacheStorageHasError( 95 version_->scope().GetOrigin(),
52 request_id, 96 base::UTF16ToUTF8(cache_name),
53 blink::WebServiceWorkerCacheErrorNotImplemented)); 97 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageHasCallback,
98 weak_factory_.GetWeakPtr(),
99 request_id));
54 } 100 }
55 101
56 void ServiceWorkerStoresListener::OnCacheStorageCreate( 102 void ServiceWorkerCacheListener::OnCacheStorageCreate(
57 int request_id, 103 int request_id,
58 const base::string16& fetch_store_name) { 104 const base::string16& cache_name) {
59 // TODO(gavinp,jkarlin): Implement this method. 105 context_->cache_manager()->CreateCache(
60 Send(ServiceWorkerMsg_CacheStorageCreateError( 106 version_->scope().GetOrigin(),
61 request_id, 107 base::UTF16ToUTF8(cache_name),
62 blink::WebServiceWorkerCacheErrorNotImplemented)); 108 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageCreateCallback,
109 weak_factory_.GetWeakPtr(),
110 request_id));
63 } 111 }
64 112
65 void ServiceWorkerStoresListener::OnCacheStorageDelete( 113 void ServiceWorkerCacheListener::OnCacheStorageDelete(
66 int request_id, 114 int request_id,
67 const base::string16& fetch_store_name) { 115 const base::string16& cache_name) {
68 // TODO(gavinp,jkarlin): Implement this method. 116 context_->cache_manager()->DeleteCache(
69 Send(ServiceWorkerMsg_CacheStorageDeleteError( 117 version_->scope().GetOrigin(),
70 request_id, blink::WebServiceWorkerCacheErrorNotImplemented)); 118 base::UTF16ToUTF8(cache_name),
119 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageDeleteCallback,
120 weak_factory_.GetWeakPtr(),
121 request_id));
71 } 122 }
72 123
73 void ServiceWorkerStoresListener::OnCacheStorageKeys( 124 void ServiceWorkerCacheListener::OnCacheStorageKeys(int request_id) {
74 int request_id) { 125 context_->cache_manager()->EnumerateCaches(
75 // TODO(gavinp,jkarlin): Implement this method. 126 version_->scope().GetOrigin(),
76 Send(ServiceWorkerMsg_CacheStorageKeysError( 127 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageKeysCallback,
77 request_id, blink::WebServiceWorkerCacheErrorNotImplemented)); 128 weak_factory_.GetWeakPtr(),
129 request_id));
78 } 130 }
79 131
80 void ServiceWorkerStoresListener::Send(const IPC::Message& message) { 132 void ServiceWorkerCacheListener::Send(const IPC::Message& message) {
81 version_->embedded_worker()->SendMessage(message); 133 version_->embedded_worker()->SendMessage(message);
82 } 134 }
83 135
136 void ServiceWorkerCacheListener::OnCacheStorageGetCallback(
137 int request_id,
138 int cache_id,
139 ServiceWorkerCacheStorage::CacheStorageError error) {
140 if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
141 Send(ServiceWorkerMsg_CacheStorageGetError(
142 request_id, ToWebServiceWorkerCacheError(error)));
143 return;
144 }
145 Send(ServiceWorkerMsg_CacheStorageGetSuccess(request_id, cache_id));
146 }
147
148 void ServiceWorkerCacheListener::OnCacheStorageHasCallback(
149 int request_id,
150 bool has_cache,
151 ServiceWorkerCacheStorage::CacheStorageError error) {
152 if (!has_cache ||
153 error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
154 Send(ServiceWorkerMsg_CacheStorageHasError(
155 request_id, ToWebServiceWorkerCacheError(error)));
falken 2014/08/12 01:37:33 Looks like HasCache returns false with NO_ERROR wh
jkarlin 2014/08/12 15:17:48 Great catch! Thanks. Done.
156 return;
157 }
158 Send(ServiceWorkerMsg_CacheStorageHasSuccess(request_id));
159 }
160
161 void ServiceWorkerCacheListener::OnCacheStorageCreateCallback(
162 int request_id,
163 int cache_id,
164 ServiceWorkerCacheStorage::CacheStorageError error) {
165 if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
166 Send(ServiceWorkerMsg_CacheStorageCreateError(
167 request_id, ToWebServiceWorkerCacheError(error)));
168 return;
169 }
170 Send(ServiceWorkerMsg_CacheStorageCreateSuccess(request_id, cache_id));
171 }
172
173 void ServiceWorkerCacheListener::OnCacheStorageDeleteCallback(
174 int request_id,
175 bool deleted,
176 ServiceWorkerCacheStorage::CacheStorageError error) {
177 if (!deleted ||
178 error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
179 Send(ServiceWorkerMsg_CacheStorageDeleteError(
180 request_id, ToWebServiceWorkerCacheError(error)));
181 return;
182 }
183 Send(ServiceWorkerMsg_CacheStorageDeleteSuccess(request_id));
184 }
185
186 void ServiceWorkerCacheListener::OnCacheStorageKeysCallback(
187 int request_id,
188 const std::vector<std::string>& strings,
189 ServiceWorkerCacheStorage::CacheStorageError error) {
190 if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
191 Send(ServiceWorkerMsg_CacheStorageKeysError(
192 request_id, ToWebServiceWorkerCacheError(error)));
193 return;
194 }
195
196 std::vector<base::string16> string16s;
197 for (size_t i = 0, max = strings.size(); i < max; ++i) {
198 string16s.push_back(base::UTF8ToUTF16(strings[i]));
michaeln 2014/08/12 03:59:23 nit: the type translation to blink'ish speak (WebS
jkarlin 2014/08/12 15:17:48 Looking at service_worker_messages.h it looks like
199 }
200 Send(ServiceWorkerMsg_CacheStorageKeysSuccess(request_id, string16s));
201 }
202
84 } // namespace content 203 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698