File: command_service.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (736 lines) | stat: -rw-r--r-- 28,470 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/commands/command_service.h"

#include <memory>
#include <string_view>
#include <utility>
#include <vector>

#include "base/lazy_instance.h"
#include "base/observer_list.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_commands_global_registry.h"
#include "chrome/browser/extensions/extension_keybinding_registry.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/accelerator_utils.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "extensions/browser/extension_function_registry.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/api/commands/commands_handler.h"
#include "extensions/common/command.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/feature_switch.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/permissions/permissions_data.h"
#include "ui/base/accelerators/command.h"

namespace extensions {
namespace {

const char kExtension[] = "extension";
const char kCommandName[] = "command_name";
const char kGlobal[] = "global";

// A preference that stores keybinding state associated with extension commands.
const char kCommands[] = "commands";

// Preference key name for saving the extension-suggested key.
const char kSuggestedKey[] = "suggested_key";

// Preference key name for saving whether the extension-suggested key was
// actually assigned.
const char kSuggestedKeyWasAssigned[] = "was_assigned";

std::string GetPlatformKeybindingKeyForAccelerator(
    const ui::Accelerator& accelerator,
    const ExtensionId& extension_id) {
  std::string key = Command::CommandPlatform() + ":" +
                    Command::AcceleratorToString(accelerator);

  // Media keys have a 1-to-many relationship with targets, unlike regular
  // shortcut (1-to-1 relationship). That means two or more extensions can
  // register for the same media key so the extension ID needs to be added to
  // the key to make sure the key is unique.
  if (accelerator.IsMediaKey()) {
    key += ":" + extension_id;
  }

  return key;
}

bool IsForCurrentPlatform(const std::string& key) {
  return base::StartsWith(key, Command::CommandPlatform() + ":",
                          base::CompareCase::SENSITIVE);
}

// Merge |suggested_key_prefs| into the saved preferences for the extension. We
// merge rather than overwrite to preserve existing was_assigned preferences.
void MergeSuggestedKeyPrefs(const ExtensionId& extension_id,
                            ExtensionPrefs* extension_prefs,
                            base::Value::Dict suggested_key_prefs) {
  const base::Value::Dict* current_prefs =
      extension_prefs->ReadPrefAsDict(extension_id, kCommands);
  if (current_prefs) {
    base::Value::Dict new_prefs = current_prefs->Clone();
    new_prefs.Merge(std::move(suggested_key_prefs));
    suggested_key_prefs = std::move(new_prefs);
  }

  extension_prefs->UpdateExtensionPref(
      extension_id, kCommands, base::Value(std::move(suggested_key_prefs)));
}

}  // namespace

// static
void CommandService::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
  registry->RegisterDictionaryPref(
      prefs::kExtensionCommands,
      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
}

CommandService::CommandService(content::BrowserContext* context)
    : profile_(Profile::FromBrowserContext(context)) {
  extension_registry_observation_.Observe(ExtensionRegistry::Get(profile_));
}

CommandService::~CommandService() {
  for (auto& observer : observers_)
    observer.OnCommandServiceDestroying();
}

static base::LazyInstance<BrowserContextKeyedAPIFactory<CommandService>>::
    DestructorAtExit g_command_service_factory = LAZY_INSTANCE_INITIALIZER;

// static
BrowserContextKeyedAPIFactory<CommandService>*
CommandService::GetFactoryInstance() {
  return g_command_service_factory.Pointer();
}

// static
CommandService* CommandService::Get(content::BrowserContext* context) {
  return BrowserContextKeyedAPIFactory<CommandService>::Get(context);
}

bool CommandService::GetNamedCommands(const ExtensionId& extension_id,
                                      QueryType type,
                                      CommandScope scope,
                                      ui::CommandMap* command_map) const {
  const Extension* extension =
      GetExtensionInEnabledOrDisabledExtensions(extension_id);
  if (!extension) {
    return false;
  }

  command_map->clear();
  const ui::CommandMap* commands = CommandsInfo::GetNamedCommands(extension);
  if (!commands)
    return false;

  for (const auto& named_command : *commands) {
    // Look up to see if the user has overridden how the command should work.
    Command saved_command =
        FindCommandByName(extension_id, named_command.second.command_name());
    ui::Accelerator shortcut_assigned = saved_command.accelerator();

    if (type == ACTIVE && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN)
      continue;

    ui::Command command = named_command.second;
    if (scope != ANY_SCOPE && ((scope == GLOBAL) != saved_command.global()))
      continue;

    if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN)
      command.set_accelerator(shortcut_assigned);
    command.set_global(saved_command.global());

    (*command_map)[named_command.second.command_name()] = command;
  }

  return !command_map->empty();
}

bool CommandService::AddKeybindingPref(const ui::Accelerator& accelerator,
                                       const ExtensionId& extension_id,
                                       const std::string& command_name,
                                       bool allow_overrides,
                                       bool global) {
  if (accelerator.key_code() == ui::VKEY_UNKNOWN)
    return false;

  // Nothing needs to be done if the existing command is the same as the desired
  // new one.
  Command existing_command = FindCommandByName(extension_id, command_name);
  if (existing_command.accelerator() == accelerator &&
      existing_command.global() == global)
    return true;

  // Media Keys are allowed to be used by named command only.
  DCHECK(!accelerator.IsMediaKey() ||
         !Command::IsActionRelatedCommand(command_name));

  ScopedDictPrefUpdate updater(profile_->GetPrefs(), prefs::kExtensionCommands);
  base::Value::Dict& bindings = updater.Get();

  std::string key = GetPlatformKeybindingKeyForAccelerator(accelerator,
                                                           extension_id);

  if (bindings.Find(key)) {
    if (!allow_overrides)
      return false;  // Already taken.

    // If the shortcut has been assigned to another command, it should be
    // removed before overriding, so that |ExtensionKeybindingRegistry| can get
    // a chance to do clean-up.
    const base::Value::Dict* item = bindings.FindDict(key);
    const ExtensionId* old_extension_id = item->FindString(kExtension);
    const std::string* old_command_name = item->FindString(kCommandName);
    RemoveKeybindingPrefs(old_extension_id ? *old_extension_id : std::string(),
                          old_command_name ? *old_command_name : std::string());
  }

  // If the command that is taking a new shortcut already has a shortcut, remove
  // it before assigning the new one.
  if (existing_command.accelerator().key_code() != ui::VKEY_UNKNOWN)
    RemoveKeybindingPrefs(extension_id, command_name);

  // Set the keybinding pref.
  base::Value::Dict keybinding;
  keybinding.Set(kExtension, extension_id);
  keybinding.Set(kCommandName, command_name);
  keybinding.Set(kGlobal, global);

  bindings.Set(key, std::move(keybinding));

  // Set the was_assigned pref for the suggested key.
  base::Value::Dict command_keys;
  command_keys.Set(kSuggestedKeyWasAssigned, true);
  base::Value::Dict suggested_key_prefs;
  suggested_key_prefs.Set(command_name, base::Value(std::move(command_keys)));
  MergeSuggestedKeyPrefs(extension_id, ExtensionPrefs::Get(profile_),
                         std::move(suggested_key_prefs));

  // Fetch the newly-updated command, and notify the observers.
  Command command = FindCommandByName(extension_id, command_name);
  for (auto& observer : observers_)
    observer.OnExtensionCommandAdded(extension_id, command);

  return true;
}

void CommandService::OnExtensionWillBeInstalled(
    content::BrowserContext* browser_context,
    const Extension* extension,
    bool is_update,
    const std::string& old_name) {
  UpdateKeybindings(extension);
}

void CommandService::OnExtensionUninstalled(
    content::BrowserContext* browser_context,
    const Extension* extension,
    extensions::UninstallReason reason) {
  // Adding a component extensions will only trigger install the first time on a
  // clean profile or on a version increase (see
  // ComponentLoader::AddComponentExtension). It will, however, always trigger
  // an uninstall on removal. See http://crbug.com/458612. Isolate this case and
  // ignore it.
  if (reason == extensions::UNINSTALL_REASON_COMPONENT_REMOVED)
    return;

  RemoveKeybindingPrefs(extension->id(), std::string());
}

void CommandService::UpdateKeybindingPrefs(const ExtensionId& extension_id,
                                           const std::string& command_name,
                                           const std::string& keystroke) {
  Command command = FindCommandByName(extension_id, command_name);

  // The extension command might be assigned another shortcut. Remove that
  // shortcut before proceeding.
  RemoveKeybindingPrefs(extension_id, command_name);

  ui::Accelerator accelerator =
      Command::StringToAccelerator(keystroke, command_name);
  AddKeybindingPref(accelerator, extension_id, command_name,
                    true, command.global());
}

bool CommandService::SetScope(const ExtensionId& extension_id,
                              const std::string& command_name,
                              bool global) {
  Command command = FindCommandByName(extension_id, command_name);
  if (global == command.global())
    return false;

  // Pre-existing shortcuts must be removed before proceeding because the
  // handlers for global and non-global extensions are not one and the same.
  RemoveKeybindingPrefs(extension_id, command_name);
  AddKeybindingPref(command.accelerator(), extension_id,
                    command_name, true, global);
  return true;
}

Command CommandService::FindCommandByName(const ExtensionId& extension_id,
                                          const std::string& command) const {
  const base::Value::Dict& bindings =
      profile_->GetPrefs()->GetDict(prefs::kExtensionCommands);
  for (const auto it : bindings) {
    const ExtensionId* extension = it.second.GetDict().FindString(kExtension);
    if (!extension || *extension != extension_id)
      continue;
    const std::string* command_name =
        it.second.GetDict().FindString(kCommandName);
    if (!command_name || *command_name != command)
      continue;
    // Format stored in Preferences is: "Platform:Shortcut[:ExtensionId]".
    std::string shortcut = it.first;
    if (!IsForCurrentPlatform(shortcut))
      continue;
    std::optional<bool> global = it.second.GetDict().FindBool(kGlobal);

    std::vector<std::string_view> tokens = base::SplitStringPiece(
        shortcut, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
    CHECK(tokens.size() >= 2);

    return Command(*command_name, std::u16string(), std::string(tokens[1]),
                   global.value_or(false));
  }

  return Command();
}

void CommandService::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void CommandService::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

const Extension* CommandService::GetExtensionInEnabledOrDisabledExtensions(
    const ExtensionId& extension_id) const {
  const ExtensionSet& enabled_extensions =
      ExtensionRegistry::Get(profile_)->enabled_extensions();
  const Extension* enabled_extension = enabled_extensions.GetByID(extension_id);
  if (enabled_extension) {
    return enabled_extension;
  }
  const ExtensionSet& disabled_extensions =
      ExtensionRegistry::Get(profile_)->disabled_extensions();
  return disabled_extensions.GetByID(extension_id);
}

bool CommandService::IsUpgradeFromMV2ToMV3(
    const Extension* extension,
    const std::string& existing_command_name) const {
  bool browser_or_page_action_command_in_bindings =
      existing_command_name == manifest_values::kBrowserActionCommandEvent ||
      existing_command_name == manifest_values::kPageActionCommandEvent;
  return browser_or_page_action_command_in_bindings &&
         CommandsInfo::GetActionCommand(extension);
}

void CommandService::UpdateKeybindings(const Extension* extension) {
  if (GetExtensionInEnabledOrDisabledExtensions(extension->id())) {
    RemoveRelinquishedKeybindings(extension);
  }
  AssignKeybindings(extension);
  UpdateExtensionSuggestedCommandPrefs(extension);
  RemoveDefunctExtensionSuggestedCommandPrefs(extension);
}

void CommandService::RemoveRelinquishedKeybindings(const Extension* extension) {
  // Remove keybindings if they have been removed by the extension and the user
  // has not modified them.
  ui::CommandMap existing_command_map;
  if (GetNamedCommands(extension->id(),
                       CommandService::ACTIVE,
                       CommandService::REGULAR,
                       &existing_command_map)) {
    const ui::CommandMap* new_command_map =
        CommandsInfo::GetNamedCommands(extension);
    for (ui::CommandMap::const_iterator it = existing_command_map.begin();
         it != existing_command_map.end(); ++it) {
      std::string command_name = it->first;
      if (new_command_map->find(command_name) == new_command_map->end() &&
          !IsCommandShortcutUserModified(extension, command_name)) {
        RemoveKeybindingPrefs(extension->id(), command_name);
      }
    }
  }

  auto remove_overrides_if_unused = [this, extension](ActionInfo::Type type) {
    Command existing_command;
    if (!GetExtensionActionCommand(extension->id(), type,
                                   CommandService::ACTIVE, &existing_command,
                                   nullptr)) {
      // No keybindings to remove.
      return;
    }

    const std::string& existing_command_name = existing_command.command_name();
    bool is_shortcut_user_modified =
        IsCommandShortcutUserModified(extension, existing_command_name);
    bool is_upgrade_from_mv2_to_mv3 =
        IsUpgradeFromMV2ToMV3(extension, existing_command_name);
    if (is_shortcut_user_modified && is_upgrade_from_mv2_to_mv3) {
      // TODO(jlulejian): Could this be an out param to IsUpgradeFromMV2ToMV3?
      const Command* action_command = CommandsInfo::GetActionCommand(extension);
      AddKeybindingPref(existing_command.accelerator(), extension->id(),
                        action_command->command_name(), true,
                        action_command->global());
    } else if (is_shortcut_user_modified) {
      // Don't relinquish user-modified shortcuts otherwise.
      return;
    }

    const Command* new_command = nullptr;
    switch (type) {
      case ActionInfo::Type::kAction:
        new_command = CommandsInfo::GetActionCommand(extension);
        break;
      case ActionInfo::Type::kBrowser:
        new_command = CommandsInfo::GetBrowserActionCommand(extension);
        break;
      case ActionInfo::Type::kPage:
        new_command = CommandsInfo::GetPageActionCommand(extension);
        break;
    }

    // The shortcuts should be removed if there is no command specified in the
    // new extension, or the only command specified is synthesized (i.e.,
    // assigned to ui::VKEY_UNKNOWN), which happens for browser action commands.
    // See CommandsHandler::MaybeSetActionDefault().
    // TODO(devlin): Should this logic apply to ActionInfo::Type::kAction?
    // See https://crbug.com/893373.
    const bool should_relinquish =
        !new_command ||
        (type == ActionInfo::Type::kBrowser &&
         new_command->accelerator().key_code() == ui::VKEY_UNKNOWN);

    if (!should_relinquish)
      return;

    RemoveKeybindingPrefs(extension->id(), existing_command_name);
  };

  // TODO(crbug.com/40124879): Extensions shouldn't be able to specify
  // commands for actions they don't have, so we should just be able to query
  // for a single action type.
  for (ActionInfo::Type type :
       {ActionInfo::Type::kAction, ActionInfo::Type::kBrowser,
        ActionInfo::Type::kPage}) {
    remove_overrides_if_unused(type);
  }
}

void CommandService::AssignKeybindings(const Extension* extension) {
  const ui::CommandMap* commands = CommandsInfo::GetNamedCommands(extension);
  if (!commands)
    return;

  for (const auto& named_command : *commands) {
    const ui::Command command = named_command.second;
    if (CanAutoAssign(command, extension)) {
      AddKeybindingPref(command.accelerator(),
                        extension->id(),
                        command.command_name(),
                        false,  // Overwriting not allowed.
                        command.global());
    }
  }

  const Command* browser_action_command =
      CommandsInfo::GetBrowserActionCommand(extension);
  if (browser_action_command &&
      CanAutoAssign(*browser_action_command, extension)) {
    AddKeybindingPref(browser_action_command->accelerator(),
                      extension->id(),
                      browser_action_command->command_name(),
                      false,   // Overwriting not allowed.
                      false);  // Not global.
  }

  const Command* page_action_command =
      CommandsInfo::GetPageActionCommand(extension);
  if (page_action_command && CanAutoAssign(*page_action_command, extension)) {
    AddKeybindingPref(page_action_command->accelerator(),
                      extension->id(),
                      page_action_command->command_name(),
                      false,   // Overwriting not allowed.
                      false);  // Not global.
  }

  const Command* action_command = CommandsInfo::GetActionCommand(extension);
  if (action_command && CanAutoAssign(*action_command, extension)) {
    AddKeybindingPref(action_command->accelerator(), extension->id(),
                      action_command->command_name(),
                      false,   // Overwriting not allowed.
                      false);  // Not global.
  }
}

bool CommandService::CanAutoAssign(const ui::Command& command,
                                   const Extension* extension) {
  // Extensions are allowed to auto-assign updated keys if the user has not
  // changed from the previous value.
  if (IsCommandShortcutUserModified(extension, command.command_name()))
    return false;

  // Media Keys are non-exclusive, so allow auto-assigning them.
  if (command.accelerator().IsMediaKey()) {
    return true;
  }

  if (command.global()) {
    if (Command::IsActionRelatedCommand(command.command_name()))
      return false;  // Browser and page actions are not global in nature.

    if (extension->permissions_data()->HasAPIPermission(
            mojom::APIPermissionID::kCommandsAccessibility))
      return true;

    // Global shortcuts are restricted to (Ctrl|Command)+Shift+[0-9].
#if BUILDFLAG(IS_MAC)
    if (!command.accelerator().IsCmdDown())
      return false;
#else
    if (!command.accelerator().IsCtrlDown())
      return false;
#endif
    if (!command.accelerator().IsShiftDown())
      return false;
    return (command.accelerator().key_code() >= ui::VKEY_0 &&
            command.accelerator().key_code() <= ui::VKEY_9);
  }

  // Not a global command, check if the command is a Chrome shortcut.
  return !chrome::IsChromeAccelerator(command.accelerator());
}

void CommandService::UpdateExtensionSuggestedCommandPrefs(
    const Extension* extension) {
  base::Value::Dict suggested_key_prefs;

  const ui::CommandMap* commands = CommandsInfo::GetNamedCommands(extension);
  if (commands) {
    for (const auto& named_command : *commands) {
      const ui::Command command = named_command.second;
      base::Value::Dict command_keys;
      command_keys.Set(kSuggestedKey,
                       Command::AcceleratorToString(command.accelerator()));
      suggested_key_prefs.Set(command.command_name(), std::move(command_keys));
    }
  }

  const Command* browser_action_command =
      CommandsInfo::GetBrowserActionCommand(extension);
  // The browser action command may be defaulted to an unassigned accelerator if
  // a browser action is specified by the extension but a keybinding is not
  // declared. See CommandsHandler::MaybeSetActionDefault.
  if (browser_action_command &&
      browser_action_command->accelerator().key_code() != ui::VKEY_UNKNOWN) {
    base::Value::Dict command_keys;
    command_keys.Set(kSuggestedKey, Command::AcceleratorToString(
                                        browser_action_command->accelerator()));
    suggested_key_prefs.Set(browser_action_command->command_name(),
                            std::move(command_keys));
  }

  const Command* page_action_command =
      CommandsInfo::GetPageActionCommand(extension);
  if (page_action_command) {
    base::Value::Dict command_keys;
    command_keys.Set(kSuggestedKey, Command::AcceleratorToString(
                                        page_action_command->accelerator()));
    suggested_key_prefs.Set(page_action_command->command_name(),
                            std::move(command_keys));
  }

  // Merge into current prefs, if present.
  MergeSuggestedKeyPrefs(extension->id(), ExtensionPrefs::Get(profile_),
                         std::move(suggested_key_prefs));
}

void CommandService::RemoveDefunctExtensionSuggestedCommandPrefs(
    const Extension* extension) {
  ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
  const base::Value::Dict* current_prefs =
      extension_prefs->ReadPrefAsDict(extension->id(), kCommands);

  if (current_prefs) {
    base::Value::Dict suggested_key_prefs = current_prefs->Clone();

    const ui::CommandMap* named_commands =
        CommandsInfo::GetNamedCommands(extension);

    const Command* browser_action_command =
        CommandsInfo::GetBrowserActionCommand(extension);
    for (const auto [key, _] : *current_prefs) {
      if (key == manifest_values::kBrowserActionCommandEvent) {
        // The browser action command may be defaulted to an unassigned
        // accelerator if a browser action is specified by the extension but a
        // keybinding is not declared. See
        // CommandsHandler::MaybeSetActionDefault.
        if (!browser_action_command ||
            browser_action_command->accelerator().key_code() ==
                ui::VKEY_UNKNOWN) {
          suggested_key_prefs.Remove(key);
        }
      } else if (key == manifest_values::kPageActionCommandEvent) {
        if (!CommandsInfo::GetPageActionCommand(extension))
          suggested_key_prefs.Remove(key);
      } else if (key == manifest_values::kActionCommandEvent) {
        if (!CommandsInfo::GetActionCommand(extension))
          suggested_key_prefs.Remove(key);
      } else if (named_commands) {
        if (named_commands->find(key) == named_commands->end())
          suggested_key_prefs.Remove(key);
      }
    }

    extension_prefs->UpdateExtensionPref(
        extension->id(), kCommands,
        base::Value(std::move(suggested_key_prefs)));
  }
}

bool CommandService::IsCommandShortcutUserModified(
    const Extension* extension,
    const std::string& command_name) {
  // Get the previous suggested key, if any.
  ui::Accelerator suggested_key;
  std::optional<bool> suggested_key_was_assigned;
  ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
  const base::Value::Dict* commands_prefs =
      extension_prefs->ReadPrefAsDict(extension->id(), kCommands);
  if (commands_prefs) {
    const base::Value::Dict* suggested_key_prefs =
        commands_prefs->FindDict(command_name);
    if (suggested_key_prefs) {
      const std::string* suggested_key_string =
          suggested_key_prefs->FindString(kSuggestedKey);
      if (suggested_key_string) {
        suggested_key =
            Command::StringToAccelerator(*suggested_key_string, command_name);
      }
      suggested_key_was_assigned =
          suggested_key_prefs->FindBool(kSuggestedKeyWasAssigned);
    }
  }

  // Get the active shortcut from the prefs, if any.
  Command active_command = FindCommandByName(extension->id(), command_name);

  return suggested_key_was_assigned.value_or(false)
             ? active_command.accelerator() != suggested_key
             : active_command.accelerator().key_code() != ui::VKEY_UNKNOWN;
}

void CommandService::RemoveKeybindingPrefs(const ExtensionId& extension_id,
                                           const std::string& command_name) {
  ScopedDictPrefUpdate updater(profile_->GetPrefs(), prefs::kExtensionCommands);
  base::Value::Dict& bindings = updater.Get();

  typedef std::vector<std::string> KeysToRemove;
  KeysToRemove keys_to_remove;
  std::vector<Command> removed_commands;
  for (const auto it : bindings) {
    // Removal of keybinding preference should be limited to current platform.
    if (!IsForCurrentPlatform(it.first))
      continue;

    const base::Value::Dict& dict = it.second.GetDict();
    const ExtensionId* extension = dict.FindString(kExtension);

    if (extension && *extension == extension_id) {
      // If |command_name| is specified, delete only that command. Otherwise,
      // delete all commands.
      const std::string* command = dict.FindString(kCommandName);
      if (command && !command_name.empty() && command_name != *command)
        continue;

      removed_commands.push_back(FindCommandByName(extension_id, *command));
      keys_to_remove.push_back(it.first);
    }
  }

  for (KeysToRemove::const_iterator it = keys_to_remove.begin();
       it != keys_to_remove.end(); ++it) {
    std::string key = *it;
    bindings.Remove(key);
  }

  for (const Command& removed_command : removed_commands) {
    for (auto& observer : observers_)
      observer.OnExtensionCommandRemoved(extension_id, removed_command);
  }
}

bool CommandService::GetExtensionActionCommand(const ExtensionId& extension_id,
                                               ActionInfo::Type action_type,
                                               QueryType query_type,
                                               Command* command,
                                               bool* active) const {
  const Extension* extension =
      GetExtensionInEnabledOrDisabledExtensions(extension_id);
  if (!extension) {
    return false;
  }

  if (active)
    *active = false;

  const Command* requested_command = nullptr;
  switch (action_type) {
    case ActionInfo::Type::kBrowser:
      requested_command = CommandsInfo::GetBrowserActionCommand(extension);
      break;
    case ActionInfo::Type::kPage:
      requested_command = CommandsInfo::GetPageActionCommand(extension);
      break;
    case ActionInfo::Type::kAction:
      requested_command = CommandsInfo::GetActionCommand(extension);
      break;
  }
  if (!requested_command)
    return false;

  // Look up to see if the user has overridden how the command should work.
  Command saved_command =
      FindCommandByName(extension_id, requested_command->command_name());
  ui::Accelerator shortcut_assigned = saved_command.accelerator();

  if (active)
    *active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN);

  if (query_type == ACTIVE && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN)
    return false;

  *command = *requested_command;
  if (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN)
    command->set_accelerator(shortcut_assigned);

  return true;
}

template <>
void BrowserContextKeyedAPIFactory<
    CommandService>::DeclareFactoryDependencies() {
  DependsOn(ExtensionCommandsGlobalRegistry::GetFactoryInstance());
}

}  // namespace extensions