summaryrefslogtreecommitdiffstats
path: root/xpcom/glue/GenericModule.cpp
blob: 59ca381d8b324c8f970caa7c92a16aeba1124f6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/ModuleUtils.h"
#include "mozilla/GenericFactory.h"

#include "nsICategoryManager.h"
#include "nsIComponentManager.h"
#include "nsIComponentRegistrar.h"
#include "nsServiceManagerUtils.h"
#include "nsXPCOMCID.h"
#include "nsStringAPI.h"

namespace mozilla {

NS_IMPL_ISUPPORTS(GenericModule, nsIModule)

NS_IMETHODIMP
GenericModule::GetClassObject(nsIComponentManager* aCompMgr,
                              const nsCID& aCID,
                              const nsIID& aIID,
                              void** aResult)
{
  for (const Module::CIDEntry* e = mData->mCIDs; e->cid; ++e) {
    if (e->cid->Equals(aCID)) {
      nsCOMPtr<nsIFactory> f;
      if (e->getFactoryProc) {
        f = e->getFactoryProc(*mData, *e);
      } else {
        NS_ASSERTION(e->constructorProc, "No constructor proc?");
        f = new GenericFactory(e->constructorProc);
      }
      if (!f) {
        return NS_ERROR_FAILURE;
      }

      return f->QueryInterface(aIID, aResult);
    }
  }
  NS_ERROR("Asking a module for a CID it doesn't implement.");
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
GenericModule::RegisterSelf(nsIComponentManager* aCompMgr,
                            nsIFile* aLocation,
                            const char* aLoaderStr,
                            const char* aType)
{
  nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(aCompMgr);
  for (const Module::CIDEntry* e = mData->mCIDs; e->cid; ++e) {
    registrar->RegisterFactoryLocation(*e->cid, "", nullptr, aLocation,
                                       aLoaderStr, aType);
  }

  for (const Module::ContractIDEntry* e = mData->mContractIDs;
       e && e->contractid;
       ++e) {
    registrar->RegisterFactoryLocation(*e->cid, "", e->contractid, aLocation,
                                       aLoaderStr, aType);
  }

  nsCOMPtr<nsICategoryManager> catman;
  for (const Module::CategoryEntry* e = mData->mCategoryEntries;
       e && e->category;
       ++e) {
    if (!catman) {
      catman = do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
    }

    nsAutoCString prevValue;
    catman->AddCategoryEntry(e->category, e->entry, e->value, true, true,
                             getter_Copies(prevValue));
  }
  return NS_OK;
}

NS_IMETHODIMP
GenericModule::UnregisterSelf(nsIComponentManager* aCompMgr,
                              nsIFile* aFile,
                              const char* aLoaderStr)
{
  NS_ERROR("Nobody should ever call UnregisterSelf!");
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
GenericModule::CanUnload(nsIComponentManager* aCompMgr, bool* aResult)
{
  NS_ERROR("Nobody should ever call CanUnload!");
  *aResult = false;
  return NS_OK;
}

} // namespace mozilla