1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/bookmarks/enhanced_bookmark_key_service.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/extensions/api/commands/command_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/accelerator_utils.h"
14 #include "chrome/common/extensions/command.h"
15 #include "components/crx_file/id_util.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "extensions/browser/notification_types.h"
18 #include "extensions/common/manifest_constants.h"
20 EnhancedBookmarkKeyService::EnhancedBookmarkKeyService(
21 content::BrowserContext
* context
) : browser_context_(context
) {
22 Profile
* profile
= Profile::FromBrowserContext(browser_context_
);
24 extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED
,
25 content::Source
<Profile
>(profile
->GetOriginalProfile()));
28 EnhancedBookmarkKeyService::~EnhancedBookmarkKeyService() {
31 void EnhancedBookmarkKeyService::Observe(
33 const content::NotificationSource
& source
,
34 const content::NotificationDetails
& details
) {
35 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED
, type
);
37 const extensions::Extension
* enhanced_bookmark_extension
=
38 GetEnhancedBookmarkExtension();
39 if (!enhanced_bookmark_extension
)
42 extensions::ExtensionCommandRemovedDetails
* payload
=
43 content::Details
<extensions::ExtensionCommandRemovedDetails
>(details
)
46 if (payload
->extension_id
== enhanced_bookmark_extension
->id())
49 ui::Accelerator key
= extensions::Command::StringToAccelerator(
50 payload
->accelerator
, payload
->command_name
);
51 ui::Accelerator bookmark_accelerator
=
52 chrome::GetPrimaryChromeAcceleratorForCommandId(IDC_BOOKMARK_PAGE
);
53 if (key
== bookmark_accelerator
) {
54 extensions::CommandService
* command_service
=
55 extensions::CommandService::Get(browser_context_
);
56 extensions::Command existing_command
;
57 if (!command_service
->GetPageActionCommand(
58 enhanced_bookmark_extension
->id(),
59 extensions::CommandService::ACTIVE
,
60 &existing_command
, nullptr)) {
61 command_service
->AddKeybindingPref(
62 bookmark_accelerator
, enhanced_bookmark_extension
->id(),
63 extensions::manifest_values::kPageActionCommandEvent
, false,
69 const extensions::Extension
*
70 EnhancedBookmarkKeyService::GetEnhancedBookmarkExtension() const {
71 const extensions::ExtensionSet
& extensions
=
72 extensions::ExtensionRegistry::Get(browser_context_
)
73 ->enabled_extensions();
74 extensions::ExtensionSet::const_iterator loc
=
75 std::find_if(extensions
.begin(), extensions
.end(),
76 [](scoped_refptr
<const extensions::Extension
> extension
) {
77 static const char enhanced_ext_hash
[] =
78 // http://crbug.com/312900
79 "D5736E4B5CF695CB93A2FB57E4FDC6E5AFAB6FE2";
80 std::string hashed_id
=
81 crx_file::id_util::HashedIdInHex(extension
->id());
82 return hashed_id
== enhanced_ext_hash
;
84 return loc
!= extensions
.end() ? loc
->get() : nullptr;