Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / web_view / web_view_internal_api.cc
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/web_view_internal_api.h"
6
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/guest_view/web_view/web_view_permission_helper.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/extensions/api/web_view_internal.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/storage_partition.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/stop_find_action.h"
20 #include "extensions/common/error_utils.h"
21 #include "third_party/WebKit/public/web/WebFindOptions.h"
22
23 using content::WebContents;
24 using extensions::api::tabs::InjectDetails;
25 using extensions::api::web_view_internal::SetPermission::Params;
26 namespace helpers = extensions::context_menus_api_helpers;
27 namespace webview = extensions::api::web_view_internal;
28
29 namespace extensions {
30
31 namespace {
32 int MaskForKey(const char* key) {
33   if (strcmp(key, extension_browsing_data_api_constants::kAppCacheKey) == 0)
34     return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
35   if (strcmp(key, extension_browsing_data_api_constants::kCookiesKey) == 0)
36     return content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
37   if (strcmp(key, extension_browsing_data_api_constants::kFileSystemsKey) == 0)
38     return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
39   if (strcmp(key, extension_browsing_data_api_constants::kIndexedDBKey) == 0)
40     return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
41   if (strcmp(key, extension_browsing_data_api_constants::kLocalStorageKey) == 0)
42     return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
43   if (strcmp(key, extension_browsing_data_api_constants::kWebSQLKey) == 0)
44     return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
45   return 0;
46 }
47
48 }  // namespace
49
50 bool WebViewInternalExtensionFunction::RunAsync() {
51   int instance_id = 0;
52   EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
53   WebViewGuest* guest = WebViewGuest::From(
54       render_view_host()->GetProcess()->GetID(), instance_id);
55   if (!guest)
56     return false;
57
58   return RunAsyncSafe(guest);
59 }
60
61 // TODO(lazyboy): Add checks similar to
62 // WebViewInternalExtensionFunction::RunAsyncSafe(WebViewGuest*).
63 bool WebViewInternalContextMenusCreateFunction::RunAsync() {
64   scoped_ptr<webview::ContextMenusCreate::Params> params(
65       webview::ContextMenusCreate::Params::Create(*args_));
66   EXTENSION_FUNCTION_VALIDATE(params.get());
67
68   MenuItem::Id id(
69       Profile::FromBrowserContext(browser_context())->IsOffTheRecord(),
70       MenuItem::ExtensionKey(extension_id(), params->instance_id));
71
72   if (params->create_properties.id.get()) {
73     id.string_uid = *params->create_properties.id;
74   } else {
75     // The Generated Id is added by web_view_internal_custom_bindings.js.
76     base::DictionaryValue* properties = NULL;
77     EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &properties));
78     EXTENSION_FUNCTION_VALIDATE(
79         properties->GetInteger(helpers::kGeneratedIdKey, &id.uid));
80   }
81
82   bool success = extensions::context_menus_api_helpers::CreateMenuItem(
83       params->create_properties,
84       Profile::FromBrowserContext(browser_context()),
85       extension(),
86       id,
87       &error_);
88
89   SendResponse(success);
90   return success;
91 }
92
93 bool WebViewInternalNavigateFunction::RunAsyncSafe(WebViewGuest* guest) {
94   scoped_ptr<webview::Navigate::Params> params(
95       webview::Navigate::Params::Create(*args_));
96   EXTENSION_FUNCTION_VALIDATE(params.get());
97   std::string src = params->src;
98   guest->NavigateGuest(src);
99   return true;
100 }
101
102 bool WebViewInternalContextMenusUpdateFunction::RunAsync() {
103   scoped_ptr<webview::ContextMenusUpdate::Params> params(
104       webview::ContextMenusUpdate::Params::Create(*args_));
105   EXTENSION_FUNCTION_VALIDATE(params.get());
106
107   Profile* profile = Profile::FromBrowserContext(browser_context());
108   MenuItem::Id item_id(
109       profile->IsOffTheRecord(),
110       MenuItem::ExtensionKey(extension_id(), params->instance_id));
111
112   if (params->id.as_string)
113     item_id.string_uid = *params->id.as_string;
114   else if (params->id.as_integer)
115     item_id.uid = *params->id.as_integer;
116   else
117     NOTREACHED();
118
119   bool success = extensions::context_menus_api_helpers::UpdateMenuItem(
120       params->update_properties, profile, extension(), item_id, &error_);
121   SendResponse(success);
122   return success;
123 }
124
125 bool WebViewInternalContextMenusRemoveFunction::RunAsync() {
126   scoped_ptr<webview::ContextMenusRemove::Params> params(
127       webview::ContextMenusRemove::Params::Create(*args_));
128   EXTENSION_FUNCTION_VALIDATE(params.get());
129
130   MenuManager* menu_manager =
131       MenuManager::Get(Profile::FromBrowserContext(browser_context()));
132
133   MenuItem::Id id(
134       Profile::FromBrowserContext(browser_context())->IsOffTheRecord(),
135       MenuItem::ExtensionKey(extension_id(), params->instance_id));
136
137   if (params->menu_item_id.as_string) {
138     id.string_uid = *params->menu_item_id.as_string;
139   } else if (params->menu_item_id.as_integer) {
140     id.uid = *params->menu_item_id.as_integer;
141   } else {
142     NOTREACHED();
143   }
144
145   bool success = true;
146   MenuItem* item = menu_manager->GetItemById(id);
147   // Ensure one <webview> can't remove another's menu items.
148   if (!item || item->id().extension_key != id.extension_key) {
149     error_ = ErrorUtils::FormatErrorMessage(
150         context_menus_api_helpers::kCannotFindItemError,
151         context_menus_api_helpers::GetIDString(id));
152     success = false;
153   } else if (!menu_manager->RemoveContextMenuItem(id)) {
154     success = false;
155   }
156
157   SendResponse(success);
158   return success;
159 }
160
161 bool WebViewInternalContextMenusRemoveAllFunction::RunAsync() {
162   scoped_ptr<webview::ContextMenusRemoveAll::Params> params(
163       webview::ContextMenusRemoveAll::Params::Create(*args_));
164   EXTENSION_FUNCTION_VALIDATE(params.get());
165
166   MenuManager* menu_manager =
167       MenuManager::Get(Profile::FromBrowserContext(browser_context()));
168
169   int webview_instance_id = params->instance_id;
170   menu_manager->RemoveAllContextItems(
171       MenuItem::ExtensionKey(extension()->id(), webview_instance_id));
172   SendResponse(true);
173   return true;
174 }
175
176 WebViewInternalClearDataFunction::WebViewInternalClearDataFunction()
177     : remove_mask_(0), bad_message_(false) {
178 }
179
180 WebViewInternalClearDataFunction::~WebViewInternalClearDataFunction() {
181 }
182
183 // Parses the |dataToRemove| argument to generate the remove mask. Sets
184 // |bad_message_| (like EXTENSION_FUNCTION_VALIDATE would if this were a bool
185 // method) if 'dataToRemove' is not present.
186 uint32 WebViewInternalClearDataFunction::GetRemovalMask() {
187   base::DictionaryValue* data_to_remove;
188   if (!args_->GetDictionary(2, &data_to_remove)) {
189     bad_message_ = true;
190     return 0;
191   }
192
193   uint32 remove_mask = 0;
194   for (base::DictionaryValue::Iterator i(*data_to_remove); !i.IsAtEnd();
195        i.Advance()) {
196     bool selected = false;
197     if (!i.value().GetAsBoolean(&selected)) {
198       bad_message_ = true;
199       return 0;
200     }
201     if (selected)
202       remove_mask |= MaskForKey(i.key().c_str());
203   }
204
205   return remove_mask;
206 }
207
208 // TODO(lazyboy): Parameters in this extension function are similar (or a
209 // sub-set) to BrowsingDataRemoverFunction. How can we share this code?
210 bool WebViewInternalClearDataFunction::RunAsyncSafe(WebViewGuest* guest) {
211   // Grab the initial |options| parameter, and parse out the arguments.
212   base::DictionaryValue* options;
213   EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options));
214   DCHECK(options);
215
216   // If |ms_since_epoch| isn't set, default it to 0.
217   double ms_since_epoch;
218   if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey,
219                           &ms_since_epoch)) {
220     ms_since_epoch = 0;
221   }
222
223   // base::Time takes a double that represents seconds since epoch. JavaScript
224   // gives developers milliseconds, so do a quick conversion before populating
225   // the object. Also, Time::FromDoubleT converts double time 0 to empty Time
226   // object. So we need to do special handling here.
227   remove_since_ = (ms_since_epoch == 0)
228                       ? base::Time::UnixEpoch()
229                       : base::Time::FromDoubleT(ms_since_epoch / 1000.0);
230
231   remove_mask_ = GetRemovalMask();
232   if (bad_message_)
233     return false;
234
235   AddRef();  // Balanced below or in WebViewInternalClearDataFunction::Done().
236
237   bool scheduled = false;
238   if (remove_mask_) {
239     scheduled = guest->ClearData(
240         remove_since_,
241         remove_mask_,
242         base::Bind(&WebViewInternalClearDataFunction::ClearDataDone, this));
243   }
244   if (!remove_mask_ || !scheduled) {
245     SendResponse(false);
246     Release();  // Balanced above.
247     return false;
248   }
249
250   // Will finish asynchronously.
251   return true;
252 }
253
254 void WebViewInternalClearDataFunction::ClearDataDone() {
255   Release();  // Balanced in RunAsync().
256   SendResponse(true);
257 }
258
259 WebViewInternalExecuteCodeFunction::WebViewInternalExecuteCodeFunction()
260     : guest_instance_id_(0), guest_src_(GURL::EmptyGURL()) {
261 }
262
263 WebViewInternalExecuteCodeFunction::~WebViewInternalExecuteCodeFunction() {
264 }
265
266 bool WebViewInternalExecuteCodeFunction::Init() {
267   if (details_.get())
268     return true;
269
270   if (!args_->GetInteger(0, &guest_instance_id_))
271     return false;
272
273   if (!guest_instance_id_)
274     return false;
275
276   std::string src;
277   if (!args_->GetString(1, &src))
278     return false;
279
280   guest_src_ = GURL(src);
281   if (!guest_src_.is_valid())
282     return false;
283
284   base::DictionaryValue* details_value = NULL;
285   if (!args_->GetDictionary(2, &details_value))
286     return false;
287   scoped_ptr<InjectDetails> details(new InjectDetails());
288   if (!InjectDetails::Populate(*details_value, details.get()))
289     return false;
290
291   details_ = details.Pass();
292   return true;
293 }
294
295 bool WebViewInternalExecuteCodeFunction::ShouldInsertCSS() const {
296   return false;
297 }
298
299 bool WebViewInternalExecuteCodeFunction::CanExecuteScriptOnPage() {
300   return true;
301 }
302
303 extensions::ScriptExecutor*
304 WebViewInternalExecuteCodeFunction::GetScriptExecutor() {
305   WebViewGuest* guest = WebViewGuest::From(
306       render_view_host()->GetProcess()->GetID(), guest_instance_id_);
307   if (!guest)
308     return NULL;
309
310   return guest->script_executor();
311 }
312
313 bool WebViewInternalExecuteCodeFunction::IsWebView() const {
314   return true;
315 }
316
317 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const {
318   return guest_src_;
319 }
320
321 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() {
322 }
323
324 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished(
325     const std::string& error,
326     const GURL& on_url,
327     const base::ListValue& result) {
328   if (error.empty())
329     SetResult(result.DeepCopy());
330   WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished(
331       error, on_url, result);
332 }
333
334 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() {
335 }
336
337 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const {
338   return true;
339 }
340
341 WebViewInternalCaptureVisibleRegionFunction::
342     WebViewInternalCaptureVisibleRegionFunction() {
343 }
344
345 WebViewInternalCaptureVisibleRegionFunction::
346     ~WebViewInternalCaptureVisibleRegionFunction() {
347 }
348
349 bool WebViewInternalCaptureVisibleRegionFunction::IsScreenshotEnabled() {
350   return true;
351 }
352
353 WebContents* WebViewInternalCaptureVisibleRegionFunction::GetWebContentsForID(
354     int instance_id) {
355   WebViewGuest* guest = WebViewGuest::From(
356       render_view_host()->GetProcess()->GetID(), instance_id);
357   return guest ? guest->guest_web_contents() : NULL;
358 }
359
360 void WebViewInternalCaptureVisibleRegionFunction::OnCaptureFailure(
361     FailureReason reason) {
362   SendResponse(false);
363 }
364
365 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() {
366 }
367
368 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() {
369 }
370
371 WebViewInternalSetZoomFunction::WebViewInternalSetZoomFunction() {
372 }
373
374 WebViewInternalSetZoomFunction::~WebViewInternalSetZoomFunction() {
375 }
376
377 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) {
378   scoped_ptr<webview::SetName::Params> params(
379       webview::SetName::Params::Create(*args_));
380   EXTENSION_FUNCTION_VALIDATE(params.get());
381   guest->SetName(params->frame_name);
382   SendResponse(true);
383   return true;
384 }
385
386 bool WebViewInternalSetZoomFunction::RunAsyncSafe(WebViewGuest* guest) {
387   scoped_ptr<webview::SetZoom::Params> params(
388       webview::SetZoom::Params::Create(*args_));
389   EXTENSION_FUNCTION_VALIDATE(params.get());
390   guest->SetZoom(params->zoom_factor);
391
392   SendResponse(true);
393   return true;
394 }
395
396 WebViewInternalGetZoomFunction::WebViewInternalGetZoomFunction() {
397 }
398
399 WebViewInternalGetZoomFunction::~WebViewInternalGetZoomFunction() {
400 }
401
402 bool WebViewInternalGetZoomFunction::RunAsyncSafe(WebViewGuest* guest) {
403   scoped_ptr<webview::GetZoom::Params> params(
404       webview::GetZoom::Params::Create(*args_));
405   EXTENSION_FUNCTION_VALIDATE(params.get());
406
407   double zoom_factor = guest->GetZoom();
408   SetResult(new base::FundamentalValue(zoom_factor));
409   SendResponse(true);
410   return true;
411 }
412
413 WebViewInternalFindFunction::WebViewInternalFindFunction() {
414 }
415
416 WebViewInternalFindFunction::~WebViewInternalFindFunction() {
417 }
418
419 bool WebViewInternalFindFunction::RunAsyncSafe(WebViewGuest* guest) {
420   scoped_ptr<webview::Find::Params> params(
421       webview::Find::Params::Create(*args_));
422   EXTENSION_FUNCTION_VALIDATE(params.get());
423
424   // Convert the std::string search_text to string16.
425   base::string16 search_text;
426   base::UTF8ToUTF16(
427       params->search_text.c_str(), params->search_text.length(), &search_text);
428
429   // Set the find options to their default values.
430   blink::WebFindOptions options;
431   if (params->options) {
432     options.forward =
433         params->options->backward ? !*params->options->backward : true;
434     options.matchCase =
435         params->options->match_case ? *params->options->match_case : false;
436   }
437
438   guest->Find(search_text, options, this);
439   return true;
440 }
441
442 WebViewInternalStopFindingFunction::WebViewInternalStopFindingFunction() {
443 }
444
445 WebViewInternalStopFindingFunction::~WebViewInternalStopFindingFunction() {
446 }
447
448 bool WebViewInternalStopFindingFunction::RunAsyncSafe(WebViewGuest* guest) {
449   scoped_ptr<webview::StopFinding::Params> params(
450       webview::StopFinding::Params::Create(*args_));
451   EXTENSION_FUNCTION_VALIDATE(params.get());
452
453   // Set the StopFindAction.
454   content::StopFindAction action;
455   switch (params->action) {
456     case webview::StopFinding::Params::ACTION_CLEAR:
457       action = content::STOP_FIND_ACTION_CLEAR_SELECTION;
458       break;
459     case webview::StopFinding::Params::ACTION_KEEP:
460       action = content::STOP_FIND_ACTION_KEEP_SELECTION;
461       break;
462     case webview::StopFinding::Params::ACTION_ACTIVATE:
463       action = content::STOP_FIND_ACTION_ACTIVATE_SELECTION;
464       break;
465     default:
466       action = content::STOP_FIND_ACTION_KEEP_SELECTION;
467   }
468
469   guest->StopFinding(action);
470   return true;
471 }
472
473 WebViewInternalGoFunction::WebViewInternalGoFunction() {
474 }
475
476 WebViewInternalGoFunction::~WebViewInternalGoFunction() {
477 }
478
479 bool WebViewInternalGoFunction::RunAsyncSafe(WebViewGuest* guest) {
480   scoped_ptr<webview::Go::Params> params(webview::Go::Params::Create(*args_));
481   EXTENSION_FUNCTION_VALIDATE(params.get());
482
483   guest->Go(params->relative_index);
484   return true;
485 }
486
487 WebViewInternalReloadFunction::WebViewInternalReloadFunction() {
488 }
489
490 WebViewInternalReloadFunction::~WebViewInternalReloadFunction() {
491 }
492
493 bool WebViewInternalReloadFunction::RunAsyncSafe(WebViewGuest* guest) {
494   guest->Reload();
495   return true;
496 }
497
498 WebViewInternalSetPermissionFunction::WebViewInternalSetPermissionFunction() {
499 }
500
501 WebViewInternalSetPermissionFunction::~WebViewInternalSetPermissionFunction() {
502 }
503
504 bool WebViewInternalSetPermissionFunction::RunAsyncSafe(WebViewGuest* guest) {
505   scoped_ptr<webview::SetPermission::Params> params(
506       webview::SetPermission::Params::Create(*args_));
507   EXTENSION_FUNCTION_VALIDATE(params.get());
508
509   WebViewPermissionHelper::PermissionResponseAction action =
510       WebViewPermissionHelper::DEFAULT;
511   switch (params->action) {
512     case Params::ACTION_ALLOW:
513       action = WebViewPermissionHelper::ALLOW;
514       break;
515     case Params::ACTION_DENY:
516       action = WebViewPermissionHelper::DENY;
517       break;
518     case Params::ACTION_DEFAULT:
519       break;
520     default:
521       NOTREACHED();
522   }
523
524   std::string user_input;
525   if (params->user_input)
526     user_input = *params->user_input;
527
528   WebViewPermissionHelper* web_view_permission_helper =
529       WebViewPermissionHelper:: FromWebContents(guest->guest_web_contents());
530
531   WebViewPermissionHelper::SetPermissionResult result =
532       web_view_permission_helper->SetPermission(
533           params->request_id, action, user_input);
534
535   EXTENSION_FUNCTION_VALIDATE(
536       result != WebViewPermissionHelper::SET_PERMISSION_INVALID);
537
538   SetResult(new base::FundamentalValue(
539       result == WebViewPermissionHelper::SET_PERMISSION_ALLOWED));
540   SendResponse(true);
541   return true;
542 }
543
544 WebViewInternalShowContextMenuFunction::
545     WebViewInternalShowContextMenuFunction() {
546 }
547
548 WebViewInternalShowContextMenuFunction::
549     ~WebViewInternalShowContextMenuFunction() {
550 }
551
552 bool WebViewInternalShowContextMenuFunction::RunAsyncSafe(WebViewGuest* guest) {
553   scoped_ptr<webview::ShowContextMenu::Params> params(
554       webview::ShowContextMenu::Params::Create(*args_));
555   EXTENSION_FUNCTION_VALIDATE(params.get());
556
557   // TODO(lazyboy): Actually implement filtering menu items, we pass NULL for
558   // now.
559   guest->ShowContextMenu(params->request_id, NULL);
560
561   SendResponse(true);
562   return true;
563 }
564
565 WebViewInternalOverrideUserAgentFunction::
566     WebViewInternalOverrideUserAgentFunction() {
567 }
568
569 WebViewInternalOverrideUserAgentFunction::
570     ~WebViewInternalOverrideUserAgentFunction() {
571 }
572
573 bool WebViewInternalOverrideUserAgentFunction::RunAsyncSafe(
574     WebViewGuest* guest) {
575   scoped_ptr<webview::OverrideUserAgent::Params> params(
576       webview::OverrideUserAgent::Params::Create(*args_));
577   EXTENSION_FUNCTION_VALIDATE(params.get());
578
579   guest->SetUserAgentOverride(params->user_agent_override);
580   return true;
581 }
582
583 WebViewInternalStopFunction::WebViewInternalStopFunction() {
584 }
585
586 WebViewInternalStopFunction::~WebViewInternalStopFunction() {
587 }
588
589 bool WebViewInternalStopFunction::RunAsyncSafe(WebViewGuest* guest) {
590   guest->Stop();
591   return true;
592 }
593
594 WebViewInternalTerminateFunction::WebViewInternalTerminateFunction() {
595 }
596
597 WebViewInternalTerminateFunction::~WebViewInternalTerminateFunction() {
598 }
599
600 bool WebViewInternalTerminateFunction::RunAsyncSafe(WebViewGuest* guest) {
601   guest->Terminate();
602   return true;
603 }
604
605 }  // namespace extensions