Bug 1734943 [wpt PR 31170] - Correct scrolling contents cull rect, a=testonly
[gecko.git] / uriloader / exthandler / ContentHandlerService.cpp
blob715bdddd47b0f14d0520fa86845f5e7e2c0e2098
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;
19 namespace mozilla {
20 namespace dom {
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;
36 return NS_OK;
39 void ContentHandlerService::nsIHandlerInfoToHandlerInfo(
40 nsIHandlerInfo* aInfo, HandlerInfo* aHandlerInfo) {
41 nsCString type;
42 aInfo->GetType(type);
43 nsCOMPtr<nsIMIMEInfo> mimeInfo = do_QueryInterface(aInfo);
44 bool isMIMEInfo = !!mimeInfo;
45 nsString description;
46 aInfo->GetDescription(description);
47 bool alwaysAskBeforeHandling;
48 aInfo->GetAlwaysAskBeforeHandling(&alwaysAskBeforeHandling);
49 nsCOMPtr<nsIHandlerApp> app;
50 aInfo->GetPreferredApplicationHandler(getter_AddRefs(app));
51 nsString name;
52 nsString detailedDescription;
53 if (app) {
54 app->GetName(name);
55 app->GetDetailedDescription(detailedDescription);
57 HandlerApp happ(name, detailedDescription);
58 nsTArray<HandlerApp> happs;
59 nsCOMPtr<nsIMutableArray> apps;
60 aInfo->GetPossibleApplicationHandlers(getter_AddRefs(apps));
61 if (apps) {
62 unsigned int length;
63 apps->GetLength(&length);
64 for (unsigned int i = 0; i < length; i++) {
65 apps->QueryElementAt(i, NS_GET_IID(nsIHandlerApp), getter_AddRefs(app));
66 app->GetName(name);
67 app->GetDetailedDescription(detailedDescription);
68 happs.AppendElement(HandlerApp(name, detailedDescription));
72 nsTArray<nsCString> extensions;
74 if (isMIMEInfo) {
75 nsCOMPtr<nsIUTF8StringEnumerator> extensionsIter;
76 mimeInfo->GetFileExtensions(getter_AddRefs(extensionsIter));
77 if (extensionsIter) {
78 bool hasMore = false;
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);
92 *aHandlerInfo = info;
95 NS_IMETHODIMP RemoteHandlerApp::GetName(nsAString& aName) {
96 aName.Assign(mAppChild.name());
97 return NS_OK;
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());
107 return NS_OK;
110 NS_IMETHODIMP RemoteHandlerApp::SetDetailedDescription(
111 const nsAString& aDetailedDescription) {
112 return NS_ERROR_NOT_IMPLEMENTED;
115 NS_IMETHODIMP RemoteHandlerApp::Equals(nsIHandlerApp* aHandlerApp,
116 bool* _retval) {
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));
140 MOZ_ASSERT(mimeInfo,
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),
161 &returnedInfo);
162 CopyHanderInfoTonsIHandlerInfo(returnedInfo, aHandlerInfo);
163 return NS_OK;
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))) {
178 return rv;
181 CopyHanderInfoTonsIHandlerInfo(returnedInfo, aHandlerInfo);
182 return NS_OK;
185 NS_IMETHODIMP ContentHandlerService::Store(nsIHandlerInfo* aHandlerInfo) {
186 return NS_ERROR_NOT_IMPLEMENTED;
189 NS_IMETHODIMP ContentHandlerService::Exists(nsIHandlerInfo* aHandlerInfo,
190 bool* _retval) {
191 HandlerInfo info;
192 nsIHandlerInfoToHandlerInfo(aHandlerInfo, &info);
193 mHandlerServiceChild->SendExists(info, _retval);
194 return NS_OK;
197 NS_IMETHODIMP ContentHandlerService::Remove(nsIHandlerInfo* aHandlerInfo) {
198 return NS_ERROR_NOT_IMPLEMENTED;
201 NS_IMETHODIMP
202 ContentHandlerService::ExistsForProtocolOS(const nsACString& aProtocolScheme,
203 bool* aRetval) {
204 if (!mHandlerServiceChild->SendExistsForProtocolOS(nsCString(aProtocolScheme),
205 aRetval)) {
206 return NS_ERROR_FAILURE;
208 return NS_OK;
211 NS_IMETHODIMP
212 ContentHandlerService::ExistsForProtocol(const nsACString& aProtocolScheme,
213 bool* aRetval) {
214 if (!mHandlerServiceChild->SendExistsForProtocol(nsCString(aProtocolScheme),
215 aRetval)) {
216 return NS_ERROR_FAILURE;
218 return NS_OK;
221 NS_IMETHODIMP ContentHandlerService::GetTypeFromExtension(
222 const nsACString& aFileExtension, nsACString& _retval) {
223 _retval.Assign(*mExtToTypeMap.LookupOrInsertWith(aFileExtension, [&] {
224 nsCString type;
225 mHandlerServiceChild->SendGetTypeFromExtension(nsCString(aFileExtension),
226 &type);
227 return MakeUnique<nsCString>(type);
228 }));
229 return NS_OK;
232 NS_IMETHODIMP ContentHandlerService::GetApplicationDescription(
233 const nsACString& aProtocolScheme, nsAString& aRetVal) {
234 nsresult rv = NS_ERROR_FAILURE;
235 nsAutoCString scheme(aProtocolScheme);
236 nsAutoString desc;
237 mHandlerServiceChild->SendGetApplicationDescription(scheme, &rv, &desc);
238 aRetVal.Assign(desc);
239 return rv;
242 } // namespace dom
243 } // namespace mozilla