Home | History | Annotate | Download | only in commands
      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 
      9 namespace {
     10 
     11 base::DictionaryValue* CreateCommandValue(
     12     const extensions::Command& command, bool active) {
     13   base::DictionaryValue* result = new base::DictionaryValue();
     14   result->SetString("name", command.command_name());
     15   result->SetString("description", command.description());
     16   result->SetString("shortcut",
     17                     active ? command.accelerator().GetShortcutText() :
     18                              string16());
     19   return result;
     20 }
     21 
     22 }  // namespace
     23 
     24 bool GetAllCommandsFunction::RunImpl() {
     25   base::ListValue* command_list = new base::ListValue();
     26 
     27   extensions::CommandService* command_service =
     28       extensions::CommandService::Get(profile_);
     29 
     30   extensions::Command browser_action;
     31   bool active = false;
     32   if (command_service->GetBrowserActionCommand(extension_->id(),
     33           extensions::CommandService::ALL,
     34           &browser_action,
     35           &active)) {
     36     command_list->Append(CreateCommandValue(browser_action, active));
     37   }
     38 
     39   extensions::Command page_action;
     40   if (command_service->GetPageActionCommand(extension_->id(),
     41           extensions::CommandService::ALL,
     42           &page_action,
     43           &active)) {
     44     command_list->Append(CreateCommandValue(page_action, active));
     45   }
     46 
     47   extensions::Command script_badge;
     48   if (command_service->GetScriptBadgeCommand(extension_->id(),
     49           extensions::CommandService::ALL,
     50           &script_badge,
     51           &active)) {
     52     command_list->Append(CreateCommandValue(script_badge, active));
     53   }
     54 
     55   extensions::CommandMap named_commands;
     56   command_service->GetNamedCommands(extension_->id(),
     57                                     extensions::CommandService::ALL,
     58                                     &named_commands);
     59 
     60   for (extensions::CommandMap::const_iterator iter = named_commands.begin();
     61        iter != named_commands.end(); ++iter) {
     62     ui::Accelerator shortcut_assigned =
     63         command_service->FindShortcutForCommand(
     64             extension_->id(), iter->second.command_name());
     65     active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN);
     66 
     67     command_list->Append(CreateCommandValue(iter->second, active));
     68   }
     69 
     70   SetResult(command_list);
     71   return true;
     72 }
     73