Commit 0e53cdbb authored by Devlin Cronin's avatar Devlin Cronin Committed by Commit Bot

[Extensions] Remove NOTIFICATION_EXTENSION_COMMAND_[ADDED|REMOVED]

The NotificationService is deprecated. Remove two notifications from it:
NOTIFICATION_EXTENSION_COMMAND_ADDED and
NOTIFICATION_EXTENSION_COMMAND_REMOVED. These (as the names imply) were
used to notify of when an extension command was added or removed, but
can be substituted with the CommandService::Observer.

This also fixes a bad cast bug.

Bug: 411569
Change-Id: I7b2f0a1cf006a80d62571d20cf8a20968f2d0ac0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2155248Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Commit-Queue: Devlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#761093}
parent 594d7a08
...@@ -69,14 +69,6 @@ bool IsForCurrentPlatform(const std::string& key) { ...@@ -69,14 +69,6 @@ bool IsForCurrentPlatform(const std::string& key) {
base::CompareCase::SENSITIVE); base::CompareCase::SENSITIVE);
} }
std::string StripCurrentPlatform(const std::string& key) {
DCHECK(IsForCurrentPlatform(key));
std::string result = key;
base::ReplaceFirstSubstringAfterOffset(
&result, 0, Command::CommandPlatform() + ":", base::StringPiece());
return result;
}
// Merge |suggested_key_prefs| into the saved preferences for the extension. We // Merge |suggested_key_prefs| into the saved preferences for the extension. We
// merge rather than overwrite to preserve existing was_assigned preferences. // merge rather than overwrite to preserve existing was_assigned preferences.
void MergeSuggestedKeyPrefs( void MergeSuggestedKeyPrefs(
...@@ -115,6 +107,8 @@ CommandService::CommandService(content::BrowserContext* context) ...@@ -115,6 +107,8 @@ CommandService::CommandService(content::BrowserContext* context)
} }
CommandService::~CommandService() { CommandService::~CommandService() {
for (auto& observer : observers_)
observer.OnCommandServiceDestroying();
} }
static base::LazyInstance<BrowserContextKeyedAPIFactory<CommandService>>:: static base::LazyInstance<BrowserContextKeyedAPIFactory<CommandService>>::
...@@ -236,19 +230,9 @@ bool CommandService::AddKeybindingPref( ...@@ -236,19 +230,9 @@ bool CommandService::AddKeybindingPref(
std::move(suggested_key_prefs)); std::move(suggested_key_prefs));
// Fetch the newly-updated command, and notify the observers. // Fetch the newly-updated command, and notify the observers.
for (auto& observer : observers_) { Command command = FindCommandByName(extension_id, command_name);
observer.OnExtensionCommandAdded( for (auto& observer : observers_)
extension_id, FindCommandByName(extension_id, command_name)); observer.OnExtensionCommandAdded(extension_id, command);
}
// TODO(devlin): Deprecate this notification in favor of the observers.
std::pair<const std::string, const std::string> details =
std::make_pair(extension_id, command_name);
content::NotificationService::current()->Notify(
extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED,
content::Source<Profile>(profile_),
content::Details<std::pair<const std::string, const std::string> >(
&details));
return true; return true;
} }
...@@ -653,14 +637,6 @@ void CommandService::RemoveKeybindingPrefs(const std::string& extension_id, ...@@ -653,14 +637,6 @@ void CommandService::RemoveKeybindingPrefs(const std::string& extension_id,
it != keys_to_remove.end(); ++it) { it != keys_to_remove.end(); ++it) {
std::string key = *it; std::string key = *it;
bindings->Remove(key, NULL); bindings->Remove(key, NULL);
// TODO(devlin): Deprecate this notification in favor of the observers.
ExtensionCommandRemovedDetails details(extension_id, command_name,
StripCurrentPlatform(key));
content::NotificationService::current()->Notify(
extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED,
content::Source<Profile>(profile_),
content::Details<ExtensionCommandRemovedDetails>(&details));
} }
for (const Command& removed_command : removed_commands) { for (const Command& removed_command : removed_commands) {
......
...@@ -78,6 +78,10 @@ class CommandService : public BrowserContextKeyedAPI, ...@@ -78,6 +78,10 @@ class CommandService : public BrowserContextKeyedAPI,
// Called when an extension command is removed. // Called when an extension command is removed.
virtual void OnExtensionCommandRemoved(const std::string& extension_id, virtual void OnExtensionCommandRemoved(const std::string& extension_id,
const Command& command) {} const Command& command) {}
// Called when the CommandService is being destroyed.
virtual void OnCommandServiceDestroying() {}
protected: protected:
virtual ~Observer() {} virtual ~Observer() {}
}; };
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/browser/media_keys_listener_manager.h" #include "content/public/browser/media_keys_listener_manager.h"
#include "extensions/browser/event_router.h" #include "extensions/browser/event_router.h"
#include "extensions/browser/notification_types.h"
#include "extensions/common/extension_set.h" #include "extensions/common/extension_set.h"
#include "extensions/common/manifest_constants.h" #include "extensions/common/manifest_constants.h"
...@@ -39,14 +38,7 @@ ExtensionKeybindingRegistry::ExtensionKeybindingRegistry( ...@@ -39,14 +38,7 @@ ExtensionKeybindingRegistry::ExtensionKeybindingRegistry(
delegate_(delegate), delegate_(delegate),
shortcut_handling_suspended_(false) { shortcut_handling_suspended_(false) {
extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
command_service_observer_.Add(CommandService::Get(browser_context_));
Profile* profile = Profile::FromBrowserContext(browser_context_);
registrar_.Add(this,
extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED,
content::Source<Profile>(profile->GetOriginalProfile()));
registrar_.Add(this,
extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED,
content::Source<Profile>(profile->GetOriginalProfile()));
media_keys_listener_ = ui::MediaKeysListener::Create( media_keys_listener_ = ui::MediaKeysListener::Create(
this, ui::MediaKeysListener::Scope::kFocused); this, ui::MediaKeysListener::Scope::kFocused);
} }
...@@ -241,43 +233,44 @@ void ExtensionKeybindingRegistry::OnExtensionUnloaded( ...@@ -241,43 +233,44 @@ void ExtensionKeybindingRegistry::OnExtensionUnloaded(
RemoveExtensionKeybinding(extension, std::string()); RemoveExtensionKeybinding(extension, std::string());
} }
void ExtensionKeybindingRegistry::Observe( void ExtensionKeybindingRegistry::OnExtensionCommandAdded(
int type, const std::string& extension_id,
const content::NotificationSource& source, const Command& command) {
const content::NotificationDetails& details) { const Extension* extension = ExtensionRegistry::Get(browser_context_)
switch (type) { ->enabled_extensions()
case extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED: .GetByID(extension_id);
case extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { // During install and uninstall the extension won't be found. We'll catch
ExtensionCommandRemovedDetails* payload = // those events above, with the OnExtension[Unloaded|Loaded], so we ignore
content::Details<ExtensionCommandRemovedDetails>(details).ptr(); // this event.
if (!extension || !ExtensionMatchesFilter(extension))
const Extension* extension = ExtensionRegistry::Get(browser_context_) return;
->enabled_extensions()
.GetByID(payload->extension_id); // Component extensions trigger OnExtensionLoaded() for extension
// During install and uninstall the extension won't be found. We'll catch // installs as well as loads. This can cause adding of multiple key
// those events above, with the LOADED/UNLOADED, so we ignore this event. // targets.
if (!extension) if (extension->location() == Manifest::COMPONENT)
return; return;
if (ExtensionMatchesFilter(extension)) { AddExtensionKeybindings(extension, command.command_name());
if (type == extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED) { }
// Component extensions triggers OnExtensionLoaded for extension
// installs as well as loads. This can cause adding of multiple key void ExtensionKeybindingRegistry::OnExtensionCommandRemoved(
// targets. const std::string& extension_id,
if (extension->location() == Manifest::COMPONENT) const Command& command) {
return; const Extension* extension = ExtensionRegistry::Get(browser_context_)
->enabled_extensions()
AddExtensionKeybindings(extension, payload->command_name); .GetByID(extension_id);
} else { // During install and uninstall the extension won't be found. We'll catch
RemoveExtensionKeybinding(extension, payload->command_name); // those events above, with the OnExtension[Unloaded|Loaded], so we ignore
} // this event.
} if (!extension || !ExtensionMatchesFilter(extension))
break; return;
}
default: RemoveExtensionKeybinding(extension, command.command_name());
NOTREACHED(); }
break;
} void ExtensionKeybindingRegistry::OnCommandServiceDestroying() {
command_service_observer_.RemoveAll();
} }
void ExtensionKeybindingRegistry::OnMediaKeysAccelerator( void ExtensionKeybindingRegistry::OnMediaKeysAccelerator(
......
...@@ -13,10 +13,7 @@ ...@@ -13,10 +13,7 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
#include "content/public/browser/notification_details.h" #include "chrome/browser/extensions/api/commands/command_service.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_source.h"
#include "extensions/browser/extension_registry.h" #include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h" #include "extensions/browser/extension_registry_observer.h"
#include "ui/base/accelerators/media_keys_listener.h" #include "ui/base/accelerators/media_keys_listener.h"
...@@ -37,7 +34,7 @@ class Extension; ...@@ -37,7 +34,7 @@ class Extension;
// The ExtensionKeybindingRegistry is a class that handles the cross-platform // The ExtensionKeybindingRegistry is a class that handles the cross-platform
// logic for keyboard accelerators. See platform-specific implementations for // logic for keyboard accelerators. See platform-specific implementations for
// implementation details for each platform. // implementation details for each platform.
class ExtensionKeybindingRegistry : public content::NotificationObserver, class ExtensionKeybindingRegistry : public CommandService::Observer,
public ExtensionRegistryObserver, public ExtensionRegistryObserver,
public ui::MediaKeysListener::Delegate { public ui::MediaKeysListener::Delegate {
public: public:
...@@ -134,10 +131,12 @@ class ExtensionKeybindingRegistry : public content::NotificationObserver, ...@@ -134,10 +131,12 @@ class ExtensionKeybindingRegistry : public content::NotificationObserver,
content::BrowserContext* browser_context() const { return browser_context_; } content::BrowserContext* browser_context() const { return browser_context_; }
private: private:
// Overridden from content::NotificationObserver: // extensions::CommandService::Observer:
void Observe(int type, void OnExtensionCommandAdded(const std::string& extension_id,
const content::NotificationSource& source, const Command& command) override;
const content::NotificationDetails& details) override; void OnExtensionCommandRemoved(const std::string& extension_id,
const Command& command) override;
void OnCommandServiceDestroying() override;
// ExtensionRegistryObserver implementation. // ExtensionRegistryObserver implementation.
void OnExtensionLoaded(content::BrowserContext* browser_context, void OnExtensionLoaded(content::BrowserContext* browser_context,
...@@ -162,9 +161,6 @@ class ExtensionKeybindingRegistry : public content::NotificationObserver, ...@@ -162,9 +161,6 @@ class ExtensionKeybindingRegistry : public content::NotificationObserver,
// Returns true if any media keys are registered. // Returns true if any media keys are registered.
bool IsListeningToAnyMediaKeys() const; bool IsListeningToAnyMediaKeys() const;
// The content notification registrar for listening to extension events.
content::NotificationRegistrar registrar_;
content::BrowserContext* browser_context_; content::BrowserContext* browser_context_;
// What extensions to register keybindings for. // What extensions to register keybindings for.
...@@ -188,6 +184,9 @@ class ExtensionKeybindingRegistry : public content::NotificationObserver, ...@@ -188,6 +184,9 @@ class ExtensionKeybindingRegistry : public content::NotificationObserver,
ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
extension_registry_observer_{this}; extension_registry_observer_{this};
ScopedObserver<CommandService, CommandService::Observer>
command_service_observer_{this};
// Keeps track of whether shortcut handling is currently suspended. Shortcuts // Keeps track of whether shortcut handling is currently suspended. Shortcuts
// are suspended briefly while capturing which shortcut to assign to an // are suspended briefly while capturing which shortcut to assign to an
// extension command in the Config UI. If handling isn't suspended while // extension command in the Config UI. If handling isn't suspended while
......
...@@ -20,9 +20,6 @@ ...@@ -20,9 +20,6 @@
#include "chrome/browser/ui/views/toolbar/toolbar_view.h" #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/common/extensions/api/extension_action/action_info.h" #include "chrome/common/extensions/api/extension_action/action_info.h"
#include "chrome/common/extensions/command.h" #include "chrome/common/extensions/command.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "extensions/browser/notification_types.h"
#include "extensions/common/extension.h" #include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h" #include "extensions/common/manifest_constants.h"
#include "ui/views/view.h" #include "ui/views/view.h"
...@@ -39,14 +36,8 @@ ExtensionActionPlatformDelegate::Create( ...@@ -39,14 +36,8 @@ ExtensionActionPlatformDelegate::Create(
ExtensionActionPlatformDelegateViews::ExtensionActionPlatformDelegateViews( ExtensionActionPlatformDelegateViews::ExtensionActionPlatformDelegateViews(
ExtensionActionViewController* controller) ExtensionActionViewController* controller)
: controller_(controller) { : controller_(controller) {
content::NotificationSource notification_source = content::Source<Profile>( command_service_observer_.Add(
controller_->browser()->profile()->GetOriginalProfile()); extensions::CommandService::Get(controller_->browser()->profile()));
registrar_.Add(this,
extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED,
notification_source);
registrar_.Add(this,
extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED,
notification_source);
} }
ExtensionActionPlatformDelegateViews::~ExtensionActionPlatformDelegateViews() { ExtensionActionPlatformDelegateViews::~ExtensionActionPlatformDelegateViews() {
...@@ -91,25 +82,42 @@ void ExtensionActionPlatformDelegateViews::ShowContextMenu() { ...@@ -91,25 +82,42 @@ void ExtensionActionPlatformDelegateViews::ShowContextMenu() {
view, view->GetKeyboardContextMenuLocation(), ui::MENU_SOURCE_NONE); view, view->GetKeyboardContextMenuLocation(), ui::MENU_SOURCE_NONE);
} }
void ExtensionActionPlatformDelegateViews::Observe( void ExtensionActionPlatformDelegateViews::OnExtensionCommandAdded(
int type, const std::string& extension_id,
const content::NotificationSource& source, const extensions::Command& command) {
const content::NotificationDetails& details) { if (extension_id != controller_->extension()->id())
DCHECK(type == extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED || return; // Not this action's extension.
type == extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED);
extensions::ExtensionCommandRemovedDetails* payload = if (command.command_name() !=
content::Details<extensions::ExtensionCommandRemovedDetails>(details) extensions::manifest_values::kBrowserActionCommandEvent &&
.ptr(); command.command_name() !=
if (controller_->extension()->id() == payload->extension_id && extensions::manifest_values::kPageActionCommandEvent) {
(payload->command_name == // Not an action-related command.
extensions::manifest_values::kBrowserActionCommandEvent || return;
payload->command_name ==
extensions::manifest_values::kPageActionCommandEvent)) {
if (type == extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED)
RegisterCommand();
else
UnregisterCommand(true);
} }
RegisterCommand();
}
void ExtensionActionPlatformDelegateViews::OnExtensionCommandRemoved(
const std::string& extension_id,
const extensions::Command& command) {
if (extension_id != controller_->extension()->id())
return;
if (command.command_name() !=
extensions::manifest_values::kBrowserActionCommandEvent &&
command.command_name() !=
extensions::manifest_values::kPageActionCommandEvent) {
// Not an action-related command.
return;
}
UnregisterCommand(/*only_if_removed=*/true);
}
void ExtensionActionPlatformDelegateViews::OnCommandServiceDestroying() {
command_service_observer_.RemoveAll();
} }
bool ExtensionActionPlatformDelegateViews::AcceleratorPressed( bool ExtensionActionPlatformDelegateViews::AcceleratorPressed(
......
...@@ -6,9 +6,9 @@ ...@@ -6,9 +6,9 @@
#define CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_PLATFORM_DELEGATE_VIEWS_H_ #define CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_PLATFORM_DELEGATE_VIEWS_H_
#include "base/macros.h" #include "base/macros.h"
#include "base/scoped_observer.h"
#include "chrome/browser/extensions/api/commands/command_service.h"
#include "chrome/browser/ui/extensions/extension_action_platform_delegate.h" #include "chrome/browser/ui/extensions/extension_action_platform_delegate.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "ui/base/accelerators/accelerator.h" #include "ui/base/accelerators/accelerator.h"
class ToolbarActionViewDelegateViews; class ToolbarActionViewDelegateViews;
...@@ -23,7 +23,7 @@ class ToolbarActionViewDelegateViews; ...@@ -23,7 +23,7 @@ class ToolbarActionViewDelegateViews;
// the views::View wrapper. // the views::View wrapper.
class ExtensionActionPlatformDelegateViews class ExtensionActionPlatformDelegateViews
: public ExtensionActionPlatformDelegate, : public ExtensionActionPlatformDelegate,
public content::NotificationObserver, public extensions::CommandService::Observer,
public ui::AcceleratorTarget { public ui::AcceleratorTarget {
public: public:
ExtensionActionPlatformDelegateViews( ExtensionActionPlatformDelegateViews(
...@@ -39,10 +39,12 @@ class ExtensionActionPlatformDelegateViews ...@@ -39,10 +39,12 @@ class ExtensionActionPlatformDelegateViews
ExtensionActionViewController::PopupShowAction show_action) override; ExtensionActionViewController::PopupShowAction show_action) override;
void ShowContextMenu() override; void ShowContextMenu() override;
// content::NotificationObserver: // extensions::CommandService::Observer:
void Observe(int type, void OnExtensionCommandAdded(const std::string& extension_id,
const content::NotificationSource& source, const extensions::Command& command) override;
const content::NotificationDetails& details) override; void OnExtensionCommandRemoved(const std::string& extension_id,
const extensions::Command& command) override;
void OnCommandServiceDestroying() override;
// ui::AcceleratorTarget: // ui::AcceleratorTarget:
bool AcceleratorPressed(const ui::Accelerator& accelerator) override; bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
...@@ -62,7 +64,9 @@ class ExtensionActionPlatformDelegateViews ...@@ -62,7 +64,9 @@ class ExtensionActionPlatformDelegateViews
// for (to show the popup). // for (to show the popup).
std::unique_ptr<ui::Accelerator> action_keybinding_; std::unique_ptr<ui::Accelerator> action_keybinding_;
content::NotificationRegistrar registrar_; ScopedObserver<extensions::CommandService,
extensions::CommandService::Observer>
command_service_observer_{this};
DISALLOW_COPY_AND_ASSIGN(ExtensionActionPlatformDelegateViews); DISALLOW_COPY_AND_ASSIGN(ExtensionActionPlatformDelegateViews);
}; };
......
...@@ -57,9 +57,6 @@ class ExtensionKeybindingRegistryViews ...@@ -57,9 +57,6 @@ class ExtensionKeybindingRegistryViews
// accelerators with. Not owned by us. // accelerators with. Not owned by us.
views::FocusManager* focus_manager_; views::FocusManager* focus_manager_;
// The content notification registrar for listening to extension events.
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(ExtensionKeybindingRegistryViews); DISALLOW_COPY_AND_ASSIGN(ExtensionKeybindingRegistryViews);
}; };
......
...@@ -282,7 +282,6 @@ jumbo_source_set("browser_sources") { ...@@ -282,7 +282,6 @@ jumbo_source_set("browser_sources") {
"media_capture_util.h", "media_capture_util.h",
"mojo/keep_alive_impl.cc", "mojo/keep_alive_impl.cc",
"mojo/keep_alive_impl.h", "mojo/keep_alive_impl.h",
"notification_types.cc",
"notification_types.h", "notification_types.h",
"null_app_sorting.cc", "null_app_sorting.cc",
"null_app_sorting.h", "null_app_sorting.h",
......
// Copyright 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 "extensions/browser/notification_types.h"
namespace extensions {
ExtensionCommandRemovedDetails::ExtensionCommandRemovedDetails(
const std::string& extension_id,
const std::string& command_name,
const std::string& accelerator)
: extension_id(extension_id),
command_name(command_name),
accelerator(accelerator) {
}
} // namespace extensions
...@@ -106,17 +106,6 @@ enum NotificationType { ...@@ -106,17 +106,6 @@ enum NotificationType {
// Sent when a background page is ready so other components can load. // Sent when a background page is ready so other components can load.
NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY, NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
// Sent when an extension command has been removed. The source is the
// BrowserContext* and the details is an ExtensionCommandRemovedDetails
// consisting of std::strings representing an extension ID, the name of the
// command being removed, and the accelerator associated with the command.
NOTIFICATION_EXTENSION_COMMAND_REMOVED,
// Sent when an extension command has been added. The source is the
// BrowserContext* and the details is a std::pair of two std::string objects
// (an extension ID and the name of the command being added).
NOTIFICATION_EXTENSION_COMMAND_ADDED,
// Sent by an extension to notify the browser about the results of a unit // Sent by an extension to notify the browser about the results of a unit
// test. // test.
NOTIFICATION_EXTENSION_TEST_PASSED, NOTIFICATION_EXTENSION_TEST_PASSED,
...@@ -168,16 +157,6 @@ enum NotificationType { ...@@ -168,16 +157,6 @@ enum NotificationType {
NOTIFICATION_EXTENSIONS_END NOTIFICATION_EXTENSIONS_END
}; };
struct ExtensionCommandRemovedDetails {
ExtensionCommandRemovedDetails(const std::string& extension_id,
const std::string& command_name,
const std::string& accelerator);
std::string extension_id;
std::string command_name;
std::string accelerator;
};
// ** // **
// ** NOTICE // ** NOTICE
// ** // **
......
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