diff options
author | lionel.g.landwerlin <lionel.g.landwerlin@intel.com> | 2015-07-09 02:49:40 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-09 09:50:04 +0000 |
commit | ff6f732408e41f6095447e4bdef2b9c777549fa5 (patch) | |
tree | 949d350f6f945ca89cf718ca306fbdd403133896 | |
parent | 17399fb2b0a5fcba7c2dcb76ab2c597e6cf7c0db (diff) | |
download | chromium_src-ff6f732408e41f6095447e4bdef2b9c777549fa5.zip chromium_src-ff6f732408e41f6095447e4bdef2b9c777549fa5.tar.gz chromium_src-ff6f732408e41f6095447e4bdef2b9c777549fa5.tar.bz2 |
extensions: factor out utility function from ChromeExtensionFunction*
BUG=394341
TEST=browser_tests --gtest_filter=ExtensionTabsTest.*
Review URL: https://codereview.chromium.org/1226013002
Cr-Commit-Position: refs/heads/master@{#338005}
9 files changed, 42 insertions, 62 deletions
diff --git a/chrome/browser/extensions/api/tabs/tabs_api.cc b/chrome/browser/extensions/api/tabs/tabs_api.cc index 3ca5535..83827fb 100644 --- a/chrome/browser/extensions/api/tabs/tabs_api.cc +++ b/chrome/browser/extensions/api/tabs/tabs_api.cc @@ -346,7 +346,7 @@ bool WindowsGetAllFunction::RunSync() { for (WindowControllerList::ControllerList::const_iterator iter = windows.begin(); iter != windows.end(); ++iter) { - if (!this->CanOperateOnWindow(*iter)) + if (!windows_util::CanOperateOnWindow(this, *iter)) continue; if (populate_tabs) window_list->Append((*iter)->CreateWindowValueWithTabs(extension())); diff --git a/chrome/browser/extensions/api/tabs/windows_util.cc b/chrome/browser/extensions/api/tabs/windows_util.cc index 48c6824..0e2ead5 100644 --- a/chrome/browser/extensions/api/tabs/windows_util.cc +++ b/chrome/browser/extensions/api/tabs/windows_util.cc @@ -10,16 +10,17 @@ #include "chrome/browser/extensions/chrome_extension_function_details.h" #include "chrome/browser/extensions/window_controller.h" #include "chrome/browser/extensions/window_controller_list.h" +#include "chrome/browser/profiles/profile.h" +#include "extensions/browser/extension_function.h" #include "extensions/browser/extension_function_dispatcher.h" #include "extensions/common/constants.h" #include "extensions/common/error_utils.h" namespace windows_util { -bool GetWindowFromWindowID(ChromeUIThreadExtensionFunction* function, +bool GetWindowFromWindowID(UIThreadExtensionFunction* function, int window_id, extensions::WindowController** controller) { - ChromeExtensionFunctionDetails function_details(function); if (window_id == extension_misc::kCurrentWindowId) { extensions::WindowController* extension_window_controller = function->dispatcher()->GetExtensionWindowController(); @@ -29,7 +30,7 @@ bool GetWindowFromWindowID(ChromeUIThreadExtensionFunction* function, } else { // Otherwise get the focused or most recently added window. *controller = extensions::WindowControllerList::GetInstance() - ->CurrentWindowForFunction(function_details); + ->CurrentWindowForFunction(function); } if (!(*controller)) { function->SetError(extensions::tabs_constants::kNoCurrentWindowError); @@ -37,7 +38,7 @@ bool GetWindowFromWindowID(ChromeUIThreadExtensionFunction* function, } } else { *controller = extensions::WindowControllerList::GetInstance() - ->FindWindowForFunctionById(function_details, window_id); + ->FindWindowForFunctionById(function, window_id); if (!(*controller)) { function->SetError(extensions::ErrorUtils::FormatErrorMessage( extensions::tabs_constants::kWindowNotFoundError, @@ -48,4 +49,22 @@ bool GetWindowFromWindowID(ChromeUIThreadExtensionFunction* function, return true; } +bool CanOperateOnWindow(const UIThreadExtensionFunction* function, + const extensions::WindowController* controller) { + if (function->extension() != NULL && + !controller->IsVisibleToExtension(function->extension())) { + return false; + } + + if (function->browser_context() == controller->profile()) + return true; + + if (!function->include_incognito()) + return false; + + Profile* profile = Profile::FromBrowserContext(function->browser_context()); + return profile->HasOffTheRecordProfile() && + profile->GetOffTheRecordProfile() == controller->profile(); +} + } // namespace windows_util diff --git a/chrome/browser/extensions/api/tabs/windows_util.h b/chrome/browser/extensions/api/tabs/windows_util.h index 61f8c4b..0e0a8d2 100644 --- a/chrome/browser/extensions/api/tabs/windows_util.h +++ b/chrome/browser/extensions/api/tabs/windows_util.h @@ -5,7 +5,7 @@ #ifndef CHROME_BROWSER_EXTENSIONS_API_TABS_WINDOWS_UTIL_H__ #define CHROME_BROWSER_EXTENSIONS_API_TABS_WINDOWS_UTIL_H__ -class ChromeUIThreadExtensionFunction; +class UIThreadExtensionFunction; namespace extensions { class WindowController; @@ -15,10 +15,15 @@ namespace windows_util { // Populates |controller| for given |window_id|. If the window is not found, // returns false and sets UIThreadExtensionFunction error_. -bool GetWindowFromWindowID(ChromeUIThreadExtensionFunction* function, +bool GetWindowFromWindowID(UIThreadExtensionFunction* function, int window_id, extensions::WindowController** controller); +// Returns true if |function| (and the profile and extension that it was +// invoked from) can operate on the window wrapped by |window_controller|. +bool CanOperateOnWindow(const UIThreadExtensionFunction* function, + const extensions::WindowController* controller); + } // namespace windows_util #endif // CHROME_BROWSER_EXTENSIONS_API_TABS_WINDOWS_UTIL_H__ diff --git a/chrome/browser/extensions/chrome_extension_function.cc b/chrome/browser/extensions/chrome_extension_function.cc index ddd0b9d..fe662dd 100644 --- a/chrome/browser/extensions/chrome_extension_function.cc +++ b/chrome/browser/extensions/chrome_extension_function.cc @@ -25,24 +25,6 @@ Profile* ChromeUIThreadExtensionFunction::GetProfile() const { return Profile::FromBrowserContext(context_); } -bool ChromeUIThreadExtensionFunction::CanOperateOnWindow( - const extensions::WindowController* window_controller) const { - // |extension()| is NULL for unit tests only. - if (extension() != NULL && - !window_controller->IsVisibleToExtension(extension())) { - return false; - } - - if (GetProfile() == window_controller->profile()) - return true; - - if (!include_incognito()) - return false; - - return GetProfile()->HasOffTheRecordProfile() && - GetProfile()->GetOffTheRecordProfile() == window_controller->profile(); -} - // TODO(stevenjb): Replace this with GetExtensionWindowController(). Browser* ChromeUIThreadExtensionFunction::GetCurrentBrowser() { // If the delegate has an associated browser, return it. @@ -94,7 +76,7 @@ ChromeUIThreadExtensionFunction::GetExtensionWindowController() { } return extensions::WindowControllerList::GetInstance() - ->CurrentWindowForFunction(ChromeExtensionFunctionDetails(this)); + ->CurrentWindowForFunction(this); } content::WebContents* diff --git a/chrome/browser/extensions/chrome_extension_function.h b/chrome/browser/extensions/chrome_extension_function.h index dbf0099..fefcda9 100644 --- a/chrome/browser/extensions/chrome_extension_function.h +++ b/chrome/browser/extensions/chrome_extension_function.h @@ -30,11 +30,6 @@ class ChromeUIThreadExtensionFunction : public UIThreadExtensionFunction { Profile* GetProfile() const; - // Returns true if this function (and the profile and extension that it was - // invoked from) can operate on the window wrapped by |window_controller|. - bool CanOperateOnWindow(const extensions::WindowController* window_controller) - const; - // Gets the "current" browser, if any. // // Many extension APIs operate relative to the current browser, which is the diff --git a/chrome/browser/extensions/chrome_extension_function_details.cc b/chrome/browser/extensions/chrome_extension_function_details.cc index 922c96b..460f490 100644 --- a/chrome/browser/extensions/chrome_extension_function_details.cc +++ b/chrome/browser/extensions/chrome_extension_function_details.cc @@ -31,24 +31,6 @@ Profile* ChromeExtensionFunctionDetails::GetProfile() const { return Profile::FromBrowserContext(function_->browser_context()); } -bool ChromeExtensionFunctionDetails::CanOperateOnWindow( - const extensions::WindowController* window_controller) const { - // |extension()| is NULL for unit tests only. - if (function_->extension() != NULL && - !window_controller->IsVisibleToExtension(function_->extension())) { - return false; - } - - if (GetProfile() == window_controller->profile()) - return true; - - if (!function_->include_incognito()) - return false; - - return GetProfile()->HasOffTheRecordProfile() && - GetProfile()->GetOffTheRecordProfile() == window_controller->profile(); -} - // TODO(stevenjb): Replace this with GetExtensionWindowController(). Browser* ChromeExtensionFunctionDetails::GetCurrentBrowser() const { // If the delegate has an associated browser, return it. @@ -99,7 +81,7 @@ ChromeExtensionFunctionDetails::GetExtensionWindowController() const { } return extensions::WindowControllerList::GetInstance() - ->CurrentWindowForFunction(*this); + ->CurrentWindowForFunction(function_); } content::WebContents* diff --git a/chrome/browser/extensions/chrome_extension_function_details.h b/chrome/browser/extensions/chrome_extension_function_details.h index 2484158..ba09652 100644 --- a/chrome/browser/extensions/chrome_extension_function_details.h +++ b/chrome/browser/extensions/chrome_extension_function_details.h @@ -30,11 +30,6 @@ class ChromeExtensionFunctionDetails { Profile* GetProfile() const; - // Returns true if this function (and the profile and extension that it was - // invoked from) can operate on the window wrapped by |window_controller|. - bool CanOperateOnWindow( - const extensions::WindowController* window_controller) const; - // Gets the "current" browser, if any. // // Many extension APIs operate relative to the current browser, which is the diff --git a/chrome/browser/extensions/window_controller_list.cc b/chrome/browser/extensions/window_controller_list.cc index 0eadbea..cea69d5 100644 --- a/chrome/browser/extensions/window_controller_list.cc +++ b/chrome/browser/extensions/window_controller_list.cc @@ -6,9 +6,11 @@ #include <algorithm> +#include "chrome/browser/extensions/api/tabs/windows_util.h" #include "chrome/browser/extensions/chrome_extension_function_details.h" #include "chrome/browser/extensions/window_controller_list_observer.h" #include "components/sessions/session_id.h" +#include "extensions/browser/extension_function.h" #include "ui/base/base_window.h" namespace extensions { @@ -62,21 +64,21 @@ WindowController* WindowControllerList::FindWindowById(int id) const { } WindowController* WindowControllerList::FindWindowForFunctionById( - const ChromeExtensionFunctionDetails& function_details, + const UIThreadExtensionFunction* function, int id) const { WindowController* controller = FindWindowById(id); - if (controller && function_details.CanOperateOnWindow(controller)) + if (controller && windows_util::CanOperateOnWindow(function, controller)) return controller; return NULL; } WindowController* WindowControllerList::CurrentWindowForFunction( - const ChromeExtensionFunctionDetails& function_details) const { + const UIThreadExtensionFunction* function) const { WindowController* result = NULL; // Returns either the focused window (if any), or the last window in the list. for (ControllerList::const_iterator iter = windows().begin(); iter != windows().end(); ++iter) { - if (function_details.CanOperateOnWindow(*iter)) { + if (windows_util::CanOperateOnWindow(function, *iter)) { result = *iter; if (result->window()->IsActive()) break; // use focused window diff --git a/chrome/browser/extensions/window_controller_list.h b/chrome/browser/extensions/window_controller_list.h index 7b7ab48..c280e07 100644 --- a/chrome/browser/extensions/window_controller_list.h +++ b/chrome/browser/extensions/window_controller_list.h @@ -13,7 +13,7 @@ #include "chrome/browser/extensions/window_controller.h" class Profile; -class ChromeExtensionFunctionDetails; +class UIThreadExtensionFunction; namespace extensions { @@ -38,13 +38,13 @@ class WindowControllerList { // Returns a window matching the context the function was invoked in. WindowController* FindWindowForFunctionById( - const ChromeExtensionFunctionDetails& function_details, + const UIThreadExtensionFunction* function, int id) const; // Returns the focused or last added window matching the context the function // was invoked in. WindowController* CurrentWindowForFunction( - const ChromeExtensionFunctionDetails& function_details) const; + const UIThreadExtensionFunction* function) const; const ControllerList& windows() const { return windows_; } |