1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim: set ts=4 et sw=4 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 #ifndef mozilla_XPTInterfaceInfoManager_h_
8 #define mozilla_XPTInterfaceInfoManager_h_
9 
10 #include "nsIInterfaceInfoManager.h"
11 #include "nsIMemoryReporter.h"
12 
13 #include "mozilla/MemoryReporting.h"
14 #include "mozilla/Mutex.h"
15 #include "mozilla/ReentrantMonitor.h"
16 #include "nsDataHashtable.h"
17 
18 template<typename T> class nsCOMArray;
19 class nsIMemoryReporter;
20 struct XPTHeader;
21 struct XPTInterfaceDirectoryEntry;
22 class xptiInterfaceEntry;
23 class xptiInterfaceInfo;
24 class xptiTypelibGuts;
25 
26 namespace mozilla {
27 
28 class XPTInterfaceInfoManager final
29     : public nsIInterfaceInfoManager
30     , public nsIMemoryReporter
31 {
32     NS_DECL_THREADSAFE_ISUPPORTS
33     NS_DECL_NSIINTERFACEINFOMANAGER
34     NS_DECL_NSIMEMORYREPORTER
35 
36 public:
37     // GetSingleton() is infallible
38     static XPTInterfaceInfoManager* GetSingleton();
39     static void FreeInterfaceInfoManager();
40 
41     void GetScriptableInterfaces(nsCOMArray<nsIInterfaceInfo>& aInterfaces);
42 
43     void RegisterBuffer(char *buf, uint32_t length);
44 
GetResolveLock()45     static Mutex& GetResolveLock()
46     {
47         return GetSingleton()->mResolveLock;
48     }
49 
50     xptiInterfaceEntry* GetInterfaceEntryForIID(const nsIID *iid);
51 
52     size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
53 
54 private:
55     XPTInterfaceInfoManager();
56     ~XPTInterfaceInfoManager();
57 
58     void InitMemoryReporter();
59 
60     void RegisterXPTHeader(XPTHeader* aHeader);
61 
62     // idx is the index of this interface in the XPTHeader
63     void VerifyAndAddEntryIfNew(XPTInterfaceDirectoryEntry* iface,
64                                 uint16_t idx,
65                                 xptiTypelibGuts* typelib);
66 
67 private:
68 
69     class xptiWorkingSet
70     {
71     public:
72         xptiWorkingSet();
73         ~xptiWorkingSet();
74 
75         bool IsValid() const;
76 
77         void InvalidateInterfaceInfos();
78         void ClearHashTables();
79 
80         // utility methods...
81 
82         enum {NOT_FOUND = 0xffffffff};
83 
84         // Directory stuff...
85 
86         uint32_t GetDirectoryCount();
87         nsresult GetCloneOfDirectoryAt(uint32_t i, nsIFile** dir);
88         nsresult GetDirectoryAt(uint32_t i, nsIFile** dir);
89         bool     FindDirectory(nsIFile* dir, uint32_t* index);
90         bool     FindDirectoryOfFile(nsIFile* file, uint32_t* index);
91         bool     DirectoryAtMatchesPersistentDescriptor(uint32_t i, const char* desc);
92 
93     private:
94         uint32_t        mFileCount;
95         uint32_t        mMaxFileCount;
96 
97     public:
98         // XXX make these private with accessors
99         // mTableMonitor must be held across:
100         //  * any read from or write to mIIDTable or mNameTable
101         //  * any writing to the links between an xptiInterfaceEntry
102         //    and its xptiInterfaceInfo (mEntry/mInfo)
103         mozilla::ReentrantMonitor mTableReentrantMonitor;
104         nsDataHashtable<nsIDHashKey, xptiInterfaceEntry*> mIIDTable;
105         nsDataHashtable<nsDepCharHashKey, xptiInterfaceEntry*> mNameTable;
106     };
107 
108     // XXX xptiInterfaceInfo want's to poke at the working set itself
109     friend class ::xptiInterfaceInfo;
110     friend class ::xptiInterfaceEntry;
111     friend class ::xptiTypelibGuts;
112 
113     xptiWorkingSet               mWorkingSet;
114     Mutex                        mResolveLock;
115 };
116 
117 } // namespace mozilla
118 
119 #endif
120