Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / extensions / chrome_extension_function_details.cc
blob5d2e480bb9675c45f4c8fa23c66292b3a01e03c5
1 // Copyright 2014 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_details.h"
7 #include "chrome/browser/extensions/extension_tab_util.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/browser_window.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "components/web_modal/web_contents_modal_dialog_manager.h"
16 #include "content/public/browser/render_frame_host.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "extensions/browser/app_window/app_window.h"
19 #include "extensions/browser/app_window/app_window_registry.h"
20 #include "extensions/browser/extension_function.h"
21 #include "extensions/browser/extension_function_dispatcher.h"
23 using content::WebContents;
24 using content::RenderViewHost;
25 using extensions::WindowController;
27 ChromeExtensionFunctionDetails::ChromeExtensionFunctionDetails(
28 UIThreadExtensionFunction* function)
29 : function_(function) {
32 ChromeExtensionFunctionDetails::~ChromeExtensionFunctionDetails() {
35 Profile* ChromeExtensionFunctionDetails::GetProfile() const {
36 return Profile::FromBrowserContext(function_->browser_context());
39 // TODO(stevenjb): Replace this with GetExtensionWindowController().
40 Browser* ChromeExtensionFunctionDetails::GetCurrentBrowser() const {
41 // If the delegate has an associated browser, return it.
42 if (function_->dispatcher()) {
43 extensions::WindowController* window_controller =
44 function_->dispatcher()->GetExtensionWindowController();
45 if (window_controller) {
46 Browser* browser = window_controller->GetBrowser();
47 if (browser)
48 return browser;
52 // Otherwise, try to default to a reasonable browser. If |include_incognito_|
53 // is true, we will also search browsers in the incognito version of this
54 // profile. Note that the profile may already be incognito, in which case
55 // we will search the incognito version only, regardless of the value of
56 // |include_incognito|. Look only for browsers on the active desktop as it is
57 // preferable to pretend no browser is open then to return a browser on
58 // another desktop.
59 if (function_->render_frame_host()) {
60 Profile* profile = Profile::FromBrowserContext(
61 function_->render_frame_host()->GetProcess()->GetBrowserContext());
62 Browser* browser = chrome::FindAnyBrowser(
63 profile, function_->include_incognito(), chrome::GetActiveDesktop());
64 if (browser)
65 return browser;
68 // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
69 // a background_page onload chrome.tabs api call can make it into here
70 // before the browser is sufficiently initialized to return here, or
71 // all of this profile's browser windows may have been closed.
72 // A similar situation may arise during shutdown.
73 // TODO(rafaelw): Delay creation of background_page until the browser
74 // is available. http://code.google.com/p/chromium/issues/detail?id=13284
75 return NULL;
78 extensions::WindowController*
79 ChromeExtensionFunctionDetails::GetExtensionWindowController() const {
80 // If the delegate has an associated window controller, return it.
81 if (function_->dispatcher()) {
82 extensions::WindowController* window_controller =
83 function_->dispatcher()->GetExtensionWindowController();
84 if (window_controller)
85 return window_controller;
88 return extensions::WindowControllerList::GetInstance()
89 ->CurrentWindowForFunction(function_);
92 content::WebContents*
93 ChromeExtensionFunctionDetails::GetAssociatedWebContents() {
94 content::WebContents* web_contents = function_->GetAssociatedWebContents();
95 if (web_contents)
96 return web_contents;
98 Browser* browser = GetCurrentBrowser();
99 if (!browser)
100 return NULL;
101 return browser->tab_strip_model()->GetActiveWebContents();
104 content::WebContents* ChromeExtensionFunctionDetails::GetOriginWebContents() {
105 WebContents* contents = function_->GetSenderWebContents();
106 if (!contents)
107 return nullptr;
109 // Hack: use the existence of a WebContentsModalDialogManager to decide
110 // whether the sender web contents is visible.
111 web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager =
112 web_modal::WebContentsModalDialogManager::FromWebContents(contents);
113 if (web_contents_modal_dialog_manager)
114 return contents;
116 // If there is no WebContentsModalDialogManager, then this contents is
117 // probably the background page for an extension or app.
118 contents = nullptr;
120 int source_tab_id = function_->source_tab_id();
121 if (source_tab_id != TabStripModel::kNoTab) {
122 // When the request originated from a background page, but there is no
123 // app window open, check to see if it originated from a tab and display
124 // the dialog in that tab.
125 if (extensions::ExtensionTabUtil::GetTabById(
126 source_tab_id, GetProfile(), true /* include_incognito */, nullptr,
127 nullptr, &contents, nullptr))
128 return contents;
131 // Try to find an app window and get its web contents.
132 const extensions::Extension* extension = function_->extension();
133 if (extension) {
134 extensions::AppWindow* window =
135 extensions::AppWindowRegistry::Get(GetProfile())
136 ->GetCurrentAppWindowForApp(extension->id());
137 if (window)
138 return window->web_contents();
140 return contents;
143 gfx::NativeWindow ChromeExtensionFunctionDetails::GetNativeWindowForUI() {
144 // Try to use WindowControllerList first because WebContents's
145 // GetTopLevelNativeWindow() can't return the top level window when the tab
146 // is not focused.
147 WindowController* controller =
148 extensions::WindowControllerList::GetInstance()->CurrentWindowForFunction(
149 function_);
150 if (controller)
151 return controller->window()->GetNativeWindow();
153 // CurrentWindowForFunction() can't find app's window.
154 WebContents* contents = GetOriginWebContents();
155 if (contents)
156 return contents->GetTopLevelNativeWindow();
158 Browser* browser =
159 chrome::FindBrowserWithProfile(GetProfile(), chrome::GetActiveDesktop());
160 return browser->window()->GetNativeWindow();