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