Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / chrome_extension_function.cc
blobfe662dd4a97ff96e56f3041e2f9ac2717fe3e3e3
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.
31 if (dispatcher()) {
32 extensions::WindowController* window_controller =
33 dispatcher()->GetExtensionWindowController();
34 if (window_controller) {
35 Browser* browser = window_controller->GetBrowser();
36 if (browser)
37 return browser;
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
47 // another desktop.
48 content::WebContents* web_contents = GetSenderWebContents();
49 if (web_contents) {
50 Profile* profile =
51 Profile::FromBrowserContext(web_contents->GetBrowserContext());
52 Browser* browser = chrome::FindAnyBrowser(
53 profile, include_incognito_, chrome::GetActiveDesktop());
54 if (browser)
55 return browser;
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
65 return NULL;
68 extensions::WindowController*
69 ChromeUIThreadExtensionFunction::GetExtensionWindowController() {
70 // If the delegate has an associated window controller, return it.
71 if (dispatcher()) {
72 extensions::WindowController* window_controller =
73 dispatcher()->GetExtensionWindowController();
74 if (window_controller)
75 return window_controller;
78 return extensions::WindowControllerList::GetInstance()
79 ->CurrentWindowForFunction(this);
82 content::WebContents*
83 ChromeUIThreadExtensionFunction::GetAssociatedWebContents() {
84 content::WebContents* web_contents =
85 UIThreadExtensionFunction::GetAssociatedWebContents();
86 if (web_contents)
87 return web_contents;
89 Browser* browser = GetCurrentBrowser();
90 if (!browser)
91 return NULL;
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_));
107 // static
108 bool ChromeAsyncExtensionFunction::ValidationFailure(
109 ChromeAsyncExtensionFunction* function) {
110 return false;
113 ChromeSyncExtensionFunction::ChromeSyncExtensionFunction() {
116 ChromeSyncExtensionFunction::~ChromeSyncExtensionFunction() {}
118 ExtensionFunction::ResponseAction ChromeSyncExtensionFunction::Run() {
119 return RespondNow(RunSync() ? ArgumentList(results_.Pass()) : Error(error_));
122 // static
123 bool ChromeSyncExtensionFunction::ValidationFailure(
124 ChromeSyncExtensionFunction* function) {
125 return false;