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" |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 browser, tab_strip, contents, tab_index)) | 188 browser, tab_strip, contents, tab_index)) |
189 return true; | 189 return true; |
190 | 190 |
191 if (error_message) | 191 if (error_message) |
192 *error_message = ExtensionErrorUtils::FormatErrorMessage( | 192 *error_message = ExtensionErrorUtils::FormatErrorMessage( |
193 keys::kTabNotFoundError, base::IntToString(tab_id)); | 193 keys::kTabNotFoundError, base::IntToString(tab_id)); |
194 | 194 |
195 return false; | 195 return false; |
196 } | 196 } |
197 | 197 |
198 // Reads the |value| as either a single integer value or a list of integers. | |
199 bool ReadOneOrMoreIntegers( | |
200 Value* value, std::vector<int>* result) { | |
201 if (value->IsType(Value::TYPE_INTEGER)) { | |
202 int tab_id = -1; | |
203 if (!value->GetAsInteger(&tab_id)) | |
204 return false; | |
205 result->push_back(tab_id); | |
206 return true; | |
207 | |
208 } else if (value->IsType(Value::TYPE_LIST)) { | |
209 ListValue* tabs = static_cast<ListValue*>(value); | |
210 for (size_t i = 0; i < tabs->GetSize(); ++i) { | |
211 int tab_id = -1; | |
212 if (!tabs->GetInteger(i, &tab_id)) | |
213 return false; | |
214 result->push_back(tab_id); | |
215 } | |
216 return true; | |
217 } | |
218 return false; | |
219 } | |
220 | |
221 // A three state enum to distinguish between when a boolean query argument is | 198 // A three state enum to distinguish between when a boolean query argument is |
222 // set or not. | 199 // set or not. |
223 enum QueryArg { | 200 enum QueryArg { |
224 NOT_SET = -1, | 201 NOT_SET = -1, |
225 MATCH_FALSE, | 202 MATCH_FALSE, |
226 MATCH_TRUE | 203 MATCH_TRUE |
227 }; | 204 }; |
228 | 205 |
229 bool MatchesQueryArg(QueryArg arg, bool value) { | 206 bool MatchesQueryArg(QueryArg arg, bool value) { |
230 if (arg == NOT_SET) | 207 if (arg == NOT_SET) |
(...skipping 903 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1134 return false; | 1111 return false; |
1135 | 1112 |
1136 TabStripModel* tabstrip = browser->tabstrip_model(); | 1113 TabStripModel* tabstrip = browser->tabstrip_model(); |
1137 TabStripSelectionModel selection; | 1114 TabStripSelectionModel selection; |
1138 int active_index = -1; | 1115 int active_index = -1; |
1139 | 1116 |
1140 Value* tab_value = NULL; | 1117 Value* tab_value = NULL; |
1141 EXTENSION_FUNCTION_VALIDATE(info->Get(keys::kTabsKey, &tab_value)); | 1118 EXTENSION_FUNCTION_VALIDATE(info->Get(keys::kTabsKey, &tab_value)); |
1142 | 1119 |
1143 std::vector<int> tab_indices; | 1120 std::vector<int> tab_indices; |
1144 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_indices)); | 1121 EXTENSION_FUNCTION_VALIDATE(ExtensionTabUtil::ReadOneOrMoreIntegers( |
| 1122 tab_value, &tab_indices)); |
1145 | 1123 |
1146 // Create a new selection model as we read the list of tab indices. | 1124 // Create a new selection model as we read the list of tab indices. |
1147 for (size_t i = 0; i < tab_indices.size(); ++i) { | 1125 for (size_t i = 0; i < tab_indices.size(); ++i) { |
1148 int index = tab_indices[i]; | 1126 int index = tab_indices[i]; |
1149 | 1127 |
1150 // Make sure the index is in range. | 1128 // Make sure the index is in range. |
1151 if (!tabstrip->ContainsIndex(index)) { | 1129 if (!tabstrip->ContainsIndex(index)) { |
1152 error_ = ExtensionErrorUtils::FormatErrorMessage( | 1130 error_ = ExtensionErrorUtils::FormatErrorMessage( |
1153 keys::kTabIndexNotFoundError, base::IntToString(index)); | 1131 keys::kTabIndexNotFoundError, base::IntToString(index)); |
1154 return false; | 1132 return false; |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1396 | 1374 |
1397 Observe(NULL); | 1375 Observe(NULL); |
1398 Release(); // Balanced in UpdateURLIfPresent(). | 1376 Release(); // Balanced in UpdateURLIfPresent(). |
1399 } | 1377 } |
1400 | 1378 |
1401 bool MoveTabsFunction::RunImpl() { | 1379 bool MoveTabsFunction::RunImpl() { |
1402 Value* tab_value = NULL; | 1380 Value* tab_value = NULL; |
1403 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); | 1381 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); |
1404 | 1382 |
1405 std::vector<int> tab_ids; | 1383 std::vector<int> tab_ids; |
1406 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_ids)); | 1384 EXTENSION_FUNCTION_VALIDATE(ExtensionTabUtil::ReadOneOrMoreIntegers( |
| 1385 tab_value, &tab_ids)); |
1407 | 1386 |
1408 DictionaryValue* update_props = NULL; | 1387 DictionaryValue* update_props = NULL; |
1409 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); | 1388 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); |
1410 | 1389 |
1411 int new_index = -1; | 1390 int new_index = -1; |
1412 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( | 1391 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( |
1413 keys::kIndexKey, &new_index)); | 1392 keys::kIndexKey, &new_index)); |
1414 EXTENSION_FUNCTION_VALIDATE(new_index >= 0); | 1393 EXTENSION_FUNCTION_VALIDATE(new_index >= 0); |
1415 | 1394 |
1416 ListValue tab_values; | 1395 ListValue tab_values; |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1565 } | 1544 } |
1566 | 1545 |
1567 return true; | 1546 return true; |
1568 } | 1547 } |
1569 | 1548 |
1570 bool RemoveTabsFunction::RunImpl() { | 1549 bool RemoveTabsFunction::RunImpl() { |
1571 Value* tab_value = NULL; | 1550 Value* tab_value = NULL; |
1572 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); | 1551 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); |
1573 | 1552 |
1574 std::vector<int> tab_ids; | 1553 std::vector<int> tab_ids; |
1575 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_ids)); | 1554 EXTENSION_FUNCTION_VALIDATE(ExtensionTabUtil::ReadOneOrMoreIntegers( |
| 1555 tab_value, &tab_ids)); |
1576 | 1556 |
1577 for (size_t i = 0; i < tab_ids.size(); ++i) { | 1557 for (size_t i = 0; i < tab_ids.size(); ++i) { |
1578 Browser* browser = NULL; | 1558 Browser* browser = NULL; |
1579 TabContentsWrapper* contents = NULL; | 1559 TabContentsWrapper* contents = NULL; |
1580 if (!GetTabById(tab_ids[i], profile(), include_incognito(), | 1560 if (!GetTabById(tab_ids[i], profile(), include_incognito(), |
1581 &browser, NULL, &contents, NULL, &error_)) | 1561 &browser, NULL, &contents, NULL, &error_)) |
1582 return false; | 1562 return false; |
1583 | 1563 |
1584 // Don't let the extension remove a tab if the user is dragging tabs around. | 1564 // Don't let the extension remove a tab if the user is dragging tabs around. |
1585 if (!browser->IsTabStripEditable()) { | 1565 if (!browser->IsTabStripEditable()) { |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1828 // called for every API call the extension made. | 1808 // called for every API call the extension made. |
1829 GotLanguage(language); | 1809 GotLanguage(language); |
1830 } | 1810 } |
1831 | 1811 |
1832 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { | 1812 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { |
1833 result_.reset(Value::CreateStringValue(language.c_str())); | 1813 result_.reset(Value::CreateStringValue(language.c_str())); |
1834 SendResponse(true); | 1814 SendResponse(true); |
1835 | 1815 |
1836 Release(); // Balanced in Run() | 1816 Release(); // Balanced in Run() |
1837 } | 1817 } |
OLD | NEW |