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 "extensions/browser/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 "content/public/browser/render_process_host.h" | 8 #include "content/public/browser/render_process_host.h" |
9 #include "content/public/browser/render_view_host.h" | 9 #include "content/public/browser/render_view_host.h" |
| 10 #include "content/public/browser/storage_partition.h" |
10 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
11 #include "content/public/common/stop_find_action.h" | 12 #include "content/public/common/stop_find_action.h" |
12 #include "extensions/common/api/web_view_internal.h" | 13 #include "extensions/common/api/web_view_internal.h" |
13 #include "third_party/WebKit/public/web/WebFindOptions.h" | 14 #include "third_party/WebKit/public/web/WebFindOptions.h" |
14 | 15 |
15 using content::WebContents; | 16 using content::WebContents; |
16 using extensions::core_api::web_view_internal::SetPermission::Params; | 17 using extensions::core_api::web_view_internal::SetPermission::Params; |
17 using extensions::core_api::extension_types::InjectDetails; | 18 using extensions::core_api::extension_types::InjectDetails; |
18 namespace webview = extensions::core_api::web_view_internal; | 19 namespace webview = extensions::core_api::web_view_internal; |
19 | 20 |
| 21 namespace { |
| 22 |
| 23 const char kAppCacheKey[] = "appcache"; |
| 24 const char kCookiesKey[] = "cookies"; |
| 25 const char kFileSystemsKey[] = "fileSystems"; |
| 26 const char kIndexedDBKey[] = "indexedDB"; |
| 27 const char kLocalStorageKey[] = "localStorage"; |
| 28 const char kWebSQLKey[] = "webSQL"; |
| 29 const char kSinceKey[] = "since"; |
| 30 |
| 31 int MaskForKey(const char* key) { |
| 32 if (strcmp(key, kAppCacheKey) == 0) |
| 33 return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE; |
| 34 if (strcmp(key, kCookiesKey) == 0) |
| 35 return content::StoragePartition::REMOVE_DATA_MASK_COOKIES; |
| 36 if (strcmp(key, kFileSystemsKey) == 0) |
| 37 return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS; |
| 38 if (strcmp(key, kIndexedDBKey) == 0) |
| 39 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB; |
| 40 if (strcmp(key, kLocalStorageKey) == 0) |
| 41 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; |
| 42 if (strcmp(key, kWebSQLKey) == 0) |
| 43 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL; |
| 44 return 0; |
| 45 } |
| 46 |
| 47 } // namespace |
| 48 |
20 namespace extensions { | 49 namespace extensions { |
21 | 50 |
22 bool WebViewInternalExtensionFunction::RunAsync() { | 51 bool WebViewInternalExtensionFunction::RunAsync() { |
23 int instance_id = 0; | 52 int instance_id = 0; |
24 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); | 53 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); |
25 WebViewGuest* guest = WebViewGuest::From( | 54 WebViewGuest* guest = WebViewGuest::From( |
26 render_view_host()->GetProcess()->GetID(), instance_id); | 55 render_view_host()->GetProcess()->GetID(), instance_id); |
27 if (!guest) | 56 if (!guest) |
28 return false; | 57 return false; |
29 | 58 |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 } | 386 } |
358 | 387 |
359 WebViewInternalTerminateFunction::~WebViewInternalTerminateFunction() { | 388 WebViewInternalTerminateFunction::~WebViewInternalTerminateFunction() { |
360 } | 389 } |
361 | 390 |
362 bool WebViewInternalTerminateFunction::RunAsyncSafe(WebViewGuest* guest) { | 391 bool WebViewInternalTerminateFunction::RunAsyncSafe(WebViewGuest* guest) { |
363 guest->Terminate(); | 392 guest->Terminate(); |
364 return true; | 393 return true; |
365 } | 394 } |
366 | 395 |
| 396 WebViewInternalClearDataFunction::WebViewInternalClearDataFunction() |
| 397 : remove_mask_(0), bad_message_(false) { |
| 398 } |
| 399 |
| 400 WebViewInternalClearDataFunction::~WebViewInternalClearDataFunction() { |
| 401 } |
| 402 |
| 403 // Parses the |dataToRemove| argument to generate the remove mask. Sets |
| 404 // |bad_message_| (like EXTENSION_FUNCTION_VALIDATE would if this were a bool |
| 405 // method) if 'dataToRemove' is not present. |
| 406 uint32 WebViewInternalClearDataFunction::GetRemovalMask() { |
| 407 base::DictionaryValue* data_to_remove; |
| 408 if (!args_->GetDictionary(2, &data_to_remove)) { |
| 409 bad_message_ = true; |
| 410 return 0; |
| 411 } |
| 412 |
| 413 uint32 remove_mask = 0; |
| 414 for (base::DictionaryValue::Iterator i(*data_to_remove); !i.IsAtEnd(); |
| 415 i.Advance()) { |
| 416 bool selected = false; |
| 417 if (!i.value().GetAsBoolean(&selected)) { |
| 418 bad_message_ = true; |
| 419 return 0; |
| 420 } |
| 421 if (selected) |
| 422 remove_mask |= MaskForKey(i.key().c_str()); |
| 423 } |
| 424 |
| 425 return remove_mask; |
| 426 } |
| 427 |
| 428 // TODO(lazyboy): Parameters in this extension function are similar (or a |
| 429 // sub-set) to BrowsingDataRemoverFunction. How can we share this code? |
| 430 bool WebViewInternalClearDataFunction::RunAsyncSafe(WebViewGuest* guest) { |
| 431 // Grab the initial |options| parameter, and parse out the arguments. |
| 432 base::DictionaryValue* options; |
| 433 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); |
| 434 DCHECK(options); |
| 435 |
| 436 // If |ms_since_epoch| isn't set, default it to 0. |
| 437 double ms_since_epoch; |
| 438 if (!options->GetDouble(kSinceKey, &ms_since_epoch)) { |
| 439 ms_since_epoch = 0; |
| 440 } |
| 441 |
| 442 // base::Time takes a double that represents seconds since epoch. JavaScript |
| 443 // gives developers milliseconds, so do a quick conversion before populating |
| 444 // the object. Also, Time::FromDoubleT converts double time 0 to empty Time |
| 445 // object. So we need to do special handling here. |
| 446 remove_since_ = (ms_since_epoch == 0) |
| 447 ? base::Time::UnixEpoch() |
| 448 : base::Time::FromDoubleT(ms_since_epoch / 1000.0); |
| 449 |
| 450 remove_mask_ = GetRemovalMask(); |
| 451 if (bad_message_) |
| 452 return false; |
| 453 |
| 454 AddRef(); // Balanced below or in WebViewInternalClearDataFunction::Done(). |
| 455 |
| 456 bool scheduled = false; |
| 457 if (remove_mask_) { |
| 458 scheduled = guest->ClearData( |
| 459 remove_since_, |
| 460 remove_mask_, |
| 461 base::Bind(&WebViewInternalClearDataFunction::ClearDataDone, this)); |
| 462 } |
| 463 if (!remove_mask_ || !scheduled) { |
| 464 SendResponse(false); |
| 465 Release(); // Balanced above. |
| 466 return false; |
| 467 } |
| 468 |
| 469 // Will finish asynchronously. |
| 470 return true; |
| 471 } |
| 472 |
| 473 void WebViewInternalClearDataFunction::ClearDataDone() { |
| 474 Release(); // Balanced in RunAsync(). |
| 475 SendResponse(true); |
| 476 } |
| 477 |
367 } // namespace extensions | 478 } // namespace extensions |
OLD | NEW |