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