/src/mozilla-central/uriloader/exthandler/ContentHandlerService.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "ContentHandlerService.h" |
2 | | #include "HandlerServiceChild.h" |
3 | | #include "ContentChild.h" |
4 | | #include "nsIMutableArray.h" |
5 | | #include "nsIMIMEInfo.h" |
6 | | #include "nsIStringEnumerator.h" |
7 | | |
8 | | using mozilla::dom::ContentChild; |
9 | | using mozilla::dom::PHandlerServiceChild; |
10 | | using mozilla::dom::HandlerInfo; |
11 | | |
12 | | namespace mozilla { |
13 | | namespace dom { |
14 | | |
15 | | NS_IMPL_ISUPPORTS(ContentHandlerService, nsIHandlerService) |
16 | | |
17 | | ContentHandlerService::ContentHandlerService() |
18 | 0 | { |
19 | 0 | } |
20 | | |
21 | | nsresult |
22 | | ContentHandlerService::Init() |
23 | 0 | { |
24 | 0 | if (!XRE_IsContentProcess()) { |
25 | 0 | return NS_ERROR_FAILURE; |
26 | 0 | } |
27 | 0 | ContentChild* cpc = ContentChild::GetSingleton(); |
28 | 0 |
|
29 | 0 | mHandlerServiceChild = static_cast<HandlerServiceChild*>(cpc->SendPHandlerServiceConstructor()); |
30 | 0 | return NS_OK; |
31 | 0 | } |
32 | | |
33 | | void |
34 | | ContentHandlerService::nsIHandlerInfoToHandlerInfo(nsIHandlerInfo* aInfo, |
35 | | HandlerInfo* aHandlerInfo) |
36 | 0 | { |
37 | 0 | nsCString type; |
38 | 0 | aInfo->GetType(type); |
39 | 0 | nsCOMPtr<nsIMIMEInfo> mimeInfo = do_QueryInterface(aInfo); |
40 | 0 | bool isMIMEInfo = !!mimeInfo; |
41 | 0 | nsString description; |
42 | 0 | aInfo->GetDescription(description); |
43 | 0 | bool alwaysAskBeforeHandling; |
44 | 0 | aInfo->GetAlwaysAskBeforeHandling(&alwaysAskBeforeHandling); |
45 | 0 | nsCOMPtr<nsIHandlerApp> app; |
46 | 0 | aInfo->GetPreferredApplicationHandler(getter_AddRefs(app)); |
47 | 0 | nsString name; |
48 | 0 | nsString detailedDescription; |
49 | 0 | if (app) { |
50 | 0 | app->GetName(name); |
51 | 0 | app->GetDetailedDescription(detailedDescription); |
52 | 0 | } |
53 | 0 | HandlerApp happ(name, detailedDescription); |
54 | 0 | nsTArray<HandlerApp> happs; |
55 | 0 | nsCOMPtr<nsIMutableArray> apps; |
56 | 0 | aInfo->GetPossibleApplicationHandlers(getter_AddRefs(apps)); |
57 | 0 | if (apps) { |
58 | 0 | unsigned int length; |
59 | 0 | apps->GetLength(&length); |
60 | 0 | for (unsigned int i = 0; i < length; i++) { |
61 | 0 | apps->QueryElementAt(i, NS_GET_IID(nsIHandlerApp), getter_AddRefs(app)); |
62 | 0 | app->GetName(name); |
63 | 0 | app->GetDetailedDescription(detailedDescription); |
64 | 0 | happs.AppendElement(HandlerApp(name, detailedDescription)); |
65 | 0 | } |
66 | 0 | } |
67 | 0 |
|
68 | 0 | nsTArray<nsCString> extensions; |
69 | 0 |
|
70 | 0 | if (isMIMEInfo) { |
71 | 0 | nsCOMPtr<nsIUTF8StringEnumerator> extensionsIter; |
72 | 0 | mimeInfo->GetFileExtensions(getter_AddRefs(extensionsIter)); |
73 | 0 | if (extensionsIter) { |
74 | 0 | bool hasMore = false; |
75 | 0 | while (NS_SUCCEEDED(extensionsIter->HasMore(&hasMore)) && hasMore) { |
76 | 0 | nsAutoCString extension; |
77 | 0 | if (NS_SUCCEEDED(extensionsIter->GetNext(extension))) { |
78 | 0 | extensions.AppendElement(std::move(extension)); |
79 | 0 | } |
80 | 0 | } |
81 | 0 | } |
82 | 0 | } |
83 | 0 |
|
84 | 0 | nsHandlerInfoAction action; |
85 | 0 | aInfo->GetPreferredAction(&action); |
86 | 0 | HandlerInfo info(type, |
87 | 0 | isMIMEInfo, |
88 | 0 | description, |
89 | 0 | alwaysAskBeforeHandling, |
90 | 0 | std::move(extensions), |
91 | 0 | happ, |
92 | 0 | happs, |
93 | 0 | action); |
94 | 0 | *aHandlerInfo = info; |
95 | 0 | } |
96 | | |
97 | | |
98 | | NS_IMETHODIMP RemoteHandlerApp::GetName(nsAString & aName) |
99 | 0 | { |
100 | 0 | aName.Assign(mAppChild.name()); |
101 | 0 | return NS_OK; |
102 | 0 | } |
103 | | |
104 | | NS_IMETHODIMP RemoteHandlerApp::SetName(const nsAString & aName) |
105 | 0 | { |
106 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
107 | 0 | } |
108 | | |
109 | | NS_IMETHODIMP RemoteHandlerApp::GetDetailedDescription(nsAString & aDetailedDescription) |
110 | 0 | { |
111 | 0 | aDetailedDescription.Assign(mAppChild.detailedDescription()); |
112 | 0 | return NS_OK; |
113 | 0 | } |
114 | | |
115 | | NS_IMETHODIMP RemoteHandlerApp::SetDetailedDescription(const nsAString & aDetailedDescription) |
116 | 0 | { |
117 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
118 | 0 | } |
119 | | |
120 | | NS_IMETHODIMP RemoteHandlerApp::Equals(nsIHandlerApp *aHandlerApp, bool *_retval) |
121 | 0 | { |
122 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
123 | 0 | } |
124 | | |
125 | | NS_IMETHODIMP RemoteHandlerApp::LaunchWithURI(nsIURI *aURI, nsIInterfaceRequestor *aWindowContext) |
126 | 0 | { |
127 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
128 | 0 | } |
129 | | |
130 | | NS_IMPL_ISUPPORTS(RemoteHandlerApp, nsIHandlerApp) |
131 | | |
132 | | static inline void CopyHanderInfoTonsIHandlerInfo(const HandlerInfo& info, nsIHandlerInfo* aHandlerInfo) |
133 | 0 | { |
134 | 0 | HandlerApp preferredApplicationHandler = info.preferredApplicationHandler(); |
135 | 0 | nsCOMPtr<nsIHandlerApp> preferredApp(new RemoteHandlerApp(preferredApplicationHandler)); |
136 | 0 | aHandlerInfo->SetPreferredApplicationHandler(preferredApp); |
137 | 0 | nsCOMPtr<nsIMutableArray> possibleHandlers; |
138 | 0 | aHandlerInfo->GetPossibleApplicationHandlers(getter_AddRefs(possibleHandlers)); |
139 | 0 | possibleHandlers->AppendElement(preferredApp); |
140 | 0 |
|
141 | 0 | if (info.isMIMEInfo()) { |
142 | 0 | const auto& fileExtensions = info.extensions(); |
143 | 0 | bool first = true; |
144 | 0 | nsAutoCString extensionsStr; |
145 | 0 | for (const auto& extension : fileExtensions) { |
146 | 0 | if (!first) { |
147 | 0 | extensionsStr.Append(','); |
148 | 0 | } |
149 | 0 |
|
150 | 0 | extensionsStr.Append(extension); |
151 | 0 | first = false; |
152 | 0 | } |
153 | 0 |
|
154 | 0 | nsCOMPtr<nsIMIMEInfo> mimeInfo(do_QueryInterface(aHandlerInfo)); |
155 | 0 | MOZ_ASSERT(mimeInfo, "parent and child don't agree on whether this is a MIME info"); |
156 | 0 | mimeInfo->SetFileExtensions(extensionsStr); |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | ContentHandlerService::~ContentHandlerService() |
161 | 0 | { |
162 | 0 | } |
163 | | |
164 | | NS_IMETHODIMP ContentHandlerService::AsyncInit() |
165 | 0 | { |
166 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
167 | 0 | } |
168 | | |
169 | | NS_IMETHODIMP ContentHandlerService::Enumerate(nsISimpleEnumerator * *_retval) |
170 | 0 | { |
171 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
172 | 0 | } |
173 | | |
174 | | NS_IMETHODIMP ContentHandlerService::FillHandlerInfo(nsIHandlerInfo *aHandlerInfo, const nsACString & aOverrideType) |
175 | 0 | { |
176 | 0 | HandlerInfo info, returnedInfo; |
177 | 0 | nsIHandlerInfoToHandlerInfo(aHandlerInfo, &info); |
178 | 0 | mHandlerServiceChild->SendFillHandlerInfo(info, nsCString(aOverrideType), &returnedInfo); |
179 | 0 | CopyHanderInfoTonsIHandlerInfo(returnedInfo, aHandlerInfo); |
180 | 0 | return NS_OK; |
181 | 0 | } |
182 | | |
183 | | NS_IMETHODIMP ContentHandlerService::Store(nsIHandlerInfo *aHandlerInfo) |
184 | 0 | { |
185 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
186 | 0 | } |
187 | | |
188 | | NS_IMETHODIMP ContentHandlerService::Exists(nsIHandlerInfo *aHandlerInfo, bool *_retval) |
189 | 0 | { |
190 | 0 | HandlerInfo info; |
191 | 0 | nsIHandlerInfoToHandlerInfo(aHandlerInfo, &info); |
192 | 0 | mHandlerServiceChild->SendExists(info, _retval); |
193 | 0 | return NS_OK; |
194 | 0 | } |
195 | | |
196 | | NS_IMETHODIMP ContentHandlerService::Remove(nsIHandlerInfo *aHandlerInfo) |
197 | 0 | { |
198 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
199 | 0 | } |
200 | | |
201 | | NS_IMETHODIMP |
202 | | ContentHandlerService::ExistsForProtocol(const nsACString& aProtocolScheme, bool* aRetval) |
203 | 0 | { |
204 | 0 | if (!mHandlerServiceChild->SendExistsForProtocol(nsCString(aProtocolScheme), aRetval)) { |
205 | 0 | return NS_ERROR_FAILURE; |
206 | 0 | } |
207 | 0 | return NS_OK; |
208 | 0 | } |
209 | | |
210 | | NS_IMETHODIMP ContentHandlerService::GetTypeFromExtension(const nsACString & aFileExtension, nsACString & _retval) |
211 | 0 | { |
212 | 0 | nsCString* cachedType = nullptr; |
213 | 0 | if (!!mExtToTypeMap.Get(aFileExtension, &cachedType) && !!cachedType) { |
214 | 0 | _retval.Assign(*cachedType); |
215 | 0 | return NS_OK; |
216 | 0 | } |
217 | 0 | nsCString type; |
218 | 0 | mHandlerServiceChild->SendGetTypeFromExtension(nsCString(aFileExtension), &type); |
219 | 0 | _retval.Assign(type); |
220 | 0 | mExtToTypeMap.Put(nsCString(aFileExtension), new nsCString(type)); |
221 | 0 |
|
222 | 0 | return NS_OK; |
223 | 0 | } |
224 | | |
225 | | } |
226 | | } |