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 // 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:: |
| 233 ChromeWebViewInternalShowContextMenuFunction() { |
| 234 } |
| 235 |
| 236 ChromeWebViewInternalShowContextMenuFunction:: |
| 237 ~ChromeWebViewInternalShowContextMenuFunction() { |
| 238 } |
| 239 |
| 240 bool ChromeWebViewInternalShowContextMenuFunction::RunAsyncSafe( |
| 241 WebViewGuest* guest) { |
| 242 scoped_ptr<webview::ShowContextMenu::Params> params( |
| 243 webview::ShowContextMenu::Params::Create(*args_)); |
| 244 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 245 |
| 246 // TODO(lazyboy): Actually implement filtering menu items, we pass NULL for |
| 247 // now. |
| 248 guest->ShowContextMenu(params->request_id, NULL); |
| 249 |
| 250 SendResponse(true); |
| 251 return true; |
| 252 } |
| 253 |
| 254 } // namespace extensions |
OLD | NEW |