OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/chrome_extension_function_details.h" | 5 #include "chrome/browser/extensions/chrome_extension_function_details.h" |
6 | 6 |
| 7 #include "chrome/browser/extensions/extension_tab_util.h" |
7 #include "chrome/browser/extensions/window_controller.h" | 8 #include "chrome/browser/extensions/window_controller.h" |
8 #include "chrome/browser/extensions/window_controller_list.h" | 9 #include "chrome/browser/extensions/window_controller_list.h" |
9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/browser_finder.h" | 12 #include "chrome/browser/ui/browser_finder.h" |
| 13 #include "chrome/browser/ui/browser_window.h" |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
13 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
14 #include "content/public/browser/render_process_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" |
15 #include "extensions/browser/extension_function.h" | 20 #include "extensions/browser/extension_function.h" |
16 #include "extensions/browser/extension_function_dispatcher.h" | 21 #include "extensions/browser/extension_function_dispatcher.h" |
17 | 22 |
18 using content::WebContents; | 23 using content::WebContents; |
19 using content::RenderViewHost; | 24 using content::RenderViewHost; |
20 using extensions::WindowController; | 25 using extensions::WindowController; |
21 | 26 |
22 ChromeExtensionFunctionDetails::ChromeExtensionFunctionDetails( | 27 ChromeExtensionFunctionDetails::ChromeExtensionFunctionDetails( |
23 UIThreadExtensionFunction* function) | 28 UIThreadExtensionFunction* function) |
24 : function_(function) { | 29 : function_(function) { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 ChromeExtensionFunctionDetails::GetAssociatedWebContents() { | 93 ChromeExtensionFunctionDetails::GetAssociatedWebContents() { |
89 content::WebContents* web_contents = function_->GetAssociatedWebContents(); | 94 content::WebContents* web_contents = function_->GetAssociatedWebContents(); |
90 if (web_contents) | 95 if (web_contents) |
91 return web_contents; | 96 return web_contents; |
92 | 97 |
93 Browser* browser = GetCurrentBrowser(); | 98 Browser* browser = GetCurrentBrowser(); |
94 if (!browser) | 99 if (!browser) |
95 return NULL; | 100 return NULL; |
96 return browser->tab_strip_model()->GetActiveWebContents(); | 101 return browser->tab_strip_model()->GetActiveWebContents(); |
97 } | 102 } |
| 103 |
| 104 content::WebContents* ChromeExtensionFunctionDetails::GetOriginWebContents() { |
| 105 WebContents* contents = function_->GetSenderWebContents(); |
| 106 if (!contents) |
| 107 return nullptr; |
| 108 |
| 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; |
| 115 |
| 116 // If there is no WebContentsModalDialogManager, then this contents is |
| 117 // probably the background page for an extension or app. |
| 118 contents = nullptr; |
| 119 |
| 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; |
| 129 } |
| 130 |
| 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(); |
| 139 } |
| 140 return contents; |
| 141 } |
| 142 |
| 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(); |
| 152 |
| 153 // CurrentWindowForFunction() can't find app's window. |
| 154 WebContents* contents = GetOriginWebContents(); |
| 155 if (contents) |
| 156 return contents->GetTopLevelNativeWindow(); |
| 157 |
| 158 Browser* browser = |
| 159 chrome::FindBrowserWithProfile(GetProfile(), chrome::GetActiveDesktop()); |
| 160 return browser->window()->GetNativeWindow(); |
| 161 } |
OLD | NEW |