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/window_controller.h"
8 #include "chrome/browser/extensions/window_controller_list.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_finder.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "extensions/browser/extension_function_dispatcher.h"
17 using content::RenderViewHost;
18 using content::WebContents;
20 ChromeUIThreadExtensionFunction::ChromeUIThreadExtensionFunction() {
23 Profile* ChromeUIThreadExtensionFunction::GetProfile() const {
24 return Profile::FromBrowserContext(context_);
27 bool ChromeUIThreadExtensionFunction::CanOperateOnWindow(
28 const extensions::WindowController* window_controller) const {
29 const extensions::Extension* extension = GetExtension();
30 // |extension| is NULL for unit tests only.
31 if (extension != NULL && !window_controller->IsVisibleToExtension(extension))
34 if (GetProfile() == window_controller->profile())
37 if (!include_incognito())
40 return GetProfile()->HasOffTheRecordProfile() &&
41 GetProfile()->GetOffTheRecordProfile() == window_controller->profile();
44 // TODO(stevenjb): Replace this with GetExtensionWindowController().
45 Browser* ChromeUIThreadExtensionFunction::GetCurrentBrowser() {
46 // If the delegate has an associated browser, return it.
48 extensions::WindowController* window_controller =
49 dispatcher()->delegate()->GetExtensionWindowController();
50 if (window_controller) {
51 Browser* browser = window_controller->GetBrowser();
57 // Otherwise, try to default to a reasonable browser. If |include_incognito_|
58 // is true, we will also search browsers in the incognito version of this
59 // profile. Note that the profile may already be incognito, in which case
60 // we will search the incognito version only, regardless of the value of
61 // |include_incognito|. Look only for browsers on the active desktop as it is
62 // preferable to pretend no browser is open then to return a browser on
64 if (render_view_host_) {
65 Profile* profile = Profile::FromBrowserContext(
66 render_view_host_->GetProcess()->GetBrowserContext());
67 Browser* browser = chrome::FindAnyBrowser(
68 profile, include_incognito_, chrome::GetActiveDesktop());
73 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
74 // a background_page onload chrome.tabs api call can make it into here
75 // before the browser is sufficiently initialized to return here, or
76 // all of this profile's browser windows may have been closed.
77 // A similar situation may arise during shutdown.
78 // TODO(rafaelw): Delay creation of background_page until the browser
79 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
83 extensions::WindowController*
84 ChromeUIThreadExtensionFunction::GetExtensionWindowController() {
85 // If the delegate has an associated window controller, return it.
87 extensions::WindowController* window_controller =
88 dispatcher()->delegate()->GetExtensionWindowController();
89 if (window_controller)
90 return window_controller;
93 return extensions::WindowControllerList::GetInstance()
94 ->CurrentWindowForFunction(this);
98 ChromeUIThreadExtensionFunction::GetAssociatedWebContents() {
99 content::WebContents* web_contents =
100 UIThreadExtensionFunction::GetAssociatedWebContents();
104 Browser* browser = GetCurrentBrowser();
107 return browser->tab_strip_model()->GetActiveWebContents();
110 ChromeUIThreadExtensionFunction::~ChromeUIThreadExtensionFunction() {
113 ChromeAsyncExtensionFunction::ChromeAsyncExtensionFunction() {
116 ChromeAsyncExtensionFunction::~ChromeAsyncExtensionFunction() {}
118 ExtensionFunction::ResponseAction ChromeAsyncExtensionFunction::Run() {
119 return RunAsync() ? RespondLater() : RespondNow(Error(error_));
123 bool ChromeAsyncExtensionFunction::ValidationFailure(
124 ChromeAsyncExtensionFunction* function) {
128 ChromeSyncExtensionFunction::ChromeSyncExtensionFunction() {
131 ChromeSyncExtensionFunction::~ChromeSyncExtensionFunction() {}
133 ExtensionFunction::ResponseAction ChromeSyncExtensionFunction::Run() {
134 return RespondNow(RunSync() ? MultipleArguments(results_.get())
139 bool ChromeSyncExtensionFunction::ValidationFailure(
140 ChromeSyncExtensionFunction* function) {