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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/* -*- 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 "AddonPathService.h"
#include "amIAddonManager.h"
#include "nsIURI.h"
#include "nsXULAppAPI.h"
#include "jsapi.h"
#include "nsServiceManagerUtils.h"
#include "nsLiteralString.h"
#include "nsThreadUtils.h"
#include "nsIIOService.h"
#include "nsNetUtil.h"
#include "nsIFileURL.h"
#include "nsIResProtocolHandler.h"
#include "nsIChromeRegistry.h"
#include "nsIJARURI.h"
#include "nsJSUtils.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/ToJSValue.h"
#include "mozilla/AddonPathService.h"
#include "mozilla/Omnijar.h"
#include <algorithm>
namespace mozilla {
struct PathEntryComparator
{
typedef AddonPathService::PathEntry PathEntry;
bool Equals(const PathEntry& entry1, const PathEntry& entry2) const
{
return entry1.mPath == entry2.mPath;
}
bool LessThan(const PathEntry& entry1, const PathEntry& entry2) const
{
return entry1.mPath < entry2.mPath;
}
};
AddonPathService::AddonPathService()
{
}
AddonPathService::~AddonPathService()
{
sInstance = nullptr;
}
NS_IMPL_ISUPPORTS(AddonPathService, amIAddonPathService)
AddonPathService *AddonPathService::sInstance;
/* static */ AddonPathService*
AddonPathService::GetInstance()
{
if (!sInstance) {
sInstance = new AddonPathService();
}
NS_ADDREF(sInstance);
return sInstance;
}
static JSAddonId*
ConvertAddonId(const nsAString& addonIdString)
{
AutoSafeJSContext cx;
JS::RootedValue strv(cx);
if (!mozilla::dom::ToJSValue(cx, addonIdString, &strv)) {
return nullptr;
}
JS::RootedString str(cx, strv.toString());
return JS::NewAddonId(cx, str);
}
JSAddonId*
AddonPathService::Find(const nsAString& path)
{
// Use binary search to find the nearest entry that is <= |path|.
PathEntryComparator comparator;
unsigned index = mPaths.IndexOfFirstElementGt(PathEntry(path, nullptr), comparator);
if (index == 0) {
return nullptr;
}
const PathEntry& entry = mPaths[index - 1];
// Return the entry's addon if its path is a prefix of |path|.
if (StringBeginsWith(path, entry.mPath)) {
return entry.mAddonId;
}
return nullptr;
}
NS_IMETHODIMP
AddonPathService::FindAddonId(const nsAString& path, nsAString& addonIdString)
{
if (JSAddonId* id = Find(path)) {
JSFlatString* flat = JS_ASSERT_STRING_IS_FLAT(JS::StringOfAddonId(id));
AssignJSFlatString(addonIdString, flat);
}
return NS_OK;
}
/* static */ JSAddonId*
AddonPathService::FindAddonId(const nsAString& path)
{
// If no service has been created, then we're not going to find anything.
if (!sInstance) {
return nullptr;
}
return sInstance->Find(path);
}
NS_IMETHODIMP
AddonPathService::InsertPath(const nsAString& path, const nsAString& addonIdString)
{
JSAddonId* addonId = ConvertAddonId(addonIdString);
// Add the new path in sorted order.
PathEntryComparator comparator;
mPaths.InsertElementSorted(PathEntry(path, addonId), comparator);
return NS_OK;
}
NS_IMETHODIMP
AddonPathService::MapURIToAddonId(nsIURI* aURI, nsAString& addonIdString)
{
if (JSAddonId* id = MapURIToAddonID(aURI)) {
JSFlatString* flat = JS_ASSERT_STRING_IS_FLAT(JS::StringOfAddonId(id));
AssignJSFlatString(addonIdString, flat);
}
return NS_OK;
}
static nsresult
ResolveURI(nsIURI* aURI, nsAString& out)
{
bool equals;
nsresult rv;
nsCOMPtr<nsIURI> uri;
nsAutoCString spec;
// Resolve resource:// URIs. At the end of this if/else block, we
// have both spec and uri variables identifying the same URI.
if (NS_SUCCEEDED(aURI->SchemeIs("resource", &equals)) && equals) {
nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
if (NS_WARN_IF(NS_FAILED(rv)))
return rv;
nsCOMPtr<nsIProtocolHandler> ph;
rv = ioService->GetProtocolHandler("resource", getter_AddRefs(ph));
if (NS_WARN_IF(NS_FAILED(rv)))
return rv;
nsCOMPtr<nsIResProtocolHandler> irph(do_QueryInterface(ph, &rv));
if (NS_WARN_IF(NS_FAILED(rv)))
return rv;
rv = irph->ResolveURI(aURI, spec);
if (NS_WARN_IF(NS_FAILED(rv)))
return rv;
rv = ioService->NewURI(spec, nullptr, nullptr, getter_AddRefs(uri));
if (NS_WARN_IF(NS_FAILED(rv)))
return rv;
} else if (NS_SUCCEEDED(aURI->SchemeIs("chrome", &equals)) && equals) {
nsCOMPtr<nsIChromeRegistry> chromeReg =
mozilla::services::GetChromeRegistryService();
if (NS_WARN_IF(!chromeReg))
return NS_ERROR_UNEXPECTED;
rv = chromeReg->ConvertChromeURL(aURI, getter_AddRefs(uri));
if (NS_WARN_IF(NS_FAILED(rv)))
return rv;
} else {
uri = aURI;
}
if (NS_SUCCEEDED(uri->SchemeIs("jar", &equals)) && equals) {
nsCOMPtr<nsIJARURI> jarURI = do_QueryInterface(uri, &rv);
if (NS_WARN_IF(NS_FAILED(rv)))
return rv;
nsCOMPtr<nsIURI> jarFileURI;
rv = jarURI->GetJARFile(getter_AddRefs(jarFileURI));
if (NS_WARN_IF(NS_FAILED(rv)))
return rv;
return ResolveURI(jarFileURI, out);
}
if (NS_SUCCEEDED(uri->SchemeIs("file", &equals)) && equals) {
nsCOMPtr<nsIFileURL> baseFileURL = do_QueryInterface(uri, &rv);
if (NS_WARN_IF(NS_FAILED(rv)))
return rv;
nsCOMPtr<nsIFile> file;
rv = baseFileURL->GetFile(getter_AddRefs(file));
if (NS_WARN_IF(NS_FAILED(rv)))
return rv;
return file->GetPath(out);
}
return NS_ERROR_FAILURE;
}
JSAddonId*
MapURIToAddonID(nsIURI* aURI)
{
if (!NS_IsMainThread() || !XRE_IsParentProcess()) {
return nullptr;
}
nsAutoString filePath;
nsresult rv = ResolveURI(aURI, filePath);
if (NS_FAILED(rv))
return nullptr;
nsCOMPtr<nsIFile> greJar = Omnijar::GetPath(Omnijar::GRE);
nsCOMPtr<nsIFile> appJar = Omnijar::GetPath(Omnijar::APP);
if (greJar && appJar) {
nsAutoString greJarString, appJarString;
if (NS_FAILED(greJar->GetPath(greJarString)) || NS_FAILED(appJar->GetPath(appJarString)))
return nullptr;
// If |aURI| is part of either Omnijar, then it can't be part of an
// add-on. This catches pretty much all URLs for Firefox content.
if (filePath.Equals(greJarString) || filePath.Equals(appJarString))
return nullptr;
}
// If it's not part of Firefox, we resort to binary searching through the
// add-on paths.
return AddonPathService::FindAddonId(filePath);
}
}
|