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 @@
using content::RenderViewHost;
using content::WebContents;
ChromeUIThreadExtensionFunction::ChromeUIThreadExtensionFunction() {
}
ChromeUIThreadExtensionFunction::ChromeUIThreadExtensionFunction()
: chrome_details_(this) {}
Profile* ChromeUIThreadExtensionFunction::GetProfile() const {
return Profile::FromBrowserContext(context_);
......@@ -29,66 +29,17 @@ Profile* ChromeUIThreadExtensionFunction::GetProfile() const {
// TODO(stevenjb): Replace this with GetExtensionWindowController().
Browser* ChromeUIThreadExtensionFunction::GetCurrentBrowser() {
// If the delegate has an associated browser, return it.
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;
return chrome_details_.GetCurrentBrowser();
}
extensions::WindowController*
ChromeUIThreadExtensionFunction::GetExtensionWindowController() {
// If the delegate has an associated window controller, return it.
if (dispatcher()) {
extensions::WindowController* window_controller =
dispatcher()->GetExtensionWindowController();
if (window_controller)
return window_controller;
}
return extensions::WindowControllerList::GetInstance()
->CurrentWindowForFunction(this);
return chrome_details_.GetExtensionWindowController();
}
content::WebContents*
ChromeUIThreadExtensionFunction::GetAssociatedWebContents() {
content::WebContents* web_contents =
UIThreadExtensionFunction::GetAssociatedWebContents();
if (web_contents)
return web_contents;
Browser* browser = GetCurrentBrowser();
if (!browser)
return NULL;
return browser->tab_strip_model()->GetActiveWebContents();
return chrome_details_.GetAssociatedWebContents();
}
ChromeUIThreadExtensionFunction::~ChromeUIThreadExtensionFunction() {
......
......@@ -5,6 +5,7 @@
#ifndef 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"
class Browser;
......@@ -59,6 +60,9 @@ class ChromeUIThreadExtensionFunction : public UIThreadExtensionFunction {
protected:
~ChromeUIThreadExtensionFunction() override;
private:
ChromeExtensionFunctionDetails chrome_details_;
};
// A chrome specific analog to AsyncExtensionFunction. This has access to a
......
......@@ -56,14 +56,14 @@ Browser* ChromeExtensionFunctionDetails::GetCurrentBrowser() const {
// |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.
if (function_->render_frame_host()) {
Profile* profile = Profile::FromBrowserContext(
function_->render_frame_host()->GetProcess()->GetBrowserContext());
Browser* browser =
chrome::FindAnyBrowser(profile, function_->include_incognito());
if (browser)
return browser;
}
content::WebContents* web_contents = function_->GetSenderWebContents();
Profile* profile = Profile::FromBrowserContext(
web_contents ? web_contents->GetBrowserContext()
: function_->browser_context());
Browser* browser =
chrome::FindAnyBrowser(profile, function_->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
......@@ -91,9 +91,12 @@ ChromeExtensionFunctionDetails::GetExtensionWindowController() const {
content::WebContents*
ChromeExtensionFunctionDetails::GetAssociatedWebContents() {
content::WebContents* web_contents = function_->GetAssociatedWebContents();
if (web_contents)
return web_contents;
if (function_->dispatcher()) {
content::WebContents* web_contents =
function_->dispatcher()->GetAssociatedWebContents();
if (web_contents)
return web_contents;
}
Browser* browser = GetCurrentBrowser();
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