blob: 4973a6256acd5f11e8d53185b6ff26faa7e22aad [file] [log] [blame]
// Copyright 2015 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
#include <map>
#include <set>
#include <string>
#include "base/callback.h"
#include "base/memory/singleton.h"
#include "base/synchronization/lock.h"
namespace base {
// The console command system allows the app to register actions that can be
// triggered by the debug console.
// Any component can create a handler object of the CommandHandler class to
// listen for commands on a particular channel. A CommandHandler object
// automatically registers itself with the ConsoleCommandManager singleton on
// construction and unregisters itself on destruction.
// Each CommandHandler object specifies a callback and a help string. When the
// ConsoleCommandManager receives a command broadcast on a particular channel,
// it runs the callback of the CommandHandler object registered to that
// channel. The help string is used within the debug console to document the
// channels that have been registered to listen for commands.
class ConsoleCommandManager {
// Type for a callback to handle a command.
typedef base::Callback<void(const std::string& message)> CommandCallback;
// Command handler that registers itself with this object.
class CommandHandler {
CommandHandler(const std::string& channel, const CommandCallback& callback,
const std::string& short_help, const std::string& long_help);
const std::string& channel() const { return channel_; }
const CommandCallback& callback() const { return callback_; }
const std::string& short_help() const { return short_help_; }
const std::string& long_help() const { return long_help_; }
std::string channel_;
CommandCallback callback_;
std::string short_help_;
std::string long_help_;
friend struct StaticMemorySingletonTraits<ConsoleCommandManager>;
// Method to get the singleton instance of this class.
static ConsoleCommandManager* GetInstance();
// Handles a command by posting the message to the handler registered for
// the specified channel, if any.
void HandleCommand(const std::string& channel,
const std::string& message) const;
// Returns a set of all the currently registered channels.
std::set<std::string> GetRegisteredChannels() const;
// Returns the help strings for a channel.
std::string GetShortHelp(const std::string& channel) const;
std::string GetLongHelp(const std::string& channel) const;
// Class can only be instanced via the singleton
ConsoleCommandManager() {}
~ConsoleCommandManager() {}
// Command handler map type.
typedef std::multimap<std::string, const CommandHandler*> CommandHandlerMap;
// Methods to register/unregister command handlers.
// These are intended only to be called from the command handler objects.
friend class CommandHandler;
void RegisterCommandHandler(const CommandHandler* handler);
void UnregisterCommandHandler(const CommandHandler* handler);
mutable base::Lock lock_;
// Map of command handlers, one for each channel.
CommandHandlerMap command_channel_map_;
} // namespace base