1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/chrome_extension_function.h"
7 #include "chrome/browser/extensions/chrome_extension_function_details.h"
8 #include "chrome/browser/extensions/window_controller.h"
9 #include "chrome/browser/extensions/window_controller_list.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "extensions/browser/extension_function_dispatcher.h"
18 using content::RenderViewHost
;
19 using content::WebContents
;
21 ChromeUIThreadExtensionFunction::ChromeUIThreadExtensionFunction() {
24 Profile
* ChromeUIThreadExtensionFunction::GetProfile() const {
25 return Profile::FromBrowserContext(context_
);
28 bool ChromeUIThreadExtensionFunction::CanOperateOnWindow(
29 const extensions::WindowController
* window_controller
) const {
30 // |extension()| is NULL for unit tests only.
31 if (extension() != NULL
&&
32 !window_controller
->IsVisibleToExtension(extension())) {
36 if (GetProfile() == window_controller
->profile())
39 if (!include_incognito())
42 return GetProfile()->HasOffTheRecordProfile() &&
43 GetProfile()->GetOffTheRecordProfile() == window_controller
->profile();
46 // TODO(stevenjb): Replace this with GetExtensionWindowController().
47 Browser
* ChromeUIThreadExtensionFunction::GetCurrentBrowser() {
48 // If the delegate has an associated browser, return it.
50 extensions::WindowController
* window_controller
=
51 dispatcher()->delegate()->GetExtensionWindowController();
52 if (window_controller
) {
53 Browser
* browser
= window_controller
->GetBrowser();
59 // Otherwise, try to default to a reasonable browser. If |include_incognito_|
60 // is true, we will also search browsers in the incognito version of this
61 // profile. Note that the profile may already be incognito, in which case
62 // we will search the incognito version only, regardless of the value of
63 // |include_incognito|. Look only for browsers on the active desktop as it is
64 // preferable to pretend no browser is open then to return a browser on
66 if (render_view_host_
) {
67 Profile
* profile
= Profile::FromBrowserContext(
68 render_view_host_
->GetProcess()->GetBrowserContext());
69 Browser
* browser
= chrome::FindAnyBrowser(
70 profile
, include_incognito_
, chrome::GetActiveDesktop());
75 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
76 // a background_page onload chrome.tabs api call can make it into here
77 // before the browser is sufficiently initialized to return here, or
78 // all of this profile's browser windows may have been closed.
79 // A similar situation may arise during shutdown.
80 // TODO(rafaelw): Delay creation of background_page until the browser
81 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
85 extensions::WindowController
*
86 ChromeUIThreadExtensionFunction::GetExtensionWindowController() {
87 // If the delegate has an associated window controller, return it.
89 extensions::WindowController
* window_controller
=
90 dispatcher()->delegate()->GetExtensionWindowController();
91 if (window_controller
)
92 return window_controller
;
95 return extensions::WindowControllerList::GetInstance()
96 ->CurrentWindowForFunction(ChromeExtensionFunctionDetails(this));
100 ChromeUIThreadExtensionFunction::GetAssociatedWebContents() {
101 content::WebContents
* web_contents
=
102 UIThreadExtensionFunction::GetAssociatedWebContents();
106 Browser
* browser
= GetCurrentBrowser();
109 return browser
->tab_strip_model()->GetActiveWebContents();
112 ChromeUIThreadExtensionFunction::~ChromeUIThreadExtensionFunction() {
115 ChromeAsyncExtensionFunction::ChromeAsyncExtensionFunction() {
118 ChromeAsyncExtensionFunction::~ChromeAsyncExtensionFunction() {}
120 ExtensionFunction::ResponseAction
ChromeAsyncExtensionFunction::Run() {
121 return RunAsync() ? RespondLater() : RespondNow(Error(error_
));
125 bool ChromeAsyncExtensionFunction::ValidationFailure(
126 ChromeAsyncExtensionFunction
* function
) {
130 ChromeSyncExtensionFunction::ChromeSyncExtensionFunction() {
133 ChromeSyncExtensionFunction::~ChromeSyncExtensionFunction() {}
135 ExtensionFunction::ResponseAction
ChromeSyncExtensionFunction::Run() {
136 return RespondNow(RunSync() ? ArgumentList(results_
.Pass()) : Error(error_
));
140 bool ChromeSyncExtensionFunction::ValidationFailure(
141 ChromeSyncExtensionFunction
* function
) {