| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/webview/webview_api.h" | 5 #include "chrome/browser/extensions/api/webview/webview_api.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/browsing_data/browsing_data_api.h" | 7 #include "chrome/browser/extensions/api/browsing_data/browsing_data_api.h" |
| 8 #include "chrome/browser/extensions/tab_helper.h" | 8 #include "chrome/browser/extensions/tab_helper.h" |
| 9 #include "chrome/browser/guestview/webview/webview_guest.h" | |
| 10 #include "chrome/common/extensions/api/webview.h" | 9 #include "chrome/common/extensions/api/webview.h" |
| 11 #include "content/public/browser/render_process_host.h" | 10 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/browser/render_view_host.h" | 11 #include "content/public/browser/render_view_host.h" |
| 13 #include "content/public/browser/storage_partition.h" | 12 #include "content/public/browser/storage_partition.h" |
| 14 #include "content/public/browser/user_metrics.h" | 13 #include "content/public/browser/user_metrics.h" |
| 15 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 16 #include "extensions/common/error_utils.h" | 15 #include "extensions/common/error_utils.h" |
| 17 | 16 |
| 18 using extensions::api::tabs::InjectDetails; | 17 using extensions::api::tabs::InjectDetails; |
| 19 namespace webview = extensions::api::webview; | 18 namespace webview = extensions::api::webview; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 32 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB; | 31 return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB; |
| 33 if (strcmp(key, extension_browsing_data_api_constants::kLocalStorageKey) == 0) | 32 if (strcmp(key, extension_browsing_data_api_constants::kLocalStorageKey) == 0) |
| 34 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; | 33 return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; |
| 35 if (strcmp(key, extension_browsing_data_api_constants::kWebSQLKey) == 0) | 34 if (strcmp(key, extension_browsing_data_api_constants::kWebSQLKey) == 0) |
| 36 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL; | 35 return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL; |
| 37 return 0; | 36 return 0; |
| 38 } | 37 } |
| 39 | 38 |
| 40 } // namespace | 39 } // namespace |
| 41 | 40 |
| 41 bool WebviewExtensionFunction::RunImpl() { |
| 42 int instance_id = 0; |
| 43 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); |
| 44 WebViewGuest* guest = WebViewGuest::From( |
| 45 render_view_host()->GetProcess()->GetID(), instance_id); |
| 46 if (!guest) |
| 47 return false; |
| 48 |
| 49 return RunImplSafe(guest); |
| 50 } |
| 51 |
| 42 WebviewClearDataFunction::WebviewClearDataFunction() | 52 WebviewClearDataFunction::WebviewClearDataFunction() |
| 43 : remove_mask_(0), | 53 : remove_mask_(0), |
| 44 bad_message_(false) { | 54 bad_message_(false) { |
| 45 }; | 55 }; |
| 46 | 56 |
| 47 WebviewClearDataFunction::~WebviewClearDataFunction() { | 57 WebviewClearDataFunction::~WebviewClearDataFunction() { |
| 48 }; | 58 }; |
| 49 | 59 |
| 50 // Parses the |dataToRemove| argument to generate the remove mask. Sets | 60 // Parses the |dataToRemove| argument to generate the remove mask. Sets |
| 51 // |bad_message_| (like EXTENSION_FUNCTION_VALIDATE would if this were a bool | 61 // |bad_message_| (like EXTENSION_FUNCTION_VALIDATE would if this were a bool |
| (...skipping 16 matching lines...) Expand all Loading... |
| 68 } | 78 } |
| 69 if (selected) | 79 if (selected) |
| 70 remove_mask |= MaskForKey(i.key().c_str()); | 80 remove_mask |= MaskForKey(i.key().c_str()); |
| 71 } | 81 } |
| 72 | 82 |
| 73 return remove_mask; | 83 return remove_mask; |
| 74 } | 84 } |
| 75 | 85 |
| 76 // TODO(lazyboy): Parameters in this extension function are similar (or a | 86 // TODO(lazyboy): Parameters in this extension function are similar (or a |
| 77 // sub-set) to BrowsingDataRemoverFunction. How can we share this code? | 87 // sub-set) to BrowsingDataRemoverFunction. How can we share this code? |
| 78 bool WebviewClearDataFunction::RunImpl() { | 88 bool WebviewClearDataFunction::RunImplSafe(WebViewGuest* guest) { |
| 79 content::RecordAction(content::UserMetricsAction("WebView.ClearData")); | 89 content::RecordAction(content::UserMetricsAction("WebView.ClearData")); |
| 80 int instance_id = 0; | |
| 81 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); | |
| 82 | 90 |
| 83 // Grab the initial |options| parameter, and parse out the arguments. | 91 // Grab the initial |options| parameter, and parse out the arguments. |
| 84 base::DictionaryValue* options; | 92 base::DictionaryValue* options; |
| 85 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); | 93 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); |
| 86 DCHECK(options); | 94 DCHECK(options); |
| 87 | 95 |
| 88 // If |ms_since_epoch| isn't set, default it to 0. | 96 // If |ms_since_epoch| isn't set, default it to 0. |
| 89 double ms_since_epoch; | 97 double ms_since_epoch; |
| 90 if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey, | 98 if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey, |
| 91 &ms_since_epoch)) { | 99 &ms_since_epoch)) { |
| 92 ms_since_epoch = 0; | 100 ms_since_epoch = 0; |
| 93 } | 101 } |
| 94 | 102 |
| 95 // base::Time takes a double that represents seconds since epoch. JavaScript | 103 // base::Time takes a double that represents seconds since epoch. JavaScript |
| 96 // gives developers milliseconds, so do a quick conversion before populating | 104 // gives developers milliseconds, so do a quick conversion before populating |
| 97 // the object. Also, Time::FromDoubleT converts double time 0 to empty Time | 105 // the object. Also, Time::FromDoubleT converts double time 0 to empty Time |
| 98 // object. So we need to do special handling here. | 106 // object. So we need to do special handling here. |
| 99 remove_since_ = (ms_since_epoch == 0) ? | 107 remove_since_ = (ms_since_epoch == 0) ? |
| 100 base::Time::UnixEpoch() : | 108 base::Time::UnixEpoch() : |
| 101 base::Time::FromDoubleT(ms_since_epoch / 1000.0); | 109 base::Time::FromDoubleT(ms_since_epoch / 1000.0); |
| 102 | 110 |
| 103 remove_mask_ = GetRemovalMask(); | 111 remove_mask_ = GetRemovalMask(); |
| 104 if (bad_message_) | 112 if (bad_message_) |
| 105 return false; | 113 return false; |
| 106 | 114 |
| 107 WebViewGuest* guest = WebViewGuest::From( | |
| 108 render_view_host()->GetProcess()->GetID(), instance_id); | |
| 109 if (!guest) | |
| 110 return false; | |
| 111 | |
| 112 AddRef(); // Balanced below or in WebviewClearDataFunction::Done(). | 115 AddRef(); // Balanced below or in WebviewClearDataFunction::Done(). |
| 113 | 116 |
| 114 bool scheduled = false; | 117 bool scheduled = false; |
| 115 if (remove_mask_) { | 118 if (remove_mask_) { |
| 116 scheduled = guest->ClearData( | 119 scheduled = guest->ClearData( |
| 117 remove_since_, | 120 remove_since_, |
| 118 remove_mask_, | 121 remove_mask_, |
| 119 base::Bind(&WebviewClearDataFunction::ClearDataDone, | 122 base::Bind(&WebviewClearDataFunction::ClearDataDone, |
| 120 this)); | 123 this)); |
| 121 } | 124 } |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 bool WebviewInsertCSSFunction::ShouldInsertCSS() const { | 207 bool WebviewInsertCSSFunction::ShouldInsertCSS() const { |
| 205 return true; | 208 return true; |
| 206 } | 209 } |
| 207 | 210 |
| 208 WebviewGoFunction::WebviewGoFunction() { | 211 WebviewGoFunction::WebviewGoFunction() { |
| 209 } | 212 } |
| 210 | 213 |
| 211 WebviewGoFunction::~WebviewGoFunction() { | 214 WebviewGoFunction::~WebviewGoFunction() { |
| 212 } | 215 } |
| 213 | 216 |
| 214 bool WebviewGoFunction::RunImpl() { | 217 bool WebviewGoFunction::RunImplSafe(WebViewGuest* guest) { |
| 215 content::RecordAction(content::UserMetricsAction("WebView.Go")); | 218 content::RecordAction(content::UserMetricsAction("WebView.Go")); |
| 216 scoped_ptr<webview::Go::Params> params(webview::Go::Params::Create(*args_)); | 219 scoped_ptr<webview::Go::Params> params(webview::Go::Params::Create(*args_)); |
| 217 EXTENSION_FUNCTION_VALIDATE(params.get()); | 220 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 218 | 221 |
| 219 WebViewGuest* guest = WebViewGuest::From( | |
| 220 render_view_host()->GetProcess()->GetID(), params->instance_id); | |
| 221 if (!guest) | |
| 222 return false; | |
| 223 | |
| 224 guest->Go(params->relative_index); | 222 guest->Go(params->relative_index); |
| 225 return true; | 223 return true; |
| 226 } | 224 } |
| 227 | 225 |
| 228 WebviewReloadFunction::WebviewReloadFunction() { | 226 WebviewReloadFunction::WebviewReloadFunction() { |
| 229 } | 227 } |
| 230 | 228 |
| 231 WebviewReloadFunction::~WebviewReloadFunction() { | 229 WebviewReloadFunction::~WebviewReloadFunction() { |
| 232 } | 230 } |
| 233 | 231 |
| 234 bool WebviewReloadFunction::RunImpl() { | 232 bool WebviewReloadFunction::RunImplSafe(WebViewGuest* guest) { |
| 235 content::RecordAction(content::UserMetricsAction("WebView.Reload")); | 233 content::RecordAction(content::UserMetricsAction("WebView.Reload")); |
| 236 scoped_ptr<webview::Reload::Params> params( | 234 scoped_ptr<webview::Reload::Params> params( |
| 237 webview::Reload::Params::Create(*args_)); | 235 webview::Reload::Params::Create(*args_)); |
| 238 EXTENSION_FUNCTION_VALIDATE(params.get()); | 236 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 239 | 237 |
| 240 WebViewGuest* guest = WebViewGuest::From( | |
| 241 render_view_host()->GetProcess()->GetID(), params->instance_id); | |
| 242 if (!guest) | |
| 243 return false; | |
| 244 | |
| 245 guest->Reload(); | 238 guest->Reload(); |
| 246 return true; | 239 return true; |
| 247 } | 240 } |
| 248 | 241 |
| 249 WebviewSetPermissionFunction::WebviewSetPermissionFunction() { | 242 WebviewSetPermissionFunction::WebviewSetPermissionFunction() { |
| 250 } | 243 } |
| 251 | 244 |
| 252 WebviewSetPermissionFunction::~WebviewSetPermissionFunction() { | 245 WebviewSetPermissionFunction::~WebviewSetPermissionFunction() { |
| 253 } | 246 } |
| 254 | 247 |
| 255 bool WebviewSetPermissionFunction::RunImpl() { | 248 bool WebviewSetPermissionFunction::RunImplSafe(WebViewGuest* guest) { |
| 256 scoped_ptr<webview::SetPermission::Params> params( | 249 scoped_ptr<webview::SetPermission::Params> params( |
| 257 webview::SetPermission::Params::Create(*args_)); | 250 webview::SetPermission::Params::Create(*args_)); |
| 258 EXTENSION_FUNCTION_VALIDATE(params.get()); | 251 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 259 | 252 |
| 260 WebViewGuest* guest = WebViewGuest::From( | |
| 261 render_view_host()->GetProcess()->GetID(), params->instance_id); | |
| 262 if (!guest) | |
| 263 return false; | |
| 264 | |
| 265 EXTENSION_FUNCTION_VALIDATE( | 253 EXTENSION_FUNCTION_VALIDATE( |
| 266 guest->SetPermission(params->request_id, | 254 guest->SetPermission(params->request_id, |
| 267 params->should_allow, | 255 params->should_allow, |
| 268 params->user_input)); | 256 params->user_input)); |
| 269 return true; | 257 return true; |
| 270 } | 258 } |
| 271 | 259 |
| 272 WebviewOverrideUserAgentFunction::WebviewOverrideUserAgentFunction() { | 260 WebviewOverrideUserAgentFunction::WebviewOverrideUserAgentFunction() { |
| 273 } | 261 } |
| 274 | 262 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 290 guest->SetUserAgentOverride(user_agent_override); | 278 guest->SetUserAgentOverride(user_agent_override); |
| 291 return true; | 279 return true; |
| 292 } | 280 } |
| 293 | 281 |
| 294 WebviewStopFunction::WebviewStopFunction() { | 282 WebviewStopFunction::WebviewStopFunction() { |
| 295 } | 283 } |
| 296 | 284 |
| 297 WebviewStopFunction::~WebviewStopFunction() { | 285 WebviewStopFunction::~WebviewStopFunction() { |
| 298 } | 286 } |
| 299 | 287 |
| 300 bool WebviewStopFunction::RunImpl() { | 288 bool WebviewStopFunction::RunImplSafe(WebViewGuest* guest) { |
| 301 content::RecordAction(content::UserMetricsAction("WebView.Stop")); | 289 content::RecordAction(content::UserMetricsAction("WebView.Stop")); |
| 302 scoped_ptr<webview::Stop::Params> params( | 290 scoped_ptr<webview::Stop::Params> params( |
| 303 webview::Stop::Params::Create(*args_)); | 291 webview::Stop::Params::Create(*args_)); |
| 304 EXTENSION_FUNCTION_VALIDATE(params.get()); | 292 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 305 | 293 |
| 306 WebViewGuest* guest = WebViewGuest::From( | |
| 307 render_view_host()->GetProcess()->GetID(), params->instance_id); | |
| 308 if (!guest) | |
| 309 return false; | |
| 310 | |
| 311 guest->Stop(); | 294 guest->Stop(); |
| 312 return true; | 295 return true; |
| 313 } | 296 } |
| 314 | 297 |
| 315 WebviewTerminateFunction::WebviewTerminateFunction() { | 298 WebviewTerminateFunction::WebviewTerminateFunction() { |
| 316 } | 299 } |
| 317 | 300 |
| 318 WebviewTerminateFunction::~WebviewTerminateFunction() { | 301 WebviewTerminateFunction::~WebviewTerminateFunction() { |
| 319 } | 302 } |
| 320 | 303 |
| 321 bool WebviewTerminateFunction::RunImpl() { | 304 bool WebviewTerminateFunction::RunImplSafe(WebViewGuest* guest) { |
| 322 content::RecordAction(content::UserMetricsAction("WebView.Terminate")); | 305 content::RecordAction(content::UserMetricsAction("WebView.Terminate")); |
| 323 scoped_ptr<webview::Terminate::Params> params( | 306 scoped_ptr<webview::Terminate::Params> params( |
| 324 webview::Terminate::Params::Create(*args_)); | 307 webview::Terminate::Params::Create(*args_)); |
| 325 EXTENSION_FUNCTION_VALIDATE(params.get()); | 308 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 326 | 309 |
| 327 WebViewGuest* guest = WebViewGuest::From( | |
| 328 render_view_host()->GetProcess()->GetID(), params->instance_id); | |
| 329 if (!guest) | |
| 330 return false; | |
| 331 | |
| 332 guest->Terminate(); | 310 guest->Terminate(); |
| 333 return true; | 311 return true; |
| 334 } | 312 } |
| 335 | 313 |
| 336 } // namespace extensions | 314 } // namespace extensions |
| OLD | NEW |