Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: chrome/browser/extensions/extension_tabs_module.cc

Issue 10175008: Improving the process model extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Schema file synced with docs, some minor cleanup. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 browser, tab_strip, contents, tab_index)) 189 browser, tab_strip, contents, tab_index))
190 return true; 190 return true;
191 191
192 if (error_message) 192 if (error_message)
193 *error_message = ExtensionErrorUtils::FormatErrorMessage( 193 *error_message = ExtensionErrorUtils::FormatErrorMessage(
194 keys::kTabNotFoundError, base::IntToString(tab_id)); 194 keys::kTabNotFoundError, base::IntToString(tab_id));
195 195
196 return false; 196 return false;
197 } 197 }
198 198
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 199 // A three state enum to distinguish between when a boolean query argument is
223 // set or not. 200 // set or not.
224 enum QueryArg { 201 enum QueryArg {
225 NOT_SET = -1, 202 NOT_SET = -1,
226 MATCH_FALSE, 203 MATCH_FALSE,
227 MATCH_TRUE 204 MATCH_TRUE
228 }; 205 };
229 206
230 bool MatchesQueryArg(QueryArg arg, bool value) { 207 bool MatchesQueryArg(QueryArg arg, bool value) {
231 if (arg == NOT_SET) 208 if (arg == NOT_SET)
(...skipping 903 matching lines...) Expand 10 before | Expand all | Expand 10 after
1135 return false; 1112 return false;
1136 1113
1137 TabStripModel* tabstrip = browser->tabstrip_model(); 1114 TabStripModel* tabstrip = browser->tabstrip_model();
1138 TabStripSelectionModel selection; 1115 TabStripSelectionModel selection;
1139 int active_index = -1; 1116 int active_index = -1;
1140 1117
1141 Value* tab_value = NULL; 1118 Value* tab_value = NULL;
1142 EXTENSION_FUNCTION_VALIDATE(info->Get(keys::kTabsKey, &tab_value)); 1119 EXTENSION_FUNCTION_VALIDATE(info->Get(keys::kTabsKey, &tab_value));
1143 1120
1144 std::vector<int> tab_indices; 1121 std::vector<int> tab_indices;
1145 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_indices)); 1122 EXTENSION_FUNCTION_VALIDATE(ExtensionTabUtil::ReadOneOrMoreIntegers(
1123 tab_value, &tab_indices));
1146 1124
1147 // Create a new selection model as we read the list of tab indices. 1125 // Create a new selection model as we read the list of tab indices.
1148 for (size_t i = 0; i < tab_indices.size(); ++i) { 1126 for (size_t i = 0; i < tab_indices.size(); ++i) {
1149 int index = tab_indices[i]; 1127 int index = tab_indices[i];
1150 1128
1151 // Make sure the index is in range. 1129 // Make sure the index is in range.
1152 if (!tabstrip->ContainsIndex(index)) { 1130 if (!tabstrip->ContainsIndex(index)) {
1153 error_ = ExtensionErrorUtils::FormatErrorMessage( 1131 error_ = ExtensionErrorUtils::FormatErrorMessage(
1154 keys::kTabIndexNotFoundError, base::IntToString(index)); 1132 keys::kTabIndexNotFoundError, base::IntToString(index));
1155 return false; 1133 return false;
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
1397 1375
1398 Observe(NULL); 1376 Observe(NULL);
1399 Release(); // Balanced in UpdateURLIfPresent(). 1377 Release(); // Balanced in UpdateURLIfPresent().
1400 } 1378 }
1401 1379
1402 bool MoveTabsFunction::RunImpl() { 1380 bool MoveTabsFunction::RunImpl() {
1403 Value* tab_value = NULL; 1381 Value* tab_value = NULL;
1404 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); 1382 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value));
1405 1383
1406 std::vector<int> tab_ids; 1384 std::vector<int> tab_ids;
1407 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_ids)); 1385 EXTENSION_FUNCTION_VALIDATE(ExtensionTabUtil::ReadOneOrMoreIntegers(
1386 tab_value, &tab_ids));
1408 1387
1409 DictionaryValue* update_props = NULL; 1388 DictionaryValue* update_props = NULL;
1410 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); 1389 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props));
1411 1390
1412 int new_index = -1; 1391 int new_index = -1;
1413 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( 1392 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(
1414 keys::kIndexKey, &new_index)); 1393 keys::kIndexKey, &new_index));
1415 EXTENSION_FUNCTION_VALIDATE(new_index >= 0); 1394 EXTENSION_FUNCTION_VALIDATE(new_index >= 0);
1416 1395
1417 ListValue tab_values; 1396 ListValue tab_values;
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
1566 } 1545 }
1567 1546
1568 return true; 1547 return true;
1569 } 1548 }
1570 1549
1571 bool RemoveTabsFunction::RunImpl() { 1550 bool RemoveTabsFunction::RunImpl() {
1572 Value* tab_value = NULL; 1551 Value* tab_value = NULL;
1573 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); 1552 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value));
1574 1553
1575 std::vector<int> tab_ids; 1554 std::vector<int> tab_ids;
1576 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_ids)); 1555 EXTENSION_FUNCTION_VALIDATE(ExtensionTabUtil::ReadOneOrMoreIntegers(
1556 tab_value, &tab_ids));
1577 1557
1578 for (size_t i = 0; i < tab_ids.size(); ++i) { 1558 for (size_t i = 0; i < tab_ids.size(); ++i) {
1579 Browser* browser = NULL; 1559 Browser* browser = NULL;
1580 TabContentsWrapper* contents = NULL; 1560 TabContentsWrapper* contents = NULL;
1581 if (!GetTabById(tab_ids[i], profile(), include_incognito(), 1561 if (!GetTabById(tab_ids[i], profile(), include_incognito(),
1582 &browser, NULL, &contents, NULL, &error_)) 1562 &browser, NULL, &contents, NULL, &error_))
1583 return false; 1563 return false;
1584 1564
1585 // Don't let the extension remove a tab if the user is dragging tabs around. 1565 // Don't let the extension remove a tab if the user is dragging tabs around.
1586 if (!browser->IsTabStripEditable()) { 1566 if (!browser->IsTabStripEditable()) {
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
1846 // called for every API call the extension made. 1826 // called for every API call the extension made.
1847 GotLanguage(language); 1827 GotLanguage(language);
1848 } 1828 }
1849 1829
1850 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { 1830 void DetectTabLanguageFunction::GotLanguage(const std::string& language) {
1851 result_.reset(Value::CreateStringValue(language.c_str())); 1831 result_.reset(Value::CreateStringValue(language.c_str()));
1852 SendResponse(true); 1832 SendResponse(true);
1853 1833
1854 Release(); // Balanced in Run() 1834 Release(); // Balanced in Run()
1855 } 1835 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698