editor/composer/src/nsComposerRegistration.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/composer/src/nsComposerRegistration.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,228 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include <stddef.h>                     // for nullptr
    1.10 +
    1.11 +#include "mozilla/Module.h"             // for Module, Module::CIDEntry, etc
    1.12 +#include "mozilla/ModuleUtils.h"
    1.13 +#include "mozilla/mozalloc.h"           // for operator new
    1.14 +#include "nsCOMPtr.h"                   // for nsCOMPtr, getter_AddRefs, etc
    1.15 +#include "nsComponentManagerUtils.h"    // for do_CreateInstance
    1.16 +#include "nsComposeTxtSrvFilter.h"      // for nsComposeTxtSrvFilter, etc
    1.17 +#include "nsComposerController.h"       // for nsComposerController, etc
    1.18 +#include "nsDebug.h"                    // for NS_ENSURE_SUCCESS
    1.19 +#include "nsEditingSession.h"           // for NS_EDITINGSESSION_CID, etc
    1.20 +#include "nsEditorSpellCheck.h"         // for NS_EDITORSPELLCHECK_CID, etc
    1.21 +#include "nsError.h"                    // for NS_ERROR_NO_AGGREGATION, etc
    1.22 +#include "nsIController.h"              // for nsIController
    1.23 +#include "nsIControllerCommandTable.h"  // for nsIControllerCommandTable, etc
    1.24 +#include "nsIControllerContext.h"       // for nsIControllerContext
    1.25 +#include "nsID.h"                       // for NS_DEFINE_NAMED_CID, etc
    1.26 +#include "nsISupportsImpl.h"
    1.27 +#include "nsISupportsUtils.h"           // for NS_ADDREF, NS_RELEASE
    1.28 +#include "nsServiceManagerUtils.h"      // for do_GetService
    1.29 +#include "nscore.h"                     // for nsresult
    1.30 +
    1.31 +class nsISupports;
    1.32 +
    1.33 +#define NS_HTMLEDITOR_COMMANDTABLE_CID \
    1.34 +{ 0x13e50d8d, 0x9cee, 0x4ad1, { 0xa3, 0xa2, 0x4a, 0x44, 0x2f, 0xdf, 0x7d, 0xfa } }
    1.35 +
    1.36 +#define NS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID \
    1.37 +{ 0xa33982d3, 0x1adf, 0x4162, { 0x99, 0x41, 0xf7, 0x34, 0xbc, 0x45, 0xe4, 0xed } }
    1.38 +
    1.39 +
    1.40 +static NS_DEFINE_CID(kHTMLEditorCommandTableCID, NS_HTMLEDITOR_COMMANDTABLE_CID);
    1.41 +static NS_DEFINE_CID(kHTMLEditorDocStateCommandTableCID, NS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID);
    1.42 +
    1.43 +
    1.44 +////////////////////////////////////////////////////////////////////////
    1.45 +// Define the contructor function for the objects
    1.46 +//
    1.47 +// NOTE: This creates an instance of objects by using the default constructor
    1.48 +//
    1.49 +
    1.50 +NS_GENERIC_FACTORY_CONSTRUCTOR(nsEditingSession)
    1.51 +NS_GENERIC_FACTORY_CONSTRUCTOR(nsEditorSpellCheck)
    1.52 +
    1.53 +// There are no macros that enable us to have 2 constructors 
    1.54 +// for the same object
    1.55 +//
    1.56 +// Here we are creating the same object with two different contract IDs
    1.57 +// and then initializing it different.
    1.58 +// Basically, we need to tell the filter whether it is doing mail or not
    1.59 +static nsresult
    1.60 +nsComposeTxtSrvFilterConstructor(nsISupports *aOuter, REFNSIID aIID,
    1.61 +                                 void **aResult, bool aIsForMail)
    1.62 +{
    1.63 +    *aResult = nullptr;
    1.64 +    if (nullptr != aOuter) 
    1.65 +    {
    1.66 +        return NS_ERROR_NO_AGGREGATION;
    1.67 +    }
    1.68 +    nsComposeTxtSrvFilter * inst = new nsComposeTxtSrvFilter();
    1.69 +    if (nullptr == inst) 
    1.70 +    {
    1.71 +        return NS_ERROR_OUT_OF_MEMORY;
    1.72 +    }
    1.73 +    NS_ADDREF(inst);
    1.74 +	  inst->Init(aIsForMail);
    1.75 +    nsresult rv = inst->QueryInterface(aIID, aResult);
    1.76 +    NS_RELEASE(inst);
    1.77 +    return rv;
    1.78 +}
    1.79 +
    1.80 +static nsresult
    1.81 +nsComposeTxtSrvFilterConstructorForComposer(nsISupports *aOuter, 
    1.82 +                                            REFNSIID aIID,
    1.83 +                                            void **aResult)
    1.84 +{
    1.85 +    return nsComposeTxtSrvFilterConstructor(aOuter, aIID, aResult, false);
    1.86 +}
    1.87 +
    1.88 +static nsresult
    1.89 +nsComposeTxtSrvFilterConstructorForMail(nsISupports *aOuter, 
    1.90 +                                        REFNSIID aIID,
    1.91 +                                        void **aResult)
    1.92 +{
    1.93 +    return nsComposeTxtSrvFilterConstructor(aOuter, aIID, aResult, true);
    1.94 +}
    1.95 +
    1.96 +
    1.97 +// Constructor for a controller set up with a command table specified
    1.98 +// by the CID passed in. This function uses do_GetService to get the
    1.99 +// command table, so that every controller shares a single command
   1.100 +// table, for space-efficiency.
   1.101 +// 
   1.102 +// The only reason to go via the service manager for the command table
   1.103 +// is that it holds onto the singleton for us, avoiding static variables here.
   1.104 +static nsresult
   1.105 +CreateControllerWithSingletonCommandTable(const nsCID& inCommandTableCID, nsIController **aResult)
   1.106 +{
   1.107 +  nsresult rv;
   1.108 +  nsCOMPtr<nsIController> controller = do_CreateInstance("@mozilla.org/embedcomp/base-command-controller;1", &rv);
   1.109 +  NS_ENSURE_SUCCESS(rv, rv);
   1.110 +
   1.111 +  nsCOMPtr<nsIControllerCommandTable> composerCommandTable = do_GetService(inCommandTableCID, &rv);
   1.112 +  NS_ENSURE_SUCCESS(rv, rv);
   1.113 +  
   1.114 +  // this guy is a singleton, so make it immutable
   1.115 +  composerCommandTable->MakeImmutable();
   1.116 +  
   1.117 +  nsCOMPtr<nsIControllerContext> controllerContext = do_QueryInterface(controller, &rv);
   1.118 +  NS_ENSURE_SUCCESS(rv, rv);
   1.119 +  
   1.120 +  rv = controllerContext->Init(composerCommandTable);
   1.121 +  NS_ENSURE_SUCCESS(rv, rv);
   1.122 +  
   1.123 +  *aResult = controller;
   1.124 +  NS_ADDREF(*aResult);
   1.125 +  return NS_OK;
   1.126 +}
   1.127 +
   1.128 +
   1.129 +// Here we make an instance of the controller that holds doc state commands.
   1.130 +// We set it up with a singleton command table.
   1.131 +static nsresult
   1.132 +nsHTMLEditorDocStateControllerConstructor(nsISupports *aOuter, REFNSIID aIID, 
   1.133 +                                              void **aResult)
   1.134 +{
   1.135 +  nsCOMPtr<nsIController> controller;
   1.136 +  nsresult rv = CreateControllerWithSingletonCommandTable(kHTMLEditorDocStateCommandTableCID, getter_AddRefs(controller));
   1.137 +  NS_ENSURE_SUCCESS(rv, rv);
   1.138 +
   1.139 +  return controller->QueryInterface(aIID, aResult);
   1.140 +}
   1.141 +
   1.142 +// Tere we make an instance of the controller that holds composer commands.
   1.143 +// We set it up with a singleton command table.
   1.144 +static nsresult
   1.145 +nsHTMLEditorControllerConstructor(nsISupports *aOuter, REFNSIID aIID, void **aResult)
   1.146 +{
   1.147 +  nsCOMPtr<nsIController> controller;
   1.148 +  nsresult rv = CreateControllerWithSingletonCommandTable(kHTMLEditorCommandTableCID, getter_AddRefs(controller));
   1.149 +  NS_ENSURE_SUCCESS(rv, rv);
   1.150 +
   1.151 +  return controller->QueryInterface(aIID, aResult);
   1.152 +}
   1.153 +
   1.154 +// Constructor for a command table that is pref-filled with HTML editor commands
   1.155 +static nsresult
   1.156 +nsHTMLEditorCommandTableConstructor(nsISupports *aOuter, REFNSIID aIID, 
   1.157 +                                              void **aResult)
   1.158 +{
   1.159 +  nsresult rv;
   1.160 +  nsCOMPtr<nsIControllerCommandTable> commandTable =
   1.161 +      do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
   1.162 +  NS_ENSURE_SUCCESS(rv, rv);
   1.163 +  
   1.164 +  rv = nsComposerController::RegisterHTMLEditorCommands(commandTable);
   1.165 +  NS_ENSURE_SUCCESS(rv, rv);
   1.166 +  
   1.167 +  // we don't know here whether we're being created as an instance,
   1.168 +  // or a service, so we can't become immutable
   1.169 +  
   1.170 +  return commandTable->QueryInterface(aIID, aResult);
   1.171 +}
   1.172 +
   1.173 +
   1.174 +// Constructor for a command table that is pref-filled with HTML editor doc state commands
   1.175 +static nsresult
   1.176 +nsHTMLEditorDocStateCommandTableConstructor(nsISupports *aOuter, REFNSIID aIID, 
   1.177 +                                              void **aResult)
   1.178 +{
   1.179 +  nsresult rv;
   1.180 +  nsCOMPtr<nsIControllerCommandTable> commandTable =
   1.181 +      do_CreateInstance(NS_CONTROLLERCOMMANDTABLE_CONTRACTID, &rv);
   1.182 +  NS_ENSURE_SUCCESS(rv, rv);
   1.183 +  
   1.184 +  rv = nsComposerController::RegisterEditorDocStateCommands(commandTable);
   1.185 +  NS_ENSURE_SUCCESS(rv, rv);
   1.186 +  
   1.187 +  // we don't know here whether we're being created as an instance,
   1.188 +  // or a service, so we can't become immutable
   1.189 +  
   1.190 +  return commandTable->QueryInterface(aIID, aResult);
   1.191 +}
   1.192 +
   1.193 +NS_DEFINE_NAMED_CID(NS_HTMLEDITORCONTROLLER_CID);
   1.194 +NS_DEFINE_NAMED_CID(NS_EDITORDOCSTATECONTROLLER_CID);
   1.195 +NS_DEFINE_NAMED_CID(NS_HTMLEDITOR_COMMANDTABLE_CID);
   1.196 +NS_DEFINE_NAMED_CID(NS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID);
   1.197 +NS_DEFINE_NAMED_CID(NS_EDITINGSESSION_CID);
   1.198 +NS_DEFINE_NAMED_CID(NS_EDITORSPELLCHECK_CID);
   1.199 +NS_DEFINE_NAMED_CID(NS_COMPOSERTXTSRVFILTER_CID);
   1.200 +NS_DEFINE_NAMED_CID(NS_COMPOSERTXTSRVFILTERMAIL_CID);
   1.201 +
   1.202 +
   1.203 +static const mozilla::Module::CIDEntry kComposerCIDs[] = {
   1.204 +  { &kNS_HTMLEDITORCONTROLLER_CID, false, nullptr, nsHTMLEditorControllerConstructor },
   1.205 +  { &kNS_EDITORDOCSTATECONTROLLER_CID, false, nullptr, nsHTMLEditorDocStateControllerConstructor },
   1.206 +  { &kNS_HTMLEDITOR_COMMANDTABLE_CID, false, nullptr, nsHTMLEditorCommandTableConstructor },
   1.207 +  { &kNS_HTMLEDITOR_DOCSTATE_COMMANDTABLE_CID, false, nullptr, nsHTMLEditorDocStateCommandTableConstructor },
   1.208 +  { &kNS_EDITINGSESSION_CID, false, nullptr, nsEditingSessionConstructor },
   1.209 +  { &kNS_EDITORSPELLCHECK_CID, false, nullptr, nsEditorSpellCheckConstructor },
   1.210 +  { &kNS_COMPOSERTXTSRVFILTER_CID, false, nullptr, nsComposeTxtSrvFilterConstructorForComposer },
   1.211 +  { &kNS_COMPOSERTXTSRVFILTERMAIL_CID, false, nullptr, nsComposeTxtSrvFilterConstructorForMail },
   1.212 +  { nullptr }
   1.213 +};
   1.214 +
   1.215 +static const mozilla::Module::ContractIDEntry kComposerContracts[] = {
   1.216 +  { "@mozilla.org/editor/htmleditorcontroller;1", &kNS_HTMLEDITORCONTROLLER_CID },
   1.217 +  { "@mozilla.org/editor/editordocstatecontroller;1", &kNS_EDITORDOCSTATECONTROLLER_CID },
   1.218 +  { "@mozilla.org/editor/editingsession;1", &kNS_EDITINGSESSION_CID },
   1.219 +  { "@mozilla.org/editor/editorspellchecker;1", &kNS_EDITORSPELLCHECK_CID },
   1.220 +  { COMPOSER_TXTSRVFILTER_CONTRACTID, &kNS_COMPOSERTXTSRVFILTER_CID },
   1.221 +  { COMPOSER_TXTSRVFILTERMAIL_CONTRACTID, &kNS_COMPOSERTXTSRVFILTERMAIL_CID },
   1.222 +  { nullptr }
   1.223 +};
   1.224 +
   1.225 +static const mozilla::Module kComposerModule = {
   1.226 +  mozilla::Module::kVersion,
   1.227 +  kComposerCIDs,
   1.228 +  kComposerContracts
   1.229 +};
   1.230 +
   1.231 +NSMODULE_DEFN(nsComposerModule) = &kComposerModule;

mercurial