| 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 "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_dispatcher.h" | 15 #include "extensions/browser/extension_function_dispatcher.h" |
| 16 | 16 |
| 17 using content::RenderViewHost; | 17 using content::RenderViewHost; |
| 18 using content::WebContents; | 18 using content::WebContents; |
| 19 | 19 |
| 20 ChromeAsyncExtensionFunction::ChromeAsyncExtensionFunction() {} | 20 ChromeUIThreadExtensionFunction::ChromeUIThreadExtensionFunction() { |
| 21 } |
| 21 | 22 |
| 22 Profile* ChromeAsyncExtensionFunction::GetProfile() const { | 23 Profile* ChromeUIThreadExtensionFunction::GetProfile() const { |
| 23 return Profile::FromBrowserContext(context_); | 24 return Profile::FromBrowserContext(context_); |
| 24 } | 25 } |
| 25 | 26 |
| 26 bool ChromeAsyncExtensionFunction::CanOperateOnWindow( | 27 bool ChromeUIThreadExtensionFunction::CanOperateOnWindow( |
| 27 const extensions::WindowController* window_controller) const { | 28 const extensions::WindowController* window_controller) const { |
| 28 const extensions::Extension* extension = GetExtension(); | 29 const extensions::Extension* extension = GetExtension(); |
| 29 // |extension| is NULL for unit tests only. | 30 // |extension| is NULL for unit tests only. |
| 30 if (extension != NULL && !window_controller->IsVisibleToExtension(extension)) | 31 if (extension != NULL && !window_controller->IsVisibleToExtension(extension)) |
| 31 return false; | 32 return false; |
| 32 | 33 |
| 33 if (GetProfile() == window_controller->profile()) | 34 if (GetProfile() == window_controller->profile()) |
| 34 return true; | 35 return true; |
| 35 | 36 |
| 36 if (!include_incognito()) | 37 if (!include_incognito()) |
| 37 return false; | 38 return false; |
| 38 | 39 |
| 39 return GetProfile()->HasOffTheRecordProfile() && | 40 return GetProfile()->HasOffTheRecordProfile() && |
| 40 GetProfile()->GetOffTheRecordProfile() == window_controller->profile(); | 41 GetProfile()->GetOffTheRecordProfile() == window_controller->profile(); |
| 41 } | 42 } |
| 42 | 43 |
| 43 // TODO(stevenjb): Replace this with GetExtensionWindowController(). | 44 // TODO(stevenjb): Replace this with GetExtensionWindowController(). |
| 44 Browser* ChromeAsyncExtensionFunction::GetCurrentBrowser() { | 45 Browser* ChromeUIThreadExtensionFunction::GetCurrentBrowser() { |
| 45 // If the delegate has an associated browser, return it. | 46 // If the delegate has an associated browser, return it. |
| 46 if (dispatcher()) { | 47 if (dispatcher()) { |
| 47 extensions::WindowController* window_controller = | 48 extensions::WindowController* window_controller = |
| 48 dispatcher()->delegate()->GetExtensionWindowController(); | 49 dispatcher()->delegate()->GetExtensionWindowController(); |
| 49 if (window_controller) { | 50 if (window_controller) { |
| 50 Browser* browser = window_controller->GetBrowser(); | 51 Browser* browser = window_controller->GetBrowser(); |
| 51 if (browser) | 52 if (browser) |
| 52 return browser; | 53 return browser; |
| 53 } | 54 } |
| 54 } | 55 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 73 // a background_page onload chrome.tabs api call can make it into here | 74 // a background_page onload chrome.tabs api call can make it into here |
| 74 // before the browser is sufficiently initialized to return here, or | 75 // before the browser is sufficiently initialized to return here, or |
| 75 // all of this profile's browser windows may have been closed. | 76 // all of this profile's browser windows may have been closed. |
| 76 // A similar situation may arise during shutdown. | 77 // A similar situation may arise during shutdown. |
| 77 // TODO(rafaelw): Delay creation of background_page until the browser | 78 // TODO(rafaelw): Delay creation of background_page until the browser |
| 78 // is available. http://code.google.com/p/chromium/issues/detail?id=13284 | 79 // is available. http://code.google.com/p/chromium/issues/detail?id=13284 |
| 79 return NULL; | 80 return NULL; |
| 80 } | 81 } |
| 81 | 82 |
| 82 extensions::WindowController* | 83 extensions::WindowController* |
| 83 ChromeAsyncExtensionFunction::GetExtensionWindowController() { | 84 ChromeUIThreadExtensionFunction::GetExtensionWindowController() { |
| 84 // If the delegate has an associated window controller, return it. | 85 // If the delegate has an associated window controller, return it. |
| 85 if (dispatcher()) { | 86 if (dispatcher()) { |
| 86 extensions::WindowController* window_controller = | 87 extensions::WindowController* window_controller = |
| 87 dispatcher()->delegate()->GetExtensionWindowController(); | 88 dispatcher()->delegate()->GetExtensionWindowController(); |
| 88 if (window_controller) | 89 if (window_controller) |
| 89 return window_controller; | 90 return window_controller; |
| 90 } | 91 } |
| 91 | 92 |
| 92 return extensions::WindowControllerList::GetInstance() | 93 return extensions::WindowControllerList::GetInstance() |
| 93 ->CurrentWindowForFunction(this); | 94 ->CurrentWindowForFunction(this); |
| 94 } | 95 } |
| 95 | 96 |
| 96 content::WebContents* ChromeAsyncExtensionFunction::GetAssociatedWebContents() { | 97 content::WebContents* |
| 98 ChromeUIThreadExtensionFunction::GetAssociatedWebContents() { |
| 97 content::WebContents* web_contents = | 99 content::WebContents* web_contents = |
| 98 UIThreadExtensionFunction::GetAssociatedWebContents(); | 100 UIThreadExtensionFunction::GetAssociatedWebContents(); |
| 99 if (web_contents) | 101 if (web_contents) |
| 100 return web_contents; | 102 return web_contents; |
| 101 | 103 |
| 102 Browser* browser = GetCurrentBrowser(); | 104 Browser* browser = GetCurrentBrowser(); |
| 103 if (!browser) | 105 if (!browser) |
| 104 return NULL; | 106 return NULL; |
| 105 return browser->tab_strip_model()->GetActiveWebContents(); | 107 return browser->tab_strip_model()->GetActiveWebContents(); |
| 106 } | 108 } |
| 107 | 109 |
| 110 ChromeUIThreadExtensionFunction::~ChromeUIThreadExtensionFunction() { |
| 111 } |
| 112 |
| 113 ChromeAsyncExtensionFunction::ChromeAsyncExtensionFunction() { |
| 114 } |
| 115 |
| 108 ChromeAsyncExtensionFunction::~ChromeAsyncExtensionFunction() {} | 116 ChromeAsyncExtensionFunction::~ChromeAsyncExtensionFunction() {} |
| 109 | 117 |
| 110 ChromeSyncExtensionFunction::ChromeSyncExtensionFunction() {} | 118 ExtensionFunction::ResponseAction ChromeAsyncExtensionFunction::Run() { |
| 119 return RunAsync() ? RespondLater() : RespondNow(Error(error_)); |
| 120 } |
| 111 | 121 |
| 112 bool ChromeSyncExtensionFunction::RunImpl() { | 122 ChromeSyncExtensionFunction::ChromeSyncExtensionFunction() { |
| 113 SendResponse(RunSync()); | |
| 114 return true; | |
| 115 } | 123 } |
| 116 | 124 |
| 117 ChromeSyncExtensionFunction::~ChromeSyncExtensionFunction() {} | 125 ChromeSyncExtensionFunction::~ChromeSyncExtensionFunction() {} |
| 126 |
| 127 ExtensionFunction::ResponseAction ChromeSyncExtensionFunction::Run() { |
| 128 return RespondNow(RunSync() ? MultipleArguments(results_.get()) |
| 129 : Error(error_)); |
| 130 } |
| OLD | NEW |