| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extension_tabs_module.h" | 5 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/string16.h" | 16 #include "base/string16.h" |
| 17 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
| 18 #include "base/string_util.h" | 18 #include "base/string_util.h" |
| 19 #include "base/stringprintf.h" | 19 #include "base/stringprintf.h" |
| 20 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 21 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 21 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| 22 #include "chrome/browser/extensions/extension_host.h" | 22 #include "chrome/browser/extensions/extension_host.h" |
| 23 #include "chrome/browser/extensions/extension_service.h" | 23 #include "chrome/browser/extensions/extension_service.h" |
| 24 #include "chrome/browser/extensions/extension_tab_helper.h" | 24 #include "chrome/browser/extensions/extension_tab_helper.h" |
| 25 #include "chrome/browser/extensions/extension_tab_util.h" | 25 #include "chrome/browser/extensions/extension_tab_util.h" |
| 26 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 26 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
| 27 #include "chrome/browser/extensions/extension_window_controller.h" | 27 #include "chrome/browser/extensions/extension_window_controller.h" |
| 28 #include "chrome/browser/extensions/extension_window_list.h" | 28 #include "chrome/browser/extensions/extension_window_list.h" |
| 29 #include "chrome/browser/net/url_fixer_upper.h" |
| 29 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 30 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
| 30 #include "chrome/browser/profiles/profile.h" | 31 #include "chrome/browser/profiles/profile.h" |
| 31 #include "chrome/browser/sessions/restore_tab_helper.h" | 32 #include "chrome/browser/sessions/restore_tab_helper.h" |
| 32 #include "chrome/browser/tabs/tab_strip_model.h" | 33 #include "chrome/browser/tabs/tab_strip_model.h" |
| 33 #include "chrome/browser/translate/translate_tab_helper.h" | 34 #include "chrome/browser/translate/translate_tab_helper.h" |
| 34 #include "chrome/browser/ui/browser.h" | 35 #include "chrome/browser/ui/browser.h" |
| 35 #include "chrome/browser/ui/browser_list.h" | 36 #include "chrome/browser/ui/browser_list.h" |
| 36 #include "chrome/browser/ui/browser_navigator.h" | 37 #include "chrome/browser/ui/browser_navigator.h" |
| 37 #include "chrome/browser/ui/browser_window.h" | 38 #include "chrome/browser/ui/browser_window.h" |
| 38 #include "chrome/browser/ui/panels/panel_manager.h" | 39 #include "chrome/browser/ui/panels/panel_manager.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 browser, tab_strip, contents, tab_index)) | 184 browser, tab_strip, contents, tab_index)) |
| 184 return true; | 185 return true; |
| 185 | 186 |
| 186 if (error_message) | 187 if (error_message) |
| 187 *error_message = ExtensionErrorUtils::FormatErrorMessage( | 188 *error_message = ExtensionErrorUtils::FormatErrorMessage( |
| 188 keys::kTabNotFoundError, base::IntToString(tab_id)); | 189 keys::kTabNotFoundError, base::IntToString(tab_id)); |
| 189 | 190 |
| 190 return false; | 191 return false; |
| 191 } | 192 } |
| 192 | 193 |
| 194 // Takes |url_string| and returns a GURL which is either valid and absolute |
| 195 // or invalid. If |url_string| is not directly interpretable as a valid (it is |
| 196 // likely a relative URL) an attempt is made to resolve it. |extension| is |
| 197 // provided so it can be resolved relative to its extension base |
| 198 // (chrome-extension://<id>/). Using the source frame url would be more correct, |
| 199 // but because the api shipped with urls resolved relative to their extension |
| 200 // base, we decided it wasn't worth breaking existing extensions to fix. |
| 201 GURL ResolvePossiblyRelativeURL(const std::string& url_string, |
| 202 const Extension* extension) { |
| 203 GURL url = GURL(url_string); |
| 204 if (!url.is_valid()) |
| 205 url = extension->GetResourceURL(url_string); |
| 206 |
| 207 return url; |
| 208 } |
| 209 |
| 210 bool IsCrashURL(const GURL& url) { |
| 211 // Check a fixed-up URL, to normalize the scheme and parse hosts correctly. |
| 212 GURL fixed_url = |
| 213 URLFixerUpper::FixupURL(url.possibly_invalid_spec(), std::string()); |
| 214 return (fixed_url.SchemeIs(chrome::kChromeUIScheme) && |
| 215 (fixed_url.host() == chrome::kChromeUIBrowserCrashHost || |
| 216 fixed_url.host() == chrome::kChromeUICrashHost)); |
| 217 } |
| 218 |
| 193 // Reads the |value| as either a single integer value or a list of integers. | 219 // Reads the |value| as either a single integer value or a list of integers. |
| 194 bool ReadOneOrMoreIntegers( | 220 bool ReadOneOrMoreIntegers( |
| 195 Value* value, std::vector<int>* result) { | 221 Value* value, std::vector<int>* result) { |
| 196 if (value->IsType(Value::TYPE_INTEGER)) { | 222 if (value->IsType(Value::TYPE_INTEGER)) { |
| 197 int tab_id = -1; | 223 int tab_id = -1; |
| 198 if (!value->GetAsInteger(&tab_id)) | 224 if (!value->GetAsInteger(&tab_id)) |
| 199 return false; | 225 return false; |
| 200 result->push_back(tab_id); | 226 result->push_back(tab_id); |
| 201 return true; | 227 return true; |
| 202 | 228 |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 for (size_t i = 0; i < url_list->GetSize(); ++i) { | 435 for (size_t i = 0; i < url_list->GetSize(); ++i) { |
| 410 std::string url_string; | 436 std::string url_string; |
| 411 EXTENSION_FUNCTION_VALIDATE(url_list->GetString(i, &url_string)); | 437 EXTENSION_FUNCTION_VALIDATE(url_list->GetString(i, &url_string)); |
| 412 url_strings.push_back(url_string); | 438 url_strings.push_back(url_string); |
| 413 } | 439 } |
| 414 } | 440 } |
| 415 | 441 |
| 416 // Second, resolve, validate and convert them to GURLs. | 442 // Second, resolve, validate and convert them to GURLs. |
| 417 for (std::vector<std::string>::iterator i = url_strings.begin(); | 443 for (std::vector<std::string>::iterator i = url_strings.begin(); |
| 418 i != url_strings.end(); ++i) { | 444 i != url_strings.end(); ++i) { |
| 419 GURL url = ExtensionTabUtil::ResolvePossiblyRelativeURL( | 445 GURL url = ResolvePossiblyRelativeURL(*i, GetExtension()); |
| 420 *i, GetExtension()); | |
| 421 if (!url.is_valid()) { | 446 if (!url.is_valid()) { |
| 422 error_ = ExtensionErrorUtils::FormatErrorMessage( | 447 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 423 keys::kInvalidUrlError, *i); | 448 keys::kInvalidUrlError, *i); |
| 424 return false; | 449 return false; |
| 425 } | 450 } |
| 426 // Don't let the extension crash the browser or renderers. | 451 // Don't let the extension crash the browser or renderers. |
| 427 if (ExtensionTabUtil::IsCrashURL(url)) { | 452 if (IsCrashURL(url)) { |
| 428 error_ = keys::kNoCrashBrowserError; | 453 error_ = keys::kNoCrashBrowserError; |
| 429 return false; | 454 return false; |
| 430 } | 455 } |
| 431 urls.push_back(url); | 456 urls.push_back(url); |
| 432 } | 457 } |
| 433 } | 458 } |
| 434 } | 459 } |
| 435 | 460 |
| 436 // Look for optional tab id. | 461 // Look for optional tab id. |
| 437 if (args) { | 462 if (args) { |
| (...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 978 | 1003 |
| 979 // TODO(rafaelw): handle setting remaining tab properties: | 1004 // TODO(rafaelw): handle setting remaining tab properties: |
| 980 // -title | 1005 // -title |
| 981 // -favIconUrl | 1006 // -favIconUrl |
| 982 | 1007 |
| 983 std::string url_string; | 1008 std::string url_string; |
| 984 GURL url; | 1009 GURL url; |
| 985 if (args->HasKey(keys::kUrlKey)) { | 1010 if (args->HasKey(keys::kUrlKey)) { |
| 986 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, | 1011 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, |
| 987 &url_string)); | 1012 &url_string)); |
| 988 url = ExtensionTabUtil::ResolvePossiblyRelativeURL(url_string, | 1013 url = ResolvePossiblyRelativeURL(url_string, GetExtension()); |
| 989 GetExtension()); | |
| 990 if (!url.is_valid()) { | 1014 if (!url.is_valid()) { |
| 991 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, | 1015 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, |
| 992 url_string); | 1016 url_string); |
| 993 return false; | 1017 return false; |
| 994 } | 1018 } |
| 995 } | 1019 } |
| 996 | 1020 |
| 997 // Don't let extensions crash the browser or renderers. | 1021 // Don't let extensions crash the browser or renderers. |
| 998 if (ExtensionTabUtil::IsCrashURL(url)) { | 1022 if (IsCrashURL(url)) { |
| 999 error_ = keys::kNoCrashBrowserError; | 1023 error_ = keys::kNoCrashBrowserError; |
| 1000 return false; | 1024 return false; |
| 1001 } | 1025 } |
| 1002 | 1026 |
| 1003 // Default to foreground for the new tab. The presence of 'selected' property | 1027 // Default to foreground for the new tab. The presence of 'selected' property |
| 1004 // will override this default. This property is deprecated ('active' should | 1028 // will override this default. This property is deprecated ('active' should |
| 1005 // be used instead). | 1029 // be used instead). |
| 1006 bool active = true; | 1030 bool active = true; |
| 1007 if (args->HasKey(keys::kSelectedKey)) | 1031 if (args->HasKey(keys::kSelectedKey)) |
| 1008 EXTENSION_FUNCTION_VALIDATE( | 1032 EXTENSION_FUNCTION_VALIDATE( |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1185 } | 1209 } |
| 1186 | 1210 |
| 1187 int tab_index = -1; | 1211 int tab_index = -1; |
| 1188 TabStripModel* tab_strip = NULL; | 1212 TabStripModel* tab_strip = NULL; |
| 1189 if (!GetTabById(tab_id, profile(), include_incognito(), | 1213 if (!GetTabById(tab_id, profile(), include_incognito(), |
| 1190 NULL, &tab_strip, &contents, &tab_index, &error_)) { | 1214 NULL, &tab_strip, &contents, &tab_index, &error_)) { |
| 1191 return false; | 1215 return false; |
| 1192 } | 1216 } |
| 1193 | 1217 |
| 1194 web_contents_ = contents->web_contents(); | 1218 web_contents_ = contents->web_contents(); |
| 1219 NavigationController& controller = web_contents_->GetController(); |
| 1195 | 1220 |
| 1196 // TODO(rafaelw): handle setting remaining tab properties: | 1221 // TODO(rafaelw): handle setting remaining tab properties: |
| 1197 // -title | 1222 // -title |
| 1198 // -favIconUrl | 1223 // -favIconUrl |
| 1199 | 1224 |
| 1225 // We wait to fire the callback when executing 'javascript:' URLs in tabs. |
| 1226 bool is_async = false; |
| 1227 |
| 1200 // Navigate the tab to a new location if the url is different. | 1228 // Navigate the tab to a new location if the url is different. |
| 1201 bool is_async = false; | 1229 std::string url_string; |
| 1202 if (!UpdateURLIfPresent(update_props, &is_async)) | 1230 if (update_props->HasKey(keys::kUrlKey)) { |
| 1203 return false; | 1231 EXTENSION_FUNCTION_VALIDATE(update_props->GetString( |
| 1232 keys::kUrlKey, &url_string)); |
| 1233 GURL url = ResolvePossiblyRelativeURL(url_string, GetExtension()); |
| 1234 |
| 1235 if (!url.is_valid()) { |
| 1236 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, |
| 1237 url_string); |
| 1238 return false; |
| 1239 } |
| 1240 |
| 1241 // Don't let the extension crash the browser or renderers. |
| 1242 if (IsCrashURL(url)) { |
| 1243 error_ = keys::kNoCrashBrowserError; |
| 1244 return false; |
| 1245 } |
| 1246 |
| 1247 // JavaScript URLs can do the same kinds of things as cross-origin XHR, so |
| 1248 // we need to check host permissions before allowing them. |
| 1249 if (url.SchemeIs(chrome::kJavaScriptScheme)) { |
| 1250 if (!GetExtension()->CanExecuteScriptOnPage( |
| 1251 web_contents_->GetURL(), NULL, &error_)) { |
| 1252 return false; |
| 1253 } |
| 1254 |
| 1255 ExtensionMsg_ExecuteCode_Params params; |
| 1256 params.request_id = request_id(); |
| 1257 params.extension_id = extension_id(); |
| 1258 params.is_javascript = true; |
| 1259 params.code = url.path(); |
| 1260 params.all_frames = false; |
| 1261 params.in_main_world = true; |
| 1262 |
| 1263 RenderViewHost* render_view_host = web_contents_->GetRenderViewHost(); |
| 1264 render_view_host->Send( |
| 1265 new ExtensionMsg_ExecuteCode(render_view_host->GetRoutingID(), |
| 1266 params)); |
| 1267 |
| 1268 Observe(web_contents_); |
| 1269 AddRef(); // Balanced in OnExecuteCodeFinished(). |
| 1270 |
| 1271 is_async = true; |
| 1272 } |
| 1273 |
| 1274 controller.LoadURL( |
| 1275 url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); |
| 1276 |
| 1277 // The URL of a tab contents never actually changes to a JavaScript URL, so |
| 1278 // this check only makes sense in other cases. |
| 1279 if (!url.SchemeIs(chrome::kJavaScriptScheme)) |
| 1280 DCHECK_EQ(url.spec(), web_contents_->GetURL().spec()); |
| 1281 } |
| 1204 | 1282 |
| 1205 bool active = false; | 1283 bool active = false; |
| 1206 // TODO(rafaelw): Setting |active| from js doesn't make much sense. | 1284 // TODO(rafaelw): Setting |active| from js doesn't make much sense. |
| 1207 // Move tab selection management up to window. | 1285 // Move tab selection management up to window. |
| 1208 if (update_props->HasKey(keys::kSelectedKey)) | 1286 if (update_props->HasKey(keys::kSelectedKey)) |
| 1209 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( | 1287 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( |
| 1210 keys::kSelectedKey, &active)); | 1288 keys::kSelectedKey, &active)); |
| 1211 | 1289 |
| 1212 // The 'active' property has replaced 'selected'. | 1290 // The 'active' property has replaced 'selected'. |
| 1213 if (update_props->HasKey(keys::kActiveKey)) | 1291 if (update_props->HasKey(keys::kActiveKey)) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1255 tab_index, &opener_contents->web_contents()->GetController()); | 1333 tab_index, &opener_contents->web_contents()->GetController()); |
| 1256 } | 1334 } |
| 1257 | 1335 |
| 1258 if (!is_async) { | 1336 if (!is_async) { |
| 1259 PopulateResult(); | 1337 PopulateResult(); |
| 1260 SendResponse(true); | 1338 SendResponse(true); |
| 1261 } | 1339 } |
| 1262 return true; | 1340 return true; |
| 1263 } | 1341 } |
| 1264 | 1342 |
| 1265 bool UpdateTabFunction::UpdateURLIfPresent(DictionaryValue* update_props, | |
| 1266 bool* is_async) { | |
| 1267 if (!update_props->HasKey(keys::kUrlKey)) | |
| 1268 return true; | |
| 1269 | |
| 1270 std::string url_string; | |
| 1271 EXTENSION_FUNCTION_VALIDATE(update_props->GetString( | |
| 1272 keys::kUrlKey, &url_string)); | |
| 1273 GURL url = ExtensionTabUtil::ResolvePossiblyRelativeURL( | |
| 1274 url_string, GetExtension()); | |
| 1275 | |
| 1276 if (!url.is_valid()) { | |
| 1277 error_ = ExtensionErrorUtils::FormatErrorMessage( | |
| 1278 keys::kInvalidUrlError, url_string); | |
| 1279 return false; | |
| 1280 } | |
| 1281 | |
| 1282 // Don't let the extension crash the browser or renderers. | |
| 1283 if (ExtensionTabUtil::IsCrashURL(url)) { | |
| 1284 error_ = keys::kNoCrashBrowserError; | |
| 1285 return false; | |
| 1286 } | |
| 1287 | |
| 1288 // JavaScript URLs can do the same kinds of things as cross-origin XHR, so | |
| 1289 // we need to check host permissions before allowing them. | |
| 1290 if (url.SchemeIs(chrome::kJavaScriptScheme)) { | |
| 1291 if (!GetExtension()->CanExecuteScriptOnPage( | |
| 1292 web_contents_->GetURL(), NULL, &error_)) { | |
| 1293 return false; | |
| 1294 } | |
| 1295 | |
| 1296 ExtensionMsg_ExecuteCode_Params params; | |
| 1297 params.request_id = request_id(); | |
| 1298 params.extension_id = extension_id(); | |
| 1299 params.is_javascript = true; | |
| 1300 params.code = url.path(); | |
| 1301 params.all_frames = false; | |
| 1302 params.in_main_world = true; | |
| 1303 | |
| 1304 RenderViewHost* render_view_host = web_contents_->GetRenderViewHost(); | |
| 1305 render_view_host->Send( | |
| 1306 new ExtensionMsg_ExecuteCode(render_view_host->GetRoutingID(), params)); | |
| 1307 | |
| 1308 Observe(web_contents_); | |
| 1309 AddRef(); // Balanced in OnExecuteCodeFinished(). | |
| 1310 | |
| 1311 *is_async = true; | |
| 1312 return true; | |
| 1313 } | |
| 1314 | |
| 1315 web_contents_->GetController().LoadURL( | |
| 1316 url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); | |
| 1317 | |
| 1318 // The URL of a tab contents never actually changes to a JavaScript URL, so | |
| 1319 // this check only makes sense in other cases. | |
| 1320 if (!url.SchemeIs(chrome::kJavaScriptScheme)) | |
| 1321 DCHECK_EQ(url.spec(), web_contents_->GetURL().spec()); | |
| 1322 | |
| 1323 return true; | |
| 1324 } | |
| 1325 | |
| 1326 void UpdateTabFunction::PopulateResult() { | 1343 void UpdateTabFunction::PopulateResult() { |
| 1327 if (!has_callback()) | 1344 if (!has_callback()) |
| 1328 return; | 1345 return; |
| 1329 | 1346 |
| 1330 if (GetExtension()->HasAPIPermission(ExtensionAPIPermission::kTab) && | 1347 if (GetExtension()->HasAPIPermission(ExtensionAPIPermission::kTab) && |
| 1331 web_contents_ != NULL) { | 1348 web_contents_ != NULL) { |
| 1332 result_.reset(ExtensionTabUtil::CreateTabValue(web_contents_)); | 1349 result_.reset(ExtensionTabUtil::CreateTabValue(web_contents_)); |
| 1333 } else { | 1350 } else { |
| 1334 result_.reset(Value::CreateNullValue()); | 1351 result_.reset(Value::CreateNullValue()); |
| 1335 } | 1352 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1367 if (!error.empty()) { | 1384 if (!error.empty()) { |
| 1368 CHECK(!success); | 1385 CHECK(!success); |
| 1369 error_ = error; | 1386 error_ = error; |
| 1370 } | 1387 } |
| 1371 | 1388 |
| 1372 if (success) | 1389 if (success) |
| 1373 PopulateResult(); | 1390 PopulateResult(); |
| 1374 SendResponse(success); | 1391 SendResponse(success); |
| 1375 | 1392 |
| 1376 Observe(NULL); | 1393 Observe(NULL); |
| 1377 Release(); // Balanced in UpdateURLIfPresent(). | 1394 Release(); // Balanced in RunImpl(). |
| 1378 } | 1395 } |
| 1379 | 1396 |
| 1380 bool MoveTabsFunction::RunImpl() { | 1397 bool MoveTabsFunction::RunImpl() { |
| 1381 Value* tab_value = NULL; | 1398 Value* tab_value = NULL; |
| 1382 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); | 1399 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); |
| 1383 | 1400 |
| 1384 std::vector<int> tab_ids; | 1401 std::vector<int> tab_ids; |
| 1385 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_ids)); | 1402 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_ids)); |
| 1386 | 1403 |
| 1387 DictionaryValue* update_props = NULL; | 1404 DictionaryValue* update_props = NULL; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1570 // is being dragged, or we're in some other nested event loop. This code | 1587 // is being dragged, or we're in some other nested event loop. This code |
| 1571 // path should ensure that the tab is safely closed under such | 1588 // path should ensure that the tab is safely closed under such |
| 1572 // circumstances, whereas |Browser::CloseTabContents()| does not. | 1589 // circumstances, whereas |Browser::CloseTabContents()| does not. |
| 1573 RenderViewHost* render_view_host = | 1590 RenderViewHost* render_view_host = |
| 1574 contents->web_contents()->GetRenderViewHost(); | 1591 contents->web_contents()->GetRenderViewHost(); |
| 1575 render_view_host->GetDelegate()->Close(render_view_host); | 1592 render_view_host->GetDelegate()->Close(render_view_host); |
| 1576 } | 1593 } |
| 1577 return true; | 1594 return true; |
| 1578 } | 1595 } |
| 1579 | 1596 |
| 1580 bool CaptureVisibleTabFunction::GetTabToCapture( | 1597 bool CaptureVisibleTabFunction::RunImpl() { |
| 1581 WebContents** web_contents, TabContentsWrapper** wrapper) { | |
| 1582 Browser* browser = NULL; | 1598 Browser* browser = NULL; |
| 1583 // windowId defaults to "current" window. | 1599 // windowId defaults to "current" window. |
| 1584 int window_id = extension_misc::kCurrentWindowId; | 1600 int window_id = extension_misc::kCurrentWindowId; |
| 1585 | 1601 |
| 1586 if (HasOptionalArgument(0)) | 1602 if (HasOptionalArgument(0)) |
| 1587 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 1603 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
| 1588 | 1604 |
| 1589 if (!GetBrowserFromWindowID(this, window_id, &browser)) | 1605 if (!GetBrowserFromWindowID(this, window_id, &browser)) |
| 1590 return false; | 1606 return false; |
| 1591 | 1607 |
| 1592 *web_contents = browser->GetSelectedWebContents(); | |
| 1593 if (*web_contents == NULL) { | |
| 1594 error_ = keys::kInternalVisibleTabCaptureError; | |
| 1595 return false; | |
| 1596 } | |
| 1597 | |
| 1598 *wrapper = browser->GetSelectedTabContentsWrapper(); | |
| 1599 | |
| 1600 return true; | |
| 1601 }; | |
| 1602 | |
| 1603 bool CaptureVisibleTabFunction::RunImpl() { | |
| 1604 WebContents* web_contents = NULL; | |
| 1605 TabContentsWrapper* wrapper = NULL; | |
| 1606 if (!GetTabToCapture(&web_contents, &wrapper)) | |
| 1607 return false; | |
| 1608 | |
| 1609 image_format_ = FORMAT_JPEG; // Default format is JPEG. | 1608 image_format_ = FORMAT_JPEG; // Default format is JPEG. |
| 1610 image_quality_ = kDefaultQuality; // Default quality setting. | 1609 image_quality_ = kDefaultQuality; // Default quality setting. |
| 1611 | 1610 |
| 1612 if (HasOptionalArgument(1)) { | 1611 if (HasOptionalArgument(1)) { |
| 1613 DictionaryValue* options = NULL; | 1612 DictionaryValue* options = NULL; |
| 1614 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); | 1613 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); |
| 1615 | 1614 |
| 1616 if (options->HasKey(keys::kFormatKey)) { | 1615 if (options->HasKey(keys::kFormatKey)) { |
| 1617 std::string format; | 1616 std::string format; |
| 1618 EXTENSION_FUNCTION_VALIDATE( | 1617 EXTENSION_FUNCTION_VALIDATE( |
| 1619 options->GetString(keys::kFormatKey, &format)); | 1618 options->GetString(keys::kFormatKey, &format)); |
| 1620 | 1619 |
| 1621 if (format == keys::kFormatValueJpeg) { | 1620 if (format == keys::kFormatValueJpeg) { |
| 1622 image_format_ = FORMAT_JPEG; | 1621 image_format_ = FORMAT_JPEG; |
| 1623 } else if (format == keys::kFormatValuePng) { | 1622 } else if (format == keys::kFormatValuePng) { |
| 1624 image_format_ = FORMAT_PNG; | 1623 image_format_ = FORMAT_PNG; |
| 1625 } else { | 1624 } else { |
| 1626 // Schema validation should make this unreachable. | 1625 // Schema validation should make this unreachable. |
| 1627 EXTENSION_FUNCTION_VALIDATE(0); | 1626 EXTENSION_FUNCTION_VALIDATE(0); |
| 1628 } | 1627 } |
| 1629 } | 1628 } |
| 1630 | 1629 |
| 1631 if (options->HasKey(keys::kQualityKey)) { | 1630 if (options->HasKey(keys::kQualityKey)) { |
| 1632 EXTENSION_FUNCTION_VALIDATE( | 1631 EXTENSION_FUNCTION_VALIDATE( |
| 1633 options->GetInteger(keys::kQualityKey, &image_quality_)); | 1632 options->GetInteger(keys::kQualityKey, &image_quality_)); |
| 1634 } | 1633 } |
| 1635 } | 1634 } |
| 1636 | 1635 |
| 1636 WebContents* web_contents = browser->GetSelectedWebContents(); |
| 1637 if (!web_contents) { |
| 1638 error_ = keys::kInternalVisibleTabCaptureError; |
| 1639 return false; |
| 1640 } |
| 1641 |
| 1637 // captureVisibleTab() can return an image containing sensitive information | 1642 // captureVisibleTab() can return an image containing sensitive information |
| 1638 // that the browser would otherwise protect. Ensure the extension has | 1643 // that the browser would otherwise protect. Ensure the extension has |
| 1639 // permission to do this. | 1644 // permission to do this. |
| 1640 if (!GetExtension()->CanCaptureVisiblePage(web_contents->GetURL(), &error_)) | 1645 if (!GetExtension()->CanCaptureVisiblePage(web_contents->GetURL(), &error_)) |
| 1641 return false; | 1646 return false; |
| 1642 | 1647 |
| 1643 RenderViewHost* render_view_host = web_contents->GetRenderViewHost(); | 1648 RenderViewHost* render_view_host = web_contents->GetRenderViewHost(); |
| 1644 | 1649 |
| 1645 // If a backing store is cached for the tab we want to capture, | 1650 // If a backing store is cached for the tab we want to capture, |
| 1646 // and it can be copied into a bitmap, then use it to generate the image. | 1651 // and it can be copied into a bitmap, then use it to generate the image. |
| 1652 // This may fail if we can not copy a backing store into a bitmap. |
| 1647 // For example, some uncommon X11 visual modes are not supported by | 1653 // For example, some uncommon X11 visual modes are not supported by |
| 1648 // CopyFromBackingStore(). | 1654 // CopyFromBackingStore(). |
| 1649 skia::PlatformCanvas temp_canvas; | 1655 skia::PlatformCanvas temp_canvas; |
| 1650 if (render_view_host->CopyFromBackingStore( | 1656 if (render_view_host->CopyFromBackingStore( |
| 1651 gfx::Rect(), gfx::Size(), &temp_canvas)) { | 1657 gfx::Rect(), gfx::Size(), &temp_canvas)) { |
| 1652 VLOG(1) << "captureVisibleTab() got image from backing store."; | 1658 VLOG(1) << "captureVisibleTab() got image from backing store."; |
| 1653 SendResultFromBitmap(skia::GetTopDevice(temp_canvas)->accessBitmap(false)); | 1659 SendResultFromBitmap(skia::GetTopDevice(temp_canvas)->accessBitmap(false)); |
| 1654 return true; | 1660 return true; |
| 1655 } | 1661 } |
| 1656 | 1662 |
| 1657 // Ask the renderer for a snapshot of the tab. | 1663 // Ask the renderer for a snapshot of the tab. |
| 1658 wrapper->snapshot_tab_helper()->CaptureSnapshot(); | 1664 TabContentsWrapper* wrapper = browser->GetSelectedTabContentsWrapper(); |
| 1659 registrar_.Add(this, | 1665 registrar_.Add(this, |
| 1660 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, | 1666 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, |
| 1661 content::Source<WebContents>(wrapper->web_contents())); | 1667 content::Source<WebContents>(wrapper->web_contents())); |
| 1662 AddRef(); // Balanced in CaptureVisibleTabFunction::Observe(). | 1668 AddRef(); // Balanced in CaptureVisibleTabFunction::Observe(). |
| 1663 wrapper->snapshot_tab_helper()->CaptureSnapshot(); | 1669 wrapper->snapshot_tab_helper()->CaptureSnapshot(); |
| 1664 | 1670 |
| 1665 return true; | 1671 return true; |
| 1666 } | 1672 } |
| 1667 | 1673 |
| 1668 // If a backing store was not available in CaptureVisibleTabFunction::RunImpl, | 1674 // If a backing store was not available in CaptureVisibleTabFunction::RunImpl, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1807 // called for every API call the extension made. | 1813 // called for every API call the extension made. |
| 1808 GotLanguage(language); | 1814 GotLanguage(language); |
| 1809 } | 1815 } |
| 1810 | 1816 |
| 1811 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { | 1817 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { |
| 1812 result_.reset(Value::CreateStringValue(language.c_str())); | 1818 result_.reset(Value::CreateStringValue(language.c_str())); |
| 1813 SendResponse(true); | 1819 SendResponse(true); |
| 1814 | 1820 |
| 1815 Release(); // Balanced in Run() | 1821 Release(); // Balanced in Run() |
| 1816 } | 1822 } |
| OLD | NEW |