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 "mozilla/ModuleUtils.h"
8 #include "mozilla/GenericFactory.h"
9 
10 #include "nsICategoryManager.h"
11 #include "nsIComponentManager.h"
12 #include "nsIComponentRegistrar.h"
13 #include "nsServiceManagerUtils.h"
14 #include "nsXPCOMCID.h"
15 #include "nsStringAPI.h"
16 
17 namespace mozilla {
18 
NS_IMPL_ISUPPORTS(GenericModule,nsIModule)19 NS_IMPL_ISUPPORTS(GenericModule, nsIModule)
20 
21 NS_IMETHODIMP
22 GenericModule::GetClassObject(nsIComponentManager* aCompMgr,
23                               const nsCID& aCID,
24                               const nsIID& aIID,
25                               void** aResult)
26 {
27   for (const Module::CIDEntry* e = mData->mCIDs; e->cid; ++e) {
28     if (e->cid->Equals(aCID)) {
29       nsCOMPtr<nsIFactory> f;
30       if (e->getFactoryProc) {
31         f = e->getFactoryProc(*mData, *e);
32       } else {
33         NS_ASSERTION(e->constructorProc, "No constructor proc?");
34         f = new GenericFactory(e->constructorProc);
35       }
36       if (!f) {
37         return NS_ERROR_FAILURE;
38       }
39 
40       return f->QueryInterface(aIID, aResult);
41     }
42   }
43   NS_ERROR("Asking a module for a CID it doesn't implement.");
44   return NS_ERROR_NOT_IMPLEMENTED;
45 }
46 
47 NS_IMETHODIMP
RegisterSelf(nsIComponentManager * aCompMgr,nsIFile * aLocation,const char * aLoaderStr,const char * aType)48 GenericModule::RegisterSelf(nsIComponentManager* aCompMgr,
49                             nsIFile* aLocation,
50                             const char* aLoaderStr,
51                             const char* aType)
52 {
53   nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(aCompMgr);
54   for (const Module::CIDEntry* e = mData->mCIDs; e->cid; ++e) {
55     registrar->RegisterFactoryLocation(*e->cid, "", nullptr, aLocation,
56                                        aLoaderStr, aType);
57   }
58 
59   for (const Module::ContractIDEntry* e = mData->mContractIDs;
60        e && e->contractid;
61        ++e) {
62     registrar->RegisterFactoryLocation(*e->cid, "", e->contractid, aLocation,
63                                        aLoaderStr, aType);
64   }
65 
66   nsCOMPtr<nsICategoryManager> catman;
67   for (const Module::CategoryEntry* e = mData->mCategoryEntries;
68        e && e->category;
69        ++e) {
70     if (!catman) {
71       catman = do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
72     }
73 
74     nsAutoCString prevValue;
75     catman->AddCategoryEntry(e->category, e->entry, e->value, true, true,
76                              getter_Copies(prevValue));
77   }
78   return NS_OK;
79 }
80 
81 NS_IMETHODIMP
UnregisterSelf(nsIComponentManager * aCompMgr,nsIFile * aFile,const char * aLoaderStr)82 GenericModule::UnregisterSelf(nsIComponentManager* aCompMgr,
83                               nsIFile* aFile,
84                               const char* aLoaderStr)
85 {
86   NS_ERROR("Nobody should ever call UnregisterSelf!");
87   return NS_ERROR_NOT_IMPLEMENTED;
88 }
89 
90 NS_IMETHODIMP
CanUnload(nsIComponentManager * aCompMgr,bool * aResult)91 GenericModule::CanUnload(nsIComponentManager* aCompMgr, bool* aResult)
92 {
93   NS_ERROR("Nobody should ever call CanUnload!");
94   *aResult = false;
95   return NS_OK;
96 }
97 
98 } // namespace mozilla
99