Commit eaa93027 authored by rdevlin.cronin's avatar rdevlin.cronin Committed by Commit bot

[Extensions] Reuse ChromeExtensionFunctionDetails

ChromeExtensionFunctionDetails and ChromeUIThreadExtensionFunction have
many identical methods, and the implementations are nearly identical as
well (down to the comments being replicated). Instead, just give
ChromeUIThreadExtensionFunction a ChromeExtensionFunctionDetails and
use it directly to reduce duplicate code.

BUG=None

Review-Url: https://codereview.chromium.org/2296603005
Cr-Commit-Position: refs/heads/master@{#415790}
parent ffdd85c2
...@@ -20,8 +20,8 @@ ...@@ -20,8 +20,8 @@
using content::RenderViewHost; using content::RenderViewHost;
using content::WebContents; using content::WebContents;
ChromeUIThreadExtensionFunction::ChromeUIThreadExtensionFunction() { ChromeUIThreadExtensionFunction::ChromeUIThreadExtensionFunction()
} : chrome_details_(this) {}
Profile* ChromeUIThreadExtensionFunction::GetProfile() const { Profile* ChromeUIThreadExtensionFunction::GetProfile() const {
return Profile::FromBrowserContext(context_); return Profile::FromBrowserContext(context_);
...@@ -29,66 +29,17 @@ Profile* ChromeUIThreadExtensionFunction::GetProfile() const { ...@@ -29,66 +29,17 @@ Profile* ChromeUIThreadExtensionFunction::GetProfile() const {
// TODO(stevenjb): Replace this with GetExtensionWindowController(). // TODO(stevenjb): Replace this with GetExtensionWindowController().
Browser* ChromeUIThreadExtensionFunction::GetCurrentBrowser() { Browser* ChromeUIThreadExtensionFunction::GetCurrentBrowser() {
// If the delegate has an associated browser, return it. return chrome_details_.GetCurrentBrowser();
if (dispatcher()) {
extensions::WindowController* window_controller =
dispatcher()->GetExtensionWindowController();
if (window_controller) {
Browser* browser = window_controller->GetBrowser();
if (browser)
return browser;
}
}
// Otherwise, try to default to a reasonable browser. If |include_incognito_|
// is true, we will also search browsers in the incognito version of this
// profile. Note that the profile may already be incognito, in which case
// we will search the incognito version only, regardless of the value of
// |include_incognito|. Look only for browsers on the active desktop as it is
// preferable to pretend no browser is open then to return a browser on
// another desktop.
content::WebContents* web_contents = GetSenderWebContents();
Profile* profile = Profile::FromBrowserContext(
web_contents ? web_contents->GetBrowserContext() : browser_context());
Browser* browser = chrome::FindAnyBrowser(profile, include_incognito_);
if (browser)
return browser;
// NOTE(rafaelw): This can return NULL in some circumstances. In particular,
// a background_page onload chrome.tabs api call can make it into here
// before the browser is sufficiently initialized to return here, or
// all of this profile's browser windows may have been closed.
// A similar situation may arise during shutdown.
// TODO(rafaelw): Delay creation of background_page until the browser
// is available. http://code.google.com/p/chromium/issues/detail?id=13284
return NULL;
} }
extensions::WindowController* extensions::WindowController*
ChromeUIThreadExtensionFunction::GetExtensionWindowController() { ChromeUIThreadExtensionFunction::GetExtensionWindowController() {
// If the delegate has an associated window controller, return it. return chrome_details_.GetExtensionWindowController();
if (dispatcher()) {
extensions::WindowController* window_controller =
dispatcher()->GetExtensionWindowController();
if (window_controller)
return window_controller;
}
return extensions::WindowControllerList::GetInstance()
->CurrentWindowForFunction(this);
} }
content::WebContents* content::WebContents*
ChromeUIThreadExtensionFunction::GetAssociatedWebContents() { ChromeUIThreadExtensionFunction::GetAssociatedWebContents() {
content::WebContents* web_contents = return chrome_details_.GetAssociatedWebContents();
UIThreadExtensionFunction::GetAssociatedWebContents();
if (web_contents)
return web_contents;
Browser* browser = GetCurrentBrowser();
if (!browser)
return NULL;
return browser->tab_strip_model()->GetActiveWebContents();
} }
ChromeUIThreadExtensionFunction::~ChromeUIThreadExtensionFunction() { ChromeUIThreadExtensionFunction::~ChromeUIThreadExtensionFunction() {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_EXTENSIONS_CHROME_EXTENSION_FUNCTION_H_ #ifndef CHROME_BROWSER_EXTENSIONS_CHROME_EXTENSION_FUNCTION_H_
#define CHROME_BROWSER_EXTENSIONS_CHROME_EXTENSION_FUNCTION_H_ #define CHROME_BROWSER_EXTENSIONS_CHROME_EXTENSION_FUNCTION_H_
#include "chrome/browser/extensions/chrome_extension_function_details.h"
#include "extensions/browser/extension_function.h" #include "extensions/browser/extension_function.h"
class Browser; class Browser;
...@@ -59,6 +60,9 @@ class ChromeUIThreadExtensionFunction : public UIThreadExtensionFunction { ...@@ -59,6 +60,9 @@ class ChromeUIThreadExtensionFunction : public UIThreadExtensionFunction {
protected: protected:
~ChromeUIThreadExtensionFunction() override; ~ChromeUIThreadExtensionFunction() override;
private:
ChromeExtensionFunctionDetails chrome_details_;
}; };
// A chrome specific analog to AsyncExtensionFunction. This has access to a // A chrome specific analog to AsyncExtensionFunction. This has access to a
......
...@@ -56,14 +56,14 @@ Browser* ChromeExtensionFunctionDetails::GetCurrentBrowser() const { ...@@ -56,14 +56,14 @@ Browser* ChromeExtensionFunctionDetails::GetCurrentBrowser() const {
// |include_incognito|. Look only for browsers on the active desktop as it is // |include_incognito|. Look only for browsers on the active desktop as it is
// preferable to pretend no browser is open then to return a browser on // preferable to pretend no browser is open then to return a browser on
// another desktop. // another desktop.
if (function_->render_frame_host()) { content::WebContents* web_contents = function_->GetSenderWebContents();
Profile* profile = Profile::FromBrowserContext( Profile* profile = Profile::FromBrowserContext(
function_->render_frame_host()->GetProcess()->GetBrowserContext()); web_contents ? web_contents->GetBrowserContext()
Browser* browser = : function_->browser_context());
chrome::FindAnyBrowser(profile, function_->include_incognito()); Browser* browser =
if (browser) chrome::FindAnyBrowser(profile, function_->include_incognito());
return browser; if (browser)
} return browser;
// NOTE(rafaelw): This can return NULL in some circumstances. In particular, // NOTE(rafaelw): This can return NULL in some circumstances. In particular,
// a background_page onload chrome.tabs api call can make it into here // a background_page onload chrome.tabs api call can make it into here
...@@ -91,9 +91,12 @@ ChromeExtensionFunctionDetails::GetExtensionWindowController() const { ...@@ -91,9 +91,12 @@ ChromeExtensionFunctionDetails::GetExtensionWindowController() const {
content::WebContents* content::WebContents*
ChromeExtensionFunctionDetails::GetAssociatedWebContents() { ChromeExtensionFunctionDetails::GetAssociatedWebContents() {
content::WebContents* web_contents = function_->GetAssociatedWebContents(); if (function_->dispatcher()) {
if (web_contents) content::WebContents* web_contents =
return web_contents; function_->dispatcher()->GetAssociatedWebContents();
if (web_contents)
return web_contents;
}
Browser* browser = GetCurrentBrowser(); Browser* browser = GetCurrentBrowser();
if (!browser) if (!browser)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment