1// Copyright (c) 2012 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.
4
5#include "chrome/browser/extensions/api/commands/commands.h"
6
7#include "chrome/browser/extensions/api/commands/command_service.h"
8#include "chrome/browser/profiles/profile.h"
9
10namespace {
11
12base::DictionaryValue* CreateCommandValue(
13    const extensions::Command& command, bool active) {
14  base::DictionaryValue* result = new base::DictionaryValue();
15  result->SetString("name", command.command_name());
16  result->SetString("description", command.description());
17  result->SetString("shortcut",
18                    active ? command.accelerator().GetShortcutText() :
19                             base::string16());
20  return result;
21}
22
23}  // namespace
24
25bool GetAllCommandsFunction::RunSync() {
26  base::ListValue* command_list = new base::ListValue();
27
28  extensions::CommandService* command_service =
29      extensions::CommandService::Get(GetProfile());
30
31  extensions::Command browser_action;
32  bool active = false;
33  if (command_service->GetBrowserActionCommand(extension_->id(),
34          extensions::CommandService::ALL,
35          &browser_action,
36          &active)) {
37    command_list->Append(CreateCommandValue(browser_action, active));
38  }
39
40  extensions::Command page_action;
41  if (command_service->GetPageActionCommand(extension_->id(),
42          extensions::CommandService::ALL,
43          &page_action,
44          &active)) {
45    command_list->Append(CreateCommandValue(page_action, active));
46  }
47
48  extensions::CommandMap named_commands;
49  command_service->GetNamedCommands(extension_->id(),
50                                    extensions::CommandService::ALL,
51                                    extensions::CommandService::ANY_SCOPE,
52                                    &named_commands);
53
54  for (extensions::CommandMap::const_iterator iter = named_commands.begin();
55       iter != named_commands.end(); ++iter) {
56    extensions::Command command = command_service->FindCommandByName(
57        extension_->id(), iter->second.command_name());
58    ui::Accelerator shortcut_assigned = command.accelerator();
59    active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN);
60
61    command_list->Append(CreateCommandValue(iter->second, active));
62  }
63
64  SetResult(command_list);
65  return true;
66}
67