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

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: Syncing to the latest tree after a week away. 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 <limits> 8 #include <limits>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/base64.h" 11 #include "base/base64.h"
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/ref_counted_memory.h" 14 #include "base/memory/ref_counted_memory.h"
15 #include "base/message_loop.h" 15 #include "base/message_loop.h"
16 #include "base/stl_util.h" 16 #include "base/stl_util.h"
17 #include "base/string16.h" 17 #include "base/string16.h"
18 #include "base/string_number_conversions.h" 18 #include "base/string_number_conversions.h"
19 #include "base/string_util.h" 19 #include "base/string_util.h"
20 #include "base/stringprintf.h" 20 #include "base/stringprintf.h"
21 #include "base/utf_string_conversions.h" 21 #include "base/utf_string_conversions.h"
22 #include "chrome/browser/extensions/extension_function_dispatcher.h" 22 #include "chrome/browser/extensions/extension_function_dispatcher.h"
23 #include "chrome/browser/extensions/extension_function_util.h"
23 #include "chrome/browser/extensions/extension_host.h" 24 #include "chrome/browser/extensions/extension_host.h"
24 #include "chrome/browser/extensions/extension_service.h" 25 #include "chrome/browser/extensions/extension_service.h"
25 #include "chrome/browser/extensions/extension_tab_helper.h" 26 #include "chrome/browser/extensions/extension_tab_helper.h"
26 #include "chrome/browser/extensions/extension_tab_util.h" 27 #include "chrome/browser/extensions/extension_tab_util.h"
27 #include "chrome/browser/extensions/extension_tabs_module_constants.h" 28 #include "chrome/browser/extensions/extension_tabs_module_constants.h"
28 #include "chrome/browser/extensions/extension_window_controller.h" 29 #include "chrome/browser/extensions/extension_window_controller.h"
29 #include "chrome/browser/extensions/extension_window_list.h" 30 #include "chrome/browser/extensions/extension_window_list.h"
30 #include "chrome/browser/extensions/script_executor.h" 31 #include "chrome/browser/extensions/script_executor.h"
31 #include "chrome/browser/prefs/incognito_mode_prefs.h" 32 #include "chrome/browser/prefs/incognito_mode_prefs.h"
32 #include "chrome/browser/profiles/profile.h" 33 #include "chrome/browser/profiles/profile.h"
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 browser, tab_strip, contents, tab_index)) 195 browser, tab_strip, contents, tab_index))
195 return true; 196 return true;
196 197
197 if (error_message) 198 if (error_message)
198 *error_message = ExtensionErrorUtils::FormatErrorMessage( 199 *error_message = ExtensionErrorUtils::FormatErrorMessage(
199 keys::kTabNotFoundError, base::IntToString(tab_id)); 200 keys::kTabNotFoundError, base::IntToString(tab_id));
200 201
201 return false; 202 return false;
202 } 203 }
203 204
204 // Reads the |value| as either a single integer value or a list of integers.
205 bool ReadOneOrMoreIntegers(
206 Value* value, std::vector<int>* result) {
207 if (value->IsType(Value::TYPE_INTEGER)) {
208 int tab_id = -1;
209 if (!value->GetAsInteger(&tab_id))
210 return false;
211 result->push_back(tab_id);
212 return true;
213
214 } else if (value->IsType(Value::TYPE_LIST)) {
215 ListValue* tabs = static_cast<ListValue*>(value);
216 for (size_t i = 0; i < tabs->GetSize(); ++i) {
217 int tab_id = -1;
218 if (!tabs->GetInteger(i, &tab_id))
219 return false;
220 result->push_back(tab_id);
221 }
222 return true;
223 }
224 return false;
225 }
226
227 // A three state enum to distinguish between when a boolean query argument is 205 // A three state enum to distinguish between when a boolean query argument is
228 // set or not. 206 // set or not.
229 enum QueryArg { 207 enum QueryArg {
230 NOT_SET = -1, 208 NOT_SET = -1,
231 MATCH_FALSE, 209 MATCH_FALSE,
232 MATCH_TRUE 210 MATCH_TRUE
233 }; 211 };
234 212
235 bool MatchesQueryArg(QueryArg arg, bool value) { 213 bool MatchesQueryArg(QueryArg arg, bool value) {
236 if (arg == NOT_SET) 214 if (arg == NOT_SET)
(...skipping 904 matching lines...) Expand 10 before | Expand all | Expand 10 after
1141 return false; 1119 return false;
1142 1120
1143 TabStripModel* tabstrip = browser->tab_strip_model(); 1121 TabStripModel* tabstrip = browser->tab_strip_model();
1144 TabStripSelectionModel selection; 1122 TabStripSelectionModel selection;
1145 int active_index = -1; 1123 int active_index = -1;
1146 1124
1147 Value* tab_value = NULL; 1125 Value* tab_value = NULL;
1148 EXTENSION_FUNCTION_VALIDATE(info->Get(keys::kTabsKey, &tab_value)); 1126 EXTENSION_FUNCTION_VALIDATE(info->Get(keys::kTabsKey, &tab_value));
1149 1127
1150 std::vector<int> tab_indices; 1128 std::vector<int> tab_indices;
1151 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_indices)); 1129 EXTENSION_FUNCTION_VALIDATE(extensions::ReadOneOrMoreIntegers(
1130 tab_value, &tab_indices));
1152 1131
1153 // Create a new selection model as we read the list of tab indices. 1132 // Create a new selection model as we read the list of tab indices.
1154 for (size_t i = 0; i < tab_indices.size(); ++i) { 1133 for (size_t i = 0; i < tab_indices.size(); ++i) {
1155 int index = tab_indices[i]; 1134 int index = tab_indices[i];
1156 1135
1157 // Make sure the index is in range. 1136 // Make sure the index is in range.
1158 if (!tabstrip->ContainsIndex(index)) { 1137 if (!tabstrip->ContainsIndex(index)) {
1159 error_ = ExtensionErrorUtils::FormatErrorMessage( 1138 error_ = ExtensionErrorUtils::FormatErrorMessage(
1160 keys::kTabIndexNotFoundError, base::IntToString(index)); 1139 keys::kTabIndexNotFoundError, base::IntToString(index));
1161 return false; 1140 return false;
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
1366 if (success) 1345 if (success)
1367 PopulateResult(); 1346 PopulateResult();
1368 SendResponse(success); 1347 SendResponse(success);
1369 } 1348 }
1370 1349
1371 bool MoveTabsFunction::RunImpl() { 1350 bool MoveTabsFunction::RunImpl() {
1372 Value* tab_value = NULL; 1351 Value* tab_value = NULL;
1373 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); 1352 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value));
1374 1353
1375 std::vector<int> tab_ids; 1354 std::vector<int> tab_ids;
1376 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_ids)); 1355 EXTENSION_FUNCTION_VALIDATE(extensions::ReadOneOrMoreIntegers(
1356 tab_value, &tab_ids));
1377 1357
1378 DictionaryValue* update_props = NULL; 1358 DictionaryValue* update_props = NULL;
1379 int new_index; 1359 int new_index;
1380 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); 1360 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props));
1381 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(keys::kIndexKey, 1361 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(keys::kIndexKey,
1382 &new_index)); 1362 &new_index));
1383 1363
1384 ListValue tab_values; 1364 ListValue tab_values;
1385 for (size_t i = 0; i < tab_ids.size(); ++i) { 1365 for (size_t i = 0; i < tab_ids.size(); ++i) {
1386 Browser* source_browser = NULL; 1366 Browser* source_browser = NULL;
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
1536 } 1516 }
1537 1517
1538 return true; 1518 return true;
1539 } 1519 }
1540 1520
1541 bool RemoveTabsFunction::RunImpl() { 1521 bool RemoveTabsFunction::RunImpl() {
1542 Value* tab_value = NULL; 1522 Value* tab_value = NULL;
1543 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); 1523 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value));
1544 1524
1545 std::vector<int> tab_ids; 1525 std::vector<int> tab_ids;
1546 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_ids)); 1526 EXTENSION_FUNCTION_VALIDATE(extensions::ReadOneOrMoreIntegers(
1527 tab_value, &tab_ids));
1547 1528
1548 for (size_t i = 0; i < tab_ids.size(); ++i) { 1529 for (size_t i = 0; i < tab_ids.size(); ++i) {
1549 Browser* browser = NULL; 1530 Browser* browser = NULL;
1550 TabContentsWrapper* contents = NULL; 1531 TabContentsWrapper* contents = NULL;
1551 if (!GetTabById(tab_ids[i], profile(), include_incognito(), 1532 if (!GetTabById(tab_ids[i], profile(), include_incognito(),
1552 &browser, NULL, &contents, NULL, &error_)) 1533 &browser, NULL, &contents, NULL, &error_))
1553 return false; 1534 return false;
1554 1535
1555 // Don't let the extension remove a tab if the user is dragging tabs around. 1536 // Don't let the extension remove a tab if the user is dragging tabs around.
1556 if (!browser->IsTabStripEditable()) { 1537 if (!browser->IsTabStripEditable()) {
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
1816 // called for every API call the extension made. 1797 // called for every API call the extension made.
1817 GotLanguage(language); 1798 GotLanguage(language);
1818 } 1799 }
1819 1800
1820 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { 1801 void DetectTabLanguageFunction::GotLanguage(const std::string& language) {
1821 result_.reset(Value::CreateStringValue(language.c_str())); 1802 result_.reset(Value::CreateStringValue(language.c_str()));
1822 SendResponse(true); 1803 SendResponse(true);
1823 1804
1824 Release(); // Balanced in Run() 1805 Release(); // Balanced in Run()
1825 } 1806 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_processes_api_constants.cc ('k') | chrome/browser/task_manager/task_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698