| 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" | |
| 30 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 29 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
| 31 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
| 32 #include "chrome/browser/sessions/restore_tab_helper.h" | 31 #include "chrome/browser/sessions/restore_tab_helper.h" |
| 33 #include "chrome/browser/tabs/tab_strip_model.h" | 32 #include "chrome/browser/tabs/tab_strip_model.h" |
| 34 #include "chrome/browser/translate/translate_tab_helper.h" | 33 #include "chrome/browser/translate/translate_tab_helper.h" |
| 35 #include "chrome/browser/ui/browser.h" | 34 #include "chrome/browser/ui/browser.h" |
| 36 #include "chrome/browser/ui/browser_list.h" | 35 #include "chrome/browser/ui/browser_list.h" |
| 37 #include "chrome/browser/ui/browser_navigator.h" | 36 #include "chrome/browser/ui/browser_navigator.h" |
| 38 #include "chrome/browser/ui/browser_window.h" | 37 #include "chrome/browser/ui/browser_window.h" |
| 39 #include "chrome/browser/ui/panels/panel_manager.h" | 38 #include "chrome/browser/ui/panels/panel_manager.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 browser, tab_strip, contents, tab_index)) | 183 browser, tab_strip, contents, tab_index)) |
| 185 return true; | 184 return true; |
| 186 | 185 |
| 187 if (error_message) | 186 if (error_message) |
| 188 *error_message = ExtensionErrorUtils::FormatErrorMessage( | 187 *error_message = ExtensionErrorUtils::FormatErrorMessage( |
| 189 keys::kTabNotFoundError, base::IntToString(tab_id)); | 188 keys::kTabNotFoundError, base::IntToString(tab_id)); |
| 190 | 189 |
| 191 return false; | 190 return false; |
| 192 } | 191 } |
| 193 | 192 |
| 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 | |
| 219 // Reads the |value| as either a single integer value or a list of integers. | 193 // Reads the |value| as either a single integer value or a list of integers. |
| 220 bool ReadOneOrMoreIntegers( | 194 bool ReadOneOrMoreIntegers( |
| 221 Value* value, std::vector<int>* result) { | 195 Value* value, std::vector<int>* result) { |
| 222 if (value->IsType(Value::TYPE_INTEGER)) { | 196 if (value->IsType(Value::TYPE_INTEGER)) { |
| 223 int tab_id = -1; | 197 int tab_id = -1; |
| 224 if (!value->GetAsInteger(&tab_id)) | 198 if (!value->GetAsInteger(&tab_id)) |
| 225 return false; | 199 return false; |
| 226 result->push_back(tab_id); | 200 result->push_back(tab_id); |
| 227 return true; | 201 return true; |
| 228 | 202 |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 for (size_t i = 0; i < url_list->GetSize(); ++i) { | 409 for (size_t i = 0; i < url_list->GetSize(); ++i) { |
| 436 std::string url_string; | 410 std::string url_string; |
| 437 EXTENSION_FUNCTION_VALIDATE(url_list->GetString(i, &url_string)); | 411 EXTENSION_FUNCTION_VALIDATE(url_list->GetString(i, &url_string)); |
| 438 url_strings.push_back(url_string); | 412 url_strings.push_back(url_string); |
| 439 } | 413 } |
| 440 } | 414 } |
| 441 | 415 |
| 442 // Second, resolve, validate and convert them to GURLs. | 416 // Second, resolve, validate and convert them to GURLs. |
| 443 for (std::vector<std::string>::iterator i = url_strings.begin(); | 417 for (std::vector<std::string>::iterator i = url_strings.begin(); |
| 444 i != url_strings.end(); ++i) { | 418 i != url_strings.end(); ++i) { |
| 445 GURL url = ResolvePossiblyRelativeURL(*i, GetExtension()); | 419 GURL url = ExtensionTabUtil::ResolvePossiblyRelativeURL( |
| 420 *i, GetExtension()); |
| 446 if (!url.is_valid()) { | 421 if (!url.is_valid()) { |
| 447 error_ = ExtensionErrorUtils::FormatErrorMessage( | 422 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 448 keys::kInvalidUrlError, *i); | 423 keys::kInvalidUrlError, *i); |
| 449 return false; | 424 return false; |
| 450 } | 425 } |
| 451 // Don't let the extension crash the browser or renderers. | 426 // Don't let the extension crash the browser or renderers. |
| 452 if (IsCrashURL(url)) { | 427 if (ExtensionTabUtil::IsCrashURL(url)) { |
| 453 error_ = keys::kNoCrashBrowserError; | 428 error_ = keys::kNoCrashBrowserError; |
| 454 return false; | 429 return false; |
| 455 } | 430 } |
| 456 urls.push_back(url); | 431 urls.push_back(url); |
| 457 } | 432 } |
| 458 } | 433 } |
| 459 } | 434 } |
| 460 | 435 |
| 461 // Look for optional tab id. | 436 // Look for optional tab id. |
| 462 if (args) { | 437 if (args) { |
| (...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1003 | 978 |
| 1004 // TODO(rafaelw): handle setting remaining tab properties: | 979 // TODO(rafaelw): handle setting remaining tab properties: |
| 1005 // -title | 980 // -title |
| 1006 // -favIconUrl | 981 // -favIconUrl |
| 1007 | 982 |
| 1008 std::string url_string; | 983 std::string url_string; |
| 1009 GURL url; | 984 GURL url; |
| 1010 if (args->HasKey(keys::kUrlKey)) { | 985 if (args->HasKey(keys::kUrlKey)) { |
| 1011 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, | 986 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, |
| 1012 &url_string)); | 987 &url_string)); |
| 1013 url = ResolvePossiblyRelativeURL(url_string, GetExtension()); | 988 url = ExtensionTabUtil::ResolvePossiblyRelativeURL(url_string, |
| 989 GetExtension()); |
| 1014 if (!url.is_valid()) { | 990 if (!url.is_valid()) { |
| 1015 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, | 991 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, |
| 1016 url_string); | 992 url_string); |
| 1017 return false; | 993 return false; |
| 1018 } | 994 } |
| 1019 } | 995 } |
| 1020 | 996 |
| 1021 // Don't let extensions crash the browser or renderers. | 997 // Don't let extensions crash the browser or renderers. |
| 1022 if (IsCrashURL(url)) { | 998 if (ExtensionTabUtil::IsCrashURL(url)) { |
| 1023 error_ = keys::kNoCrashBrowserError; | 999 error_ = keys::kNoCrashBrowserError; |
| 1024 return false; | 1000 return false; |
| 1025 } | 1001 } |
| 1026 | 1002 |
| 1027 // Default to foreground for the new tab. The presence of 'selected' property | 1003 // Default to foreground for the new tab. The presence of 'selected' property |
| 1028 // will override this default. This property is deprecated ('active' should | 1004 // will override this default. This property is deprecated ('active' should |
| 1029 // be used instead). | 1005 // be used instead). |
| 1030 bool active = true; | 1006 bool active = true; |
| 1031 if (args->HasKey(keys::kSelectedKey)) | 1007 if (args->HasKey(keys::kSelectedKey)) |
| 1032 EXTENSION_FUNCTION_VALIDATE( | 1008 EXTENSION_FUNCTION_VALIDATE( |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1209 } | 1185 } |
| 1210 | 1186 |
| 1211 int tab_index = -1; | 1187 int tab_index = -1; |
| 1212 TabStripModel* tab_strip = NULL; | 1188 TabStripModel* tab_strip = NULL; |
| 1213 if (!GetTabById(tab_id, profile(), include_incognito(), | 1189 if (!GetTabById(tab_id, profile(), include_incognito(), |
| 1214 NULL, &tab_strip, &contents, &tab_index, &error_)) { | 1190 NULL, &tab_strip, &contents, &tab_index, &error_)) { |
| 1215 return false; | 1191 return false; |
| 1216 } | 1192 } |
| 1217 | 1193 |
| 1218 web_contents_ = contents->web_contents(); | 1194 web_contents_ = contents->web_contents(); |
| 1219 NavigationController& controller = web_contents_->GetController(); | |
| 1220 | 1195 |
| 1221 // TODO(rafaelw): handle setting remaining tab properties: | 1196 // TODO(rafaelw): handle setting remaining tab properties: |
| 1222 // -title | 1197 // -title |
| 1223 // -favIconUrl | 1198 // -favIconUrl |
| 1224 | 1199 |
| 1225 // We wait to fire the callback when executing 'javascript:' URLs in tabs. | 1200 // Navigate the tab to a new location if the url is different. |
| 1226 bool is_async = false; | 1201 bool is_async = false; |
| 1227 | 1202 if (!UpdateURLIfPresent(update_props, &is_async)) |
| 1228 // Navigate the tab to a new location if the url is different. | 1203 return false; |
| 1229 std::string url_string; | |
| 1230 if (update_props->HasKey(keys::kUrlKey)) { | |
| 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 } | |
| 1282 | 1204 |
| 1283 bool active = false; | 1205 bool active = false; |
| 1284 // TODO(rafaelw): Setting |active| from js doesn't make much sense. | 1206 // TODO(rafaelw): Setting |active| from js doesn't make much sense. |
| 1285 // Move tab selection management up to window. | 1207 // Move tab selection management up to window. |
| 1286 if (update_props->HasKey(keys::kSelectedKey)) | 1208 if (update_props->HasKey(keys::kSelectedKey)) |
| 1287 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( | 1209 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( |
| 1288 keys::kSelectedKey, &active)); | 1210 keys::kSelectedKey, &active)); |
| 1289 | 1211 |
| 1290 // The 'active' property has replaced 'selected'. | 1212 // The 'active' property has replaced 'selected'. |
| 1291 if (update_props->HasKey(keys::kActiveKey)) | 1213 if (update_props->HasKey(keys::kActiveKey)) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1333 tab_index, &opener_contents->web_contents()->GetController()); | 1255 tab_index, &opener_contents->web_contents()->GetController()); |
| 1334 } | 1256 } |
| 1335 | 1257 |
| 1336 if (!is_async) { | 1258 if (!is_async) { |
| 1337 PopulateResult(); | 1259 PopulateResult(); |
| 1338 SendResponse(true); | 1260 SendResponse(true); |
| 1339 } | 1261 } |
| 1340 return true; | 1262 return true; |
| 1341 } | 1263 } |
| 1342 | 1264 |
| 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 |
| 1343 void UpdateTabFunction::PopulateResult() { | 1326 void UpdateTabFunction::PopulateResult() { |
| 1344 if (!has_callback()) | 1327 if (!has_callback()) |
| 1345 return; | 1328 return; |
| 1346 | 1329 |
| 1347 if (GetExtension()->HasAPIPermission(ExtensionAPIPermission::kTab) && | 1330 if (GetExtension()->HasAPIPermission(ExtensionAPIPermission::kTab) && |
| 1348 web_contents_ != NULL) { | 1331 web_contents_ != NULL) { |
| 1349 result_.reset(ExtensionTabUtil::CreateTabValue(web_contents_)); | 1332 result_.reset(ExtensionTabUtil::CreateTabValue(web_contents_)); |
| 1350 } else { | 1333 } else { |
| 1351 result_.reset(Value::CreateNullValue()); | 1334 result_.reset(Value::CreateNullValue()); |
| 1352 } | 1335 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1384 if (!error.empty()) { | 1367 if (!error.empty()) { |
| 1385 CHECK(!success); | 1368 CHECK(!success); |
| 1386 error_ = error; | 1369 error_ = error; |
| 1387 } | 1370 } |
| 1388 | 1371 |
| 1389 if (success) | 1372 if (success) |
| 1390 PopulateResult(); | 1373 PopulateResult(); |
| 1391 SendResponse(success); | 1374 SendResponse(success); |
| 1392 | 1375 |
| 1393 Observe(NULL); | 1376 Observe(NULL); |
| 1394 Release(); // Balanced in RunImpl(). | 1377 Release(); // Balanced in UpdateURLIfPresent(). |
| 1395 } | 1378 } |
| 1396 | 1379 |
| 1397 bool MoveTabsFunction::RunImpl() { | 1380 bool MoveTabsFunction::RunImpl() { |
| 1398 Value* tab_value = NULL; | 1381 Value* tab_value = NULL; |
| 1399 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); | 1382 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); |
| 1400 | 1383 |
| 1401 std::vector<int> tab_ids; | 1384 std::vector<int> tab_ids; |
| 1402 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_ids)); | 1385 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_ids)); |
| 1403 | 1386 |
| 1404 DictionaryValue* update_props = NULL; | 1387 DictionaryValue* update_props = NULL; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1587 // is being dragged, or we're in some other nested event loop. This code | 1570 // is being dragged, or we're in some other nested event loop. This code |
| 1588 // path should ensure that the tab is safely closed under such | 1571 // path should ensure that the tab is safely closed under such |
| 1589 // circumstances, whereas |Browser::CloseTabContents()| does not. | 1572 // circumstances, whereas |Browser::CloseTabContents()| does not. |
| 1590 RenderViewHost* render_view_host = | 1573 RenderViewHost* render_view_host = |
| 1591 contents->web_contents()->GetRenderViewHost(); | 1574 contents->web_contents()->GetRenderViewHost(); |
| 1592 render_view_host->GetDelegate()->Close(render_view_host); | 1575 render_view_host->GetDelegate()->Close(render_view_host); |
| 1593 } | 1576 } |
| 1594 return true; | 1577 return true; |
| 1595 } | 1578 } |
| 1596 | 1579 |
| 1597 bool CaptureVisibleTabFunction::RunImpl() { | 1580 bool CaptureVisibleTabFunction::GetTabToCapture( |
| 1581 WebContents** web_contents, TabContentsWrapper** wrapper) { |
| 1598 Browser* browser = NULL; | 1582 Browser* browser = NULL; |
| 1599 // windowId defaults to "current" window. | 1583 // windowId defaults to "current" window. |
| 1600 int window_id = extension_misc::kCurrentWindowId; | 1584 int window_id = extension_misc::kCurrentWindowId; |
| 1601 | 1585 |
| 1602 if (HasOptionalArgument(0)) | 1586 if (HasOptionalArgument(0)) |
| 1603 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 1587 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
| 1604 | 1588 |
| 1605 if (!GetBrowserFromWindowID(this, window_id, &browser)) | 1589 if (!GetBrowserFromWindowID(this, window_id, &browser)) |
| 1606 return false; | 1590 return false; |
| 1607 | 1591 |
| 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 |
| 1608 image_format_ = FORMAT_JPEG; // Default format is JPEG. | 1609 image_format_ = FORMAT_JPEG; // Default format is JPEG. |
| 1609 image_quality_ = kDefaultQuality; // Default quality setting. | 1610 image_quality_ = kDefaultQuality; // Default quality setting. |
| 1610 | 1611 |
| 1611 if (HasOptionalArgument(1)) { | 1612 if (HasOptionalArgument(1)) { |
| 1612 DictionaryValue* options = NULL; | 1613 DictionaryValue* options = NULL; |
| 1613 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); | 1614 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options)); |
| 1614 | 1615 |
| 1615 if (options->HasKey(keys::kFormatKey)) { | 1616 if (options->HasKey(keys::kFormatKey)) { |
| 1616 std::string format; | 1617 std::string format; |
| 1617 EXTENSION_FUNCTION_VALIDATE( | 1618 EXTENSION_FUNCTION_VALIDATE( |
| 1618 options->GetString(keys::kFormatKey, &format)); | 1619 options->GetString(keys::kFormatKey, &format)); |
| 1619 | 1620 |
| 1620 if (format == keys::kFormatValueJpeg) { | 1621 if (format == keys::kFormatValueJpeg) { |
| 1621 image_format_ = FORMAT_JPEG; | 1622 image_format_ = FORMAT_JPEG; |
| 1622 } else if (format == keys::kFormatValuePng) { | 1623 } else if (format == keys::kFormatValuePng) { |
| 1623 image_format_ = FORMAT_PNG; | 1624 image_format_ = FORMAT_PNG; |
| 1624 } else { | 1625 } else { |
| 1625 // Schema validation should make this unreachable. | 1626 // Schema validation should make this unreachable. |
| 1626 EXTENSION_FUNCTION_VALIDATE(0); | 1627 EXTENSION_FUNCTION_VALIDATE(0); |
| 1627 } | 1628 } |
| 1628 } | 1629 } |
| 1629 | 1630 |
| 1630 if (options->HasKey(keys::kQualityKey)) { | 1631 if (options->HasKey(keys::kQualityKey)) { |
| 1631 EXTENSION_FUNCTION_VALIDATE( | 1632 EXTENSION_FUNCTION_VALIDATE( |
| 1632 options->GetInteger(keys::kQualityKey, &image_quality_)); | 1633 options->GetInteger(keys::kQualityKey, &image_quality_)); |
| 1633 } | 1634 } |
| 1634 } | 1635 } |
| 1635 | 1636 |
| 1636 WebContents* web_contents = browser->GetSelectedWebContents(); | |
| 1637 if (!web_contents) { | |
| 1638 error_ = keys::kInternalVisibleTabCaptureError; | |
| 1639 return false; | |
| 1640 } | |
| 1641 | |
| 1642 // captureVisibleTab() can return an image containing sensitive information | 1637 // captureVisibleTab() can return an image containing sensitive information |
| 1643 // that the browser would otherwise protect. Ensure the extension has | 1638 // that the browser would otherwise protect. Ensure the extension has |
| 1644 // permission to do this. | 1639 // permission to do this. |
| 1645 if (!GetExtension()->CanCaptureVisiblePage(web_contents->GetURL(), &error_)) | 1640 if (!GetExtension()->CanCaptureVisiblePage(web_contents->GetURL(), &error_)) |
| 1646 return false; | 1641 return false; |
| 1647 | 1642 |
| 1648 RenderViewHost* render_view_host = web_contents->GetRenderViewHost(); | 1643 RenderViewHost* render_view_host = web_contents->GetRenderViewHost(); |
| 1649 | 1644 |
| 1650 // If a backing store is cached for the tab we want to capture, | 1645 // If a backing store is cached for the tab we want to capture, |
| 1651 // and it can be copied into a bitmap, then use it to generate the image. | 1646 // 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. | |
| 1653 // For example, some uncommon X11 visual modes are not supported by | 1647 // For example, some uncommon X11 visual modes are not supported by |
| 1654 // CopyFromBackingStore(). | 1648 // CopyFromBackingStore(). |
| 1655 skia::PlatformCanvas temp_canvas; | 1649 skia::PlatformCanvas temp_canvas; |
| 1656 if (render_view_host->CopyFromBackingStore( | 1650 if (render_view_host->CopyFromBackingStore( |
| 1657 gfx::Rect(), gfx::Size(), &temp_canvas)) { | 1651 gfx::Rect(), gfx::Size(), &temp_canvas)) { |
| 1658 VLOG(1) << "captureVisibleTab() got image from backing store."; | 1652 VLOG(1) << "captureVisibleTab() got image from backing store."; |
| 1659 SendResultFromBitmap(skia::GetTopDevice(temp_canvas)->accessBitmap(false)); | 1653 SendResultFromBitmap(skia::GetTopDevice(temp_canvas)->accessBitmap(false)); |
| 1660 return true; | 1654 return true; |
| 1661 } | 1655 } |
| 1662 | 1656 |
| 1663 // Ask the renderer for a snapshot of the tab. | 1657 // Ask the renderer for a snapshot of the tab. |
| 1664 TabContentsWrapper* wrapper = browser->GetSelectedTabContentsWrapper(); | 1658 wrapper->snapshot_tab_helper()->CaptureSnapshot(); |
| 1665 registrar_.Add(this, | 1659 registrar_.Add(this, |
| 1666 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, | 1660 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, |
| 1667 content::Source<WebContents>(wrapper->web_contents())); | 1661 content::Source<WebContents>(wrapper->web_contents())); |
| 1668 AddRef(); // Balanced in CaptureVisibleTabFunction::Observe(). | 1662 AddRef(); // Balanced in CaptureVisibleTabFunction::Observe(). |
| 1669 wrapper->snapshot_tab_helper()->CaptureSnapshot(); | 1663 wrapper->snapshot_tab_helper()->CaptureSnapshot(); |
| 1670 | 1664 |
| 1671 return true; | 1665 return true; |
| 1672 } | 1666 } |
| 1673 | 1667 |
| 1674 // If a backing store was not available in CaptureVisibleTabFunction::RunImpl, | 1668 // If a backing store was not available in CaptureVisibleTabFunction::RunImpl, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1813 // called for every API call the extension made. | 1807 // called for every API call the extension made. |
| 1814 GotLanguage(language); | 1808 GotLanguage(language); |
| 1815 } | 1809 } |
| 1816 | 1810 |
| 1817 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { | 1811 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { |
| 1818 result_.reset(Value::CreateStringValue(language.c_str())); | 1812 result_.reset(Value::CreateStringValue(language.c_str())); |
| 1819 SendResponse(true); | 1813 SendResponse(true); |
| 1820 | 1814 |
| 1821 Release(); // Balanced in Run() | 1815 Release(); // Balanced in Run() |
| 1822 } | 1816 } |
| OLD | NEW |