nsControllerCommandTable.cpp (3880B)
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 "nsControllerCommandTable.h" 8 9 #include "mozilla/ClearOnShutdown.h" 10 #include "mozilla/ControllerCommand.h" 11 #include "mozilla/EditorController.h" 12 #include "mozilla/HTMLEditorController.h" 13 #include "mozilla/StaticPtr.h" 14 #include "nsGlobalWindowCommands.h" 15 #include "nsString.h" 16 17 using mozilla::ControllerCommand; 18 19 // this value is used to size the hash table. Just a sensible upper bound 20 #define NUM_COMMANDS_LENGTH 32 21 22 nsControllerCommandTable::nsControllerCommandTable() 23 : mCommandsTable(NUM_COMMANDS_LENGTH) {} 24 25 nsControllerCommandTable::~nsControllerCommandTable() = default; 26 27 void nsControllerCommandTable::RegisterCommand(const nsACString& aName, 28 ControllerCommand* aCommand) { 29 MOZ_DIAGNOSTIC_ASSERT(mMutable); 30 mCommandsTable.InsertOrUpdate(aName, aCommand); 31 } 32 33 void nsControllerCommandTable::UnregisterCommand(const nsACString& aCommandName, 34 ControllerCommand* aCommand) { 35 MOZ_DIAGNOSTIC_ASSERT(mMutable); 36 mCommandsTable.Remove(aCommandName); 37 } 38 39 ControllerCommand* nsControllerCommandTable::FindCommandHandler( 40 const nsACString& aCommandName) const { 41 return mCommandsTable.GetWeak(aCommandName); 42 } 43 44 bool nsControllerCommandTable::IsCommandEnabled(const nsACString& aCommandName, 45 nsISupports* aContext) const { 46 RefPtr handler = FindCommandHandler(aCommandName); 47 if (!handler) { 48 NS_WARNING( 49 "Controller command table asked about a command that it does " 50 "not handle"); 51 return false; 52 } 53 return handler->IsCommandEnabled(aCommandName, aContext); 54 } 55 56 void nsControllerCommandTable::GetSupportedCommands( 57 nsTArray<nsCString>& aCommands) const { 58 mozilla::AppendToArray(aCommands, mCommandsTable.Keys()); 59 } 60 61 using CommandTableRegistrar = void (*)(nsControllerCommandTable*); 62 63 static nsControllerCommandTable* EnsureCommandTableWithCommands( 64 mozilla::StaticRefPtr<nsControllerCommandTable>& aTable, 65 CommandTableRegistrar aRegistrar) { 66 if (!aTable) { 67 aTable = new nsControllerCommandTable(); 68 aRegistrar(aTable); 69 aTable->MakeImmutable(); 70 ClearOnShutdown(&aTable); 71 } 72 return aTable; 73 } 74 75 // static 76 nsControllerCommandTable* nsControllerCommandTable::EditorCommandTable() { 77 static mozilla::StaticRefPtr<nsControllerCommandTable> sTable; 78 return EnsureCommandTableWithCommands( 79 sTable, mozilla::EditorController::RegisterEditorCommands); 80 } 81 82 // static 83 nsControllerCommandTable* nsControllerCommandTable::EditingCommandTable() { 84 static mozilla::StaticRefPtr<nsControllerCommandTable> sTable; 85 return EnsureCommandTableWithCommands( 86 sTable, mozilla::EditorController::RegisterEditingCommands); 87 } 88 89 // static 90 nsControllerCommandTable* nsControllerCommandTable::HTMLEditorCommandTable() { 91 static mozilla::StaticRefPtr<nsControllerCommandTable> sTable; 92 return EnsureCommandTableWithCommands( 93 sTable, mozilla::HTMLEditorController::RegisterHTMLEditorCommands); 94 } 95 96 // static 97 nsControllerCommandTable* 98 nsControllerCommandTable::HTMLEditorDocStateCommandTable() { 99 static mozilla::StaticRefPtr<nsControllerCommandTable> sTable; 100 return EnsureCommandTableWithCommands( 101 sTable, mozilla::HTMLEditorController::RegisterEditorDocStateCommands); 102 } 103 104 // static 105 nsControllerCommandTable* nsControllerCommandTable::WindowCommandTable() { 106 static mozilla::StaticRefPtr<nsControllerCommandTable> sTable; 107 return EnsureCommandTableWithCommands( 108 sTable, nsWindowCommandRegistration::RegisterWindowCommands); 109 }