| 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/api/tabs/tabs_api.h" | 5 #include "chrome/browser/extensions/api/tabs/tabs_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 1618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1629 } | 1629 } |
| 1630 | 1630 |
| 1631 void TabsDetectLanguageFunction::GotLanguage(const std::string& language) { | 1631 void TabsDetectLanguageFunction::GotLanguage(const std::string& language) { |
| 1632 SetResult(new base::StringValue(language.c_str())); | 1632 SetResult(new base::StringValue(language.c_str())); |
| 1633 SendResponse(true); | 1633 SendResponse(true); |
| 1634 | 1634 |
| 1635 Release(); // Balanced in Run() | 1635 Release(); // Balanced in Run() |
| 1636 } | 1636 } |
| 1637 | 1637 |
| 1638 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() | 1638 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() |
| 1639 : execute_tab_id_(-1) { | 1639 : chrome_details_(this), execute_tab_id_(-1) { |
| 1640 } | 1640 } |
| 1641 | 1641 |
| 1642 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() {} | 1642 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() {} |
| 1643 | 1643 |
| 1644 bool ExecuteCodeInTabFunction::HasPermission() { | 1644 bool ExecuteCodeInTabFunction::HasPermission() { |
| 1645 if (Init() && | 1645 if (Init() && |
| 1646 extension_->permissions_data()->HasAPIPermissionForTab( | 1646 extension_->permissions_data()->HasAPIPermissionForTab( |
| 1647 execute_tab_id_, APIPermission::kTab)) { | 1647 execute_tab_id_, APIPermission::kTab)) { |
| 1648 return true; | 1648 return true; |
| 1649 } | 1649 } |
| 1650 return ExtensionFunction::HasPermission(); | 1650 return ExtensionFunction::HasPermission(); |
| 1651 } | 1651 } |
| 1652 | 1652 |
| 1653 bool ExecuteCodeInTabFunction::Init() { |
| 1654 if (details_.get()) |
| 1655 return true; |
| 1656 |
| 1657 // |tab_id| is optional so it's ok if it's not there. |
| 1658 int tab_id = -1; |
| 1659 if (args_->GetInteger(0, &tab_id)) |
| 1660 EXTENSION_FUNCTION_VALIDATE(tab_id >= 0); |
| 1661 |
| 1662 // |details| are not optional. |
| 1663 base::DictionaryValue* details_value = NULL; |
| 1664 if (!args_->GetDictionary(1, &details_value)) |
| 1665 return false; |
| 1666 scoped_ptr<InjectDetails> details(new InjectDetails()); |
| 1667 if (!InjectDetails::Populate(*details_value, details.get())) |
| 1668 return false; |
| 1669 |
| 1670 // If the tab ID wasn't given then it needs to be converted to the |
| 1671 // currently active tab's ID. |
| 1672 if (tab_id == -1) { |
| 1673 Browser* browser = chrome_details_.GetCurrentBrowser(); |
| 1674 if (!browser) |
| 1675 return false; |
| 1676 content::WebContents* web_contents = NULL; |
| 1677 if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, &tab_id)) |
| 1678 return false; |
| 1679 } |
| 1680 |
| 1681 execute_tab_id_ = tab_id; |
| 1682 details_ = details.Pass(); |
| 1683 return true; |
| 1684 } |
| 1685 |
| 1653 bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage() { | 1686 bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage() { |
| 1654 content::WebContents* contents = NULL; | 1687 content::WebContents* contents = NULL; |
| 1655 | 1688 |
| 1656 // If |tab_id| is specified, look for the tab. Otherwise default to selected | 1689 // If |tab_id| is specified, look for the tab. Otherwise default to selected |
| 1657 // tab in the current window. | 1690 // tab in the current window. |
| 1658 CHECK_GE(execute_tab_id_, 0); | 1691 CHECK_GE(execute_tab_id_, 0); |
| 1659 if (!GetTabById(execute_tab_id_, | 1692 if (!GetTabById(execute_tab_id_, |
| 1660 GetProfile(), | 1693 chrome_details_.GetProfile(), |
| 1661 include_incognito(), | 1694 include_incognito(), |
| 1662 NULL, | 1695 NULL, |
| 1663 NULL, | 1696 NULL, |
| 1664 &contents, | 1697 &contents, |
| 1665 NULL, | 1698 NULL, |
| 1666 &error_)) { | 1699 &error_)) { |
| 1667 return false; | 1700 return false; |
| 1668 } | 1701 } |
| 1669 | 1702 |
| 1670 CHECK(contents); | 1703 CHECK(contents); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1683 } | 1716 } |
| 1684 | 1717 |
| 1685 return true; | 1718 return true; |
| 1686 } | 1719 } |
| 1687 | 1720 |
| 1688 ScriptExecutor* ExecuteCodeInTabFunction::GetScriptExecutor() { | 1721 ScriptExecutor* ExecuteCodeInTabFunction::GetScriptExecutor() { |
| 1689 Browser* browser = NULL; | 1722 Browser* browser = NULL; |
| 1690 content::WebContents* contents = NULL; | 1723 content::WebContents* contents = NULL; |
| 1691 | 1724 |
| 1692 bool success = GetTabById(execute_tab_id_, | 1725 bool success = GetTabById(execute_tab_id_, |
| 1693 GetProfile(), | 1726 chrome_details_.GetProfile(), |
| 1694 include_incognito(), | 1727 include_incognito(), |
| 1695 &browser, | 1728 &browser, |
| 1696 NULL, | 1729 NULL, |
| 1697 &contents, | 1730 &contents, |
| 1698 NULL, | 1731 NULL, |
| 1699 &error_) && | 1732 &error_) && |
| 1700 contents && browser; | 1733 contents && browser; |
| 1701 | 1734 |
| 1702 if (!success) | 1735 if (!success) |
| 1703 return NULL; | 1736 return NULL; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1719 | 1752 |
| 1720 void TabsExecuteScriptFunction::OnExecuteCodeFinished( | 1753 void TabsExecuteScriptFunction::OnExecuteCodeFinished( |
| 1721 const std::string& error, | 1754 const std::string& error, |
| 1722 const GURL& on_url, | 1755 const GURL& on_url, |
| 1723 const base::ListValue& result) { | 1756 const base::ListValue& result) { |
| 1724 if (error.empty()) | 1757 if (error.empty()) |
| 1725 SetResult(result.DeepCopy()); | 1758 SetResult(result.DeepCopy()); |
| 1726 ExecuteCodeInTabFunction::OnExecuteCodeFinished(error, on_url, result); | 1759 ExecuteCodeInTabFunction::OnExecuteCodeFinished(error, on_url, result); |
| 1727 } | 1760 } |
| 1728 | 1761 |
| 1729 bool ExecuteCodeInTabFunction::Init() { | |
| 1730 if (details_.get()) | |
| 1731 return true; | |
| 1732 | |
| 1733 // |tab_id| is optional so it's ok if it's not there. | |
| 1734 int tab_id = -1; | |
| 1735 if (args_->GetInteger(0, &tab_id)) | |
| 1736 EXTENSION_FUNCTION_VALIDATE(tab_id >= 0); | |
| 1737 | |
| 1738 // |details| are not optional. | |
| 1739 base::DictionaryValue* details_value = NULL; | |
| 1740 if (!args_->GetDictionary(1, &details_value)) | |
| 1741 return false; | |
| 1742 scoped_ptr<InjectDetails> details(new InjectDetails()); | |
| 1743 if (!InjectDetails::Populate(*details_value, details.get())) | |
| 1744 return false; | |
| 1745 | |
| 1746 // If the tab ID wasn't given then it needs to be converted to the | |
| 1747 // currently active tab's ID. | |
| 1748 if (tab_id == -1) { | |
| 1749 Browser* browser = GetCurrentBrowser(); | |
| 1750 if (!browser) | |
| 1751 return false; | |
| 1752 content::WebContents* web_contents = NULL; | |
| 1753 if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, &tab_id)) | |
| 1754 return false; | |
| 1755 } | |
| 1756 | |
| 1757 execute_tab_id_ = tab_id; | |
| 1758 details_ = details.Pass(); | |
| 1759 return true; | |
| 1760 } | |
| 1761 | |
| 1762 bool TabsInsertCSSFunction::ShouldInsertCSS() const { | 1762 bool TabsInsertCSSFunction::ShouldInsertCSS() const { |
| 1763 return true; | 1763 return true; |
| 1764 } | 1764 } |
| 1765 | 1765 |
| 1766 content::WebContents* ZoomAPIFunction::GetWebContents(int tab_id) { | 1766 content::WebContents* ZoomAPIFunction::GetWebContents(int tab_id) { |
| 1767 content::WebContents* web_contents = NULL; | 1767 content::WebContents* web_contents = NULL; |
| 1768 if (tab_id != -1) { | 1768 if (tab_id != -1) { |
| 1769 // We assume this call leaves web_contents unchanged if it is unsuccessful. | 1769 // We assume this call leaves web_contents unchanged if it is unsuccessful. |
| 1770 GetTabById(tab_id, | 1770 GetTabById(tab_id, |
| 1771 GetProfile(), | 1771 GetProfile(), |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1898 ZoomController::ZoomMode zoom_mode = zoom_controller->zoom_mode(); | 1898 ZoomController::ZoomMode zoom_mode = zoom_controller->zoom_mode(); |
| 1899 api::tabs::ZoomSettings zoom_settings; | 1899 api::tabs::ZoomSettings zoom_settings; |
| 1900 ZoomModeToZoomSettings(zoom_mode, &zoom_settings); | 1900 ZoomModeToZoomSettings(zoom_mode, &zoom_settings); |
| 1901 | 1901 |
| 1902 results_ = api::tabs::GetZoomSettings::Results::Create(zoom_settings); | 1902 results_ = api::tabs::GetZoomSettings::Results::Create(zoom_settings); |
| 1903 SendResponse(true); | 1903 SendResponse(true); |
| 1904 return true; | 1904 return true; |
| 1905 } | 1905 } |
| 1906 | 1906 |
| 1907 } // namespace extensions | 1907 } // namespace extensions |
| OLD | NEW |