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/. */
7 #include "ContentHandlerService.h"
8 #include "HandlerServiceChild.h"
9 #include "ContentChild.h"
10 #include "nsIMutableArray.h"
11 #include "nsIMIMEInfo.h"
12 #include "nsIStringEnumerator.h"
13 #include "nsReadableUtils.h"
15 using mozilla::dom::ContentChild
;
16 using mozilla::dom::HandlerInfo
;
17 using mozilla::dom::PHandlerServiceChild
;
22 NS_IMPL_ISUPPORTS(ContentHandlerService
, nsIHandlerService
)
24 ContentHandlerService::ContentHandlerService() {}
26 nsresult
ContentHandlerService::Init() {
27 if (!XRE_IsContentProcess()) {
28 return NS_ERROR_FAILURE
;
30 ContentChild
* cpc
= ContentChild::GetSingleton();
32 mHandlerServiceChild
= new HandlerServiceChild();
33 if (!cpc
->SendPHandlerServiceConstructor(mHandlerServiceChild
)) {
34 mHandlerServiceChild
= nullptr;
39 void ContentHandlerService::nsIHandlerInfoToHandlerInfo(
40 nsIHandlerInfo
* aInfo
, HandlerInfo
* aHandlerInfo
) {
43 nsCOMPtr
<nsIMIMEInfo
> mimeInfo
= do_QueryInterface(aInfo
);
44 bool isMIMEInfo
= !!mimeInfo
;
46 aInfo
->GetDescription(description
);
47 bool alwaysAskBeforeHandling
;
48 aInfo
->GetAlwaysAskBeforeHandling(&alwaysAskBeforeHandling
);
49 nsCOMPtr
<nsIHandlerApp
> app
;
50 aInfo
->GetPreferredApplicationHandler(getter_AddRefs(app
));
52 nsString detailedDescription
;
55 app
->GetDetailedDescription(detailedDescription
);
57 HandlerApp
happ(name
, detailedDescription
);
58 nsTArray
<HandlerApp
> happs
;
59 nsCOMPtr
<nsIMutableArray
> apps
;
60 aInfo
->GetPossibleApplicationHandlers(getter_AddRefs(apps
));
63 apps
->GetLength(&length
);
64 for (unsigned int i
= 0; i
< length
; i
++) {
65 apps
->QueryElementAt(i
, NS_GET_IID(nsIHandlerApp
), getter_AddRefs(app
));
67 app
->GetDetailedDescription(detailedDescription
);
68 happs
.AppendElement(HandlerApp(name
, detailedDescription
));
72 nsTArray
<nsCString
> extensions
;
75 nsCOMPtr
<nsIUTF8StringEnumerator
> extensionsIter
;
76 mimeInfo
->GetFileExtensions(getter_AddRefs(extensionsIter
));
79 while (NS_SUCCEEDED(extensionsIter
->HasMore(&hasMore
)) && hasMore
) {
80 nsAutoCString extension
;
81 if (NS_SUCCEEDED(extensionsIter
->GetNext(extension
))) {
82 extensions
.AppendElement(std::move(extension
));
88 nsHandlerInfoAction action
;
89 aInfo
->GetPreferredAction(&action
);
90 HandlerInfo
info(type
, isMIMEInfo
, description
, alwaysAskBeforeHandling
,
91 std::move(extensions
), happ
, happs
, action
);
95 NS_IMETHODIMP
RemoteHandlerApp::GetName(nsAString
& aName
) {
96 aName
.Assign(mAppChild
.name());
100 NS_IMETHODIMP
RemoteHandlerApp::SetName(const nsAString
& aName
) {
101 return NS_ERROR_NOT_IMPLEMENTED
;
104 NS_IMETHODIMP
RemoteHandlerApp::GetDetailedDescription(
105 nsAString
& aDetailedDescription
) {
106 aDetailedDescription
.Assign(mAppChild
.detailedDescription());
110 NS_IMETHODIMP
RemoteHandlerApp::SetDetailedDescription(
111 const nsAString
& aDetailedDescription
) {
112 return NS_ERROR_NOT_IMPLEMENTED
;
115 NS_IMETHODIMP
RemoteHandlerApp::Equals(nsIHandlerApp
* aHandlerApp
,
117 return NS_ERROR_NOT_IMPLEMENTED
;
120 NS_IMETHODIMP
RemoteHandlerApp::LaunchWithURI(
121 nsIURI
* aURI
, BrowsingContext
* aBrowsingContext
) {
122 return NS_ERROR_NOT_IMPLEMENTED
;
125 NS_IMPL_ISUPPORTS(RemoteHandlerApp
, nsIHandlerApp
)
127 static inline void CopyHanderInfoTonsIHandlerInfo(
128 const HandlerInfo
& info
, nsIHandlerInfo
* aHandlerInfo
) {
129 HandlerApp preferredApplicationHandler
= info
.preferredApplicationHandler();
130 nsCOMPtr
<nsIHandlerApp
> preferredApp(
131 new RemoteHandlerApp(preferredApplicationHandler
));
132 aHandlerInfo
->SetPreferredApplicationHandler(preferredApp
);
133 nsCOMPtr
<nsIMutableArray
> possibleHandlers
;
134 aHandlerInfo
->GetPossibleApplicationHandlers(
135 getter_AddRefs(possibleHandlers
));
136 possibleHandlers
->AppendElement(preferredApp
);
138 if (info
.isMIMEInfo()) {
139 nsCOMPtr
<nsIMIMEInfo
> mimeInfo(do_QueryInterface(aHandlerInfo
));
141 "parent and child don't agree on whether this is a MIME info");
142 mimeInfo
->SetFileExtensions(StringJoin(","_ns
, info
.extensions()));
146 ContentHandlerService::~ContentHandlerService() {}
148 NS_IMETHODIMP
ContentHandlerService::AsyncInit() {
149 return NS_ERROR_NOT_IMPLEMENTED
;
152 NS_IMETHODIMP
ContentHandlerService::Enumerate(nsISimpleEnumerator
** _retval
) {
153 return NS_ERROR_NOT_IMPLEMENTED
;
156 NS_IMETHODIMP
ContentHandlerService::FillHandlerInfo(
157 nsIHandlerInfo
* aHandlerInfo
, const nsACString
& aOverrideType
) {
158 HandlerInfo info
, returnedInfo
;
159 nsIHandlerInfoToHandlerInfo(aHandlerInfo
, &info
);
160 mHandlerServiceChild
->SendFillHandlerInfo(info
, nsCString(aOverrideType
),
162 CopyHanderInfoTonsIHandlerInfo(returnedInfo
, aHandlerInfo
);
166 NS_IMETHODIMP
ContentHandlerService::GetMIMEInfoFromOS(
167 nsIHandlerInfo
* aHandlerInfo
, const nsACString
& aMIMEType
,
168 const nsACString
& aExtension
, bool* aFound
) {
169 nsresult rv
= NS_ERROR_FAILURE
;
170 HandlerInfo returnedInfo
;
171 if (!mHandlerServiceChild
->SendGetMIMEInfoFromOS(nsCString(aMIMEType
),
172 nsCString(aExtension
), &rv
,
173 &returnedInfo
, aFound
)) {
174 return NS_ERROR_FAILURE
;
177 if (NS_WARN_IF(NS_FAILED(rv
))) {
181 CopyHanderInfoTonsIHandlerInfo(returnedInfo
, aHandlerInfo
);
185 NS_IMETHODIMP
ContentHandlerService::Store(nsIHandlerInfo
* aHandlerInfo
) {
186 return NS_ERROR_NOT_IMPLEMENTED
;
189 NS_IMETHODIMP
ContentHandlerService::Exists(nsIHandlerInfo
* aHandlerInfo
,
192 nsIHandlerInfoToHandlerInfo(aHandlerInfo
, &info
);
193 mHandlerServiceChild
->SendExists(info
, _retval
);
197 NS_IMETHODIMP
ContentHandlerService::Remove(nsIHandlerInfo
* aHandlerInfo
) {
198 return NS_ERROR_NOT_IMPLEMENTED
;
202 ContentHandlerService::ExistsForProtocolOS(const nsACString
& aProtocolScheme
,
204 if (!mHandlerServiceChild
->SendExistsForProtocolOS(nsCString(aProtocolScheme
),
206 return NS_ERROR_FAILURE
;
212 ContentHandlerService::ExistsForProtocol(const nsACString
& aProtocolScheme
,
214 if (!mHandlerServiceChild
->SendExistsForProtocol(nsCString(aProtocolScheme
),
216 return NS_ERROR_FAILURE
;
221 NS_IMETHODIMP
ContentHandlerService::GetTypeFromExtension(
222 const nsACString
& aFileExtension
, nsACString
& _retval
) {
223 _retval
.Assign(*mExtToTypeMap
.LookupOrInsertWith(aFileExtension
, [&] {
225 mHandlerServiceChild
->SendGetTypeFromExtension(nsCString(aFileExtension
),
227 return MakeUnique
<nsCString
>(type
);
232 NS_IMETHODIMP
ContentHandlerService::GetApplicationDescription(
233 const nsACString
& aProtocolScheme
, nsAString
& aRetVal
) {
234 nsresult rv
= NS_ERROR_FAILURE
;
235 nsAutoCString
scheme(aProtocolScheme
);
237 mHandlerServiceChild
->SendGetApplicationDescription(scheme
, &rv
, &desc
);
238 aRetVal
.Assign(desc
);
243 } // namespace mozilla