| 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/extension_tab_util.h" |
| 8 #include "chrome/browser/extensions/window_controller.h" | 8 #include "chrome/browser/extensions/window_controller.h" |
| 9 #include "chrome/browser/extensions/window_controller_list.h" | 9 #include "chrome/browser/extensions/window_controller_list.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 if (web_contents) | 97 if (web_contents) |
| 98 return web_contents; | 98 return web_contents; |
| 99 } | 99 } |
| 100 | 100 |
| 101 Browser* browser = GetCurrentBrowser(); | 101 Browser* browser = GetCurrentBrowser(); |
| 102 if (!browser) | 102 if (!browser) |
| 103 return NULL; | 103 return NULL; |
| 104 return browser->tab_strip_model()->GetActiveWebContents(); | 104 return browser->tab_strip_model()->GetActiveWebContents(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 content::WebContents* ChromeExtensionFunctionDetails::GetOriginWebContents() { | |
| 108 WebContents* contents = function_->GetSenderWebContents(); | |
| 109 if (!contents) | |
| 110 return nullptr; | |
| 111 | |
| 112 // Hack: use the existence of a WebContentsModalDialogManager to decide | |
| 113 // whether the sender web contents is visible. | |
| 114 web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager = | |
| 115 web_modal::WebContentsModalDialogManager::FromWebContents(contents); | |
| 116 if (web_contents_modal_dialog_manager) | |
| 117 return contents; | |
| 118 | |
| 119 // If there is no WebContentsModalDialogManager, then this contents is | |
| 120 // probably the background page for an extension or app. | |
| 121 contents = nullptr; | |
| 122 | |
| 123 int source_tab_id = function_->source_tab_id(); | |
| 124 if (source_tab_id != TabStripModel::kNoTab) { | |
| 125 // When the request originated from a background page, but there is no | |
| 126 // app window open, check to see if it originated from a tab and display | |
| 127 // the dialog in that tab. | |
| 128 if (extensions::ExtensionTabUtil::GetTabById( | |
| 129 source_tab_id, GetProfile(), true /* include_incognito */, nullptr, | |
| 130 nullptr, &contents, nullptr)) | |
| 131 return contents; | |
| 132 } | |
| 133 | |
| 134 // Try to find an app window and get its web contents. | |
| 135 const extensions::Extension* extension = function_->extension(); | |
| 136 if (extension) { | |
| 137 extensions::AppWindow* window = | |
| 138 extensions::AppWindowRegistry::Get(GetProfile()) | |
| 139 ->GetCurrentAppWindowForApp(extension->id()); | |
| 140 if (window) | |
| 141 return window->web_contents(); | |
| 142 } | |
| 143 return contents; | |
| 144 } | |
| 145 | |
| 146 gfx::NativeWindow ChromeExtensionFunctionDetails::GetNativeWindowForUI() { | 107 gfx::NativeWindow ChromeExtensionFunctionDetails::GetNativeWindowForUI() { |
| 147 // Try to use WindowControllerList first because WebContents's | 108 // Try to use WindowControllerList first because WebContents's |
| 148 // GetTopLevelNativeWindow() can't return the top level window when the tab | 109 // GetTopLevelNativeWindow() can't return the top level window when the tab |
| 149 // is not focused. | 110 // is not focused. |
| 150 WindowController* controller = | 111 WindowController* controller = |
| 151 extensions::WindowControllerList::GetInstance()->CurrentWindowForFunction( | 112 extensions::WindowControllerList::GetInstance()->CurrentWindowForFunction( |
| 152 function_); | 113 function_); |
| 153 if (controller) | 114 if (controller) |
| 154 return controller->window()->GetNativeWindow(); | 115 return controller->window()->GetNativeWindow(); |
| 155 | 116 |
| 156 // CurrentWindowForFunction() can't find app's window. | 117 // Next, check the sender web contents for if it supports modal dialogs. |
| 157 WebContents* contents = GetOriginWebContents(); | 118 // TODO(devlin): This seems weird. Why wouldn't we check this first? |
| 158 if (contents) | 119 content::WebContents* sender_web_contents = function_->GetSenderWebContents(); |
| 159 return contents->GetTopLevelNativeWindow(); | 120 if (sender_web_contents && |
| 121 web_modal::WebContentsModalDialogManager::FromWebContents( |
| 122 sender_web_contents)) { |
| 123 return sender_web_contents->GetTopLevelNativeWindow(); |
| 124 } |
| 160 | 125 |
| 126 // Then, check for any app windows that are open. |
| 127 if (function_->extension() && |
| 128 function_->extension()->is_app()) { |
| 129 extensions::AppWindow* window = |
| 130 extensions::AppWindowRegistry::Get(function_->browser_context()) |
| 131 ->GetCurrentAppWindowForApp(function_->extension()->id()); |
| 132 if (window) |
| 133 return window->web_contents()->GetTopLevelNativeWindow(); |
| 134 } |
| 135 |
| 136 // As a last resort, find a browser. |
| 161 Browser* browser = chrome::FindBrowserWithProfile(GetProfile()); | 137 Browser* browser = chrome::FindBrowserWithProfile(GetProfile()); |
| 162 return browser->window()->GetNativeWindow(); | 138 return browser->window()->GetNativeWindow(); |
| 163 } | 139 } |
| OLD | NEW |