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

Side by Side Diff: extensions/browser/api/web_view/web_view_internal_api.cc

Issue 541753004: Split web_view_internal_api and move part of it to extensions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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 "chrome/browser/extensions/api/web_view/web_view_internal_api.h" 5 #include "extensions/browser/api/web_view/web_view_internal_api.h"
6 6
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/api/browsing_data/browsing_data_api.h"
9 #include "chrome/browser/extensions/api/context_menus/context_menus_api.h"
10 #include "chrome/browser/extensions/api/context_menus/context_menus_api_helpers. h"
11 #include "chrome/browser/extensions/tab_helper.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/extensions/api/web_view_internal.h"
14 #include "content/public/browser/render_process_host.h" 8 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h" 9 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "content/public/browser/web_contents.h" 10 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/stop_find_action.h" 11 #include "content/public/common/stop_find_action.h"
19 #include "extensions/browser/api/capture_web_contents_function_impl.h" 12 #include "extensions/browser/api/capture_web_contents_function_impl.h"
20 #include "extensions/browser/api/execute_code_function_impl.h" 13 #include "extensions/browser/api/execute_code_function_impl.h"
21 #include "extensions/browser/guest_view/web_view/web_view_permission_helper.h" 14 #include "extensions/common/api/web_view_internal.h"
22 #include "extensions/common/error_utils.h"
23 #include "third_party/WebKit/public/web/WebFindOptions.h" 15 #include "third_party/WebKit/public/web/WebFindOptions.h"
24 16
25 using content::WebContents; 17 using content::WebContents;
26 using extensions::api::web_view_internal::SetPermission::Params; 18 using extensions::core_api::web_view_internal::SetPermission::Params;
27 using extensions::core_api::extension_types::InjectDetails; 19 using extensions::core_api::extension_types::InjectDetails;
28 namespace helpers = extensions::context_menus_api_helpers; 20 namespace webview = extensions::core_api::web_view_internal;
29 namespace webview = extensions::api::web_view_internal;
30 21
31 namespace extensions { 22 namespace extensions {
32 23
33 namespace {
34 int MaskForKey(const char* key) {
35 if (strcmp(key, extension_browsing_data_api_constants::kAppCacheKey) == 0)
36 return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
37 if (strcmp(key, extension_browsing_data_api_constants::kCookiesKey) == 0)
38 return content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
39 if (strcmp(key, extension_browsing_data_api_constants::kFileSystemsKey) == 0)
40 return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
41 if (strcmp(key, extension_browsing_data_api_constants::kIndexedDBKey) == 0)
42 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
43 if (strcmp(key, extension_browsing_data_api_constants::kLocalStorageKey) == 0)
44 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
45 if (strcmp(key, extension_browsing_data_api_constants::kWebSQLKey) == 0)
46 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
47 return 0;
48 }
49
50 } // namespace
51
52 bool WebViewInternalExtensionFunction::RunAsync() { 24 bool WebViewInternalExtensionFunction::RunAsync() {
53 int instance_id = 0; 25 int instance_id = 0;
54 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); 26 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
55 WebViewGuest* guest = WebViewGuest::From( 27 WebViewGuest* guest = WebViewGuest::From(
56 render_view_host()->GetProcess()->GetID(), instance_id); 28 render_view_host()->GetProcess()->GetID(), instance_id);
57 if (!guest) 29 if (!guest)
58 return false; 30 return false;
59 31
60 return RunAsyncSafe(guest); 32 return RunAsyncSafe(guest);
61 } 33 }
62 34
63 // TODO(lazyboy): Add checks similar to
64 // WebViewInternalExtensionFunction::RunAsyncSafe(WebViewGuest*).
65 bool WebViewInternalContextMenusCreateFunction::RunAsync() {
66 scoped_ptr<webview::ContextMenusCreate::Params> params(
67 webview::ContextMenusCreate::Params::Create(*args_));
68 EXTENSION_FUNCTION_VALIDATE(params.get());
69
70 MenuItem::Id id(
71 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(),
72 MenuItem::ExtensionKey(extension_id(), params->instance_id));
73
74 if (params->create_properties.id.get()) {
75 id.string_uid = *params->create_properties.id;
76 } else {
77 // The Generated Id is added by web_view_internal_custom_bindings.js.
78 base::DictionaryValue* properties = NULL;
79 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties));
80 EXTENSION_FUNCTION_VALIDATE(
81 properties->GetInteger(helpers::kGeneratedIdKey, &id.uid));
82 }
83
84 bool success = extensions::context_menus_api_helpers::CreateMenuItem(
85 params->create_properties,
86 Profile::FromBrowserContext(browser_context()),
87 extension(),
88 id,
89 &error_);
90
91 SendResponse(success);
92 return success;
93 }
94
95 bool WebViewInternalNavigateFunction::RunAsyncSafe(WebViewGuest* guest) { 35 bool WebViewInternalNavigateFunction::RunAsyncSafe(WebViewGuest* guest) {
96 scoped_ptr<webview::Navigate::Params> params( 36 scoped_ptr<webview::Navigate::Params> params(
97 webview::Navigate::Params::Create(*args_)); 37 webview::Navigate::Params::Create(*args_));
98 EXTENSION_FUNCTION_VALIDATE(params.get()); 38 EXTENSION_FUNCTION_VALIDATE(params.get());
99 std::string src = params->src; 39 std::string src = params->src;
100 guest->NavigateGuest(src); 40 guest->NavigateGuest(src);
101 return true; 41 return true;
102 } 42 }
103 43
104 bool WebViewInternalContextMenusUpdateFunction::RunAsync() {
105 scoped_ptr<webview::ContextMenusUpdate::Params> params(
106 webview::ContextMenusUpdate::Params::Create(*args_));
107 EXTENSION_FUNCTION_VALIDATE(params.get());
108
109 Profile* profile = Profile::FromBrowserContext(browser_context());
110 MenuItem::Id item_id(
111 profile->IsOffTheRecord(),
112 MenuItem::ExtensionKey(extension_id(), params->instance_id));
113
114 if (params->id.as_string)
115 item_id.string_uid = *params->id.as_string;
116 else if (params->id.as_integer)
117 item_id.uid = *params->id.as_integer;
118 else
119 NOTREACHED();
120
121 bool success = extensions::context_menus_api_helpers::UpdateMenuItem(
122 params->update_properties, profile, extension(), item_id, &error_);
123 SendResponse(success);
124 return success;
125 }
126
127 bool WebViewInternalContextMenusRemoveFunction::RunAsync() {
128 scoped_ptr<webview::ContextMenusRemove::Params> params(
129 webview::ContextMenusRemove::Params::Create(*args_));
130 EXTENSION_FUNCTION_VALIDATE(params.get());
131
132 MenuManager* menu_manager =
133 MenuManager::Get(Profile::FromBrowserContext(browser_context()));
134
135 MenuItem::Id id(
136 Profile::FromBrowserContext(browser_context())->IsOffTheRecord(),
137 MenuItem::ExtensionKey(extension_id(), params->instance_id));
138
139 if (params->menu_item_id.as_string) {
140 id.string_uid = *params->menu_item_id.as_string;
141 } else if (params->menu_item_id.as_integer) {
142 id.uid = *params->menu_item_id.as_integer;
143 } else {
144 NOTREACHED();
145 }
146
147 bool success = true;
148 MenuItem* item = menu_manager->GetItemById(id);
149 // Ensure one <webview> can't remove another's menu items.
150 if (!item || item->id().extension_key != id.extension_key) {
151 error_ = ErrorUtils::FormatErrorMessage(
152 context_menus_api_helpers::kCannotFindItemError,
153 context_menus_api_helpers::GetIDString(id));
154 success = false;
155 } else if (!menu_manager->RemoveContextMenuItem(id)) {
156 success = false;
157 }
158
159 SendResponse(success);
160 return success;
161 }
162
163 bool WebViewInternalContextMenusRemoveAllFunction::RunAsync() {
164 scoped_ptr<webview::ContextMenusRemoveAll::Params> params(
165 webview::ContextMenusRemoveAll::Params::Create(*args_));
166 EXTENSION_FUNCTION_VALIDATE(params.get());
167
168 MenuManager* menu_manager =
169 MenuManager::Get(Profile::FromBrowserContext(browser_context()));
170
171 int webview_instance_id = params->instance_id;
172 menu_manager->RemoveAllContextItems(
173 MenuItem::ExtensionKey(extension()->id(), webview_instance_id));
174 SendResponse(true);
175 return true;
176 }
177
178 WebViewInternalClearDataFunction::WebViewInternalClearDataFunction()
179 : remove_mask_(0), bad_message_(false) {
180 }
181
182 WebViewInternalClearDataFunction::~WebViewInternalClearDataFunction() {
183 }
184
185 // Parses the |dataToRemove| argument to generate the remove mask. Sets
186 // |bad_message_| (like EXTENSION_FUNCTION_VALIDATE would if this were a bool
187 // method) if 'dataToRemove' is not present.
188 uint32 WebViewInternalClearDataFunction::GetRemovalMask() {
189 base::DictionaryValue* data_to_remove;
190 if (!args_->GetDictionary(2, &data_to_remove)) {
191 bad_message_ = true;
192 return 0;
193 }
194
195 uint32 remove_mask = 0;
196 for (base::DictionaryValue::Iterator i(*data_to_remove); !i.IsAtEnd();
197 i.Advance()) {
198 bool selected = false;
199 if (!i.value().GetAsBoolean(&selected)) {
200 bad_message_ = true;
201 return 0;
202 }
203 if (selected)
204 remove_mask |= MaskForKey(i.key().c_str());
205 }
206
207 return remove_mask;
208 }
209
210 // TODO(lazyboy): Parameters in this extension function are similar (or a
211 // sub-set) to BrowsingDataRemoverFunction. How can we share this code?
212 bool WebViewInternalClearDataFunction::RunAsyncSafe(WebViewGuest* guest) {
213 // Grab the initial |options| parameter, and parse out the arguments.
214 base::DictionaryValue* options;
215 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options));
216 DCHECK(options);
217
218 // If |ms_since_epoch| isn't set, default it to 0.
219 double ms_since_epoch;
220 if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey,
221 &ms_since_epoch)) {
222 ms_since_epoch = 0;
223 }
224
225 // base::Time takes a double that represents seconds since epoch. JavaScript
226 // gives developers milliseconds, so do a quick conversion before populating
227 // the object. Also, Time::FromDoubleT converts double time 0 to empty Time
228 // object. So we need to do special handling here.
229 remove_since_ = (ms_since_epoch == 0)
230 ? base::Time::UnixEpoch()
231 : base::Time::FromDoubleT(ms_since_epoch / 1000.0);
232
233 remove_mask_ = GetRemovalMask();
234 if (bad_message_)
235 return false;
236
237 AddRef(); // Balanced below or in WebViewInternalClearDataFunction::Done().
238
239 bool scheduled = false;
240 if (remove_mask_) {
241 scheduled = guest->ClearData(
242 remove_since_,
243 remove_mask_,
244 base::Bind(&WebViewInternalClearDataFunction::ClearDataDone, this));
245 }
246 if (!remove_mask_ || !scheduled) {
247 SendResponse(false);
248 Release(); // Balanced above.
249 return false;
250 }
251
252 // Will finish asynchronously.
253 return true;
254 }
255
256 void WebViewInternalClearDataFunction::ClearDataDone() {
257 Release(); // Balanced in RunAsync().
258 SendResponse(true);
259 }
260
261 WebViewInternalExecuteCodeFunction::WebViewInternalExecuteCodeFunction() 44 WebViewInternalExecuteCodeFunction::WebViewInternalExecuteCodeFunction()
262 : guest_instance_id_(0), guest_src_(GURL::EmptyGURL()) { 45 : guest_instance_id_(0), guest_src_(GURL::EmptyGURL()) {
263 } 46 }
264 47
265 WebViewInternalExecuteCodeFunction::~WebViewInternalExecuteCodeFunction() { 48 WebViewInternalExecuteCodeFunction::~WebViewInternalExecuteCodeFunction() {
266 } 49 }
267 50
268 bool WebViewInternalExecuteCodeFunction::Init() { 51 bool WebViewInternalExecuteCodeFunction::Init() {
269 if (details_.get()) 52 if (details_.get())
270 return true; 53 return true;
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 if (params->user_input) 310 if (params->user_input)
528 user_input = *params->user_input; 311 user_input = *params->user_input;
529 312
530 WebViewPermissionHelper* web_view_permission_helper = 313 WebViewPermissionHelper* web_view_permission_helper =
531 WebViewPermissionHelper::FromWebContents(guest->web_contents()); 314 WebViewPermissionHelper::FromWebContents(guest->web_contents());
532 315
533 WebViewPermissionHelper::SetPermissionResult result = 316 WebViewPermissionHelper::SetPermissionResult result =
534 web_view_permission_helper->SetPermission( 317 web_view_permission_helper->SetPermission(
535 params->request_id, action, user_input); 318 params->request_id, action, user_input);
536 319
537 EXTENSION_FUNCTION_VALIDATE( 320 EXTENSION_FUNCTION_VALIDATE(result !=
538 result != WebViewPermissionHelper::SET_PERMISSION_INVALID); 321 WebViewPermissionHelper::SET_PERMISSION_INVALID);
539 322
540 SetResult(new base::FundamentalValue( 323 SetResult(new base::FundamentalValue(
541 result == WebViewPermissionHelper::SET_PERMISSION_ALLOWED)); 324 result == WebViewPermissionHelper::SET_PERMISSION_ALLOWED));
542 SendResponse(true); 325 SendResponse(true);
543 return true; 326 return true;
544 } 327 }
545 328
546 WebViewInternalShowContextMenuFunction::
547 WebViewInternalShowContextMenuFunction() {
548 }
549
550 WebViewInternalShowContextMenuFunction::
551 ~WebViewInternalShowContextMenuFunction() {
552 }
553
554 bool WebViewInternalShowContextMenuFunction::RunAsyncSafe(WebViewGuest* guest) {
555 scoped_ptr<webview::ShowContextMenu::Params> params(
556 webview::ShowContextMenu::Params::Create(*args_));
557 EXTENSION_FUNCTION_VALIDATE(params.get());
558
559 // TODO(lazyboy): Actually implement filtering menu items, we pass NULL for
560 // now.
561 guest->ShowContextMenu(params->request_id, NULL);
562
563 SendResponse(true);
564 return true;
565 }
566
567 WebViewInternalOverrideUserAgentFunction:: 329 WebViewInternalOverrideUserAgentFunction::
568 WebViewInternalOverrideUserAgentFunction() { 330 WebViewInternalOverrideUserAgentFunction() {
569 } 331 }
570 332
571 WebViewInternalOverrideUserAgentFunction:: 333 WebViewInternalOverrideUserAgentFunction::
572 ~WebViewInternalOverrideUserAgentFunction() { 334 ~WebViewInternalOverrideUserAgentFunction() {
573 } 335 }
574 336
575 bool WebViewInternalOverrideUserAgentFunction::RunAsyncSafe( 337 bool WebViewInternalOverrideUserAgentFunction::RunAsyncSafe(
576 WebViewGuest* guest) { 338 WebViewGuest* guest) {
(...skipping 21 matching lines...) Expand all
598 360
599 WebViewInternalTerminateFunction::~WebViewInternalTerminateFunction() { 361 WebViewInternalTerminateFunction::~WebViewInternalTerminateFunction() {
600 } 362 }
601 363
602 bool WebViewInternalTerminateFunction::RunAsyncSafe(WebViewGuest* guest) { 364 bool WebViewInternalTerminateFunction::RunAsyncSafe(WebViewGuest* guest) {
603 guest->Terminate(); 365 guest->Terminate();
604 return true; 366 return true;
605 } 367 }
606 368
607 } // namespace extensions 369 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698