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 // TODO(stevenjb): Replace this with GetExtensionWindowController().
29 Browser
* ChromeUIThreadExtensionFunction::GetCurrentBrowser() {
30 // If the delegate has an associated browser, return it.
32 extensions::WindowController
* window_controller
=
33 dispatcher()->GetExtensionWindowController();
34 if (window_controller
) {
35 Browser
* browser
= window_controller
->GetBrowser();
41 // Otherwise, try to default to a reasonable browser. If |include_incognito_|
42 // is true, we will also search browsers in the incognito version of this
43 // profile. Note that the profile may already be incognito, in which case
44 // we will search the incognito version only, regardless of the value of
45 // |include_incognito|. Look only for browsers on the active desktop as it is
46 // preferable to pretend no browser is open then to return a browser on
48 content::WebContents
* web_contents
= GetSenderWebContents();
51 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
52 Browser
* browser
= chrome::FindAnyBrowser(
53 profile
, include_incognito_
, chrome::GetActiveDesktop());
58 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
59 // a background_page onload chrome.tabs api call can make it into here
60 // before the browser is sufficiently initialized to return here, or
61 // all of this profile's browser windows may have been closed.
62 // A similar situation may arise during shutdown.
63 // TODO(rafaelw): Delay creation of background_page until the browser
64 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
68 extensions::WindowController
*
69 ChromeUIThreadExtensionFunction::GetExtensionWindowController() {
70 // If the delegate has an associated window controller, return it.
72 extensions::WindowController
* window_controller
=
73 dispatcher()->GetExtensionWindowController();
74 if (window_controller
)
75 return window_controller
;
78 return extensions::WindowControllerList::GetInstance()
79 ->CurrentWindowForFunction(this);
83 ChromeUIThreadExtensionFunction::GetAssociatedWebContents() {
84 content::WebContents
* web_contents
=
85 UIThreadExtensionFunction::GetAssociatedWebContents();
89 Browser
* browser
= GetCurrentBrowser();
92 return browser
->tab_strip_model()->GetActiveWebContents();
95 ChromeUIThreadExtensionFunction::~ChromeUIThreadExtensionFunction() {
98 ChromeAsyncExtensionFunction::ChromeAsyncExtensionFunction() {
101 ChromeAsyncExtensionFunction::~ChromeAsyncExtensionFunction() {}
103 ExtensionFunction::ResponseAction
ChromeAsyncExtensionFunction::Run() {
104 return RunAsync() ? RespondLater() : RespondNow(Error(error_
));
108 bool ChromeAsyncExtensionFunction::ValidationFailure(
109 ChromeAsyncExtensionFunction
* function
) {
113 ChromeSyncExtensionFunction::ChromeSyncExtensionFunction() {
116 ChromeSyncExtensionFunction::~ChromeSyncExtensionFunction() {}
118 ExtensionFunction::ResponseAction
ChromeSyncExtensionFunction::Run() {
119 return RespondNow(RunSync() ? ArgumentList(results_
.Pass()) : Error(error_
));
123 bool ChromeSyncExtensionFunction::ValidationFailure(
124 ChromeSyncExtensionFunction
* function
) {