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/api/tabs/tabs.h" | 5 #include "chrome/browser/extensions/api/tabs/tabs.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 801 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
812 Browser* browser = NULL; | 812 Browser* browser = NULL; |
813 if (!GetBrowserFromWindowID(this, window_id, &browser)) | 813 if (!GetBrowserFromWindowID(this, window_id, &browser)) |
814 return false; | 814 return false; |
815 | 815 |
816 TabStripModel* tab_strip = browser->tab_strip_model(); | 816 TabStripModel* tab_strip = browser->tab_strip_model(); |
817 TabContents* contents = tab_strip->GetActiveTabContents(); | 817 TabContents* contents = tab_strip->GetActiveTabContents(); |
818 if (!contents) { | 818 if (!contents) { |
819 error_ = keys::kNoSelectedTabError; | 819 error_ = keys::kNoSelectedTabError; |
820 return false; | 820 return false; |
821 } | 821 } |
822 SetResult(ExtensionTabUtil::CreateTabValue(contents->web_contents(), | 822 DictionaryValue* result = ExtensionTabUtil::CreateTabValue( |
Aaron Boodman
2012/08/10 06:44:51
Can we pass Extension* into CreateTabValue and let
chebert
2012/08/13 23:52:17
An overloaded version is called from the MenuManag
| |
823 tab_strip, | 823 contents->web_contents(), |
824 tab_strip->active_index())); | 824 tab_strip, |
825 tab_strip->active_index()); | |
826 if (!GetExtension()->HasAPIPermission(extensions::APIPermission::kTab)) | |
827 ExtensionTabUtil::StripTabOfSensitiveData(result); | |
828 SetResult(result); | |
825 return true; | 829 return true; |
826 } | 830 } |
827 | 831 |
828 bool GetAllTabsInWindowFunction::RunImpl() { | 832 bool GetAllTabsInWindowFunction::RunImpl() { |
829 // windowId defaults to "current" window. | 833 // windowId defaults to "current" window. |
830 int window_id = extension_misc::kCurrentWindowId; | 834 int window_id = extension_misc::kCurrentWindowId; |
831 if (HasOptionalArgument(0)) | 835 if (HasOptionalArgument(0)) |
832 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 836 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
833 | 837 |
834 Browser* browser = NULL; | 838 Browser* browser = NULL; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
938 if (!title.empty() && !MatchPattern(web_contents->GetTitle(), | 942 if (!title.empty() && !MatchPattern(web_contents->GetTitle(), |
939 UTF8ToUTF16(title))) | 943 UTF8ToUTF16(title))) |
940 continue; | 944 continue; |
941 | 945 |
942 if (!url_pattern.MatchesURL(web_contents->GetURL())) | 946 if (!url_pattern.MatchesURL(web_contents->GetURL())) |
943 continue; | 947 continue; |
944 | 948 |
945 if (!MatchesQueryArg(loading, web_contents->IsLoading())) | 949 if (!MatchesQueryArg(loading, web_contents->IsLoading())) |
946 continue; | 950 continue; |
947 | 951 |
948 result->Append(ExtensionTabUtil::CreateTabValue( | 952 DictionaryValue* tab = ExtensionTabUtil::CreateTabValue( |
949 web_contents, tab_strip, i)); | 953 web_contents, tab_strip, i); |
954 if (!GetExtension()->HasAPIPermission(extensions::APIPermission::kTab)) | |
955 ExtensionTabUtil::StripTabOfSensitiveData(tab); | |
956 result->Append(tab); | |
950 } | 957 } |
951 } | 958 } |
952 | 959 |
953 SetResult(result); | 960 SetResult(result); |
954 return true; | 961 return true; |
955 } | 962 } |
956 | 963 |
957 bool CreateTabFunction::RunImpl() { | 964 bool CreateTabFunction::RunImpl() { |
958 DictionaryValue* args = NULL; | 965 DictionaryValue* args = NULL; |
959 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); | 966 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1074 tab_strip = params.browser->tab_strip_model(); | 1081 tab_strip = params.browser->tab_strip_model(); |
1075 int new_index = tab_strip->GetIndexOfTabContents(params.target_contents); | 1082 int new_index = tab_strip->GetIndexOfTabContents(params.target_contents); |
1076 if (opener) | 1083 if (opener) |
1077 tab_strip->SetOpenerOfTabContentsAt(new_index, opener); | 1084 tab_strip->SetOpenerOfTabContentsAt(new_index, opener); |
1078 | 1085 |
1079 if (active) | 1086 if (active) |
1080 params.target_contents->web_contents()->GetView()->SetInitialFocus(); | 1087 params.target_contents->web_contents()->GetView()->SetInitialFocus(); |
1081 | 1088 |
1082 // Return data about the newly created tab. | 1089 // Return data about the newly created tab. |
1083 if (has_callback()) { | 1090 if (has_callback()) { |
1084 SetResult(ExtensionTabUtil::CreateTabValue( | 1091 DictionaryValue* result = ExtensionTabUtil::CreateTabValue( |
1085 params.target_contents->web_contents(), | 1092 params.target_contents->web_contents(), |
1086 tab_strip, new_index)); | 1093 tab_strip, new_index); |
1094 if (!GetExtension()->HasAPIPermission(extensions::APIPermission::kTab)) | |
1095 ExtensionTabUtil::StripTabOfSensitiveData(result); | |
1096 SetResult(result); | |
1087 } | 1097 } |
1088 | 1098 |
1089 return true; | 1099 return true; |
1090 } | 1100 } |
1091 | 1101 |
1092 bool GetTabFunction::RunImpl() { | 1102 bool GetTabFunction::RunImpl() { |
1093 int tab_id = -1; | 1103 int tab_id = -1; |
1094 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | 1104 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); |
1095 | 1105 |
1096 TabStripModel* tab_strip = NULL; | 1106 TabStripModel* tab_strip = NULL; |
1097 TabContents* contents = NULL; | 1107 TabContents* contents = NULL; |
1098 int tab_index = -1; | 1108 int tab_index = -1; |
1099 if (!GetTabById(tab_id, profile(), include_incognito(), | 1109 if (!GetTabById(tab_id, profile(), include_incognito(), |
1100 NULL, &tab_strip, &contents, &tab_index, &error_)) | 1110 NULL, &tab_strip, &contents, &tab_index, &error_)) |
1101 return false; | 1111 return false; |
1102 | 1112 |
1103 SetResult(ExtensionTabUtil::CreateTabValue(contents->web_contents(), | 1113 DictionaryValue* result = ExtensionTabUtil::CreateTabValue( |
1104 tab_strip, | 1114 contents->web_contents(), |
1105 tab_index)); | 1115 tab_strip, |
1116 tab_index); | |
1117 if (!GetExtension()->HasAPIPermission(extensions::APIPermission::kTab)) | |
1118 ExtensionTabUtil::StripTabOfSensitiveData(result); | |
1119 SetResult(result); | |
1106 return true; | 1120 return true; |
1107 } | 1121 } |
1108 | 1122 |
1109 bool GetCurrentTabFunction::RunImpl() { | 1123 bool GetCurrentTabFunction::RunImpl() { |
1110 DCHECK(dispatcher()); | 1124 DCHECK(dispatcher()); |
1111 | 1125 |
1112 WebContents* contents = dispatcher()->delegate()->GetAssociatedWebContents(); | 1126 WebContents* contents = dispatcher()->delegate()->GetAssociatedWebContents(); |
1113 if (contents) | 1127 if (contents) { |
1114 SetResult(ExtensionTabUtil::CreateTabValue(contents)); | 1128 DictionaryValue* result = ExtensionTabUtil::CreateTabValue(contents); |
1129 if (!GetExtension()->HasAPIPermission(extensions::APIPermission::kTab)) | |
1130 ExtensionTabUtil::StripTabOfSensitiveData(result); | |
1131 SetResult(result); | |
1132 } | |
1115 | 1133 |
1116 return true; | 1134 return true; |
1117 } | 1135 } |
1118 | 1136 |
1119 bool HighlightTabsFunction::RunImpl() { | 1137 bool HighlightTabsFunction::RunImpl() { |
1120 DictionaryValue* info = NULL; | 1138 DictionaryValue* info = NULL; |
1121 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &info)); | 1139 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &info)); |
1122 | 1140 |
1123 // Get the window id from the params; default to current window if omitted. | 1141 // Get the window id from the params; default to current window if omitted. |
1124 int window_id = extension_misc::kCurrentWindowId; | 1142 int window_id = extension_misc::kCurrentWindowId; |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1333 if (!url.SchemeIs(chrome::kJavaScriptScheme)) | 1351 if (!url.SchemeIs(chrome::kJavaScriptScheme)) |
1334 DCHECK_EQ(url.spec(), tab_contents_->web_contents()->GetURL().spec()); | 1352 DCHECK_EQ(url.spec(), tab_contents_->web_contents()->GetURL().spec()); |
1335 | 1353 |
1336 return true; | 1354 return true; |
1337 } | 1355 } |
1338 | 1356 |
1339 void UpdateTabFunction::PopulateResult() { | 1357 void UpdateTabFunction::PopulateResult() { |
1340 if (!has_callback()) | 1358 if (!has_callback()) |
1341 return; | 1359 return; |
1342 | 1360 |
1343 if (GetExtension()->HasAPIPermission(extensions::APIPermission::kTab)) { | 1361 DictionaryValue* result = ExtensionTabUtil::CreateTabValue( |
1344 SetResult(ExtensionTabUtil::CreateTabValue(tab_contents_->web_contents())); | 1362 tab_contents_->web_contents()); |
1345 } else { | 1363 if (!GetExtension()->HasAPIPermission(extensions::APIPermission::kTab)) |
1346 SetResult(Value::CreateNullValue()); | 1364 ExtensionTabUtil::StripTabOfSensitiveData(result); |
1347 } | 1365 SetResult(result); |
1348 } | 1366 } |
1349 | 1367 |
1350 void UpdateTabFunction::OnExecuteCodeFinished(bool success, | 1368 void UpdateTabFunction::OnExecuteCodeFinished(bool success, |
1351 int32 page_id, | 1369 int32 page_id, |
1352 const std::string& error, | 1370 const std::string& error, |
1353 const ListValue& script_result) { | 1371 const ListValue& script_result) { |
1354 if (!error.empty()) { | 1372 if (!error.empty()) { |
1355 CHECK(!success); | 1373 CHECK(!success); |
1356 error_ = error; | 1374 error_ = error; |
1357 } | 1375 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1432 | 1450 |
1433 // Clamp move location to the last position. | 1451 // Clamp move location to the last position. |
1434 // This is ">" because it can append to a new index position. | 1452 // This is ">" because it can append to a new index position. |
1435 // -1 means set the move location to the last position. | 1453 // -1 means set the move location to the last position. |
1436 if (new_index > target_tab_strip->count() || new_index < 0) | 1454 if (new_index > target_tab_strip->count() || new_index < 0) |
1437 new_index = target_tab_strip->count(); | 1455 new_index = target_tab_strip->count(); |
1438 | 1456 |
1439 target_tab_strip->InsertTabContentsAt( | 1457 target_tab_strip->InsertTabContentsAt( |
1440 new_index, contents, TabStripModel::ADD_NONE); | 1458 new_index, contents, TabStripModel::ADD_NONE); |
1441 | 1459 |
1442 if (has_callback()) | 1460 if (has_callback()) { |
1443 tab_values.Append(ExtensionTabUtil::CreateTabValue( | 1461 DictionaryValue* result = ExtensionTabUtil::CreateTabValue( |
1444 contents->web_contents(), target_tab_strip, new_index)); | 1462 contents->web_contents(), target_tab_strip, new_index); |
1463 if (!GetExtension()->HasAPIPermission( | |
1464 extensions::APIPermission::kTab)) { | |
1465 ExtensionTabUtil::StripTabOfSensitiveData(result); | |
1466 } | |
1467 tab_values.Append(result); | |
1468 } | |
1445 | 1469 |
1446 continue; | 1470 continue; |
1447 } | 1471 } |
1448 } | 1472 } |
1449 | 1473 |
1450 // Perform a simple within-window move. | 1474 // Perform a simple within-window move. |
1451 // Clamp move location to the last position. | 1475 // Clamp move location to the last position. |
1452 // This is ">=" because the move must be to an existing location. | 1476 // This is ">=" because the move must be to an existing location. |
1453 // -1 means set the move location to the last position. | 1477 // -1 means set the move location to the last position. |
1454 if (new_index >= source_tab_strip->count() || new_index < 0) | 1478 if (new_index >= source_tab_strip->count() || new_index < 0) |
1455 new_index = source_tab_strip->count() - 1; | 1479 new_index = source_tab_strip->count() - 1; |
1456 | 1480 |
1457 if (new_index != tab_index) | 1481 if (new_index != tab_index) |
1458 source_tab_strip->MoveTabContentsAt(tab_index, new_index, false); | 1482 source_tab_strip->MoveTabContentsAt(tab_index, new_index, false); |
1459 | 1483 |
1460 if (has_callback()) | 1484 if (has_callback()) { |
1461 tab_values.Append(ExtensionTabUtil::CreateTabValue( | 1485 DictionaryValue* result = ExtensionTabUtil::CreateTabValue( |
1462 contents->web_contents(), source_tab_strip, new_index)); | 1486 contents->web_contents(), source_tab_strip, new_index); |
1487 if (!GetExtension()->HasAPIPermission(extensions::APIPermission::kTab)) | |
1488 ExtensionTabUtil::StripTabOfSensitiveData(result); | |
1489 tab_values.Append(result); | |
1490 } | |
1463 } | 1491 } |
1464 | 1492 |
1465 if (!has_callback()) | 1493 if (!has_callback()) |
1466 return true; | 1494 return true; |
1467 | 1495 |
1468 // Only return the results as an array if there are multiple tabs. | 1496 // Only return the results as an array if there are multiple tabs. |
1469 if (tab_ids.size() > 1) { | 1497 if (tab_ids.size() > 1) { |
1470 SetResult(tab_values.DeepCopy()); | 1498 SetResult(tab_values.DeepCopy()); |
1471 } else if (tab_ids.size() == 1) { | 1499 } else if (tab_ids.size() == 1) { |
1472 Value* value = NULL; | 1500 Value* value = NULL; |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1823 // called for every API call the extension made. | 1851 // called for every API call the extension made. |
1824 GotLanguage(language); | 1852 GotLanguage(language); |
1825 } | 1853 } |
1826 | 1854 |
1827 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { | 1855 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { |
1828 SetResult(Value::CreateStringValue(language.c_str())); | 1856 SetResult(Value::CreateStringValue(language.c_str())); |
1829 SendResponse(true); | 1857 SendResponse(true); |
1830 | 1858 |
1831 Release(); // Balanced in Run() | 1859 Release(); // Balanced in Run() |
1832 } | 1860 } |
OLD | NEW |