editor/composer/src/nsComposerRegistration.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include <stddef.h> // for nullptr
michael@0 7
michael@0 8 #include "mozilla/Module.h" // for Module, Module::CIDEntry, etc
michael@0 9 #include "mozilla/ModuleUtils.h"
michael@0 10 #include "mozilla/mozalloc.h" // for operator new
michael@0 11 #include "nsCOMPtr.h" // for nsCOMPtr, getter_AddRefs, etc
michael@0 12 #include "nsComponentManagerUtils.h" // for do_CreateInstance
michael@0 13 #include "nsComposeTxtSrvFilter.h" // for nsComposeTxtSrvFilter, etc
michael@0 14 #include "nsComposerController.h" // for nsComposerController, etc
michael@0 15 #include "nsDebug.h" // for NS_ENSURE_SUCCESS
michael@0 16 #include "nsEditingSession.h" // for NS_EDITINGSESSION_CID, etc
michael@0 17 #include "nsEditorSpellCheck.h" // for NS_EDITORSPELLCHECK_CID, etc
michael@0 18 #include "nsError.h" // for NS_ERROR_NO_AGGREGATION, etc
michael@0 19 #include "nsIController.h" // for nsIController
michael@0 20 #include "nsIControllerCommandTable.h" // for nsIControllerCommandTable, etc
michael@0 21 #include "nsIControllerContext.h" // for nsIControllerContext
michael@0 22 #include "nsID.h" // for NS_DEFINE_NAMED_CID, etc
michael@0 23 #include "nsISupportsImpl.h"
michael@0 24 #include "nsISupportsUtils.h" // for NS_ADDREF, NS_RELEASE
michael@0 25 #include "nsServiceManagerUtils.h" // for do_GetService
michael@0 26 #include "nscore.h" // for nsresult
michael@0 27
michael@0 28 class nsISupports;
michael@0 29
michael@0 30 #define NS_HTMLEDITOR_COMMANDTABLE_CID \
michael@0 31 { 0x13e50d8d, 0x9cee, 0x4ad1, { 0xa3, 0xa2, 0x4a, 0x44, 0x2f, 0xdf, 0x7d, 0xfa } }
michael@0 32
michael@0 33 #define NS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID \
michael@0 34 { 0xa33982d3, 0x1adf, 0x4162, { 0x99, 0x41, 0xf7, 0x34, 0xbc, 0x45, 0xe4, 0xed } }
michael@0 35
michael@0 36
michael@0 37 static NS_DEFINE_CID(kHTMLEditorCommandTableCID, NS_HTMLEDITOR_COMMANDTABLE_CID);
michael@0 38 static NS_DEFINE_CID(kHTMLEditorDocStateCommandTableCID, NS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID);
michael@0 39
michael@0 40
michael@0 41 ////////////////////////////////////////////////////////////////////////
michael@0 42 // Define the contructor function for the objects
michael@0 43 //
michael@0 44 // NOTE: This creates an instance of objects by using the default constructor
michael@0 45 //
michael@0 46
michael@0 47 NS_GENERIC_FACTORY_CONSTRUCTOR(nsEditingSession)
michael@0 48 NS_GENERIC_FACTORY_CONSTRUCTOR(nsEditorSpellCheck)
michael@0 49
michael@0 50 // There are no macros that enable us to have 2 constructors
michael@0 51 // for the same object
michael@0 52 //
michael@0 53 // Here we are creating the same object with two different contract IDs
michael@0 54 // and then initializing it different.
michael@0 55 // Basically, we need to tell the filter whether it is doing mail or not
michael@0 56 static nsresult
michael@0 57 nsComposeTxtSrvFilterConstructor(nsISupports *aOuter, REFNSIID aIID,
michael@0 58 void **aResult, bool aIsForMail)
michael@0 59 {
michael@0 60 *aResult = nullptr;
michael@0 61 if (nullptr != aOuter)
michael@0 62 {
michael@0 63 return NS_ERROR_NO_AGGREGATION;
michael@0 64 }
michael@0 65 nsComposeTxtSrvFilter * inst = new nsComposeTxtSrvFilter();
michael@0 66 if (nullptr == inst)
michael@0 67 {
michael@0 68 return NS_ERROR_OUT_OF_MEMORY;
michael@0 69 }
michael@0 70 NS_ADDREF(inst);
michael@0 71 inst->Init(aIsForMail);
michael@0 72 nsresult rv = inst->QueryInterface(aIID, aResult);
michael@0 73 NS_RELEASE(inst);
michael@0 74 return rv;
michael@0 75 }
michael@0 76
michael@0 77 static nsresult
michael@0 78 nsComposeTxtSrvFilterConstructorForComposer(nsISupports *aOuter,
michael@0 79 REFNSIID aIID,
michael@0 80 void **aResult)
michael@0 81 {
michael@0 82 return nsComposeTxtSrvFilterConstructor(aOuter, aIID, aResult, false);
michael@0 83 }
michael@0 84
michael@0 85 static nsresult
michael@0 86 nsComposeTxtSrvFilterConstructorForMail(nsISupports *aOuter,
michael@0 87 REFNSIID aIID,
michael@0 88 void **aResult)
michael@0 89 {
michael@0 90 return nsComposeTxtSrvFilterConstructor(aOuter, aIID, aResult, true);
michael@0 91 }
michael@0 92
michael@0 93
michael@0 94 // Constructor for a controller set up with a command table specified
michael@0 95 // by the CID passed in. This function uses do_GetService to get the
michael@0 96 // command table, so that every controller shares a single command
michael@0 97 // table, for space-efficiency.
michael@0 98 //
michael@0 99 // The only reason to go via the service manager for the command table
michael@0 100 // is that it holds onto the singleton for us, avoiding static variables here.
michael@0 101 static nsresult
michael@0 102 CreateControllerWithSingletonCommandTable(const nsCID& inCommandTableCID, nsIController **aResult)
michael@0 103 {
michael@0 104 nsresult rv;
michael@0 105 nsCOMPtr<nsIController> controller = do_CreateInstance("@mozilla.org/embedcomp/base-command-controller;1", &rv);
michael@0 106 NS_ENSURE_SUCCESS(rv, rv);
michael@0 107
michael@0 108 nsCOMPtr<nsIControllerCommandTable> composerCommandTable = do_GetService(inCommandTableCID, &rv);
michael@0 109 NS_ENSURE_SUCCESS(rv, rv);
michael@0 110
michael@0 111 // this guy is a singleton, so make it immutable
michael@0 112 composerCommandTable->MakeImmutable();
michael@0 113
michael@0 114 nsCOMPtr<nsIControllerContext> controllerContext = do_QueryInterface(controller, &rv);
michael@0 115 NS_ENSURE_SUCCESS(rv, rv);
michael@0 116
michael@0 117 rv = controllerContext->Init(composerCommandTable);
michael@0 118 NS_ENSURE_SUCCESS(rv, rv);
michael@0 119
michael@0 120 *aResult = controller;
michael@0 121 NS_ADDREF(*aResult);
michael@0 122 return NS_OK;
michael@0 123 }
michael@0 124
michael@0 125
michael@0 126 // Here we make an instance of the controller that holds doc state commands.
michael@0 127 // We set it up with a singleton command table.
michael@0 128 static nsresult
michael@0 129 nsHTMLEditorDocStateControllerConstructor(nsISupports *aOuter, REFNSIID aIID,
michael@0 130 void **aResult)
michael@0 131 {
michael@0 132 nsCOMPtr<nsIController> controller;
michael@0 133 nsresult rv = CreateControllerWithSingletonCommandTable(kHTMLEditorDocStateCommandTableCID, getter_AddRefs(controller));
michael@0 134 NS_ENSURE_SUCCESS(rv, rv);
michael@0 135
michael@0 136 return controller->QueryInterface(aIID, aResult);
michael@0 137 }
michael@0 138
michael@0 139 // Tere we make an instance of the controller that holds composer commands.
michael@0 140 // We set it up with a singleton command table.
michael@0 141 static nsresult
michael@0 142 nsHTMLEditorControllerConstructor(nsISupports *aOuter, REFNSIID aIID, void **aResult)
michael@0 143 {
michael@0 144 nsCOMPtr<nsIController> controller;
michael@0 145 nsresult rv = CreateControllerWithSingletonCommandTable(kHTMLEditorCommandTableCID, getter_AddRefs(controller));
michael@0 146 NS_ENSURE_SUCCESS(rv, rv);
michael@0 147
michael@0 148 return controller->QueryInterface(aIID, aResult);
michael@0 149 }
michael@0 150
michael@0 151 // Constructor for a command table that is pref-filled with HTML editor commands
michael@0 152 static nsresult
michael@0 153 nsHTMLEditorCommandTableConstructor(nsISupports *aOuter, REFNSIID aIID,
michael@0 154 void **aResult)
michael@0 155 {
michael@0 156 nsresult rv;
michael@0 157 nsCOMPtr<nsIControllerCommandTable> commandTable =
michael@0 158 do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
michael@0 159 NS_ENSURE_SUCCESS(rv, rv);
michael@0 160
michael@0 161 rv = nsComposerController::RegisterHTMLEditorCommands(commandTable);
michael@0 162 NS_ENSURE_SUCCESS(rv, rv);
michael@0 163
michael@0 164 // we don't know here whether we're being created as an instance,
michael@0 165 // or a service, so we can't become immutable
michael@0 166
michael@0 167 return commandTable->QueryInterface(aIID, aResult);
michael@0 168 }
michael@0 169
michael@0 170
michael@0 171 // Constructor for a command table that is pref-filled with HTML editor doc state commands
michael@0 172 static nsresult
michael@0 173 nsHTMLEditorDocStateCommandTableConstructor(nsISupports *aOuter, REFNSIID aIID,
michael@0 174 void **aResult)
michael@0 175 {
michael@0 176 nsresult rv;
michael@0 177 nsCOMPtr<nsIControllerCommandTable> commandTable =
michael@0 178 do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
michael@0 179 NS_ENSURE_SUCCESS(rv, rv);
michael@0 180
michael@0 181 rv = nsComposerController::RegisterEditorDocStateCommands(commandTable);
michael@0 182 NS_ENSURE_SUCCESS(rv, rv);
michael@0 183
michael@0 184 // we don't know here whether we're being created as an instance,
michael@0 185 // or a service, so we can't become immutable
michael@0 186
michael@0 187 return commandTable->QueryInterface(aIID, aResult);
michael@0 188 }
michael@0 189
michael@0 190 NS_DEFINE_NAMED_CID(NS_HTMLEDITORCONTROLLER_CID);
michael@0 191 NS_DEFINE_NAMED_CID(NS_EDITORDOCSTATECONTROLLER_CID);
michael@0 192 NS_DEFINE_NAMED_CID(NS_HTMLEDITOR_COMMANDTABLE_CID);
michael@0 193 NS_DEFINE_NAMED_CID(NS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID);
michael@0 194 NS_DEFINE_NAMED_CID(NS_EDITINGSESSION_CID);
michael@0 195 NS_DEFINE_NAMED_CID(NS_EDITORSPELLCHECK_CID);
michael@0 196 NS_DEFINE_NAMED_CID(NS_COMPOSERTXTSRVFILTER_CID);
michael@0 197 NS_DEFINE_NAMED_CID(NS_COMPOSERTXTSRVFILTERMAIL_CID);
michael@0 198
michael@0 199
michael@0 200 static const mozilla::Module::CIDEntry kComposerCIDs[] = {
michael@0 201 { &kNS_HTMLEDITORCONTROLLER_CID, false, nullptr, nsHTMLEditorControllerConstructor },
michael@0 202 { &kNS_EDITORDOCSTATECONTROLLER_CID, false, nullptr, nsHTMLEditorDocStateControllerConstructor },
michael@0 203 { &kNS_HTMLEDITOR_COMMANDTABLE_CID, false, nullptr, nsHTMLEditorCommandTableConstructor },
michael@0 204 { &kNS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID, false, nullptr, nsHTMLEditorDocStateCommandTableConstructor },
michael@0 205 { &kNS_EDITINGSESSION_CID, false, nullptr, nsEditingSessionConstructor },
michael@0 206 { &kNS_EDITORSPELLCHECK_CID, false, nullptr, nsEditorSpellCheckConstructor },
michael@0 207 { &kNS_COMPOSERTXTSRVFILTER_CID, false, nullptr, nsComposeTxtSrvFilterConstructorForComposer },
michael@0 208 { &kNS_COMPOSERTXTSRVFILTERMAIL_CID, false, nullptr, nsComposeTxtSrvFilterConstructorForMail },
michael@0 209 { nullptr }
michael@0 210 };
michael@0 211
michael@0 212 static const mozilla::Module::ContractIDEntry kComposerContracts[] = {
michael@0 213 { "@mozilla.org/editor/htmleditorcontroller;1", &kNS_HTMLEDITORCONTROLLER_CID },
michael@0 214 { "@mozilla.org/editor/editordocstatecontroller;1", &kNS_EDITORDOCSTATECONTROLLER_CID },
michael@0 215 { "@mozilla.org/editor/editingsession;1", &kNS_EDITINGSESSION_CID },
michael@0 216 { "@mozilla.org/editor/editorspellchecker;1", &kNS_EDITORSPELLCHECK_CID },
michael@0 217 { COMPOSER_TXTSRVFILTER_CONTRACTID, &kNS_COMPOSERTXTSRVFILTER_CID },
michael@0 218 { COMPOSER_TXTSRVFILTERMAIL_CONTRACTID, &kNS_COMPOSERTXTSRVFILTERMAIL_CID },
michael@0 219 { nullptr }
michael@0 220 };
michael@0 221
michael@0 222 static const mozilla::Module kComposerModule = {
michael@0 223 mozilla::Module::kVersion,
michael@0 224 kComposerCIDs,
michael@0 225 kComposerContracts
michael@0 226 };
michael@0 227
michael@0 228 NSMODULE_DEFN(nsComposerModule) = &kComposerModule;

mercurial