Commit 2166dd4a authored by limasdf's avatar limasdf Committed by Commit bot

cleanup: Use ExtensionRegistryObserver in Uber

Use ExtensionRegistryObserver in Uber
since ExtensionRegistry replaces chrome::extension notifications.

BUG=411568
R=estade@chromium.org
TEST=browser_test --gtest_filter=UberUIBrowserTest.HistoryOverride

Review URL: https://codereview.chromium.org/1012773010

Cr-Commit-Position: refs/heads/master@{#322281}
parent 366eaf1d
...@@ -5,9 +5,6 @@ ...@@ -5,9 +5,6 @@
#include "chrome/browser/ui/webui/uber/uber_ui.h" #include "chrome/browser/ui/webui/uber/uber_ui.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
#include "chrome/browser/ui/webui/extensions/extensions_ui.h" #include "chrome/browser/ui/webui/extensions/extensions_ui.h"
#include "chrome/browser/ui/webui/options/options_ui.h" #include "chrome/browser/ui/webui/options/options_ui.h"
...@@ -16,6 +13,7 @@ ...@@ -16,6 +13,7 @@
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "chrome/grit/chromium_strings.h" #include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_controller.h" #include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h" #include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_source.h" #include "content/public/browser/notification_source.h"
...@@ -59,9 +57,10 @@ content::WebUIDataSource* CreateUberHTMLSource() { ...@@ -59,9 +57,10 @@ content::WebUIDataSource* CreateUberHTMLSource() {
} }
// Determines whether the user has an active extension of the given type. // Determines whether the user has an active extension of the given type.
bool HasExtensionType(Profile* profile, const std::string& extension_type) { bool HasExtensionType(content::BrowserContext* browser_context,
const std::string& extension_type) {
const extensions::ExtensionSet& extension_set = const extensions::ExtensionSet& extension_set =
extensions::ExtensionRegistry::Get(profile)->enabled_extensions(); extensions::ExtensionRegistry::Get(browser_context)->enabled_extensions();
for (extensions::ExtensionSet::const_iterator iter = extension_set.begin(); for (extensions::ExtensionSet::const_iterator iter = extension_set.begin();
iter != extension_set.end(); ++iter) { iter != extension_set.end(); ++iter) {
const extensions::URLOverrides::URLOverrideMap& map = const extensions::URLOverrides::URLOverrideMap& map =
...@@ -73,7 +72,8 @@ bool HasExtensionType(Profile* profile, const std::string& extension_type) { ...@@ -73,7 +72,8 @@ bool HasExtensionType(Profile* profile, const std::string& extension_type) {
return false; return false;
} }
content::WebUIDataSource* CreateUberFrameHTMLSource(Profile* profile) { content::WebUIDataSource* CreateUberFrameHTMLSource(
content::BrowserContext* browser_context) {
content::WebUIDataSource* source = content::WebUIDataSource* source =
content::WebUIDataSource::Create(chrome::kChromeUIUberFrameHost); content::WebUIDataSource::Create(chrome::kChromeUIUberFrameHost);
...@@ -106,7 +106,7 @@ content::WebUIDataSource* CreateUberFrameHTMLSource(Profile* profile) { ...@@ -106,7 +106,7 @@ content::WebUIDataSource* CreateUberFrameHTMLSource(Profile* profile) {
source->AddLocalizedString("settingsDisplayName", IDS_SETTINGS_TITLE); source->AddLocalizedString("settingsDisplayName", IDS_SETTINGS_TITLE);
source->AddString("settingsGroup", settings_group); source->AddString("settingsGroup", settings_group);
bool overridesHistory = bool overridesHistory =
HasExtensionType(profile, chrome::kChromeUIHistoryHost); HasExtensionType(browser_context, chrome::kChromeUIHistoryHost);
source->AddString("overridesHistory", overridesHistory ? "yes" : "no"); source->AddString("overridesHistory", overridesHistory ? "yes" : "no");
source->DisableDenyXFrameOptions(); source->DisableDenyXFrameOptions();
source->OverrideContentSecurityPolicyFrameSrc("frame-src chrome:;"); source->OverrideContentSecurityPolicyFrameSrc("frame-src chrome:;");
...@@ -114,11 +114,21 @@ content::WebUIDataSource* CreateUberFrameHTMLSource(Profile* profile) { ...@@ -114,11 +114,21 @@ content::WebUIDataSource* CreateUberFrameHTMLSource(Profile* profile) {
return source; return source;
} }
void UpdateHistoryNavigation(content::WebUI* web_ui) {
bool overrides_history =
HasExtensionType(web_ui->GetWebContents()->GetBrowserContext(),
chrome::kChromeUIHistoryHost);
web_ui->CallJavascriptFunction(
"uber_frame.setNavigationOverride",
base::StringValue(chrome::kChromeUIHistoryHost),
base::StringValue(overrides_history ? "yes" : "no"));
}
} // namespace } // namespace
UberUI::UberUI(content::WebUI* web_ui) : WebUIController(web_ui) { UberUI::UberUI(content::WebUI* web_ui) : WebUIController(web_ui) {
Profile* profile = Profile::FromWebUI(web_ui); content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(),
content::WebUIDataSource::Add(profile, CreateUberHTMLSource()); CreateUberHTMLSource());
RegisterSubpage(chrome::kChromeUIExtensionsFrameURL, RegisterSubpage(chrome::kChromeUIExtensionsFrameURL,
chrome::kChromeUIExtensionsHost); chrome::kChromeUIExtensionsHost);
...@@ -186,44 +196,36 @@ bool UberUI::OverrideHandleWebUIMessage(const GURL& source_url, ...@@ -186,44 +196,36 @@ bool UberUI::OverrideHandleWebUIMessage(const GURL& source_url,
// UberFrameUI // UberFrameUI
UberFrameUI::UberFrameUI(content::WebUI* web_ui) : WebUIController(web_ui) { UberFrameUI::UberFrameUI(content::WebUI* web_ui)
Profile* profile = Profile::FromWebUI(web_ui); : WebUIController(web_ui),
content::WebUIDataSource::Add(profile, CreateUberFrameHTMLSource(profile)); extension_registry_observer_(this) {
content::BrowserContext* browser_context =
web_ui->GetWebContents()->GetBrowserContext();
content::WebUIDataSource::Add(browser_context,
CreateUberFrameHTMLSource(browser_context));
// Register as an observer for when extensions are loaded and unloaded. // Register as an observer for when extensions are loaded and unloaded.
registrar_.Add(this, extension_registry_observer_.Add(
extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, extensions::ExtensionRegistry::Get(browser_context));
content::Source<Profile>(profile));
registrar_.Add(this,
extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
content::Source<Profile>(profile));
} }
UberFrameUI::~UberFrameUI() { UberFrameUI::~UberFrameUI() {
} }
void UberFrameUI::Observe(int type, void UberFrameUI::OnExtensionLoaded(content::BrowserContext* browser_context,
const content::NotificationSource& source, const extensions::Extension* extension) {
const content::NotificationDetails& details) { // We listen for notifications that indicate an extension has been loaded
switch (type) { // (i.e., has been installed and/or enabled) or unloaded (i.e., has been
// We listen for notifications that indicate an extension has been loaded // uninstalled and/or disabled). If one of these events has occurred, then
// (i.e., has been installed and/or enabled) or unloaded (i.e., has been // we must update the behavior of the History navigation element so that
// uninstalled and/or disabled). If one of these events has occurred, then // it opens the history extension if one is installed and enabled or
// we must update the behavior of the History navigation element so that // opens the default history page if one is uninstalled or disabled.
// it opens the history extension if one is installed and enabled or UpdateHistoryNavigation(web_ui());
// opens the default history page if one is uninstalled or disabled. }
case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED:
case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { void UberFrameUI::OnExtensionUnloaded(
Profile* profile = Profile::FromWebUI(web_ui()); content::BrowserContext* browser_context,
bool overrides_history = const extensions::Extension* extension,
HasExtensionType(profile, chrome::kChromeUIHistoryHost); extensions::UnloadedExtensionInfo::Reason reason) {
web_ui()->CallJavascriptFunction( UpdateHistoryNavigation(web_ui());
"uber_frame.setNavigationOverride",
base::StringValue(chrome::kChromeUIHistoryHost),
base::StringValue(overrides_history ? "yes" : "no"));
break;
}
default:
NOTREACHED();
}
} }
...@@ -7,12 +7,18 @@ ...@@ -7,12 +7,18 @@
#include <string> #include <string>
#include "base/memory/scoped_vector.h" #include "base/scoped_observer.h"
#include "base/values.h" #include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/web_ui_controller.h" #include "content/public/browser/web_ui_controller.h"
#include "extensions/browser/extension_registry_observer.h"
namespace content {
class BrowserContext;
}
namespace extensions {
class ExtensionRegistry;
}
// The WebUI class for the uber page (chrome://chrome). It manages the UI for // The WebUI class for the uber page (chrome://chrome). It manages the UI for
// the uber page (navigation bar and so forth) as well as WebUI objects for // the uber page (navigation bar and so forth) as well as WebUI objects for
...@@ -47,19 +53,24 @@ class UberUI : public content::WebUIController { ...@@ -47,19 +53,24 @@ class UberUI : public content::WebUIController {
DISALLOW_COPY_AND_ASSIGN(UberUI); DISALLOW_COPY_AND_ASSIGN(UberUI);
}; };
class UberFrameUI : public content::NotificationObserver, class UberFrameUI : public content::WebUIController,
public content::WebUIController { public extensions::ExtensionRegistryObserver {
public: public:
explicit UberFrameUI(content::WebUI* web_ui); explicit UberFrameUI(content::WebUI* web_ui);
~UberFrameUI() override; ~UberFrameUI() override;
private: private:
// content::NotificationObserver implementation. // extensions::ExtensionRegistryObserver implementation.
void Observe(int type, void OnExtensionLoaded(content::BrowserContext* browser_context,
const content::NotificationSource& source, const extensions::Extension* extension) override;
const content::NotificationDetails& details) override; void OnExtensionUnloaded(
content::BrowserContext* browser_context,
content::NotificationRegistrar registrar_; const extensions::Extension* extension,
extensions::UnloadedExtensionInfo::Reason reason) override;
ScopedObserver<extensions::ExtensionRegistry,
extensions::ExtensionRegistryObserver>
extension_registry_observer_;
DISALLOW_COPY_AND_ASSIGN(UberFrameUI); DISALLOW_COPY_AND_ASSIGN(UberFrameUI);
}; };
......
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/base/web_ui_browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
class UberUIBrowserTest : public WebUIBrowserTest {};
IN_PROC_BROWSER_TEST_F(UberUIBrowserTest, HistoryOverride) {
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIUberFrameURL));
content::WebContents* tab =
browser()->tab_strip_model()->GetActiveWebContents();
// Inject script. This script will be called when Extension is loaded.
const std::string inject_script =
"var is_called_override = false;"
"var uber_frame = {};"
"uber_frame.setNavigationOverride = function(controls, override) { "
"is_called_override = true; };";
ASSERT_TRUE(content::ExecuteScript(tab, inject_script));
scoped_refptr<const extensions::Extension> extension =
extensions::ExtensionBuilder()
.SetManifest(extensions::DictionaryBuilder()
.Set("name", "History Override")
.Set("version", "1")
.Set("manifest_version", 2)
.Set("permission",
extensions::ListBuilder().Append("history")))
.Build();
ExtensionService* service = extensions::ExtensionSystem::Get(
browser()->profile())->extension_service();
// Load extension. UberUI overrides history navigation.
// In this test, injected script will be called instead.
service->AddExtension(extension.get());
bool called_override;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
tab, "domAutomationController.send(is_called_override);",
&called_override));
EXPECT_TRUE(called_override);
}
...@@ -528,6 +528,7 @@ ...@@ -528,6 +528,7 @@
'browser/ui/webui/print_preview/print_preview_ui_browsertest.cc', 'browser/ui/webui/print_preview/print_preview_ui_browsertest.cc',
'browser/ui/webui/signin/inline_login_ui_browsertest.cc', 'browser/ui/webui/signin/inline_login_ui_browsertest.cc',
'browser/ui/webui/signin/user_manager_ui_browsertest.cc', 'browser/ui/webui/signin/user_manager_ui_browsertest.cc',
'browser/ui/webui/uber/uber_ui_browsertest.cc',
'browser/ui/webui/web_ui_test_handler.cc', 'browser/ui/webui/web_ui_test_handler.cc',
'browser/ui/webui/web_ui_test_handler.h', 'browser/ui/webui/web_ui_test_handler.h',
'browser/ui/webui/webui_webview_browsertest.cc', 'browser/ui/webui/webui_webview_browsertest.cc',
......
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