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 "chrome/browser/extensions/api/web_view/chrome_web_view_internal_api.h" | 5 #include "chrome/browser/extensions/api/web_view/chrome_web_view_internal_api.h" |
6 | 6 |
7 #include "chrome/browser/extensions/api/browsing_data/browsing_data_api.h" | |
8 #include "chrome/browser/extensions/api/context_menus/context_menus_api.h" | 7 #include "chrome/browser/extensions/api/context_menus/context_menus_api.h" |
9 #include "chrome/browser/extensions/api/context_menus/context_menus_api_helpers.
h" | 8 #include "chrome/browser/extensions/api/context_menus/context_menus_api_helpers.
h" |
10 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/common/extensions/api/chrome_web_view_internal.h" | 10 #include "chrome/common/extensions/api/chrome_web_view_internal.h" |
12 #include "content/public/browser/storage_partition.h" | |
13 #include "content/public/browser/web_contents.h" | |
14 #include "extensions/common/error_utils.h" | 11 #include "extensions/common/error_utils.h" |
15 | 12 |
16 namespace helpers = extensions::context_menus_api_helpers; | 13 namespace helpers = extensions::context_menus_api_helpers; |
17 namespace webview = extensions::api::chrome_web_view_internal; | 14 namespace webview = extensions::api::chrome_web_view_internal; |
18 | 15 |
19 namespace extensions { | 16 namespace extensions { |
20 | 17 |
21 namespace { | |
22 int MaskForKey(const char* key) { | |
23 if (strcmp(key, extension_browsing_data_api_constants::kAppCacheKey) == 0) | |
24 return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE; | |
25 if (strcmp(key, extension_browsing_data_api_constants::kCookiesKey) == 0) | |
26 return content::StoragePartition::REMOVE_DATA_MASK_COOKIES; | |
27 if (strcmp(key, extension_browsing_data_api_constants::kFileSystemsKey) == 0) | |
28 return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS; | |
29 if (strcmp(key, extension_browsing_data_api_constants::kIndexedDBKey) == 0) | |
30 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB; | |
31 if (strcmp(key, extension_browsing_data_api_constants::kLocalStorageKey) == 0) | |
32 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; | |
33 if (strcmp(key, extension_browsing_data_api_constants::kWebSQLKey) == 0) | |
34 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL; | |
35 return 0; | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 // TODO(lazyboy): Add checks similar to | 18 // TODO(lazyboy): Add checks similar to |
41 // WebViewInternalExtensionFunction::RunAsyncSafe(WebViewGuest*). | 19 // WebViewInternalExtensionFunction::RunAsyncSafe(WebViewGuest*). |
42 bool ChromeWebViewInternalContextMenusCreateFunction::RunAsync() { | 20 bool ChromeWebViewInternalContextMenusCreateFunction::RunAsync() { |
43 scoped_ptr<webview::ContextMenusCreate::Params> params( | 21 scoped_ptr<webview::ContextMenusCreate::Params> params( |
44 webview::ContextMenusCreate::Params::Create(*args_)); | 22 webview::ContextMenusCreate::Params::Create(*args_)); |
45 EXTENSION_FUNCTION_VALIDATE(params.get()); | 23 EXTENSION_FUNCTION_VALIDATE(params.get()); |
46 | 24 |
47 MenuItem::Id id( | 25 MenuItem::Id id( |
48 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(), | 26 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(), |
49 MenuItem::ExtensionKey(extension_id(), params->instance_id)); | 27 MenuItem::ExtensionKey(extension_id(), params->instance_id)); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 MenuManager* menu_manager = | 114 MenuManager* menu_manager = |
137 MenuManager::Get(Profile::FromBrowserContext(browser_context())); | 115 MenuManager::Get(Profile::FromBrowserContext(browser_context())); |
138 | 116 |
139 int webview_instance_id = params->instance_id; | 117 int webview_instance_id = params->instance_id; |
140 menu_manager->RemoveAllContextItems( | 118 menu_manager->RemoveAllContextItems( |
141 MenuItem::ExtensionKey(extension()->id(), webview_instance_id)); | 119 MenuItem::ExtensionKey(extension()->id(), webview_instance_id)); |
142 SendResponse(true); | 120 SendResponse(true); |
143 return true; | 121 return true; |
144 } | 122 } |
145 | 123 |
146 ChromeWebViewInternalClearDataFunction::ChromeWebViewInternalClearDataFunction() | |
147 : remove_mask_(0), bad_message_(false) { | |
148 } | |
149 | |
150 ChromeWebViewInternalClearDataFunction:: | |
151 ~ChromeWebViewInternalClearDataFunction() { | |
152 } | |
153 | |
154 // Parses the |dataToRemove| argument to generate the remove mask. Sets | |
155 // |bad_message_| (like EXTENSION_FUNCTION_VALIDATE would if this were a bool | |
156 // method) if 'dataToRemove' is not present. | |
157 uint32 ChromeWebViewInternalClearDataFunction::GetRemovalMask() { | |
158 base::DictionaryValue* data_to_remove; | |
159 if (!args_->GetDictionary(2, &data_to_remove)) { | |
160 bad_message_ = true; | |
161 return 0; | |
162 } | |
163 | |
164 uint32 remove_mask = 0; | |
165 for (base::DictionaryValue::Iterator i(*data_to_remove); !i.IsAtEnd(); | |
166 i.Advance()) { | |
167 bool selected = false; | |
168 if (!i.value().GetAsBoolean(&selected)) { | |
169 bad_message_ = true; | |
170 return 0; | |
171 } | |
172 if (selected) | |
173 remove_mask |= MaskForKey(i.key().c_str()); | |
174 } | |
175 | |
176 return remove_mask; | |
177 } | |
178 | |
179 // TODO(lazyboy): Parameters in this extension function are similar (or a | |
180 // sub-set) to BrowsingDataRemoverFunction. How can we share this code? | |
181 // TODO(lf): Move ClearDataFunction to extensions | |
182 bool ChromeWebViewInternalClearDataFunction::RunAsyncSafe(WebViewGuest* guest) { | |
183 // Grab the initial |options| parameter, and parse out the arguments. | |
184 base::DictionaryValue* options; | |
185 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); | |
186 DCHECK(options); | |
187 | |
188 // If |ms_since_epoch| isn't set, default it to 0. | |
189 double ms_since_epoch; | |
190 if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey, | |
191 &ms_since_epoch)) { | |
192 ms_since_epoch = 0; | |
193 } | |
194 | |
195 // base::Time takes a double that represents seconds since epoch. JavaScript | |
196 // gives developers milliseconds, so do a quick conversion before populating | |
197 // the object. Also, Time::FromDoubleT converts double time 0 to empty Time | |
198 // object. So we need to do special handling here. | |
199 remove_since_ = (ms_since_epoch == 0) | |
200 ? base::Time::UnixEpoch() | |
201 : base::Time::FromDoubleT(ms_since_epoch / 1000.0); | |
202 | |
203 remove_mask_ = GetRemovalMask(); | |
204 if (bad_message_) | |
205 return false; | |
206 | |
207 AddRef(); // Balanced below or in WebViewInternalClearDataFunction::Done(). | |
208 | |
209 bool scheduled = false; | |
210 if (remove_mask_) { | |
211 scheduled = guest->ClearData( | |
212 remove_since_, | |
213 remove_mask_, | |
214 base::Bind(&ChromeWebViewInternalClearDataFunction::ClearDataDone, | |
215 this)); | |
216 } | |
217 if (!remove_mask_ || !scheduled) { | |
218 SendResponse(false); | |
219 Release(); // Balanced above. | |
220 return false; | |
221 } | |
222 | |
223 // Will finish asynchronously. | |
224 return true; | |
225 } | |
226 | |
227 void ChromeWebViewInternalClearDataFunction::ClearDataDone() { | |
228 Release(); // Balanced in RunAsync(). | |
229 SendResponse(true); | |
230 } | |
231 | |
232 ChromeWebViewInternalShowContextMenuFunction:: | 124 ChromeWebViewInternalShowContextMenuFunction:: |
233 ChromeWebViewInternalShowContextMenuFunction() { | 125 ChromeWebViewInternalShowContextMenuFunction() { |
234 } | 126 } |
235 | 127 |
236 ChromeWebViewInternalShowContextMenuFunction:: | 128 ChromeWebViewInternalShowContextMenuFunction:: |
237 ~ChromeWebViewInternalShowContextMenuFunction() { | 129 ~ChromeWebViewInternalShowContextMenuFunction() { |
238 } | 130 } |
239 | 131 |
240 bool ChromeWebViewInternalShowContextMenuFunction::RunAsyncSafe( | 132 bool ChromeWebViewInternalShowContextMenuFunction::RunAsyncSafe( |
241 WebViewGuest* guest) { | 133 WebViewGuest* guest) { |
242 scoped_ptr<webview::ShowContextMenu::Params> params( | 134 scoped_ptr<webview::ShowContextMenu::Params> params( |
243 webview::ShowContextMenu::Params::Create(*args_)); | 135 webview::ShowContextMenu::Params::Create(*args_)); |
244 EXTENSION_FUNCTION_VALIDATE(params.get()); | 136 EXTENSION_FUNCTION_VALIDATE(params.get()); |
245 | 137 |
246 // TODO(lazyboy): Actually implement filtering menu items, we pass NULL for | 138 // TODO(lazyboy): Actually implement filtering menu items, we pass NULL for |
247 // now. | 139 // now. |
248 guest->ShowContextMenu(params->request_id, NULL); | 140 guest->ShowContextMenu(params->request_id, NULL); |
249 | 141 |
250 SendResponse(true); | 142 SendResponse(true); |
251 return true; | 143 return true; |
252 } | 144 } |
253 | 145 |
254 } // namespace extensions | 146 } // namespace extensions |
OLD | NEW |