| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/gfx/jpeg_codec.h" | 7 #include "base/gfx/jpeg_codec.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "chrome/browser/browser.h" | 9 #include "chrome/browser/browser.h" |
| 10 #include "chrome/browser/browser_list.h" | 10 #include "chrome/browser/browser_list.h" |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 // Look for optional url. | 253 // Look for optional url. |
| 254 if (!args_->IsType(Value::TYPE_NULL)) { | 254 if (!args_->IsType(Value::TYPE_NULL)) { |
| 255 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); | 255 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); |
| 256 const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); | 256 const DictionaryValue *args = static_cast<const DictionaryValue*>(args_); |
| 257 std::string url_input; | 257 std::string url_input; |
| 258 if (args->HasKey(keys::kUrlKey)) { | 258 if (args->HasKey(keys::kUrlKey)) { |
| 259 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, | 259 EXTENSION_FUNCTION_VALIDATE(args->GetString(keys::kUrlKey, |
| 260 &url_input)); | 260 &url_input)); |
| 261 url.reset(new GURL(url_input)); | 261 url.reset(new GURL(url_input)); |
| 262 if (!url->is_valid()) { | 262 if (!url->is_valid()) { |
| 263 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError, | 263 // The path as passed in is not valid. Try converting to absolute path. |
| 264 url_input); | 264 *url = GetExtension()->GetResourceURL(url_input); |
| 265 return false; | 265 if (!url->is_valid()) { |
| 266 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 267 keys::kInvalidUrlError, |
| 268 url_input); |
| 269 return false; |
| 270 } |
| 266 } | 271 } |
| 267 } | 272 } |
| 268 } | 273 } |
| 269 | 274 |
| 270 // Try to position the new browser relative its originating browser window. | 275 // Try to position the new browser relative its originating browser window. |
| 271 gfx::Rect empty_bounds; | 276 gfx::Rect empty_bounds; |
| 272 gfx::Rect bounds; | 277 gfx::Rect bounds; |
| 273 bool maximized; | 278 bool maximized; |
| 274 // The call offsets the bounds by kWindowTilePixels (defined in WindowSizer to | 279 // The call offsets the bounds by kWindowTilePixels (defined in WindowSizer to |
| 275 // be 10) | 280 // be 10) |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 if (update_props->HasKey(keys::kSelectedKey)) { | 596 if (update_props->HasKey(keys::kSelectedKey)) { |
| 592 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( | 597 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( |
| 593 keys::kSelectedKey, | 598 keys::kSelectedKey, |
| 594 &selected)); | 599 &selected)); |
| 595 if (selected && tab_strip->selected_index() != tab_index) { | 600 if (selected && tab_strip->selected_index() != tab_index) { |
| 596 tab_strip->SelectTabContentsAt(tab_index, false); | 601 tab_strip->SelectTabContentsAt(tab_index, false); |
| 597 DCHECK_EQ(contents, tab_strip->GetSelectedTabContents()); | 602 DCHECK_EQ(contents, tab_strip->GetSelectedTabContents()); |
| 598 } | 603 } |
| 599 } | 604 } |
| 600 | 605 |
| 606 if (has_callback()) |
| 607 result_.reset(ExtensionTabUtil::CreateTabValue(contents, tab_strip, |
| 608 tab_index)); |
| 609 |
| 601 return true; | 610 return true; |
| 602 } | 611 } |
| 603 | 612 |
| 604 bool MoveTabFunction::RunImpl() { | 613 bool MoveTabFunction::RunImpl() { |
| 605 int tab_id; | 614 int tab_id; |
| 606 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); | 615 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); |
| 607 const ListValue* args = static_cast<const ListValue*>(args_); | 616 const ListValue* args = static_cast<const ListValue*>(args_); |
| 608 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(0, &tab_id)); | 617 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(0, &tab_id)); |
| 609 DictionaryValue* update_props; | 618 DictionaryValue* update_props; |
| 610 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &update_props)); | 619 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &update_props)); |
| 611 | 620 |
| 612 int new_index; | 621 int new_index; |
| 613 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( | 622 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( |
| 614 keys::kIndexKey, &new_index)); | 623 keys::kIndexKey, &new_index)); |
| 615 EXTENSION_FUNCTION_VALIDATE(new_index >= 0); | 624 EXTENSION_FUNCTION_VALIDATE(new_index >= 0); |
| 616 | 625 |
| 617 Browser* source_browser = NULL; | 626 Browser* source_browser = NULL; |
| 618 TabStripModel* source_tab_strip = NULL; | 627 TabStripModel* source_tab_strip = NULL; |
| 628 TabContents* contents = NULL; |
| 619 int tab_index = -1; | 629 int tab_index = -1; |
| 620 if (!GetTabById(tab_id, profile(), &source_browser, &source_tab_strip, NULL, | 630 if (!GetTabById(tab_id, profile(), &source_browser, &source_tab_strip, |
| 621 &tab_index, &error_)) | 631 &contents, &tab_index, &error_)) |
| 622 return false; | 632 return false; |
| 623 | 633 |
| 624 if (update_props->HasKey(keys::kWindowIdKey)) { | 634 if (update_props->HasKey(keys::kWindowIdKey)) { |
| 625 Browser* target_browser; | 635 Browser* target_browser; |
| 626 int window_id; | 636 int window_id; |
| 627 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( | 637 EXTENSION_FUNCTION_VALIDATE(update_props->GetInteger( |
| 628 keys::kWindowIdKey, &window_id)); | 638 keys::kWindowIdKey, &window_id)); |
| 629 target_browser = GetBrowserInProfileWithId(profile(), window_id, | 639 target_browser = GetBrowserInProfileWithId(profile(), window_id, |
| 630 &error_); | 640 &error_); |
| 631 if (!target_browser) | 641 if (!target_browser) |
| 632 return false; | 642 return false; |
| 633 | 643 |
| 634 // If windowId is different from the current window, move between windows. | 644 // If windowId is different from the current window, move between windows. |
| 635 if (ExtensionTabUtil::GetWindowId(target_browser) != | 645 if (ExtensionTabUtil::GetWindowId(target_browser) != |
| 636 ExtensionTabUtil::GetWindowId(source_browser)) { | 646 ExtensionTabUtil::GetWindowId(source_browser)) { |
| 637 TabStripModel* target_tab_strip = target_browser->tabstrip_model(); | 647 TabStripModel* target_tab_strip = target_browser->tabstrip_model(); |
| 638 TabContents *contents = source_tab_strip->DetachTabContentsAt(tab_index); | 648 contents = source_tab_strip->DetachTabContentsAt(tab_index); |
| 639 if (!contents) { | 649 if (!contents) { |
| 640 error_ = ExtensionErrorUtils::FormatErrorMessage( | 650 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 641 keys::kTabNotFoundError, IntToString(tab_id)); | 651 keys::kTabNotFoundError, IntToString(tab_id)); |
| 642 return false; | 652 return false; |
| 643 } | 653 } |
| 644 | 654 |
| 645 // Clamp move location to the last position. | 655 // Clamp move location to the last position. |
| 646 // This is ">" because it can append to a new index position. | 656 // This is ">" because it can append to a new index position. |
| 647 if (new_index > target_tab_strip->count()) | 657 if (new_index > target_tab_strip->count()) |
| 648 new_index = target_tab_strip->count(); | 658 new_index = target_tab_strip->count(); |
| 649 | 659 |
| 650 target_tab_strip->InsertTabContentsAt(new_index, contents, | 660 target_tab_strip->InsertTabContentsAt(new_index, contents, |
| 651 false, true); | 661 false, true); |
| 652 | 662 |
| 663 if (has_callback()) |
| 664 result_.reset(ExtensionTabUtil::CreateTabValue(contents, |
| 665 target_tab_strip, new_index)); |
| 666 |
| 653 return true; | 667 return true; |
| 654 } | 668 } |
| 655 } | 669 } |
| 656 | 670 |
| 657 // Perform a simple within-window move. | 671 // Perform a simple within-window move. |
| 658 // Clamp move location to the last position. | 672 // Clamp move location to the last position. |
| 659 // This is ">=" because the move must be to an existing location. | 673 // This is ">=" because the move must be to an existing location. |
| 660 if (new_index >= source_tab_strip->count()) | 674 if (new_index >= source_tab_strip->count()) |
| 661 new_index = source_tab_strip->count() - 1; | 675 new_index = source_tab_strip->count() - 1; |
| 662 | 676 |
| 663 if (new_index != tab_index) | 677 if (new_index != tab_index) |
| 664 source_tab_strip->MoveTabContentsAt(tab_index, new_index, false); | 678 source_tab_strip->MoveTabContentsAt(tab_index, new_index, false); |
| 665 | 679 |
| 680 if (has_callback()) |
| 681 result_.reset(ExtensionTabUtil::CreateTabValue(contents, source_tab_strip, |
| 682 new_index)); |
| 666 return true; | 683 return true; |
| 667 } | 684 } |
| 668 | 685 |
| 669 | 686 |
| 670 bool RemoveTabFunction::RunImpl() { | 687 bool RemoveTabFunction::RunImpl() { |
| 671 int tab_id; | 688 int tab_id; |
| 672 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id)); | 689 EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id)); |
| 673 | 690 |
| 674 Browser* browser = NULL; | 691 Browser* browser = NULL; |
| 675 TabContents* contents = NULL; | 692 TabContents* contents = NULL; |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 851 if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip, | 868 if (ExtensionTabUtil::GetTabById(tab_id, profile, browser, tab_strip, |
| 852 contents, tab_index)) | 869 contents, tab_index)) |
| 853 return true; | 870 return true; |
| 854 | 871 |
| 855 if (error_message) | 872 if (error_message) |
| 856 *error_message = ExtensionErrorUtils::FormatErrorMessage( | 873 *error_message = ExtensionErrorUtils::FormatErrorMessage( |
| 857 keys::kTabNotFoundError, IntToString(tab_id)); | 874 keys::kTabNotFoundError, IntToString(tab_id)); |
| 858 | 875 |
| 859 return false; | 876 return false; |
| 860 } | 877 } |
| OLD | NEW |