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

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

Issue 2630753003: Separate validation failures from other failures in ExecuteCodeFunction. (Closed)
Patch Set: address comments Created 3 years, 11 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
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_api.h" 5 #include "chrome/browser/extensions/api/tabs/tabs_api.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <limits> 9 #include <limits>
10 #include <memory> 10 #include <memory>
(...skipping 1777 matching lines...) Expand 10 before | Expand all | Expand 10 after
1788 Release(); // Balanced in Run() 1788 Release(); // Balanced in Run()
1789 } 1789 }
1790 1790
1791 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() 1791 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction()
1792 : chrome_details_(this), execute_tab_id_(-1) { 1792 : chrome_details_(this), execute_tab_id_(-1) {
1793 } 1793 }
1794 1794
1795 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() {} 1795 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() {}
1796 1796
1797 bool ExecuteCodeInTabFunction::HasPermission() { 1797 bool ExecuteCodeInTabFunction::HasPermission() {
1798 if (Init() && 1798 if (Init() == SUCCESS &&
1799 // TODO(devlin/lazyboy): Consider removing the following check as it isn't
1800 // doing anything. The fallback to ExtensionFunction::HasPermission()
1801 // below dictates what this function returns.
1799 extension_->permissions_data()->HasAPIPermissionForTab( 1802 extension_->permissions_data()->HasAPIPermissionForTab(
1800 execute_tab_id_, APIPermission::kTab)) { 1803 execute_tab_id_, APIPermission::kTab)) {
1801 return true; 1804 return true;
1802 } 1805 }
1803 return ExtensionFunction::HasPermission(); 1806 return ExtensionFunction::HasPermission();
1804 } 1807 }
1805 1808
1806 bool ExecuteCodeInTabFunction::Init() { 1809 ExecuteCodeFunction::InitResult ExecuteCodeInTabFunction::Init() {
1807 if (details_.get()) 1810 if (init_result_)
1808 return true; 1811 return init_result_.value();
1809 1812
1810 // |tab_id| is optional so it's ok if it's not there. 1813 // |tab_id| is optional so it's ok if it's not there.
1811 int tab_id = -1; 1814 int tab_id = -1;
1812 if (args_->GetInteger(0, &tab_id)) 1815 if (args_->GetInteger(0, &tab_id) && tab_id < 0)
1813 EXTENSION_FUNCTION_VALIDATE(tab_id >= 0); 1816 return set_init_result(VALIDATION_FAILURE);
1814 1817
1815 // |details| are not optional. 1818 // |details| are not optional.
1816 base::DictionaryValue* details_value = NULL; 1819 base::DictionaryValue* details_value = NULL;
1817 if (!args_->GetDictionary(1, &details_value)) 1820 if (!args_->GetDictionary(1, &details_value))
1818 return false; 1821 return set_init_result(VALIDATION_FAILURE);
1819 std::unique_ptr<InjectDetails> details(new InjectDetails()); 1822 std::unique_ptr<InjectDetails> details(new InjectDetails());
1820 if (!InjectDetails::Populate(*details_value, details.get())) 1823 if (!InjectDetails::Populate(*details_value, details.get()))
1821 return false; 1824 return set_init_result(VALIDATION_FAILURE);
1822 1825
1823 // If the tab ID wasn't given then it needs to be converted to the 1826 // If the tab ID wasn't given then it needs to be converted to the
1824 // currently active tab's ID. 1827 // currently active tab's ID.
1825 if (tab_id == -1) { 1828 if (tab_id == -1) {
1826 Browser* browser = chrome_details_.GetCurrentBrowser(); 1829 Browser* browser = chrome_details_.GetCurrentBrowser();
1830 // Can happen during shutdown.
1827 if (!browser) 1831 if (!browser)
1828 return false; 1832 return set_init_result_error(keys::kNoCurrentWindowError);
1829 content::WebContents* web_contents = NULL; 1833 content::WebContents* web_contents = NULL;
1834 // Can happen during shutdown.
1830 if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, &tab_id)) 1835 if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, &tab_id))
1831 return false; 1836 return set_init_result_error(keys::kNoTabInBrowserWindowError);
1832 } 1837 }
1833 1838
1834 execute_tab_id_ = tab_id; 1839 execute_tab_id_ = tab_id;
1835 details_ = std::move(details); 1840 details_ = std::move(details);
1836 set_host_id(HostID(HostID::EXTENSIONS, extension()->id())); 1841 set_host_id(HostID(HostID::EXTENSIONS, extension()->id()));
1837 return true; 1842 return set_init_result(SUCCESS);
1838 } 1843 }
1839 1844
1840 bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage() { 1845 bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage() {
1841 content::WebContents* contents = NULL; 1846 content::WebContents* contents = NULL;
1842 1847
1843 // If |tab_id| is specified, look for the tab. Otherwise default to selected 1848 // If |tab_id| is specified, look for the tab. Otherwise default to selected
1844 // tab in the current window. 1849 // tab in the current window.
1845 CHECK_GE(execute_tab_id_, 0); 1850 CHECK_GE(execute_tab_id_, 0);
1846 if (!GetTabById(execute_tab_id_, browser_context(), include_incognito(), 1851 if (!GetTabById(execute_tab_id_, browser_context(), include_incognito(),
1847 nullptr, nullptr, &contents, nullptr, &error_)) { 1852 nullptr, nullptr, &contents, nullptr, &error_)) {
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
2110 params->tab_id 2115 params->tab_id
2111 ? ErrorUtils::FormatErrorMessage(keys::kCannotDiscardTab, 2116 ? ErrorUtils::FormatErrorMessage(keys::kCannotDiscardTab,
2112 base::IntToString(*params->tab_id)) 2117 base::IntToString(*params->tab_id))
2113 : keys::kCannotFindTabToDiscard)); 2118 : keys::kCannotFindTabToDiscard));
2114 } 2119 }
2115 2120
2116 TabsDiscardFunction::TabsDiscardFunction() {} 2121 TabsDiscardFunction::TabsDiscardFunction() {}
2117 TabsDiscardFunction::~TabsDiscardFunction() {} 2122 TabsDiscardFunction::~TabsDiscardFunction() {}
2118 2123
2119 } // namespace extensions 2124 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/tabs/tabs_api.h ('k') | chrome/browser/extensions/api/tabs/tabs_api_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698