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/extension_function_dispatcher.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"
17 using content::RenderViewHost
;
18 using content::WebContents
;
20 ChromeAsyncExtensionFunction::ChromeAsyncExtensionFunction() {}
22 Profile
* ChromeAsyncExtensionFunction::GetProfile() const {
23 return Profile::FromBrowserContext(context_
);
26 bool ChromeAsyncExtensionFunction::CanOperateOnWindow(
27 const extensions::WindowController
* window_controller
) const {
28 const extensions::Extension
* extension
= GetExtension();
29 // |extension| is NULL for unit tests only.
30 if (extension
!= NULL
&& !window_controller
->IsVisibleToExtension(extension
))
33 if (GetProfile() == window_controller
->profile())
36 if (!include_incognito())
39 return GetProfile()->HasOffTheRecordProfile() &&
40 GetProfile()->GetOffTheRecordProfile() == window_controller
->profile();
43 // TODO(stevenjb): Replace this with GetExtensionWindowController().
44 Browser
* ChromeAsyncExtensionFunction::GetCurrentBrowser() {
45 // If the delegate has an associated browser, return it.
47 extensions::WindowController
* window_controller
=
48 dispatcher()->delegate()->GetExtensionWindowController();
49 if (window_controller
) {
50 Browser
* browser
= window_controller
->GetBrowser();
56 // Otherwise, try to default to a reasonable browser. If |include_incognito_|
57 // is true, we will also search browsers in the incognito version of this
58 // profile. Note that the profile may already be incognito, in which case
59 // we will search the incognito version only, regardless of the value of
60 // |include_incognito|. Look only for browsers on the active desktop as it is
61 // preferable to pretend no browser is open then to return a browser on
63 if (render_view_host_
) {
64 Profile
* profile
= Profile::FromBrowserContext(
65 render_view_host_
->GetProcess()->GetBrowserContext());
66 Browser
* browser
= chrome::FindAnyBrowser(
67 profile
, include_incognito_
, chrome::GetActiveDesktop());
72 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
73 // a background_page onload chrome.tabs api call can make it into here
74 // before the browser is sufficiently initialized to return here, or
75 // all of this profile's browser windows may have been closed.
76 // A similar situation may arise during shutdown.
77 // TODO(rafaelw): Delay creation of background_page until the browser
78 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
82 extensions::WindowController
*
83 ChromeAsyncExtensionFunction::GetExtensionWindowController() {
84 // If the delegate has an associated window controller, return it.
86 extensions::WindowController
* window_controller
=
87 dispatcher()->delegate()->GetExtensionWindowController();
88 if (window_controller
)
89 return window_controller
;
92 return extensions::WindowControllerList::GetInstance()
93 ->CurrentWindowForFunction(this);
96 content::WebContents
* ChromeAsyncExtensionFunction::GetAssociatedWebContents() {
97 content::WebContents
* web_contents
=
98 UIThreadExtensionFunction::GetAssociatedWebContents();
102 Browser
* browser
= GetCurrentBrowser();
105 return browser
->tab_strip_model()->GetActiveWebContents();
108 ChromeAsyncExtensionFunction::~ChromeAsyncExtensionFunction() {}
110 ChromeSyncExtensionFunction::ChromeSyncExtensionFunction() {}
112 void ChromeSyncExtensionFunction::Run() { SendResponse(RunImpl()); }
114 ChromeSyncExtensionFunction::~ChromeSyncExtensionFunction() {}