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 <limits> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/base64.h" | 11 #include "base/base64.h" |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
14 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
16 #include "base/string16.h" | 17 #include "base/string16.h" |
17 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
(...skipping 1384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1402 } | 1403 } |
1403 | 1404 |
1404 bool MoveTabsFunction::RunImpl() { | 1405 bool MoveTabsFunction::RunImpl() { |
1405 Value* tab_value = NULL; | 1406 Value* tab_value = NULL; |
1406 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); | 1407 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); |
1407 | 1408 |
1408 std::vector<int> tab_ids; | 1409 std::vector<int> tab_ids; |
1409 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_ids)); | 1410 EXTENSION_FUNCTION_VALIDATE(ReadOneOrMoreIntegers(tab_value, &tab_ids)); |
1410 | 1411 |
1411 DictionaryValue* update_props = NULL; | 1412 DictionaryValue* update_props = NULL; |
| 1413 int new_index; |
1412 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); | 1414 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); |
1413 | 1415 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger(keys::kIndexKey, |
1414 int new_index = -1; | 1416 &new_index)); |
1415 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( | |
1416 keys::kIndexKey, &new_index)); | |
1417 EXTENSION_FUNCTION_VALIDATE(new_index >= 0); | |
1418 | 1417 |
1419 ListValue tab_values; | 1418 ListValue tab_values; |
1420 for (size_t i = 0; i < tab_ids.size(); ++i) { | 1419 for (size_t i = 0; i < tab_ids.size(); ++i) { |
1421 Browser* source_browser = NULL; | 1420 Browser* source_browser = NULL; |
1422 TabStripModel* source_tab_strip = NULL; | 1421 TabStripModel* source_tab_strip = NULL; |
1423 TabContentsWrapper* contents = NULL; | 1422 TabContentsWrapper* contents = NULL; |
1424 int tab_index = -1; | 1423 int tab_index = -1; |
1425 if (!GetTabById(tab_ids[i], profile(), include_incognito(), | 1424 if (!GetTabById(tab_ids[i], profile(), include_incognito(), |
1426 &source_browser, &source_tab_strip, &contents, | 1425 &source_browser, &source_tab_strip, &contents, |
1427 &tab_index, &error_)) | 1426 &tab_index, &error_)) |
1428 return false; | 1427 return false; |
1429 | 1428 |
1430 // Don't let the extension move the tab if the user is dragging tabs. | 1429 // Don't let the extension move the tab if the user is dragging tabs. |
1431 if (!source_browser->IsTabStripEditable()) { | 1430 if (!source_browser->IsTabStripEditable()) { |
1432 error_ = keys::kTabStripNotEditableError; | 1431 error_ = keys::kTabStripNotEditableError; |
1433 return false; | 1432 return false; |
1434 } | 1433 } |
1435 | 1434 |
1436 // Insert the tabs one after another. | 1435 // Insert the tabs one after another. |
1437 new_index += i; | 1436 new_index += i; |
1438 | 1437 |
1439 if (update_props->HasKey(keys::kWindowIdKey)) { | 1438 if (update_props->HasKey(keys::kWindowIdKey)) { |
1440 Browser* target_browser = NULL; | 1439 Browser* target_browser = NULL; |
1441 int window_id = extension_misc::kUnknownWindowId; | 1440 int window_id = extension_misc::kUnknownWindowId; |
1442 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( | 1441 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( |
1443 keys::kWindowIdKey, &window_id)); | 1442 keys::kWindowIdKey, &window_id)); |
| 1443 |
1444 if (!GetBrowserFromWindowID(this, window_id, &target_browser)) | 1444 if (!GetBrowserFromWindowID(this, window_id, &target_browser)) |
1445 return false; | 1445 return false; |
1446 | 1446 |
1447 if (!target_browser->IsTabStripEditable()) { | 1447 if (!target_browser->IsTabStripEditable()) { |
1448 error_ = keys::kTabStripNotEditableError; | 1448 error_ = keys::kTabStripNotEditableError; |
1449 return false; | 1449 return false; |
1450 } | 1450 } |
1451 | 1451 |
1452 if (!target_browser->is_type_tabbed()) { | 1452 if (!target_browser->is_type_tabbed()) { |
1453 error_ = keys::kCanOnlyMoveTabsWithinNormalWindowsError; | 1453 error_ = keys::kCanOnlyMoveTabsWithinNormalWindowsError; |
(...skipping 11 matching lines...) Expand all Loading... |
1465 TabStripModel* target_tab_strip = target_browser->tabstrip_model(); | 1465 TabStripModel* target_tab_strip = target_browser->tabstrip_model(); |
1466 contents = source_tab_strip->DetachTabContentsAt(tab_index); | 1466 contents = source_tab_strip->DetachTabContentsAt(tab_index); |
1467 if (!contents) { | 1467 if (!contents) { |
1468 error_ = ExtensionErrorUtils::FormatErrorMessage( | 1468 error_ = ExtensionErrorUtils::FormatErrorMessage( |
1469 keys::kTabNotFoundError, base::IntToString(tab_ids[i])); | 1469 keys::kTabNotFoundError, base::IntToString(tab_ids[i])); |
1470 return false; | 1470 return false; |
1471 } | 1471 } |
1472 | 1472 |
1473 // Clamp move location to the last position. | 1473 // Clamp move location to the last position. |
1474 // This is ">" because it can append to a new index position. | 1474 // This is ">" because it can append to a new index position. |
1475 if (new_index > target_tab_strip->count()) | 1475 // -1 means set the move location to the last position. |
| 1476 if (new_index > target_tab_strip->count() || new_index < 0) |
1476 new_index = target_tab_strip->count(); | 1477 new_index = target_tab_strip->count(); |
1477 | 1478 |
1478 target_tab_strip->InsertTabContentsAt( | 1479 target_tab_strip->InsertTabContentsAt( |
1479 new_index, contents, TabStripModel::ADD_NONE); | 1480 new_index, contents, TabStripModel::ADD_NONE); |
1480 | 1481 |
1481 if (has_callback()) | 1482 if (has_callback()) |
1482 tab_values.Append(ExtensionTabUtil::CreateTabValue( | 1483 tab_values.Append(ExtensionTabUtil::CreateTabValue( |
1483 contents->web_contents(), target_tab_strip, new_index)); | 1484 contents->web_contents(), target_tab_strip, new_index)); |
1484 | 1485 |
1485 continue; | 1486 continue; |
1486 } | 1487 } |
1487 } | 1488 } |
1488 | 1489 |
1489 // Perform a simple within-window move. | 1490 // Perform a simple within-window move. |
1490 // Clamp move location to the last position. | 1491 // Clamp move location to the last position. |
1491 // This is ">=" because the move must be to an existing location. | 1492 // This is ">=" because the move must be to an existing location. |
1492 if (new_index >= source_tab_strip->count()) | 1493 // -1 means set the move location to the last position. |
| 1494 if (new_index >= source_tab_strip->count() || new_index < 0) |
1493 new_index = source_tab_strip->count() - 1; | 1495 new_index = source_tab_strip->count() - 1; |
1494 | 1496 |
1495 if (new_index != tab_index) | 1497 if (new_index != tab_index) |
1496 source_tab_strip->MoveTabContentsAt(tab_index, new_index, false); | 1498 source_tab_strip->MoveTabContentsAt(tab_index, new_index, false); |
1497 | 1499 |
1498 if (has_callback()) | 1500 if (has_callback()) |
1499 tab_values.Append(ExtensionTabUtil::CreateTabValue( | 1501 tab_values.Append(ExtensionTabUtil::CreateTabValue( |
1500 contents->web_contents(), source_tab_strip, new_index)); | 1502 contents->web_contents(), source_tab_strip, new_index)); |
1501 } | 1503 } |
1502 | 1504 |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1848 // called for every API call the extension made. | 1850 // called for every API call the extension made. |
1849 GotLanguage(language); | 1851 GotLanguage(language); |
1850 } | 1852 } |
1851 | 1853 |
1852 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { | 1854 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { |
1853 result_.reset(Value::CreateStringValue(language.c_str())); | 1855 result_.reset(Value::CreateStringValue(language.c_str())); |
1854 SendResponse(true); | 1856 SendResponse(true); |
1855 | 1857 |
1856 Release(); // Balanced in Run() | 1858 Release(); // Balanced in Run() |
1857 } | 1859 } |
OLD | NEW |