OLD | NEW |
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 ServiceWorkerCacheStorage::CacheStorageError err) { |
| 23 switch (err) { |
| 24 case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR: |
| 25 NOTREACHED(); |
| 26 return WebServiceWorkerCacheError:: |
| 27 WebServiceWorkerCacheErrorNotImplemented; |
| 28 case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NOT_IMPLEMENTED: |
| 29 return WebServiceWorkerCacheError:: |
| 30 WebServiceWorkerCacheErrorNotImplemented; |
| 31 case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NOT_FOUND: |
| 32 return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound; |
| 33 case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_EXISTS: |
| 34 return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorExists; |
| 35 case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_STORAGE: |
| 36 // TODO(jkarlin): Changethis to CACHE_STORAGE_ERROR_STORAGE once that's |
| 37 // added. |
| 38 return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound; |
| 39 case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_EMPTY_KEY: |
| 40 // TODO(jkarlin): Update this to CACHE_STORAGE_ERROR_EMPTY_KEY once that's |
| 41 // added. |
| 42 return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound; |
| 43 } |
| 44 NOTREACHED(); |
| 45 return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotImplemented; |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 ServiceWorkerCacheListener::ServiceWorkerCacheListener( |
| 51 ServiceWorkerVersion* version, |
| 52 base::WeakPtr<ServiceWorkerContextCore> context) |
| 53 : version_(version), context_(context), weak_factory_(this) { |
| 54 } |
| 55 |
| 56 ServiceWorkerCacheListener::~ServiceWorkerCacheListener() { |
| 57 } |
| 58 |
| 59 bool ServiceWorkerCacheListener::OnMessageReceived( |
19 const IPC::Message& message) { | 60 const IPC::Message& message) { |
20 bool handled = true; | 61 bool handled = true; |
21 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerStoresListener, message) | 62 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerCacheListener, message) |
22 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageGet, | 63 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageGet, |
23 OnCacheStorageGet) | 64 OnCacheStorageGet) |
24 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageHas, | 65 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageHas, |
25 OnCacheStorageGet) | 66 OnCacheStorageGet) |
26 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageCreate, | 67 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageCreate, |
27 OnCacheStorageCreate) | 68 OnCacheStorageCreate) |
28 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageDelete, | 69 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageDelete, |
29 OnCacheStorageDelete) | 70 OnCacheStorageDelete) |
30 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageKeys, | 71 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageKeys, |
31 OnCacheStorageKeys) | 72 OnCacheStorageKeys) |
32 IPC_MESSAGE_UNHANDLED(handled = false) | 73 IPC_MESSAGE_UNHANDLED(handled = false) |
33 IPC_END_MESSAGE_MAP() | 74 IPC_END_MESSAGE_MAP() |
34 | 75 |
35 return handled; | 76 return handled; |
36 } | 77 } |
37 | 78 |
38 void ServiceWorkerStoresListener::OnCacheStorageGet( | 79 void ServiceWorkerCacheListener::OnCacheStorageGet( |
39 int request_id, | 80 int request_id, |
40 const base::string16& fetch_store_name) { | 81 const base::string16& cache_name) { |
41 // TODO(gavinp,jkarlin): Implement this method. | 82 context_->cache_manager()->GetCache( |
42 Send(ServiceWorkerMsg_CacheStorageGetError( | 83 version_->scope().GetOrigin(), |
43 request_id, | 84 base::UTF16ToUTF8(cache_name), |
44 blink::WebServiceWorkerCacheErrorNotImplemented)); | 85 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageGetCallback, |
| 86 weak_factory_.GetWeakPtr(), |
| 87 request_id)); |
45 } | 88 } |
46 | 89 |
47 void ServiceWorkerStoresListener::OnCacheStorageHas( | 90 void ServiceWorkerCacheListener::OnCacheStorageHas( |
48 int request_id, | 91 int request_id, |
49 const base::string16& fetch_store_name) { | 92 const base::string16& cache_name) { |
50 // TODO(gavinp,jkarlin): Implement this method. | 93 context_->cache_manager()->HasCache( |
51 Send(ServiceWorkerMsg_CacheStorageHasError( | 94 version_->scope().GetOrigin(), |
52 request_id, | 95 base::UTF16ToUTF8(cache_name), |
53 blink::WebServiceWorkerCacheErrorNotImplemented)); | 96 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageHasCallback, |
| 97 weak_factory_.GetWeakPtr(), |
| 98 request_id)); |
54 } | 99 } |
55 | 100 |
56 void ServiceWorkerStoresListener::OnCacheStorageCreate( | 101 void ServiceWorkerCacheListener::OnCacheStorageCreate( |
57 int request_id, | 102 int request_id, |
58 const base::string16& fetch_store_name) { | 103 const base::string16& cache_name) { |
59 // TODO(gavinp,jkarlin): Implement this method. | 104 context_->cache_manager()->CreateCache( |
60 Send(ServiceWorkerMsg_CacheStorageCreateError( | 105 version_->scope().GetOrigin(), |
61 request_id, | 106 base::UTF16ToUTF8(cache_name), |
62 blink::WebServiceWorkerCacheErrorNotImplemented)); | 107 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageCreateCallback, |
| 108 weak_factory_.GetWeakPtr(), |
| 109 request_id)); |
63 } | 110 } |
64 | 111 |
65 void ServiceWorkerStoresListener::OnCacheStorageDelete( | 112 void ServiceWorkerCacheListener::OnCacheStorageDelete( |
66 int request_id, | 113 int request_id, |
67 const base::string16& fetch_store_name) { | 114 const base::string16& cache_name) { |
68 // TODO(gavinp,jkarlin): Implement this method. | 115 context_->cache_manager()->DeleteCache( |
69 Send(ServiceWorkerMsg_CacheStorageDeleteError( | 116 version_->scope().GetOrigin(), |
70 request_id, blink::WebServiceWorkerCacheErrorNotImplemented)); | 117 base::UTF16ToUTF8(cache_name), |
| 118 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageDeleteCallback, |
| 119 weak_factory_.GetWeakPtr(), |
| 120 request_id)); |
71 } | 121 } |
72 | 122 |
73 void ServiceWorkerStoresListener::OnCacheStorageKeys( | 123 void ServiceWorkerCacheListener::OnCacheStorageKeys(int request_id) { |
74 int request_id) { | 124 context_->cache_manager()->EnumerateCaches( |
75 // TODO(gavinp,jkarlin): Implement this method. | 125 version_->scope().GetOrigin(), |
76 Send(ServiceWorkerMsg_CacheStorageKeysError( | 126 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageKeysCallback, |
77 request_id, blink::WebServiceWorkerCacheErrorNotImplemented)); | 127 weak_factory_.GetWeakPtr(), |
| 128 request_id)); |
78 } | 129 } |
79 | 130 |
80 void ServiceWorkerStoresListener::Send(const IPC::Message& message) { | 131 void ServiceWorkerCacheListener::Send(const IPC::Message& message) { |
81 version_->embedded_worker()->SendMessage(message); | 132 version_->embedded_worker()->SendMessage(message); |
82 } | 133 } |
83 | 134 |
| 135 void ServiceWorkerCacheListener::OnCacheStorageGetCallback( |
| 136 int request_id, |
| 137 int cache_id, |
| 138 ServiceWorkerCacheStorage::CacheStorageError error) { |
| 139 if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { |
| 140 Send(ServiceWorkerMsg_CacheStorageGetError( |
| 141 request_id, ToWebServiceWorkerCacheError(error))); |
| 142 return; |
| 143 } |
| 144 Send(ServiceWorkerMsg_CacheStorageGetSuccess(request_id, cache_id)); |
| 145 } |
| 146 |
| 147 void ServiceWorkerCacheListener::OnCacheStorageHasCallback( |
| 148 int request_id, |
| 149 bool has_cache, |
| 150 ServiceWorkerCacheStorage::CacheStorageError error) { |
| 151 if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { |
| 152 Send(ServiceWorkerMsg_CacheStorageHasError( |
| 153 request_id, ToWebServiceWorkerCacheError(error))); |
| 154 return; |
| 155 } |
| 156 if (!has_cache) { |
| 157 Send(ServiceWorkerMsg_CacheStorageHasError( |
| 158 request_id, |
| 159 WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound)); |
| 160 return; |
| 161 } |
| 162 Send(ServiceWorkerMsg_CacheStorageHasSuccess(request_id)); |
| 163 } |
| 164 |
| 165 void ServiceWorkerCacheListener::OnCacheStorageCreateCallback( |
| 166 int request_id, |
| 167 int cache_id, |
| 168 ServiceWorkerCacheStorage::CacheStorageError error) { |
| 169 if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { |
| 170 Send(ServiceWorkerMsg_CacheStorageCreateError( |
| 171 request_id, ToWebServiceWorkerCacheError(error))); |
| 172 return; |
| 173 } |
| 174 Send(ServiceWorkerMsg_CacheStorageCreateSuccess(request_id, cache_id)); |
| 175 } |
| 176 |
| 177 void ServiceWorkerCacheListener::OnCacheStorageDeleteCallback( |
| 178 int request_id, |
| 179 bool deleted, |
| 180 ServiceWorkerCacheStorage::CacheStorageError error) { |
| 181 if (!deleted || |
| 182 error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { |
| 183 Send(ServiceWorkerMsg_CacheStorageDeleteError( |
| 184 request_id, ToWebServiceWorkerCacheError(error))); |
| 185 return; |
| 186 } |
| 187 Send(ServiceWorkerMsg_CacheStorageDeleteSuccess(request_id)); |
| 188 } |
| 189 |
| 190 void ServiceWorkerCacheListener::OnCacheStorageKeysCallback( |
| 191 int request_id, |
| 192 const std::vector<std::string>& strings, |
| 193 ServiceWorkerCacheStorage::CacheStorageError error) { |
| 194 if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { |
| 195 Send(ServiceWorkerMsg_CacheStorageKeysError( |
| 196 request_id, ToWebServiceWorkerCacheError(error))); |
| 197 return; |
| 198 } |
| 199 |
| 200 std::vector<base::string16> string16s; |
| 201 for (size_t i = 0, max = strings.size(); i < max; ++i) { |
| 202 string16s.push_back(base::UTF8ToUTF16(strings[i])); |
| 203 } |
| 204 Send(ServiceWorkerMsg_CacheStorageKeysSuccess(request_id, string16s)); |
| 205 } |
| 206 |
84 } // namespace content | 207 } // namespace content |
OLD | NEW |