| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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.h" | 5 #include "chrome/browser/extensions/chrome_extension_function.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "chrome/browser/extensions/chrome_extension_function_details.h" | 9 #include "chrome/browser/extensions/chrome_extension_function_details.h" |
| 10 #include "chrome/browser/extensions/window_controller.h" | 10 #include "chrome/browser/extensions/window_controller.h" |
| 11 #include "chrome/browser/extensions/window_controller_list.h" | 11 #include "chrome/browser/extensions/window_controller_list.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_finder.h" | 14 #include "chrome/browser/ui/browser_finder.h" |
| 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 16 #include "content/public/browser/render_process_host.h" | 16 #include "content/public/browser/render_process_host.h" |
| 17 #include "content/public/browser/render_view_host.h" | 17 #include "content/public/browser/render_view_host.h" |
| 18 #include "extensions/browser/extension_function_dispatcher.h" | 18 #include "extensions/browser/extension_function_dispatcher.h" |
| 19 | 19 |
| 20 using content::RenderViewHost; | 20 using content::RenderViewHost; |
| 21 using content::WebContents; | 21 using content::WebContents; |
| 22 | 22 |
| 23 ChromeUIThreadExtensionFunction::ChromeUIThreadExtensionFunction() { | 23 ChromeUIThreadExtensionFunction::ChromeUIThreadExtensionFunction() |
| 24 } | 24 : chrome_details_(this) {} |
| 25 | 25 |
| 26 Profile* ChromeUIThreadExtensionFunction::GetProfile() const { | 26 Profile* ChromeUIThreadExtensionFunction::GetProfile() const { |
| 27 return Profile::FromBrowserContext(context_); | 27 return Profile::FromBrowserContext(context_); |
| 28 } | 28 } |
| 29 | 29 |
| 30 // TODO(stevenjb): Replace this with GetExtensionWindowController(). | 30 // TODO(stevenjb): Replace this with GetExtensionWindowController(). |
| 31 Browser* ChromeUIThreadExtensionFunction::GetCurrentBrowser() { | 31 Browser* ChromeUIThreadExtensionFunction::GetCurrentBrowser() { |
| 32 // If the delegate has an associated browser, return it. | 32 return chrome_details_.GetCurrentBrowser(); |
| 33 if (dispatcher()) { | |
| 34 extensions::WindowController* window_controller = | |
| 35 dispatcher()->GetExtensionWindowController(); | |
| 36 if (window_controller) { | |
| 37 Browser* browser = window_controller->GetBrowser(); | |
| 38 if (browser) | |
| 39 return browser; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 // Otherwise, try to default to a reasonable browser. If |include_incognito_| | |
| 44 // is true, we will also search browsers in the incognito version of this | |
| 45 // profile. Note that the profile may already be incognito, in which case | |
| 46 // we will search the incognito version only, regardless of the value of | |
| 47 // |include_incognito|. Look only for browsers on the active desktop as it is | |
| 48 // preferable to pretend no browser is open then to return a browser on | |
| 49 // another desktop. | |
| 50 content::WebContents* web_contents = GetSenderWebContents(); | |
| 51 Profile* profile = Profile::FromBrowserContext( | |
| 52 web_contents ? web_contents->GetBrowserContext() : browser_context()); | |
| 53 Browser* browser = chrome::FindAnyBrowser(profile, include_incognito_); | |
| 54 if (browser) | |
| 55 return browser; | |
| 56 | |
| 57 // NOTE(rafaelw): This can return NULL in some circumstances. In particular, | |
| 58 // a background_page onload chrome.tabs api call can make it into here | |
| 59 // before the browser is sufficiently initialized to return here, or | |
| 60 // all of this profile's browser windows may have been closed. | |
| 61 // A similar situation may arise during shutdown. | |
| 62 // TODO(rafaelw): Delay creation of background_page until the browser | |
| 63 // is available. http://code.google.com/p/chromium/issues/detail?id=13284 | |
| 64 return NULL; | |
| 65 } | 33 } |
| 66 | 34 |
| 67 extensions::WindowController* | 35 extensions::WindowController* |
| 68 ChromeUIThreadExtensionFunction::GetExtensionWindowController() { | 36 ChromeUIThreadExtensionFunction::GetExtensionWindowController() { |
| 69 // If the delegate has an associated window controller, return it. | 37 return chrome_details_.GetExtensionWindowController(); |
| 70 if (dispatcher()) { | |
| 71 extensions::WindowController* window_controller = | |
| 72 dispatcher()->GetExtensionWindowController(); | |
| 73 if (window_controller) | |
| 74 return window_controller; | |
| 75 } | |
| 76 | |
| 77 return extensions::WindowControllerList::GetInstance() | |
| 78 ->CurrentWindowForFunction(this); | |
| 79 } | 38 } |
| 80 | 39 |
| 81 content::WebContents* | 40 content::WebContents* |
| 82 ChromeUIThreadExtensionFunction::GetAssociatedWebContents() { | 41 ChromeUIThreadExtensionFunction::GetAssociatedWebContents() { |
| 83 content::WebContents* web_contents = | 42 return chrome_details_.GetAssociatedWebContents(); |
| 84 UIThreadExtensionFunction::GetAssociatedWebContents(); | |
| 85 if (web_contents) | |
| 86 return web_contents; | |
| 87 | |
| 88 Browser* browser = GetCurrentBrowser(); | |
| 89 if (!browser) | |
| 90 return NULL; | |
| 91 return browser->tab_strip_model()->GetActiveWebContents(); | |
| 92 } | 43 } |
| 93 | 44 |
| 94 ChromeUIThreadExtensionFunction::~ChromeUIThreadExtensionFunction() { | 45 ChromeUIThreadExtensionFunction::~ChromeUIThreadExtensionFunction() { |
| 95 } | 46 } |
| 96 | 47 |
| 97 ChromeAsyncExtensionFunction::ChromeAsyncExtensionFunction() { | 48 ChromeAsyncExtensionFunction::ChromeAsyncExtensionFunction() { |
| 98 } | 49 } |
| 99 | 50 |
| 100 ChromeAsyncExtensionFunction::~ChromeAsyncExtensionFunction() {} | 51 ChromeAsyncExtensionFunction::~ChromeAsyncExtensionFunction() {} |
| 101 | 52 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 117 ExtensionFunction::ResponseAction ChromeSyncExtensionFunction::Run() { | 68 ExtensionFunction::ResponseAction ChromeSyncExtensionFunction::Run() { |
| 118 return RespondNow(RunSync() ? ArgumentList(std::move(results_)) | 69 return RespondNow(RunSync() ? ArgumentList(std::move(results_)) |
| 119 : Error(error_)); | 70 : Error(error_)); |
| 120 } | 71 } |
| 121 | 72 |
| 122 // static | 73 // static |
| 123 bool ChromeSyncExtensionFunction::ValidationFailure( | 74 bool ChromeSyncExtensionFunction::ValidationFailure( |
| 124 ChromeSyncExtensionFunction* function) { | 75 ChromeSyncExtensionFunction* function) { |
| 125 return false; | 76 return false; |
| 126 } | 77 } |
| OLD | NEW |