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()->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 content::WebContents
* web_contents
= GetSenderWebContents();
69 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
70 Browser
* browser
= chrome::FindAnyBrowser(
71 profile
, include_incognito_
, chrome::GetActiveDesktop());
76 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
77 // a background_page onload chrome.tabs api call can make it into here
78 // before the browser is sufficiently initialized to return here, or
79 // all of this profile's browser windows may have been closed.
80 // A similar situation may arise during shutdown.
81 // TODO(rafaelw): Delay creation of background_page until the browser
82 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
86 extensions::WindowController
*
87 ChromeUIThreadExtensionFunction::GetExtensionWindowController() {
88 // If the delegate has an associated window controller, return it.
90 extensions::WindowController
* window_controller
=
91 dispatcher()->GetExtensionWindowController();
92 if (window_controller
)
93 return window_controller
;
96 return extensions::WindowControllerList::GetInstance()
97 ->CurrentWindowForFunction(ChromeExtensionFunctionDetails(this));
100 content::WebContents
*
101 ChromeUIThreadExtensionFunction::GetAssociatedWebContents() {
102 content::WebContents
* web_contents
=
103 UIThreadExtensionFunction::GetAssociatedWebContents();
107 Browser
* browser
= GetCurrentBrowser();
110 return browser
->tab_strip_model()->GetActiveWebContents();
113 ChromeUIThreadExtensionFunction::~ChromeUIThreadExtensionFunction() {
116 ChromeAsyncExtensionFunction::ChromeAsyncExtensionFunction() {
119 ChromeAsyncExtensionFunction::~ChromeAsyncExtensionFunction() {}
121 ExtensionFunction::ResponseAction
ChromeAsyncExtensionFunction::Run() {
122 return RunAsync() ? RespondLater() : RespondNow(Error(error_
));
126 bool ChromeAsyncExtensionFunction::ValidationFailure(
127 ChromeAsyncExtensionFunction
* function
) {
131 ChromeSyncExtensionFunction::ChromeSyncExtensionFunction() {
134 ChromeSyncExtensionFunction::~ChromeSyncExtensionFunction() {}
136 ExtensionFunction::ResponseAction
ChromeSyncExtensionFunction::Run() {
137 return RespondNow(RunSync() ? ArgumentList(results_
.Pass()) : Error(error_
));
141 bool ChromeSyncExtensionFunction::ValidationFailure(
142 ChromeSyncExtensionFunction
* function
) {