LCOV - code coverage report
Current view: top level - dom/commandhandler - nsControllerCommandTable.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 44 92 47.8 %
Date: 2017-07-14 16:53:18 Functions: 10 18 55.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
       3             : /* This Source Code Form is subject to the terms of the Mozilla Public
       4             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       5             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       6             : 
       7             : #include "nsString.h"
       8             : #include "nsIControllerCommand.h"
       9             : #include "nsControllerCommandTable.h"
      10             : 
      11             : nsresult NS_NewControllerCommandTable(nsIControllerCommandTable** aResult);
      12             : 
      13             : // this value is used to size the hash table. Just a sensible upper bound
      14             : #define NUM_COMMANDS_LENGTH 32
      15             : 
      16           4 : nsControllerCommandTable::nsControllerCommandTable()
      17             :   : mCommandsTable(NUM_COMMANDS_LENGTH)
      18           4 :   , mMutable(true)
      19             : {
      20           4 : }
      21             : 
      22           0 : nsControllerCommandTable::~nsControllerCommandTable()
      23             : {
      24           0 : }
      25             : 
      26          78 : NS_IMPL_ISUPPORTS(nsControllerCommandTable, nsIControllerCommandTable,
      27             :                   nsISupportsWeakReference)
      28             : 
      29             : NS_IMETHODIMP
      30           6 : nsControllerCommandTable::MakeImmutable(void)
      31             : {
      32           6 :   mMutable = false;
      33           6 :   return NS_OK;
      34             : }
      35             : 
      36             : NS_IMETHODIMP
      37         191 : nsControllerCommandTable::RegisterCommand(const char* aCommandName,
      38             :                                           nsIControllerCommand* aCommand)
      39             : {
      40         191 :   NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE);
      41             : 
      42         191 :   mCommandsTable.Put(nsDependentCString(aCommandName), aCommand);
      43             : 
      44         191 :   return NS_OK;
      45             : }
      46             : 
      47             : NS_IMETHODIMP
      48           0 : nsControllerCommandTable::UnregisterCommand(const char* aCommandName,
      49             :                                             nsIControllerCommand* aCommand)
      50             : {
      51           0 :   NS_ENSURE_TRUE(mMutable, NS_ERROR_FAILURE);
      52             : 
      53           0 :   nsDependentCString commandKey(aCommandName);
      54           0 :   if (!mCommandsTable.Get(commandKey, nullptr)) {
      55           0 :     return NS_ERROR_FAILURE;
      56             :   }
      57             : 
      58           0 :   mCommandsTable.Remove(commandKey);
      59           0 :   return NS_OK;
      60             : }
      61             : 
      62             : NS_IMETHODIMP
      63         137 : nsControllerCommandTable::FindCommandHandler(const char* aCommandName,
      64             :                                              nsIControllerCommand** aResult)
      65             : {
      66         137 :   NS_ENSURE_ARG_POINTER(aResult);
      67             : 
      68         137 :   *aResult = nullptr;
      69             : 
      70         274 :   nsCOMPtr<nsIControllerCommand> foundCommand;
      71         274 :   mCommandsTable.Get(nsDependentCString(aCommandName),
      72         411 :                      getter_AddRefs(foundCommand));
      73         137 :   if (!foundCommand) {
      74           7 :     return NS_ERROR_FAILURE;
      75             :   }
      76             : 
      77         130 :   foundCommand.forget(aResult);
      78         130 :   return NS_OK;
      79             : }
      80             : 
      81             : NS_IMETHODIMP
      82         126 : nsControllerCommandTable::IsCommandEnabled(const char* aCommandName,
      83             :                                            nsISupports* aCommandRefCon,
      84             :                                            bool* aResult)
      85             : {
      86         126 :   NS_ENSURE_ARG_POINTER(aResult);
      87             : 
      88         126 :   *aResult = false;
      89             : 
      90         252 :   nsCOMPtr<nsIControllerCommand> commandHandler;
      91         126 :   FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
      92         126 :   if (!commandHandler) {
      93             :     NS_WARNING("Controller command table asked about a command that it does "
      94           0 :                "not handle");
      95           0 :     return NS_OK;
      96             :   }
      97             : 
      98         126 :   return commandHandler->IsCommandEnabled(aCommandName, aCommandRefCon,
      99         126 :                                           aResult);
     100             : }
     101             : 
     102             : NS_IMETHODIMP
     103           0 : nsControllerCommandTable::UpdateCommandState(const char* aCommandName,
     104             :                                              nsISupports* aCommandRefCon)
     105             : {
     106           0 :   nsCOMPtr<nsIControllerCommand> commandHandler;
     107           0 :   FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
     108           0 :   if (!commandHandler) {
     109             :     NS_WARNING("Controller command table asked to update the state of a "
     110           0 :                "command that it does not handle");
     111           0 :     return NS_OK;
     112             :   }
     113             : 
     114           0 :   return NS_ERROR_NOT_IMPLEMENTED;
     115             : }
     116             : 
     117             : NS_IMETHODIMP
     118          11 : nsControllerCommandTable::SupportsCommand(const char* aCommandName,
     119             :                                           nsISupports* aCommandRefCon,
     120             :                                           bool* aResult)
     121             : {
     122          11 :   NS_ENSURE_ARG_POINTER(aResult);
     123             : 
     124             :   // XXX: need to check the readonly and disabled states
     125             : 
     126          11 :   *aResult = false;
     127             : 
     128          22 :   nsCOMPtr<nsIControllerCommand> commandHandler;
     129          11 :   FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
     130             : 
     131          11 :   *aResult = (commandHandler.get() != nullptr);
     132          11 :   return NS_OK;
     133             : }
     134             : 
     135             : NS_IMETHODIMP
     136           0 : nsControllerCommandTable::DoCommand(const char* aCommandName,
     137             :                                     nsISupports* aCommandRefCon)
     138             : {
     139           0 :   nsCOMPtr<nsIControllerCommand> commandHandler;
     140           0 :   FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
     141           0 :   if (!commandHandler) {
     142             :     NS_WARNING("Controller command table asked to do a command that it does "
     143           0 :                "not handle");
     144           0 :     return NS_OK;
     145             :   }
     146             : 
     147           0 :   return commandHandler->DoCommand(aCommandName, aCommandRefCon);
     148             : }
     149             : 
     150             : NS_IMETHODIMP
     151           0 : nsControllerCommandTable::DoCommandParams(const char* aCommandName,
     152             :                                           nsICommandParams* aParams,
     153             :                                           nsISupports* aCommandRefCon)
     154             : {
     155           0 :   nsCOMPtr<nsIControllerCommand> commandHandler;
     156           0 :   FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
     157           0 :   if (!commandHandler) {
     158             :     NS_WARNING("Controller command table asked to do a command that it does "
     159           0 :                "not handle");
     160           0 :     return NS_OK;
     161             :   }
     162           0 :   return commandHandler->DoCommandParams(aCommandName, aParams, aCommandRefCon);
     163             : }
     164             : 
     165             : NS_IMETHODIMP
     166           0 : nsControllerCommandTable::GetCommandState(const char* aCommandName,
     167             :                                           nsICommandParams* aParams,
     168             :                                           nsISupports* aCommandRefCon)
     169             : {
     170           0 :   nsCOMPtr<nsIControllerCommand> commandHandler;
     171           0 :   FindCommandHandler(aCommandName, getter_AddRefs(commandHandler));
     172           0 :   if (!commandHandler) {
     173             :     NS_WARNING("Controller command table asked to do a command that it does "
     174           0 :                "not handle");
     175           0 :     return NS_OK;
     176             :   }
     177           0 :   return commandHandler->GetCommandStateParams(aCommandName, aParams,
     178           0 :                                                aCommandRefCon);
     179             : }
     180             : 
     181             : NS_IMETHODIMP
     182           4 : nsControllerCommandTable::GetSupportedCommands(uint32_t* aCount,
     183             :                                                char*** aCommands)
     184             : {
     185             :   char** commands =
     186           4 :     static_cast<char**>(moz_xmalloc(sizeof(char*) * mCommandsTable.Count()));
     187           4 :   *aCount = mCommandsTable.Count();
     188           4 :   *aCommands = commands;
     189             : 
     190         248 :   for (auto iter = mCommandsTable.Iter(); !iter.Done(); iter.Next()) {
     191         244 :     *commands = ToNewCString(iter.Key());
     192         244 :     commands++;
     193             :   }
     194           4 :   return NS_OK;
     195             : }
     196             : 
     197             : nsresult
     198           0 : NS_NewControllerCommandTable(nsIControllerCommandTable** aResult)
     199             : {
     200           0 :   NS_PRECONDITION(aResult != nullptr, "null ptr");
     201           0 :   if (!aResult) {
     202           0 :     return NS_ERROR_NULL_POINTER;
     203             :   }
     204             : 
     205           0 :   nsControllerCommandTable* newCommandTable = new nsControllerCommandTable();
     206           0 :   NS_ADDREF(newCommandTable);
     207           0 :   *aResult = newCommandTable;
     208           0 :   return NS_OK;
     209             : }

Generated by: LCOV version 1.13