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

Side by Side Diff: chrome/browser/extensions/api/tabs/tabs.cc

Issue 11308012: Remove some TabContentses from extensions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 1 month 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/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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 *controller = WindowControllerList::GetInstance()-> 166 *controller = WindowControllerList::GetInstance()->
167 FindWindowForFunctionById(function, window_id); 167 FindWindowForFunctionById(function, window_id);
168 if (!(*controller)) { 168 if (!(*controller)) {
169 function->SetError(ExtensionErrorUtils::FormatErrorMessage( 169 function->SetError(ExtensionErrorUtils::FormatErrorMessage(
170 keys::kWindowNotFoundError, base::IntToString(window_id))); 170 keys::kWindowNotFoundError, base::IntToString(window_id)));
171 return false; 171 return false;
172 } 172 }
173 } 173 }
174 return true; 174 return true;
175 } 175 }
176
176 // |error_message| can optionally be passed in and will be set with an 177 // |error_message| can optionally be passed in and will be set with an
177 // appropriate message if the tab cannot be found by id. 178 // appropriate message if the tab cannot be found by id.
178 bool GetTabById(int tab_id, 179 bool GetTabById(int tab_id,
179 Profile* profile, 180 Profile* profile,
180 bool include_incognito, 181 bool include_incognito,
181 Browser** browser, 182 Browser** browser,
182 TabStripModel** tab_strip, 183 TabStripModel** tab_strip,
183 TabContents** contents, 184 content::WebContents** contents,
184 int* tab_index, 185 int* tab_index,
185 std::string* error_message) { 186 std::string* error_message) {
186 if (ExtensionTabUtil::GetTabById(tab_id, profile, include_incognito, 187 if (ExtensionTabUtil::GetTabById(tab_id, profile, include_incognito,
187 browser, tab_strip, contents, tab_index)) 188 browser, tab_strip, contents, tab_index))
188 return true; 189 return true;
189 190
190 if (error_message) 191 if (error_message)
191 *error_message = ExtensionErrorUtils::FormatErrorMessage( 192 *error_message = ExtensionErrorUtils::FormatErrorMessage(
192 keys::kTabNotFoundError, base::IntToString(tab_id)); 193 keys::kTabNotFoundError, base::IntToString(tab_id));
193 194
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 } 436 }
436 } 437 }
437 438
438 // Look for optional tab id. 439 // Look for optional tab id.
439 if (args) { 440 if (args) {
440 int tab_id = -1; 441 int tab_id = -1;
441 if (args->HasKey(keys::kTabIdKey)) { 442 if (args->HasKey(keys::kTabIdKey)) {
442 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kTabIdKey, &tab_id)); 443 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(keys::kTabIdKey, &tab_id));
443 444
444 // Find the tab and detach it from the original window. 445 // Find the tab and detach it from the original window.
445 Browser* source_browser = NULL;
446 TabStripModel* source_tab_strip = NULL; 446 TabStripModel* source_tab_strip = NULL;
447 int tab_index = -1; 447 int tab_index = -1;
448 if (!GetTabById(tab_id, profile(), include_incognito(), 448 if (!GetTabById(tab_id, profile(), include_incognito(),
449 &source_browser, &source_tab_strip, &contents, 449 NULL, &source_tab_strip,
450 &tab_index, &error_)) 450 NULL, &tab_index, &error_))
451 return false; 451 return false;
452 contents = source_tab_strip->DetachTabContentsAt(tab_index); 452 contents = source_tab_strip->DetachTabContentsAt(tab_index);
453 if (!contents) { 453 if (!contents) {
454 error_ = ExtensionErrorUtils::FormatErrorMessage( 454 error_ = ExtensionErrorUtils::FormatErrorMessage(
455 keys::kTabNotFoundError, base::IntToString(tab_id)); 455 keys::kTabNotFoundError, base::IntToString(tab_id));
456 return false; 456 return false;
457 } 457 }
458 } 458 }
459 } 459 }
460 460
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 return false; 999 return false;
1000 1000
1001 // TODO(jstritar): Add a constant, chrome.tabs.TAB_ID_ACTIVE, that 1001 // TODO(jstritar): Add a constant, chrome.tabs.TAB_ID_ACTIVE, that
1002 // represents the active tab. 1002 // represents the active tab.
1003 WebContents* opener = NULL; 1003 WebContents* opener = NULL;
1004 if (args->HasKey(keys::kOpenerTabIdKey)) { 1004 if (args->HasKey(keys::kOpenerTabIdKey)) {
1005 int opener_id = -1; 1005 int opener_id = -1;
1006 EXTENSION_FUNCTION_VALIDATE(args->GetInteger( 1006 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(
1007 keys::kOpenerTabIdKey, &opener_id)); 1007 keys::kOpenerTabIdKey, &opener_id));
1008 1008
1009 TabContents* opener_contents = NULL;
1010 if (!ExtensionTabUtil::GetTabById( 1009 if (!ExtensionTabUtil::GetTabById(
1011 opener_id, profile(), include_incognito(), 1010 opener_id, profile(), include_incognito(),
1012 NULL, NULL, &opener_contents, NULL)) 1011 NULL, NULL, &opener, NULL))
1013 return false; 1012 return false;
1014
1015 opener = opener_contents->web_contents();
1016 } 1013 }
1017 1014
1018 // TODO(rafaelw): handle setting remaining tab properties: 1015 // TODO(rafaelw): handle setting remaining tab properties:
1019 // -title 1016 // -title
1020 // -favIconUrl 1017 // -favIconUrl
1021 1018
1022 std::string url_string; 1019 std::string url_string;
1023 GURL url; 1020 GURL url;
1024 if (args->HasKey(keys::kUrlKey)) { 1021 if (args->HasKey(keys::kUrlKey)) {
1025 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, 1022 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey,
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1109 1106
1110 return true; 1107 return true;
1111 } 1108 }
1112 1109
1113 bool DuplicateTabFunction::RunImpl() { 1110 bool DuplicateTabFunction::RunImpl() {
1114 int tab_id = -1; 1111 int tab_id = -1;
1115 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); 1112 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id));
1116 1113
1117 Browser* browser = NULL; 1114 Browser* browser = NULL;
1118 TabStripModel* tab_strip = NULL; 1115 TabStripModel* tab_strip = NULL;
1119 TabContents* contents = NULL;
1120 int tab_index = -1; 1116 int tab_index = -1;
1121 if (!GetTabById(tab_id, profile(), include_incognito(), 1117 if (!GetTabById(tab_id, profile(), include_incognito(),
1122 &browser, &tab_strip, &contents, &tab_index, &error_)) { 1118 &browser, &tab_strip, NULL, &tab_index, &error_)) {
1123 return false; 1119 return false;
1124 } 1120 }
1125 1121
1126 TabContents* new_contents = chrome::DuplicateTabAt(browser, tab_index); 1122 TabContents* new_contents = chrome::DuplicateTabAt(browser, tab_index);
1127 if (!has_callback()) 1123 if (!has_callback())
1128 return true; 1124 return true;
1129 1125
1130 int new_index = tab_strip->GetIndexOfTabContents(new_contents); 1126 int new_index = tab_strip->GetIndexOfTabContents(new_contents);
1131 1127
1132 // Return data about the newly created tab. 1128 // Return data about the newly created tab.
1133 SetResult(ExtensionTabUtil::CreateTabValue( 1129 SetResult(ExtensionTabUtil::CreateTabValue(
1134 new_contents->web_contents(), 1130 new_contents->web_contents(),
1135 tab_strip, new_index, GetExtension())); 1131 tab_strip, new_index, GetExtension()));
1136 1132
1137 return true; 1133 return true;
1138 } 1134 }
1139 1135
1140 bool GetTabFunction::RunImpl() { 1136 bool GetTabFunction::RunImpl() {
1141 int tab_id = -1; 1137 int tab_id = -1;
1142 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); 1138 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id));
1143 1139
1144 TabStripModel* tab_strip = NULL; 1140 TabStripModel* tab_strip = NULL;
1145 TabContents* contents = NULL; 1141 WebContents* contents = NULL;
1146 int tab_index = -1; 1142 int tab_index = -1;
1147 if (!GetTabById(tab_id, profile(), include_incognito(), 1143 if (!GetTabById(tab_id, profile(), include_incognito(),
1148 NULL, &tab_strip, &contents, &tab_index, &error_)) 1144 NULL, &tab_strip, &contents, &tab_index, &error_))
1149 return false; 1145 return false;
1150 1146
1151 SetResult(ExtensionTabUtil::CreateTabValue(contents->web_contents(), 1147 SetResult(ExtensionTabUtil::CreateTabValue(contents,
1152 tab_strip, 1148 tab_strip,
1153 tab_index, 1149 tab_index,
1154 GetExtension())); 1150 GetExtension()));
1155 return true; 1151 return true;
1156 } 1152 }
1157 1153
1158 bool GetCurrentTabFunction::RunImpl() { 1154 bool GetCurrentTabFunction::RunImpl() {
1159 DCHECK(dispatcher()); 1155 DCHECK(dispatcher());
1160 1156
1161 WebContents* contents = dispatcher()->delegate()->GetAssociatedWebContents(); 1157 WebContents* contents = dispatcher()->delegate()->GetAssociatedWebContents();
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1215 } 1211 }
1216 1212
1217 selection.set_active(active_index); 1213 selection.set_active(active_index);
1218 browser->tab_strip_model()->SetSelectionFromModel(selection); 1214 browser->tab_strip_model()->SetSelectionFromModel(selection);
1219 SetResult( 1215 SetResult(
1220 browser->extension_window_controller()->CreateWindowValueWithTabs( 1216 browser->extension_window_controller()->CreateWindowValueWithTabs(
1221 GetExtension())); 1217 GetExtension()));
1222 return true; 1218 return true;
1223 } 1219 }
1224 1220
1225 UpdateTabFunction::UpdateTabFunction() : tab_contents_(NULL) { 1221 UpdateTabFunction::UpdateTabFunction() : web_contents_(NULL) {
1226 } 1222 }
1227 1223
1228 bool UpdateTabFunction::RunImpl() { 1224 bool UpdateTabFunction::RunImpl() {
1229 DictionaryValue* update_props = NULL; 1225 DictionaryValue* update_props = NULL;
1230 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); 1226 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props));
1231 1227
1232 Value* tab_value = NULL; 1228 Value* tab_value = NULL;
1233 if (HasOptionalArgument(0)) { 1229 if (HasOptionalArgument(0)) {
1234 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); 1230 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value));
1235 } 1231 }
1236 1232
1237 int tab_id = -1; 1233 int tab_id = -1;
1238 TabContents* contents = NULL; 1234 WebContents* contents = NULL;
1239 if (tab_value == NULL || tab_value->IsType(Value::TYPE_NULL)) { 1235 if (tab_value == NULL || tab_value->IsType(Value::TYPE_NULL)) {
1240 Browser* browser = GetCurrentBrowser(); 1236 Browser* browser = GetCurrentBrowser();
1241 if (!browser) { 1237 if (!browser) {
1242 error_ = keys::kNoCurrentWindowError; 1238 error_ = keys::kNoCurrentWindowError;
1243 return false; 1239 return false;
1244 } 1240 }
1245 contents = browser->tab_strip_model()->GetActiveTabContents(); 1241 contents = browser->tab_strip_model()->GetActiveWebContents();
1246 if (!contents) { 1242 if (!contents) {
1247 error_ = keys::kNoSelectedTabError; 1243 error_ = keys::kNoSelectedTabError;
1248 return false; 1244 return false;
1249 } 1245 }
1250 tab_id = SessionID::IdForTab(contents->web_contents()); 1246 tab_id = SessionID::IdForTab(contents);
1251 } else { 1247 } else {
1252 EXTENSION_FUNCTION_VALIDATE(tab_value->GetAsInteger(&tab_id)); 1248 EXTENSION_FUNCTION_VALIDATE(tab_value->GetAsInteger(&tab_id));
1253 } 1249 }
1254 1250
1255 int tab_index = -1; 1251 int tab_index = -1;
1256 TabStripModel* tab_strip = NULL; 1252 TabStripModel* tab_strip = NULL;
1257 if (!GetTabById(tab_id, profile(), include_incognito(), 1253 if (!GetTabById(tab_id, profile(), include_incognito(),
1258 NULL, &tab_strip, &contents, &tab_index, &error_)) { 1254 NULL, &tab_strip, &contents, &tab_index, &error_)) {
1259 return false; 1255 return false;
1260 } 1256 }
1261 1257
1262 tab_contents_ = contents; 1258 web_contents_ = contents;
1263 1259
1264 // TODO(rafaelw): handle setting remaining tab properties: 1260 // TODO(rafaelw): handle setting remaining tab properties:
1265 // -title 1261 // -title
1266 // -favIconUrl 1262 // -favIconUrl
1267 1263
1268 // Navigate the tab to a new location if the url is different. 1264 // Navigate the tab to a new location if the url is different.
1269 bool is_async = false; 1265 bool is_async = false;
1270 if (!UpdateURLIfPresent(update_props, tab_id, &is_async)) 1266 if (!UpdateURLIfPresent(update_props, tab_id, &is_async))
1271 return false; 1267 return false;
1272 1268
1273 bool active = false; 1269 bool active = false;
1274 // TODO(rafaelw): Setting |active| from js doesn't make much sense. 1270 // TODO(rafaelw): Setting |active| from js doesn't make much sense.
1275 // Move tab selection management up to window. 1271 // Move tab selection management up to window.
1276 if (update_props->HasKey(keys::kSelectedKey)) 1272 if (update_props->HasKey(keys::kSelectedKey))
1277 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( 1273 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean(
1278 keys::kSelectedKey, &active)); 1274 keys::kSelectedKey, &active));
1279 1275
1280 // The 'active' property has replaced 'selected'. 1276 // The 'active' property has replaced 'selected'.
1281 if (update_props->HasKey(keys::kActiveKey)) 1277 if (update_props->HasKey(keys::kActiveKey))
1282 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( 1278 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean(
1283 keys::kActiveKey, &active)); 1279 keys::kActiveKey, &active));
1284 1280
1285 if (active) { 1281 if (active) {
1286 if (tab_strip->active_index() != tab_index) { 1282 if (tab_strip->active_index() != tab_index) {
1287 tab_strip->ActivateTabAt(tab_index, false); 1283 tab_strip->ActivateTabAt(tab_index, false);
1288 DCHECK_EQ(contents, tab_strip->GetActiveTabContents()); 1284 DCHECK_EQ(contents, tab_strip->GetActiveWebContents());
1289 } 1285 }
1290 tab_contents_->web_contents()->Focus(); 1286 web_contents_->Focus();
1291 } 1287 }
1292 1288
1293 if (update_props->HasKey(keys::kHighlightedKey)) { 1289 if (update_props->HasKey(keys::kHighlightedKey)) {
1294 bool highlighted = false; 1290 bool highlighted = false;
1295 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( 1291 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean(
1296 keys::kHighlightedKey, &highlighted)); 1292 keys::kHighlightedKey, &highlighted));
1297 if (highlighted != tab_strip->IsTabSelected(tab_index)) 1293 if (highlighted != tab_strip->IsTabSelected(tab_index))
1298 tab_strip->ToggleSelectionAt(tab_index); 1294 tab_strip->ToggleSelectionAt(tab_index);
1299 } 1295 }
1300 1296
1301 if (update_props->HasKey(keys::kPinnedKey)) { 1297 if (update_props->HasKey(keys::kPinnedKey)) {
1302 bool pinned = false; 1298 bool pinned = false;
1303 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( 1299 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean(
1304 keys::kPinnedKey, &pinned)); 1300 keys::kPinnedKey, &pinned));
1305 tab_strip->SetTabPinned(tab_index, pinned); 1301 tab_strip->SetTabPinned(tab_index, pinned);
1306 1302
1307 // Update the tab index because it may move when being pinned. 1303 // Update the tab index because it may move when being pinned.
1308 tab_index = tab_strip->GetIndexOfTabContents(contents); 1304 tab_index = tab_strip->GetIndexOfWebContents(contents);
1309 } 1305 }
1310 1306
1311 if (update_props->HasKey(keys::kOpenerTabIdKey)) { 1307 if (update_props->HasKey(keys::kOpenerTabIdKey)) {
1312 int opener_id = -1; 1308 int opener_id = -1;
1313 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( 1309 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(
1314 keys::kOpenerTabIdKey, &opener_id)); 1310 keys::kOpenerTabIdKey, &opener_id));
1315 1311
1316 TabContents* opener_contents = NULL; 1312 WebContents* opener_contents = NULL;
1317 if (!ExtensionTabUtil::GetTabById( 1313 if (!ExtensionTabUtil::GetTabById(
1318 opener_id, profile(), include_incognito(), 1314 opener_id, profile(), include_incognito(),
1319 NULL, NULL, &opener_contents, NULL)) 1315 NULL, NULL, &opener_contents, NULL))
1320 return false; 1316 return false;
1321 1317
1322 tab_strip->SetOpenerOfWebContentsAt( 1318 tab_strip->SetOpenerOfWebContentsAt(tab_index, opener_contents);
1323 tab_index, opener_contents->web_contents());
1324 } 1319 }
1325 1320
1326 if (!is_async) { 1321 if (!is_async) {
1327 PopulateResult(); 1322 PopulateResult();
1328 SendResponse(true); 1323 SendResponse(true);
1329 } 1324 }
1330 return true; 1325 return true;
1331 } 1326 }
1332 1327
1333 bool UpdateTabFunction::UpdateURLIfPresent(DictionaryValue* update_props, 1328 bool UpdateTabFunction::UpdateURLIfPresent(DictionaryValue* update_props,
(...skipping 17 matching lines...) Expand all
1351 // Don't let the extension crash the browser or renderers. 1346 // Don't let the extension crash the browser or renderers.
1352 if (ExtensionTabUtil::IsCrashURL(url)) { 1347 if (ExtensionTabUtil::IsCrashURL(url)) {
1353 error_ = keys::kNoCrashBrowserError; 1348 error_ = keys::kNoCrashBrowserError;
1354 return false; 1349 return false;
1355 } 1350 }
1356 1351
1357 // JavaScript URLs can do the same kinds of things as cross-origin XHR, so 1352 // JavaScript URLs can do the same kinds of things as cross-origin XHR, so
1358 // we need to check host permissions before allowing them. 1353 // we need to check host permissions before allowing them.
1359 if (url.SchemeIs(chrome::kJavaScriptScheme)) { 1354 if (url.SchemeIs(chrome::kJavaScriptScheme)) {
1360 if (!GetExtension()->CanExecuteScriptOnPage( 1355 if (!GetExtension()->CanExecuteScriptOnPage(
1361 tab_contents_->web_contents()->GetURL(), 1356 web_contents_->GetURL(),
1362 tab_contents_->web_contents()->GetURL(), 1357 web_contents_->GetURL(),
1363 tab_id, 1358 tab_id,
1364 NULL, 1359 NULL,
1365 &error_)) { 1360 &error_)) {
1366 return false; 1361 return false;
1367 } 1362 }
1368 1363
1369 extensions::TabHelper::FromWebContents(tab_contents_->web_contents())-> 1364 extensions::TabHelper::FromWebContents(web_contents_)->
1370 script_executor()->ExecuteScript( 1365 script_executor()->ExecuteScript(
1371 extension_id(), 1366 extension_id(),
1372 ScriptExecutor::JAVASCRIPT, 1367 ScriptExecutor::JAVASCRIPT,
1373 url.path(), 1368 url.path(),
1374 ScriptExecutor::TOP_FRAME, 1369 ScriptExecutor::TOP_FRAME,
1375 extensions::UserScript::DOCUMENT_IDLE, 1370 extensions::UserScript::DOCUMENT_IDLE,
1376 ScriptExecutor::MAIN_WORLD, 1371 ScriptExecutor::MAIN_WORLD,
1377 base::Bind(&UpdateTabFunction::OnExecuteCodeFinished, this)); 1372 base::Bind(&UpdateTabFunction::OnExecuteCodeFinished, this));
1378 1373
1379 *is_async = true; 1374 *is_async = true;
1380 return true; 1375 return true;
1381 } 1376 }
1382 1377
1383 tab_contents_->web_contents()->GetController().LoadURL( 1378 web_contents_->GetController().LoadURL(
1384 url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); 1379 url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string());
1385 1380
1386 // The URL of a tab contents never actually changes to a JavaScript URL, so 1381 // The URL of a tab contents never actually changes to a JavaScript URL, so
1387 // this check only makes sense in other cases. 1382 // this check only makes sense in other cases.
1388 if (!url.SchemeIs(chrome::kJavaScriptScheme)) 1383 if (!url.SchemeIs(chrome::kJavaScriptScheme))
1389 DCHECK_EQ(url.spec(), tab_contents_->web_contents()->GetURL().spec()); 1384 DCHECK_EQ(url.spec(), web_contents_->GetURL().spec());
1390 1385
1391 return true; 1386 return true;
1392 } 1387 }
1393 1388
1394 void UpdateTabFunction::PopulateResult() { 1389 void UpdateTabFunction::PopulateResult() {
1395 if (!has_callback()) 1390 if (!has_callback())
1396 return; 1391 return;
1397 1392
1398 SetResult(ExtensionTabUtil::CreateTabValue(tab_contents_->web_contents(), 1393 SetResult(ExtensionTabUtil::CreateTabValue(web_contents_, GetExtension()));
1399 GetExtension()));
1400 } 1394 }
1401 1395
1402 void UpdateTabFunction::OnExecuteCodeFinished(const std::string& error, 1396 void UpdateTabFunction::OnExecuteCodeFinished(const std::string& error,
1403 int32 on_page_id, 1397 int32 on_page_id,
1404 const GURL& url, 1398 const GURL& url,
1405 const ListValue& script_result) { 1399 const ListValue& script_result) {
1406 if (error.empty()) 1400 if (error.empty())
1407 PopulateResult(); 1401 PopulateResult();
1408 else 1402 else
1409 error_ = error; 1403 error_ = error;
(...skipping 11 matching lines...) Expand all
1421 DictionaryValue* update_props = NULL; 1415 DictionaryValue* update_props = NULL;
1422 int new_index; 1416 int new_index;
1423 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); 1417 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props));
1424 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(keys::kIndexKey, 1418 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(keys::kIndexKey,
1425 &new_index)); 1419 &new_index));
1426 1420
1427 ListValue tab_values; 1421 ListValue tab_values;
1428 for (size_t i = 0; i < tab_ids.size(); ++i) { 1422 for (size_t i = 0; i < tab_ids.size(); ++i) {
1429 Browser* source_browser = NULL; 1423 Browser* source_browser = NULL;
1430 TabStripModel* source_tab_strip = NULL; 1424 TabStripModel* source_tab_strip = NULL;
1431 TabContents* contents = NULL; 1425 WebContents* contents = NULL;
1432 int tab_index = -1; 1426 int tab_index = -1;
1433 if (!GetTabById(tab_ids[i], profile(), include_incognito(), 1427 if (!GetTabById(tab_ids[i], profile(), include_incognito(),
1434 &source_browser, &source_tab_strip, &contents, 1428 &source_browser, &source_tab_strip, &contents,
1435 &tab_index, &error_)) 1429 &tab_index, &error_))
1436 return false; 1430 return false;
1437 1431
1438 // Don't let the extension move the tab if the user is dragging tabs. 1432 // Don't let the extension move the tab if the user is dragging tabs.
1439 if (!chrome::IsTabStripEditable(source_browser)) { 1433 if (!chrome::IsTabStripEditable(source_browser)) {
1440 error_ = keys::kTabStripNotEditableError; 1434 error_ = keys::kTabStripNotEditableError;
1441 return false; 1435 return false;
(...skipping 23 matching lines...) Expand all
1465 1459
1466 if (target_browser->profile() != source_browser->profile()) { 1460 if (target_browser->profile() != source_browser->profile()) {
1467 error_ = keys::kCanOnlyMoveTabsWithinSameProfileError; 1461 error_ = keys::kCanOnlyMoveTabsWithinSameProfileError;
1468 return false; 1462 return false;
1469 } 1463 }
1470 1464
1471 // If windowId is different from the current window, move between windows. 1465 // If windowId is different from the current window, move between windows.
1472 if (ExtensionTabUtil::GetWindowId(target_browser) != 1466 if (ExtensionTabUtil::GetWindowId(target_browser) !=
1473 ExtensionTabUtil::GetWindowId(source_browser)) { 1467 ExtensionTabUtil::GetWindowId(source_browser)) {
1474 TabStripModel* target_tab_strip = target_browser->tab_strip_model(); 1468 TabStripModel* target_tab_strip = target_browser->tab_strip_model();
1475 contents = source_tab_strip->DetachTabContentsAt(tab_index); 1469 TabContents* tab_contents =
1476 if (!contents) { 1470 source_tab_strip->DetachTabContentsAt(tab_index);
1471 if (!tab_contents) {
1477 error_ = ExtensionErrorUtils::FormatErrorMessage( 1472 error_ = ExtensionErrorUtils::FormatErrorMessage(
1478 keys::kTabNotFoundError, base::IntToString(tab_ids[i])); 1473 keys::kTabNotFoundError, base::IntToString(tab_ids[i]));
1479 return false; 1474 return false;
1480 } 1475 }
1481 1476
1482 // Clamp move location to the last position. 1477 // Clamp move location to the last position.
1483 // This is ">" because it can append to a new index position. 1478 // This is ">" because it can append to a new index position.
1484 // -1 means set the move location to the last position. 1479 // -1 means set the move location to the last position.
1485 if (new_index > target_tab_strip->count() || new_index < 0) 1480 if (new_index > target_tab_strip->count() || new_index < 0)
1486 new_index = target_tab_strip->count(); 1481 new_index = target_tab_strip->count();
1487 1482
1488 target_tab_strip->InsertTabContentsAt( 1483 target_tab_strip->InsertTabContentsAt(
1489 new_index, contents, TabStripModel::ADD_NONE); 1484 new_index, tab_contents, TabStripModel::ADD_NONE);
1490 1485
1491 if (has_callback()) { 1486 if (has_callback()) {
1492 tab_values.Append(ExtensionTabUtil::CreateTabValue( 1487 tab_values.Append(ExtensionTabUtil::CreateTabValue(
1493 contents->web_contents(), 1488 tab_contents->web_contents(),
1494 target_tab_strip, 1489 target_tab_strip,
1495 new_index, 1490 new_index,
1496 GetExtension())); 1491 GetExtension()));
1497 } 1492 }
1498 1493
1499 continue; 1494 continue;
1500 } 1495 }
1501 } 1496 }
1502 1497
1503 // Perform a simple within-window move. 1498 // Perform a simple within-window move.
1504 // Clamp move location to the last position. 1499 // Clamp move location to the last position.
1505 // This is ">=" because the move must be to an existing location. 1500 // This is ">=" because the move must be to an existing location.
1506 // -1 means set the move location to the last position. 1501 // -1 means set the move location to the last position.
1507 if (new_index >= source_tab_strip->count() || new_index < 0) 1502 if (new_index >= source_tab_strip->count() || new_index < 0)
1508 new_index = source_tab_strip->count() - 1; 1503 new_index = source_tab_strip->count() - 1;
1509 1504
1510 if (new_index != tab_index) 1505 if (new_index != tab_index)
1511 source_tab_strip->MoveTabContentsAt(tab_index, new_index, false); 1506 source_tab_strip->MoveTabContentsAt(tab_index, new_index, false);
1512 1507
1513 if (has_callback()) { 1508 if (has_callback()) {
1514 tab_values.Append(ExtensionTabUtil::CreateTabValue( 1509 tab_values.Append(ExtensionTabUtil::CreateTabValue(
1515 contents->web_contents(), source_tab_strip, new_index, 1510 contents, source_tab_strip, new_index, GetExtension()));
1516 GetExtension()));
1517 } 1511 }
1518 } 1512 }
1519 1513
1520 if (!has_callback()) 1514 if (!has_callback())
1521 return true; 1515 return true;
1522 1516
1523 // Only return the results as an array if there are multiple tabs. 1517 // Only return the results as an array if there are multiple tabs.
1524 if (tab_ids.size() > 1) { 1518 if (tab_ids.size() > 1) {
1525 SetResult(tab_values.DeepCopy()); 1519 SetResult(tab_values.DeepCopy());
1526 } else if (tab_ids.size() == 1) { 1520 } else if (tab_ids.size() == 1) {
(...skipping 10 matching lines...) Expand all
1537 DictionaryValue* reload_props = NULL; 1531 DictionaryValue* reload_props = NULL;
1538 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &reload_props)); 1532 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &reload_props));
1539 1533
1540 if (reload_props->HasKey(keys::kBypassCache)) { 1534 if (reload_props->HasKey(keys::kBypassCache)) {
1541 EXTENSION_FUNCTION_VALIDATE(reload_props->GetBoolean( 1535 EXTENSION_FUNCTION_VALIDATE(reload_props->GetBoolean(
1542 keys::kBypassCache, 1536 keys::kBypassCache,
1543 &bypass_cache)); 1537 &bypass_cache));
1544 } 1538 }
1545 } 1539 }
1546 1540
1547 TabContents* contents = NULL; 1541 content::WebContents* web_contents = NULL;
1548 1542
1549 // If |tab_id| is specified, look for it. Otherwise default to selected tab 1543 // If |tab_id| is specified, look for it. Otherwise default to selected tab
1550 // in the current window. 1544 // in the current window.
1551 Value* tab_value = NULL; 1545 Value* tab_value = NULL;
1552 if (HasOptionalArgument(0)) 1546 if (HasOptionalArgument(0))
1553 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); 1547 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value));
1554 1548
1555 if (tab_value == NULL || tab_value->IsType(Value::TYPE_NULL)) { 1549 if (tab_value == NULL || tab_value->IsType(Value::TYPE_NULL)) {
1556 Browser* browser = GetCurrentBrowser(); 1550 Browser* browser = GetCurrentBrowser();
1557 if (!browser) { 1551 if (!browser) {
1558 error_ = keys::kNoCurrentWindowError; 1552 error_ = keys::kNoCurrentWindowError;
1559 return false; 1553 return false;
1560 } 1554 }
1561 1555
1562 if (!ExtensionTabUtil::GetDefaultTab(browser, &contents, NULL)) 1556 if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, NULL))
1563 return false; 1557 return false;
1564 } else { 1558 } else {
1565 int tab_id = -1; 1559 int tab_id = -1;
1566 EXTENSION_FUNCTION_VALIDATE(tab_value->GetAsInteger(&tab_id)); 1560 EXTENSION_FUNCTION_VALIDATE(tab_value->GetAsInteger(&tab_id));
1567 1561
1568 Browser* browser = NULL; 1562 Browser* browser = NULL;
1569 if (!GetTabById(tab_id, profile(), include_incognito(), 1563 if (!GetTabById(tab_id, profile(), include_incognito(),
1570 &browser, NULL, &contents, NULL, &error_)) 1564 &browser, NULL, &web_contents, NULL, &error_))
1571 return false; 1565 return false;
1572 } 1566 }
1573 1567
1574 WebContents* web_contents = contents->web_contents();
1575 if (web_contents->ShowingInterstitialPage()) { 1568 if (web_contents->ShowingInterstitialPage()) {
1576 // This does as same as Browser::ReloadInternal. 1569 // This does as same as Browser::ReloadInternal.
1577 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); 1570 NavigationEntry* entry = web_contents->GetController().GetActiveEntry();
1578 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB, 1571 OpenURLParams params(entry->GetURL(), Referrer(), CURRENT_TAB,
1579 content::PAGE_TRANSITION_RELOAD, false); 1572 content::PAGE_TRANSITION_RELOAD, false);
1580 GetCurrentBrowser()->OpenURL(params); 1573 GetCurrentBrowser()->OpenURL(params);
1581 } else if (bypass_cache) { 1574 } else if (bypass_cache) {
1582 web_contents->GetController().ReloadIgnoringCache(true); 1575 web_contents->GetController().ReloadIgnoringCache(true);
1583 } else { 1576 } else {
1584 web_contents->GetController().Reload(true); 1577 web_contents->GetController().Reload(true);
1585 } 1578 }
1586 1579
1587 return true; 1580 return true;
1588 } 1581 }
1589 1582
1590 bool RemoveTabsFunction::RunImpl() { 1583 bool RemoveTabsFunction::RunImpl() {
1591 Value* tab_value = NULL; 1584 Value* tab_value = NULL;
1592 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); 1585 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value));
1593 1586
1594 std::vector<int> tab_ids; 1587 std::vector<int> tab_ids;
1595 EXTENSION_FUNCTION_VALIDATE(extensions::ReadOneOrMoreIntegers( 1588 EXTENSION_FUNCTION_VALIDATE(extensions::ReadOneOrMoreIntegers(
1596 tab_value, &tab_ids)); 1589 tab_value, &tab_ids));
1597 1590
1598 for (size_t i = 0; i < tab_ids.size(); ++i) { 1591 for (size_t i = 0; i < tab_ids.size(); ++i) {
1599 Browser* browser = NULL; 1592 Browser* browser = NULL;
1600 TabContents* contents = NULL; 1593 WebContents* contents = NULL;
1601 if (!GetTabById(tab_ids[i], profile(), include_incognito(), 1594 if (!GetTabById(tab_ids[i], profile(), include_incognito(),
1602 &browser, NULL, &contents, NULL, &error_)) 1595 &browser, NULL, &contents, NULL, &error_))
1603 return false; 1596 return false;
1604 1597
1605 // Don't let the extension remove a tab if the user is dragging tabs around. 1598 // Don't let the extension remove a tab if the user is dragging tabs around.
1606 if (!chrome::IsTabStripEditable(browser)) { 1599 if (!chrome::IsTabStripEditable(browser)) {
1607 error_ = keys::kTabStripNotEditableError; 1600 error_ = keys::kTabStripNotEditableError;
1608 return false; 1601 return false;
1609 } 1602 }
1610 1603
1611 // There's a chance that the tab is being dragged, or we're in some other 1604 // There's a chance that the tab is being dragged, or we're in some other
1612 // nested event loop. This code path ensures that the tab is safely closed 1605 // nested event loop. This code path ensures that the tab is safely closed
1613 // under such circumstances, whereas |chrome::CloseWebContents()| does not. 1606 // under such circumstances, whereas |chrome::CloseWebContents()| does not.
1614 contents->web_contents()->Close(); 1607 contents->Close();
1615 } 1608 }
1616 return true; 1609 return true;
1617 } 1610 }
1618 1611
1619 bool CaptureVisibleTabFunction::GetTabToCapture(WebContents** web_contents) { 1612 bool CaptureVisibleTabFunction::GetTabToCapture(WebContents** web_contents) {
1620 Browser* browser = NULL; 1613 Browser* browser = NULL;
1621 // windowId defaults to "current" window. 1614 // windowId defaults to "current" window.
1622 int window_id = extension_misc::kCurrentWindowId; 1615 int window_id = extension_misc::kCurrentWindowId;
1623 1616
1624 if (HasOptionalArgument(0)) 1617 if (HasOptionalArgument(0))
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
1799 } 1792 }
1800 1793
1801 void CaptureVisibleTabFunction::RegisterUserPrefs(PrefService* service) { 1794 void CaptureVisibleTabFunction::RegisterUserPrefs(PrefService* service) {
1802 service->RegisterBooleanPref(prefs::kDisableScreenshots, false, 1795 service->RegisterBooleanPref(prefs::kDisableScreenshots, false,
1803 PrefService::UNSYNCABLE_PREF); 1796 PrefService::UNSYNCABLE_PREF);
1804 } 1797 }
1805 1798
1806 bool DetectTabLanguageFunction::RunImpl() { 1799 bool DetectTabLanguageFunction::RunImpl() {
1807 int tab_id = 0; 1800 int tab_id = 0;
1808 Browser* browser = NULL; 1801 Browser* browser = NULL;
1809 TabContents* contents = NULL; 1802 WebContents* contents = NULL;
1810 1803
1811 // If |tab_id| is specified, look for it. Otherwise default to selected tab 1804 // If |tab_id| is specified, look for it. Otherwise default to selected tab
1812 // in the current window. 1805 // in the current window.
1813 if (HasOptionalArgument(0)) { 1806 if (HasOptionalArgument(0)) {
1814 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); 1807 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id));
1815 if (!GetTabById(tab_id, profile(), include_incognito(), 1808 if (!GetTabById(tab_id, profile(), include_incognito(),
1816 &browser, NULL, &contents, NULL, &error_)) { 1809 &browser, NULL, &contents, NULL, &error_)) {
1817 return false; 1810 return false;
1818 } 1811 }
1819 if (!browser || !contents) 1812 if (!browser || !contents)
1820 return false; 1813 return false;
1821 } else { 1814 } else {
1822 browser = GetCurrentBrowser(); 1815 browser = GetCurrentBrowser();
1823 if (!browser) 1816 if (!browser)
1824 return false; 1817 return false;
1825 contents = browser->tab_strip_model()->GetActiveTabContents(); 1818 contents = browser->tab_strip_model()->GetActiveWebContents();
1826 if (!contents) 1819 if (!contents)
1827 return false; 1820 return false;
1828 } 1821 }
1829 1822
1830 if (contents->web_contents()->GetController().NeedsReload()) { 1823 if (contents->GetController().NeedsReload()) {
1831 // If the tab hasn't been loaded, don't wait for the tab to load. 1824 // If the tab hasn't been loaded, don't wait for the tab to load.
1832 error_ = keys::kCannotDetermineLanguageOfUnloadedTab; 1825 error_ = keys::kCannotDetermineLanguageOfUnloadedTab;
1833 return false; 1826 return false;
1834 } 1827 }
1835 1828
1836 AddRef(); // Balanced in GotLanguage() 1829 AddRef(); // Balanced in GotLanguage().
1837 1830
1838 TranslateTabHelper* translate_tab_helper = 1831 TranslateTabHelper* translate_tab_helper =
1839 TranslateTabHelper::FromWebContents(contents->web_contents()); 1832 TranslateTabHelper::FromWebContents(contents);
1840 if (!translate_tab_helper->language_state().original_language().empty()) { 1833 if (!translate_tab_helper->language_state().original_language().empty()) {
1841 // Delay the callback invocation until after the current JS call has 1834 // Delay the callback invocation until after the current JS call has
1842 // returned. 1835 // returned.
1843 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( 1836 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
1844 &DetectTabLanguageFunction::GotLanguage, this, 1837 &DetectTabLanguageFunction::GotLanguage, this,
1845 translate_tab_helper->language_state().original_language())); 1838 translate_tab_helper->language_state().original_language()));
1846 return true; 1839 return true;
1847 } 1840 }
1848 // The tab contents does not know its language yet. Let's wait until it 1841 // The tab contents does not know its language yet. Let's wait until it
1849 // receives it, or until the tab is closed/navigates to some other page. 1842 // receives it, or until the tab is closed/navigates to some other page.
1850 registrar_.Add(this, chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, 1843 registrar_.Add(this, chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1851 content::Source<WebContents>(contents->web_contents())); 1844 content::Source<WebContents>(contents));
1852 registrar_.Add( 1845 registrar_.Add(
1853 this, chrome::NOTIFICATION_TAB_CLOSING, 1846 this, chrome::NOTIFICATION_TAB_CLOSING,
1854 content::Source<NavigationController>( 1847 content::Source<NavigationController>(&(contents->GetController())));
1855 &(contents->web_contents()->GetController())));
1856 registrar_.Add( 1848 registrar_.Add(
1857 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 1849 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
1858 content::Source<NavigationController>( 1850 content::Source<NavigationController>(&(contents->GetController())));
1859 &(contents->web_contents()->GetController())));
1860 return true; 1851 return true;
1861 } 1852 }
1862 1853
1863 void DetectTabLanguageFunction::Observe( 1854 void DetectTabLanguageFunction::Observe(
1864 int type, 1855 int type,
1865 const content::NotificationSource& source, 1856 const content::NotificationSource& source,
1866 const content::NotificationDetails& details) { 1857 const content::NotificationDetails& details) {
1867 std::string language; 1858 std::string language;
1868 if (type == chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED) 1859 if (type == chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED)
1869 language = *content::Details<std::string>(details).ptr(); 1860 language = *content::Details<std::string>(details).ptr();
1870 1861
1871 registrar_.RemoveAll(); 1862 registrar_.RemoveAll();
1872 1863
1873 // Call GotLanguage in all cases as we want to guarantee the callback is 1864 // Call GotLanguage in all cases as we want to guarantee the callback is
1874 // called for every API call the extension made. 1865 // called for every API call the extension made.
1875 GotLanguage(language); 1866 GotLanguage(language);
1876 } 1867 }
1877 1868
1878 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { 1869 void DetectTabLanguageFunction::GotLanguage(const std::string& language) {
1879 SetResult(Value::CreateStringValue(language.c_str())); 1870 SetResult(Value::CreateStringValue(language.c_str()));
1880 SendResponse(true); 1871 SendResponse(true);
1881 1872
1882 Release(); // Balanced in Run() 1873 Release(); // Balanced in Run()
1883 } 1874 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/tabs/tabs.h ('k') | chrome/browser/extensions/api/web_navigation/web_navigation_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698