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

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: . 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 &&
Devlin 2017/01/17 18:52:08 Per our offline discussion, I'm not sure this does
lazyboy 2017/01/18 02:29:10 I've checked an extension with only "activeTab" pe
Devlin 2017/01/24 00:11:43 The ActiveTabPermissionGranter grants both kTab an
lazyboy 2017/01/24 01:52:13 Ah ok. I've added TODO to remove this.
1799 extension_->permissions_data()->HasAPIPermissionForTab( 1799 extension_->permissions_data()->HasAPIPermissionForTab(
1800 execute_tab_id_, APIPermission::kTab)) { 1800 execute_tab_id_, APIPermission::kTab)) {
1801 return true; 1801 return true;
1802 } 1802 }
1803 return ExtensionFunction::HasPermission(); 1803 return ExtensionFunction::HasPermission();
1804 } 1804 }
1805 1805
1806 bool ExecuteCodeInTabFunction::Init() { 1806 ExecuteCodeFunction::InitResult ExecuteCodeInTabFunction::Init() {
1807 if (details_.get()) 1807 if (init_result_)
1808 return true; 1808 return init_result_.value();
1809 1809
1810 // |tab_id| is optional so it's ok if it's not there. 1810 // |tab_id| is optional so it's ok if it's not there.
1811 int tab_id = -1; 1811 int tab_id = -1;
1812 if (args_->GetInteger(0, &tab_id)) 1812 if (args_->GetInteger(0, &tab_id) && tab_id < 0)
1813 EXTENSION_FUNCTION_VALIDATE(tab_id >= 0); 1813 return set_init_result(VALIDATION_FAILURE);
1814 1814
1815 // |details| are not optional. 1815 // |details| are not optional.
1816 base::DictionaryValue* details_value = NULL; 1816 base::DictionaryValue* details_value = NULL;
1817 if (!args_->GetDictionary(1, &details_value)) 1817 if (!args_->GetDictionary(1, &details_value))
1818 return false; 1818 return set_init_result(VALIDATION_FAILURE);
1819 std::unique_ptr<InjectDetails> details(new InjectDetails()); 1819 std::unique_ptr<InjectDetails> details(new InjectDetails());
1820 if (!InjectDetails::Populate(*details_value, details.get())) 1820 if (!InjectDetails::Populate(*details_value, details.get()))
1821 return false; 1821 return set_init_result(VALIDATION_FAILURE);
1822 1822
1823 // If the tab ID wasn't given then it needs to be converted to the 1823 // If the tab ID wasn't given then it needs to be converted to the
1824 // currently active tab's ID. 1824 // currently active tab's ID.
1825 if (tab_id == -1) { 1825 if (tab_id == -1) {
1826 Browser* browser = chrome_details_.GetCurrentBrowser(); 1826 Browser* browser = chrome_details_.GetCurrentBrowser();
1827 // Can happen during shutdown.
1827 if (!browser) 1828 if (!browser)
1828 return false; 1829 return set_init_result(FAILURE, keys::kNoCurrentWindowError);
1829 content::WebContents* web_contents = NULL; 1830 content::WebContents* web_contents = NULL;
1831 // Can happen during shutdown.
1830 if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, &tab_id)) 1832 if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, &tab_id))
1831 return false; 1833 return set_init_result(FAILURE, keys::kNoTabInBrowserWindowError);
1832 } 1834 }
1833 1835
1834 execute_tab_id_ = tab_id; 1836 execute_tab_id_ = tab_id;
1835 details_ = std::move(details); 1837 details_ = std::move(details);
1836 set_host_id(HostID(HostID::EXTENSIONS, extension()->id())); 1838 set_host_id(HostID(HostID::EXTENSIONS, extension()->id()));
1837 return true; 1839 return set_init_result(SUCCESS);
1838 } 1840 }
1839 1841
1840 bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage() { 1842 bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage() {
1841 content::WebContents* contents = NULL; 1843 content::WebContents* contents = NULL;
1842 1844
1843 // If |tab_id| is specified, look for the tab. Otherwise default to selected 1845 // If |tab_id| is specified, look for the tab. Otherwise default to selected
1844 // tab in the current window. 1846 // tab in the current window.
1845 CHECK_GE(execute_tab_id_, 0); 1847 CHECK_GE(execute_tab_id_, 0);
1846 if (!GetTabById(execute_tab_id_, browser_context(), include_incognito(), 1848 if (!GetTabById(execute_tab_id_, browser_context(), include_incognito(),
1847 nullptr, nullptr, &contents, nullptr, &error_)) { 1849 nullptr, nullptr, &contents, nullptr, &error_)) {
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
2110 params->tab_id 2112 params->tab_id
2111 ? ErrorUtils::FormatErrorMessage(keys::kCannotDiscardTab, 2113 ? ErrorUtils::FormatErrorMessage(keys::kCannotDiscardTab,
2112 base::IntToString(*params->tab_id)) 2114 base::IntToString(*params->tab_id))
2113 : keys::kCannotFindTabToDiscard)); 2115 : keys::kCannotFindTabToDiscard));
2114 } 2116 }
2115 2117
2116 TabsDiscardFunction::TabsDiscardFunction() {} 2118 TabsDiscardFunction::TabsDiscardFunction() {}
2117 TabsDiscardFunction::~TabsDiscardFunction() {} 2119 TabsDiscardFunction::~TabsDiscardFunction() {}
2118 2120
2119 } // namespace extensions 2121 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698