OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/api/web_view/chrome_web_view_internal_api.h" | |
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" | |
9 #include "chrome/browser/extensions/api/context_menus/context_menus_api_helpers. h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #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" | |
15 | |
16 namespace helpers = extensions::context_menus_api_helpers; | |
17 namespace webview = extensions::api::chrome_web_view_internal; | |
18 | |
19 namespace extensions { | |
20 | |
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 | |
41 // WebViewInternalExtensionFunction::RunAsyncSafe(WebViewGuest*). | |
42 bool ChromeWebViewInternalContextMenusCreateFunction::RunAsync() { | |
43 scoped_ptr<webview::ContextMenusCreate::Params> params( | |
44 webview::ContextMenusCreate::Params::Create(*args_)); | |
45 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
46 | |
47 MenuItem::Id id( | |
48 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(), | |
49 MenuItem::ExtensionKey(extension_id(), params->instance_id)); | |
50 | |
51 if (params->create_properties.id.get()) { | |
52 id.string_uid = *params->create_properties.id; | |
53 } else { | |
54 // The Generated Id is added by web_view_internal_custom_bindings.js. | |
55 base::DictionaryValue* properties = NULL; | |
56 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties)); | |
57 EXTENSION_FUNCTION_VALIDATE( | |
58 properties->GetInteger(helpers::kGeneratedIdKey, &id.uid)); | |
59 } | |
60 | |
61 bool success = extensions::context_menus_api_helpers::CreateMenuItem( | |
62 params->create_properties, | |
63 Profile::FromBrowserContext(browser_context()), | |
64 extension(), | |
65 id, | |
66 &error_); | |
67 | |
68 SendResponse(success); | |
69 return success; | |
70 } | |
71 | |
72 bool ChromeWebViewInternalContextMenusUpdateFunction::RunAsync() { | |
73 scoped_ptr<webview::ContextMenusUpdate::Params> params( | |
74 webview::ContextMenusUpdate::Params::Create(*args_)); | |
75 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
76 | |
77 Profile* profile = Profile::FromBrowserContext(browser_context()); | |
78 MenuItem::Id item_id( | |
79 profile->IsOffTheRecord(), | |
80 MenuItem::ExtensionKey(extension_id(), params->instance_id)); | |
81 | |
82 if (params->id.as_string) | |
83 item_id.string_uid = *params->id.as_string; | |
84 else if (params->id.as_integer) | |
85 item_id.uid = *params->id.as_integer; | |
86 else | |
87 NOTREACHED(); | |
88 | |
89 bool success = extensions::context_menus_api_helpers::UpdateMenuItem( | |
90 params->update_properties, profile, extension(), item_id, &error_); | |
91 SendResponse(success); | |
92 return success; | |
93 } | |
94 | |
95 bool ChromeWebViewInternalContextMenusRemoveFunction::RunAsync() { | |
96 scoped_ptr<webview::ContextMenusRemove::Params> params( | |
97 webview::ContextMenusRemove::Params::Create(*args_)); | |
98 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
99 | |
100 MenuManager* menu_manager = | |
101 MenuManager::Get(Profile::FromBrowserContext(browser_context())); | |
102 | |
103 MenuItem::Id id( | |
104 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(), | |
105 MenuItem::ExtensionKey(extension_id(), params->instance_id)); | |
106 | |
107 if (params->menu_item_id.as_string) { | |
108 id.string_uid = *params->menu_item_id.as_string; | |
109 } else if (params->menu_item_id.as_integer) { | |
110 id.uid = *params->menu_item_id.as_integer; | |
111 } else { | |
112 NOTREACHED(); | |
113 } | |
114 | |
115 bool success = true; | |
116 MenuItem* item = menu_manager->GetItemById(id); | |
117 // Ensure one <webview> can't remove another's menu items. | |
118 if (!item || item->id().extension_key != id.extension_key) { | |
119 error_ = ErrorUtils::FormatErrorMessage( | |
120 context_menus_api_helpers::kCannotFindItemError, | |
121 context_menus_api_helpers::GetIDString(id)); | |
122 success = false; | |
123 } else if (!menu_manager->RemoveContextMenuItem(id)) { | |
124 success = false; | |
125 } | |
126 | |
127 SendResponse(success); | |
128 return success; | |
129 } | |
130 | |
131 bool ChromeWebViewInternalContextMenusRemoveAllFunction::RunAsync() { | |
132 scoped_ptr<webview::ContextMenusRemoveAll::Params> params( | |
133 webview::ContextMenusRemoveAll::Params::Create(*args_)); | |
134 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
135 | |
136 MenuManager* menu_manager = | |
137 MenuManager::Get(Profile::FromBrowserContext(browser_context())); | |
138 | |
139 int webview_instance_id = params->instance_id; | |
140 menu_manager->RemoveAllContextItems( | |
141 MenuItem::ExtensionKey(extension()->id(), webview_instance_id)); | |
142 SendResponse(true); | |
143 return true; | |
144 } | |
145 | |
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 bool ChromeWebViewInternalClearDataFunction::RunAsyncSafe(WebViewGuest* guest) { | |
Fady Samuel
2014/09/08 22:16:31
Add a TODO to move ClearData to extenisons.
| |
182 // Grab the initial |options| parameter, and parse out the arguments. | |
183 base::DictionaryValue* options; | |
184 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); | |
185 DCHECK(options); | |
186 | |
187 // If |ms_since_epoch| isn't set, default it to 0. | |
188 double ms_since_epoch; | |
189 if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey, | |
190 &ms_since_epoch)) { | |
191 ms_since_epoch = 0; | |
192 } | |
193 | |
194 // base::Time takes a double that represents seconds since epoch. JavaScript | |
195 // gives developers milliseconds, so do a quick conversion before populating | |
196 // the object. Also, Time::FromDoubleT converts double time 0 to empty Time | |
197 // object. So we need to do special handling here. | |
198 remove_since_ = (ms_since_epoch == 0) | |
199 ? base::Time::UnixEpoch() | |
200 : base::Time::FromDoubleT(ms_since_epoch / 1000.0); | |
201 | |
202 remove_mask_ = GetRemovalMask(); | |
203 if (bad_message_) | |
204 return false; | |
205 | |
206 AddRef(); // Balanced below or in WebViewInternalClearDataFunction::Done(). | |
207 | |
208 bool scheduled = false; | |
209 if (remove_mask_) { | |
210 scheduled = guest->ClearData( | |
211 remove_since_, | |
212 remove_mask_, | |
213 base::Bind(&ChromeWebViewInternalClearDataFunction::ClearDataDone, | |
214 this)); | |
215 } | |
216 if (!remove_mask_ || !scheduled) { | |
217 SendResponse(false); | |
218 Release(); // Balanced above. | |
219 return false; | |
220 } | |
221 | |
222 // Will finish asynchronously. | |
223 return true; | |
224 } | |
225 | |
226 void ChromeWebViewInternalClearDataFunction::ClearDataDone() { | |
227 Release(); // Balanced in RunAsync(). | |
228 SendResponse(true); | |
229 } | |
230 | |
231 ChromeWebViewInternalShowContextMenuFunction:: | |
232 ChromeWebViewInternalShowContextMenuFunction() { | |
233 } | |
234 | |
235 ChromeWebViewInternalShowContextMenuFunction:: | |
236 ~ChromeWebViewInternalShowContextMenuFunction() { | |
237 } | |
238 | |
239 bool ChromeWebViewInternalShowContextMenuFunction::RunAsyncSafe( | |
240 WebViewGuest* guest) { | |
241 scoped_ptr<webview::ShowContextMenu::Params> params( | |
242 webview::ShowContextMenu::Params::Create(*args_)); | |
243 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
244 | |
245 // TODO(lazyboy): Actually implement filtering menu items, we pass NULL for | |
246 // now. | |
247 guest->ShowContextMenu(params->request_id, NULL); | |
248 | |
249 SendResponse(true); | |
250 return true; | |
251 } | |
252 | |
253 } // namespace extensions | |
OLD | NEW |