NeckoParent.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "necko-config.h"
  6. #include "nsHttp.h"
  7. #include "mozilla/net/NeckoParent.h"
  8. #include "mozilla/net/HttpChannelParent.h"
  9. #include "mozilla/net/CookieServiceParent.h"
  10. #include "mozilla/net/WyciwygChannelParent.h"
  11. #include "mozilla/net/FTPChannelParent.h"
  12. #include "mozilla/net/WebSocketChannelParent.h"
  13. #include "mozilla/net/WebSocketEventListenerParent.h"
  14. #include "mozilla/net/DataChannelParent.h"
  15. #include "mozilla/net/AltDataOutputStreamParent.h"
  16. #include "mozilla/Unused.h"
  17. #ifdef NECKO_PROTOCOL_rtsp
  18. #include "mozilla/net/RtspControllerParent.h"
  19. #include "mozilla/net/RtspChannelParent.h"
  20. #endif
  21. #include "mozilla/net/DNSRequestParent.h"
  22. #include "mozilla/net/ChannelDiverterParent.h"
  23. #include "mozilla/net/IPCTransportProvider.h"
  24. #include "mozilla/dom/ChromeUtils.h"
  25. #include "mozilla/dom/ContentParent.h"
  26. #include "mozilla/dom/TabContext.h"
  27. #include "mozilla/dom/TabParent.h"
  28. #include "mozilla/dom/network/TCPSocketParent.h"
  29. #include "mozilla/dom/network/TCPServerSocketParent.h"
  30. #include "mozilla/dom/network/UDPSocketParent.h"
  31. #include "mozilla/dom/workers/ServiceWorkerManager.h"
  32. #include "mozilla/LoadContext.h"
  33. #include "mozilla/AppProcessChecker.h"
  34. #include "nsPrintfCString.h"
  35. #include "nsHTMLDNSPrefetch.h"
  36. #include "nsIAppsService.h"
  37. #include "nsEscape.h"
  38. #include "SerializedLoadContext.h"
  39. #include "nsAuthInformationHolder.h"
  40. #include "nsIAuthPromptCallback.h"
  41. #include "nsPrincipal.h"
  42. #include "nsINetworkPredictor.h"
  43. #include "nsINetworkPredictorVerifier.h"
  44. #include "nsISpeculativeConnect.h"
  45. #include "nsIOService.h"
  46. #include "mozilla/ipc/URIUtils.h"
  47. using mozilla::DocShellOriginAttributes;
  48. using mozilla::NeckoOriginAttributes;
  49. using mozilla::dom::ChromeUtils;
  50. using mozilla::dom::ContentParent;
  51. using mozilla::dom::TabContext;
  52. using mozilla::dom::TabParent;
  53. using mozilla::net::PTCPSocketParent;
  54. using mozilla::dom::TCPSocketParent;
  55. using mozilla::net::PTCPServerSocketParent;
  56. using mozilla::dom::TCPServerSocketParent;
  57. using mozilla::net::PUDPSocketParent;
  58. using mozilla::dom::UDPSocketParent;
  59. using mozilla::dom::workers::ServiceWorkerManager;
  60. using mozilla::ipc::OptionalPrincipalInfo;
  61. using mozilla::ipc::PrincipalInfo;
  62. using IPC::SerializedLoadContext;
  63. namespace mozilla {
  64. namespace net {
  65. // C++ file contents
  66. NeckoParent::NeckoParent()
  67. {
  68. // Init HTTP protocol handler now since we need atomTable up and running very
  69. // early (IPDL argument handling for PHttpChannel constructor needs it) so
  70. // normal init (during 1st Http channel request) isn't early enough.
  71. nsCOMPtr<nsIProtocolHandler> proto =
  72. do_GetService("@mozilla.org/network/protocol;1?name=http");
  73. // only register once--we will have multiple NeckoParents if there are
  74. // multiple child processes.
  75. static bool registeredBool = false;
  76. if (!registeredBool) {
  77. Preferences::AddBoolVarCache(&NeckoCommonInternal::gSecurityDisabled,
  78. "network.disable.ipc.security");
  79. registeredBool = true;
  80. }
  81. }
  82. NeckoParent::~NeckoParent()
  83. {
  84. }
  85. static PBOverrideStatus
  86. PBOverrideStatusFromLoadContext(const SerializedLoadContext& aSerialized)
  87. {
  88. if (!aSerialized.IsNotNull() && aSerialized.IsPrivateBitValid()) {
  89. return (aSerialized.mOriginAttributes.mPrivateBrowsingId > 0) ?
  90. kPBOverride_Private :
  91. kPBOverride_NotPrivate;
  92. }
  93. return kPBOverride_Unset;
  94. }
  95. static already_AddRefed<nsIPrincipal>
  96. GetRequestingPrincipal(const OptionalLoadInfoArgs aOptionalLoadInfoArgs)
  97. {
  98. if (aOptionalLoadInfoArgs.type() != OptionalLoadInfoArgs::TLoadInfoArgs) {
  99. return nullptr;
  100. }
  101. const LoadInfoArgs& loadInfoArgs = aOptionalLoadInfoArgs.get_LoadInfoArgs();
  102. const OptionalPrincipalInfo& optionalPrincipalInfo =
  103. loadInfoArgs.requestingPrincipalInfo();
  104. if (optionalPrincipalInfo.type() != OptionalPrincipalInfo::TPrincipalInfo) {
  105. return nullptr;
  106. }
  107. const PrincipalInfo& principalInfo =
  108. optionalPrincipalInfo.get_PrincipalInfo();
  109. return PrincipalInfoToPrincipal(principalInfo);
  110. }
  111. static already_AddRefed<nsIPrincipal>
  112. GetRequestingPrincipal(const HttpChannelCreationArgs& aArgs)
  113. {
  114. if (aArgs.type() != HttpChannelCreationArgs::THttpChannelOpenArgs) {
  115. return nullptr;
  116. }
  117. const HttpChannelOpenArgs& args = aArgs.get_HttpChannelOpenArgs();
  118. return GetRequestingPrincipal(args.loadInfo());
  119. }
  120. static already_AddRefed<nsIPrincipal>
  121. GetRequestingPrincipal(const FTPChannelCreationArgs& aArgs)
  122. {
  123. if (aArgs.type() != FTPChannelCreationArgs::TFTPChannelOpenArgs) {
  124. return nullptr;
  125. }
  126. const FTPChannelOpenArgs& args = aArgs.get_FTPChannelOpenArgs();
  127. return GetRequestingPrincipal(args.loadInfo());
  128. }
  129. // Bug 1289001 - If GetValidatedOriginAttributes returns an error string, that
  130. // usually leads to a content crash with very little info about the cause.
  131. // We prefer to crash on the parent, so we get the reason in the crash report.
  132. static MOZ_COLD
  133. void CrashWithReason(const char * reason)
  134. {
  135. #ifndef RELEASE_OR_BETA
  136. MOZ_CRASH_UNSAFE_OOL(reason);
  137. #endif
  138. }
  139. const char*
  140. NeckoParent::GetValidatedOriginAttributes(const SerializedLoadContext& aSerialized,
  141. PContentParent* aContent,
  142. nsIPrincipal* aRequestingPrincipal,
  143. DocShellOriginAttributes& aAttrs)
  144. {
  145. if (!UsingNeckoIPCSecurity()) {
  146. if (!aSerialized.IsNotNull()) {
  147. // If serialized is null, we cannot validate anything. We have to assume
  148. // that this requests comes from a SystemPrincipal.
  149. aAttrs = DocShellOriginAttributes(NECKO_NO_APP_ID, false);
  150. } else {
  151. aAttrs = aSerialized.mOriginAttributes;
  152. }
  153. return nullptr;
  154. }
  155. if (!aSerialized.IsNotNull()) {
  156. CrashWithReason("GetValidatedOriginAttributes | SerializedLoadContext from child is null");
  157. return "SerializedLoadContext from child is null";
  158. }
  159. nsTArray<TabContext> contextArray =
  160. static_cast<ContentParent*>(aContent)->GetManagedTabContext();
  161. nsAutoCString serializedSuffix;
  162. aSerialized.mOriginAttributes.CreateAnonymizedSuffix(serializedSuffix);
  163. nsAutoCString debugString;
  164. for (uint32_t i = 0; i < contextArray.Length(); i++) {
  165. const TabContext& tabContext = contextArray[i];
  166. if (!ChromeUtils::IsOriginAttributesEqual(aSerialized.mOriginAttributes,
  167. tabContext.OriginAttributesRef())) {
  168. debugString.Append("(");
  169. debugString.Append(serializedSuffix);
  170. debugString.Append(",");
  171. nsAutoCString tabSuffix;
  172. tabContext.OriginAttributesRef().CreateAnonymizedSuffix(tabSuffix);
  173. debugString.Append(tabSuffix);
  174. debugString.Append(")");
  175. continue;
  176. }
  177. aAttrs = aSerialized.mOriginAttributes;
  178. return nullptr;
  179. }
  180. // This may be a ServiceWorker: when a push notification is received, FF wakes
  181. // up the corrisponding service worker so that it can manage the PushEvent. At
  182. // that time we probably don't have any valid tabcontext, but still, we want
  183. // to support http channel requests coming from that ServiceWorker.
  184. if (aRequestingPrincipal) {
  185. RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
  186. if (swm &&
  187. swm->MayHaveActiveServiceWorkerInstance(static_cast<ContentParent*>(aContent),
  188. aRequestingPrincipal)) {
  189. aAttrs = aSerialized.mOriginAttributes;
  190. return nullptr;
  191. }
  192. }
  193. nsAutoCString errorString;
  194. errorString.Append("GetValidatedOriginAttributes | App does not have permission -");
  195. errorString.Append(debugString);
  196. // Leak the buffer on the heap to make sure that it lives long enough, as
  197. // MOZ_CRASH_ANNOTATE expects the pointer passed to it to live to the end of
  198. // the program.
  199. char * error = strdup(errorString.BeginReading());
  200. CrashWithReason(error);
  201. return "App does not have permission";
  202. }
  203. const char *
  204. NeckoParent::CreateChannelLoadContext(const PBrowserOrId& aBrowser,
  205. PContentParent* aContent,
  206. const SerializedLoadContext& aSerialized,
  207. nsIPrincipal* aRequestingPrincipal,
  208. nsCOMPtr<nsILoadContext> &aResult)
  209. {
  210. DocShellOriginAttributes attrs;
  211. const char* error = GetValidatedOriginAttributes(aSerialized, aContent,
  212. aRequestingPrincipal, attrs);
  213. if (error) {
  214. return error;
  215. }
  216. // if !UsingNeckoIPCSecurity(), we may not have a LoadContext to set. This is
  217. // the common case for most xpcshell tests.
  218. if (aSerialized.IsNotNull()) {
  219. attrs.SyncAttributesWithPrivateBrowsing(aSerialized.mOriginAttributes.mPrivateBrowsingId > 0);
  220. switch (aBrowser.type()) {
  221. case PBrowserOrId::TPBrowserParent:
  222. {
  223. RefPtr<TabParent> tabParent =
  224. TabParent::GetFrom(aBrowser.get_PBrowserParent());
  225. dom::Element* topFrameElement = nullptr;
  226. if (tabParent) {
  227. topFrameElement = tabParent->GetOwnerElement();
  228. }
  229. aResult = new LoadContext(aSerialized, topFrameElement, attrs);
  230. break;
  231. }
  232. case PBrowserOrId::TTabId:
  233. {
  234. aResult = new LoadContext(aSerialized, aBrowser.get_TabId(), attrs);
  235. break;
  236. }
  237. default:
  238. MOZ_CRASH();
  239. }
  240. }
  241. return nullptr;
  242. }
  243. void
  244. NeckoParent::ActorDestroy(ActorDestroyReason aWhy)
  245. {
  246. // Nothing needed here. Called right before destructor since this is a
  247. // non-refcounted class.
  248. }
  249. PHttpChannelParent*
  250. NeckoParent::AllocPHttpChannelParent(const PBrowserOrId& aBrowser,
  251. const SerializedLoadContext& aSerialized,
  252. const HttpChannelCreationArgs& aOpenArgs)
  253. {
  254. nsCOMPtr<nsIPrincipal> requestingPrincipal =
  255. GetRequestingPrincipal(aOpenArgs);
  256. nsCOMPtr<nsILoadContext> loadContext;
  257. const char *error = CreateChannelLoadContext(aBrowser, Manager(),
  258. aSerialized, requestingPrincipal,
  259. loadContext);
  260. if (error) {
  261. printf_stderr("NeckoParent::AllocPHttpChannelParent: "
  262. "FATAL error: %s: KILLING CHILD PROCESS\n",
  263. error);
  264. return nullptr;
  265. }
  266. PBOverrideStatus overrideStatus = PBOverrideStatusFromLoadContext(aSerialized);
  267. HttpChannelParent *p = new HttpChannelParent(aBrowser, loadContext, overrideStatus);
  268. p->AddRef();
  269. return p;
  270. }
  271. bool
  272. NeckoParent::DeallocPHttpChannelParent(PHttpChannelParent* channel)
  273. {
  274. HttpChannelParent *p = static_cast<HttpChannelParent *>(channel);
  275. p->Release();
  276. return true;
  277. }
  278. bool
  279. NeckoParent::RecvPHttpChannelConstructor(
  280. PHttpChannelParent* aActor,
  281. const PBrowserOrId& aBrowser,
  282. const SerializedLoadContext& aSerialized,
  283. const HttpChannelCreationArgs& aOpenArgs)
  284. {
  285. HttpChannelParent* p = static_cast<HttpChannelParent*>(aActor);
  286. return p->Init(aOpenArgs);
  287. }
  288. PAltDataOutputStreamParent*
  289. NeckoParent::AllocPAltDataOutputStreamParent(
  290. const nsCString& type,
  291. PHttpChannelParent* channel)
  292. {
  293. HttpChannelParent* chan = static_cast<HttpChannelParent*>(channel);
  294. nsCOMPtr<nsIOutputStream> stream;
  295. nsresult rv = chan->OpenAlternativeOutputStream(type, getter_AddRefs(stream));
  296. AltDataOutputStreamParent* parent = new AltDataOutputStreamParent(stream);
  297. parent->AddRef();
  298. // If the return value was not NS_OK, the error code will be sent
  299. // asynchronously to the child, after receiving the first message.
  300. parent->SetError(rv);
  301. return parent;
  302. }
  303. bool
  304. NeckoParent::DeallocPAltDataOutputStreamParent(PAltDataOutputStreamParent* aActor)
  305. {
  306. AltDataOutputStreamParent* parent = static_cast<AltDataOutputStreamParent*>(aActor);
  307. parent->Release();
  308. return true;
  309. }
  310. PFTPChannelParent*
  311. NeckoParent::AllocPFTPChannelParent(const PBrowserOrId& aBrowser,
  312. const SerializedLoadContext& aSerialized,
  313. const FTPChannelCreationArgs& aOpenArgs)
  314. {
  315. nsCOMPtr<nsIPrincipal> requestingPrincipal =
  316. GetRequestingPrincipal(aOpenArgs);
  317. nsCOMPtr<nsILoadContext> loadContext;
  318. const char *error = CreateChannelLoadContext(aBrowser, Manager(),
  319. aSerialized, requestingPrincipal,
  320. loadContext);
  321. if (error) {
  322. printf_stderr("NeckoParent::AllocPFTPChannelParent: "
  323. "FATAL error: %s: KILLING CHILD PROCESS\n",
  324. error);
  325. return nullptr;
  326. }
  327. PBOverrideStatus overrideStatus = PBOverrideStatusFromLoadContext(aSerialized);
  328. FTPChannelParent *p = new FTPChannelParent(aBrowser, loadContext, overrideStatus);
  329. p->AddRef();
  330. return p;
  331. }
  332. bool
  333. NeckoParent::DeallocPFTPChannelParent(PFTPChannelParent* channel)
  334. {
  335. FTPChannelParent *p = static_cast<FTPChannelParent *>(channel);
  336. p->Release();
  337. return true;
  338. }
  339. bool
  340. NeckoParent::RecvPFTPChannelConstructor(
  341. PFTPChannelParent* aActor,
  342. const PBrowserOrId& aBrowser,
  343. const SerializedLoadContext& aSerialized,
  344. const FTPChannelCreationArgs& aOpenArgs)
  345. {
  346. FTPChannelParent* p = static_cast<FTPChannelParent*>(aActor);
  347. return p->Init(aOpenArgs);
  348. }
  349. PCookieServiceParent*
  350. NeckoParent::AllocPCookieServiceParent()
  351. {
  352. return new CookieServiceParent();
  353. }
  354. bool
  355. NeckoParent::DeallocPCookieServiceParent(PCookieServiceParent* cs)
  356. {
  357. delete cs;
  358. return true;
  359. }
  360. PWyciwygChannelParent*
  361. NeckoParent::AllocPWyciwygChannelParent()
  362. {
  363. WyciwygChannelParent *p = new WyciwygChannelParent();
  364. p->AddRef();
  365. return p;
  366. }
  367. bool
  368. NeckoParent::DeallocPWyciwygChannelParent(PWyciwygChannelParent* channel)
  369. {
  370. WyciwygChannelParent *p = static_cast<WyciwygChannelParent *>(channel);
  371. p->Release();
  372. return true;
  373. }
  374. PWebSocketParent*
  375. NeckoParent::AllocPWebSocketParent(const PBrowserOrId& browser,
  376. const SerializedLoadContext& serialized,
  377. const uint32_t& aSerial)
  378. {
  379. nsCOMPtr<nsILoadContext> loadContext;
  380. const char *error = CreateChannelLoadContext(browser, Manager(),
  381. serialized,
  382. nullptr,
  383. loadContext);
  384. if (error) {
  385. printf_stderr("NeckoParent::AllocPWebSocketParent: "
  386. "FATAL error: %s: KILLING CHILD PROCESS\n",
  387. error);
  388. return nullptr;
  389. }
  390. RefPtr<TabParent> tabParent = TabParent::GetFrom(browser.get_PBrowserParent());
  391. PBOverrideStatus overrideStatus = PBOverrideStatusFromLoadContext(serialized);
  392. WebSocketChannelParent* p = new WebSocketChannelParent(tabParent, loadContext,
  393. overrideStatus,
  394. aSerial);
  395. p->AddRef();
  396. return p;
  397. }
  398. bool
  399. NeckoParent::DeallocPWebSocketParent(PWebSocketParent* actor)
  400. {
  401. WebSocketChannelParent* p = static_cast<WebSocketChannelParent*>(actor);
  402. p->Release();
  403. return true;
  404. }
  405. PWebSocketEventListenerParent*
  406. NeckoParent::AllocPWebSocketEventListenerParent(const uint64_t& aInnerWindowID)
  407. {
  408. RefPtr<WebSocketEventListenerParent> c =
  409. new WebSocketEventListenerParent(aInnerWindowID);
  410. return c.forget().take();
  411. }
  412. bool
  413. NeckoParent::DeallocPWebSocketEventListenerParent(PWebSocketEventListenerParent* aActor)
  414. {
  415. RefPtr<WebSocketEventListenerParent> c =
  416. dont_AddRef(static_cast<WebSocketEventListenerParent*>(aActor));
  417. MOZ_ASSERT(c);
  418. return true;
  419. }
  420. PDataChannelParent*
  421. NeckoParent::AllocPDataChannelParent(const uint32_t &channelId)
  422. {
  423. RefPtr<DataChannelParent> p = new DataChannelParent();
  424. return p.forget().take();
  425. }
  426. bool
  427. NeckoParent::DeallocPDataChannelParent(PDataChannelParent* actor)
  428. {
  429. RefPtr<DataChannelParent> p = dont_AddRef(static_cast<DataChannelParent*>(actor));
  430. return true;
  431. }
  432. bool
  433. NeckoParent::RecvPDataChannelConstructor(PDataChannelParent* actor,
  434. const uint32_t& channelId)
  435. {
  436. DataChannelParent* p = static_cast<DataChannelParent*>(actor);
  437. DebugOnly<bool> rv = p->Init(channelId);
  438. MOZ_ASSERT(rv);
  439. return true;
  440. }
  441. PRtspControllerParent*
  442. NeckoParent::AllocPRtspControllerParent()
  443. {
  444. #ifdef NECKO_PROTOCOL_rtsp
  445. RtspControllerParent* p = new RtspControllerParent();
  446. p->AddRef();
  447. return p;
  448. #else
  449. return nullptr;
  450. #endif
  451. }
  452. bool
  453. NeckoParent::DeallocPRtspControllerParent(PRtspControllerParent* actor)
  454. {
  455. #ifdef NECKO_PROTOCOL_rtsp
  456. RtspControllerParent* p = static_cast<RtspControllerParent*>(actor);
  457. p->Release();
  458. #endif
  459. return true;
  460. }
  461. PRtspChannelParent*
  462. NeckoParent::AllocPRtspChannelParent(const RtspChannelConnectArgs& aArgs)
  463. {
  464. #ifdef NECKO_PROTOCOL_rtsp
  465. nsCOMPtr<nsIURI> uri = DeserializeURI(aArgs.uri());
  466. RtspChannelParent *p = new RtspChannelParent(uri);
  467. p->AddRef();
  468. return p;
  469. #else
  470. return nullptr;
  471. #endif
  472. }
  473. bool
  474. NeckoParent::RecvPRtspChannelConstructor(
  475. PRtspChannelParent* aActor,
  476. const RtspChannelConnectArgs& aConnectArgs)
  477. {
  478. #ifdef NECKO_PROTOCOL_rtsp
  479. RtspChannelParent* p = static_cast<RtspChannelParent*>(aActor);
  480. return p->Init(aConnectArgs);
  481. #else
  482. return false;
  483. #endif
  484. }
  485. bool
  486. NeckoParent::DeallocPRtspChannelParent(PRtspChannelParent* actor)
  487. {
  488. #ifdef NECKO_PROTOCOL_rtsp
  489. RtspChannelParent* p = static_cast<RtspChannelParent*>(actor);
  490. p->Release();
  491. #endif
  492. return true;
  493. }
  494. PTCPSocketParent*
  495. NeckoParent::AllocPTCPSocketParent(const nsString& /* host */,
  496. const uint16_t& /* port */)
  497. {
  498. // We actually don't need host/port to construct a TCPSocketParent since
  499. // TCPSocketParent will maintain an internal nsIDOMTCPSocket instance which
  500. // can be delegated to get the host/port.
  501. TCPSocketParent* p = new TCPSocketParent();
  502. p->AddIPDLReference();
  503. return p;
  504. }
  505. bool
  506. NeckoParent::DeallocPTCPSocketParent(PTCPSocketParent* actor)
  507. {
  508. TCPSocketParent* p = static_cast<TCPSocketParent*>(actor);
  509. p->ReleaseIPDLReference();
  510. return true;
  511. }
  512. PTCPServerSocketParent*
  513. NeckoParent::AllocPTCPServerSocketParent(const uint16_t& aLocalPort,
  514. const uint16_t& aBacklog,
  515. const bool& aUseArrayBuffers)
  516. {
  517. TCPServerSocketParent* p = new TCPServerSocketParent(this, aLocalPort, aBacklog, aUseArrayBuffers);
  518. p->AddIPDLReference();
  519. return p;
  520. }
  521. bool
  522. NeckoParent::RecvPTCPServerSocketConstructor(PTCPServerSocketParent* aActor,
  523. const uint16_t& aLocalPort,
  524. const uint16_t& aBacklog,
  525. const bool& aUseArrayBuffers)
  526. {
  527. static_cast<TCPServerSocketParent*>(aActor)->Init();
  528. return true;
  529. }
  530. bool
  531. NeckoParent::DeallocPTCPServerSocketParent(PTCPServerSocketParent* actor)
  532. {
  533. TCPServerSocketParent* p = static_cast<TCPServerSocketParent*>(actor);
  534. p->ReleaseIPDLReference();
  535. return true;
  536. }
  537. PUDPSocketParent*
  538. NeckoParent::AllocPUDPSocketParent(const Principal& /* unused */,
  539. const nsCString& /* unused */)
  540. {
  541. RefPtr<UDPSocketParent> p = new UDPSocketParent(this);
  542. return p.forget().take();
  543. }
  544. bool
  545. NeckoParent::RecvPUDPSocketConstructor(PUDPSocketParent* aActor,
  546. const Principal& aPrincipal,
  547. const nsCString& aFilter)
  548. {
  549. return static_cast<UDPSocketParent*>(aActor)->Init(aPrincipal, aFilter);
  550. }
  551. bool
  552. NeckoParent::DeallocPUDPSocketParent(PUDPSocketParent* actor)
  553. {
  554. UDPSocketParent* p = static_cast<UDPSocketParent*>(actor);
  555. p->Release();
  556. return true;
  557. }
  558. PDNSRequestParent*
  559. NeckoParent::AllocPDNSRequestParent(const nsCString& aHost,
  560. const uint32_t& aFlags,
  561. const nsCString& aNetworkInterface)
  562. {
  563. DNSRequestParent *p = new DNSRequestParent();
  564. p->AddRef();
  565. return p;
  566. }
  567. bool
  568. NeckoParent::RecvPDNSRequestConstructor(PDNSRequestParent* aActor,
  569. const nsCString& aHost,
  570. const uint32_t& aFlags,
  571. const nsCString& aNetworkInterface)
  572. {
  573. static_cast<DNSRequestParent*>(aActor)->DoAsyncResolve(aHost, aFlags,
  574. aNetworkInterface);
  575. return true;
  576. }
  577. bool
  578. NeckoParent::DeallocPDNSRequestParent(PDNSRequestParent* aParent)
  579. {
  580. DNSRequestParent *p = static_cast<DNSRequestParent*>(aParent);
  581. p->Release();
  582. return true;
  583. }
  584. bool
  585. NeckoParent::RecvSpeculativeConnect(const URIParams& aURI,
  586. const Principal& aPrincipal,
  587. const bool& aAnonymous)
  588. {
  589. nsCOMPtr<nsISpeculativeConnect> speculator(gIOService);
  590. nsCOMPtr<nsIURI> uri = DeserializeURI(aURI);
  591. nsCOMPtr<nsIPrincipal> principal(aPrincipal);
  592. if (uri && speculator) {
  593. if (aAnonymous) {
  594. speculator->SpeculativeAnonymousConnect2(uri, principal, nullptr);
  595. } else {
  596. speculator->SpeculativeConnect2(uri, principal, nullptr);
  597. }
  598. }
  599. return true;
  600. }
  601. bool
  602. NeckoParent::RecvHTMLDNSPrefetch(const nsString& hostname,
  603. const uint16_t& flags)
  604. {
  605. nsHTMLDNSPrefetch::Prefetch(hostname, flags);
  606. return true;
  607. }
  608. bool
  609. NeckoParent::RecvCancelHTMLDNSPrefetch(const nsString& hostname,
  610. const uint16_t& flags,
  611. const nsresult& reason)
  612. {
  613. nsHTMLDNSPrefetch::CancelPrefetch(hostname, flags, reason);
  614. return true;
  615. }
  616. PChannelDiverterParent*
  617. NeckoParent::AllocPChannelDiverterParent(const ChannelDiverterArgs& channel)
  618. {
  619. return new ChannelDiverterParent();
  620. }
  621. bool
  622. NeckoParent::RecvPChannelDiverterConstructor(PChannelDiverterParent* actor,
  623. const ChannelDiverterArgs& channel)
  624. {
  625. auto parent = static_cast<ChannelDiverterParent*>(actor);
  626. parent->Init(channel);
  627. return true;
  628. }
  629. bool
  630. NeckoParent::DeallocPChannelDiverterParent(PChannelDiverterParent* parent)
  631. {
  632. delete static_cast<ChannelDiverterParent*>(parent);
  633. return true;
  634. }
  635. PTransportProviderParent*
  636. NeckoParent::AllocPTransportProviderParent()
  637. {
  638. RefPtr<TransportProviderParent> res = new TransportProviderParent();
  639. return res.forget().take();
  640. }
  641. bool
  642. NeckoParent::DeallocPTransportProviderParent(PTransportProviderParent* aActor)
  643. {
  644. RefPtr<TransportProviderParent> provider =
  645. dont_AddRef(static_cast<TransportProviderParent*>(aActor));
  646. return true;
  647. }
  648. namespace {
  649. std::map<uint64_t, nsCOMPtr<nsIAuthPromptCallback> >&
  650. CallbackMap()
  651. {
  652. MOZ_ASSERT(NS_IsMainThread());
  653. static std::map<uint64_t, nsCOMPtr<nsIAuthPromptCallback> > sCallbackMap;
  654. return sCallbackMap;
  655. }
  656. } // namespace
  657. NS_IMPL_ISUPPORTS(NeckoParent::NestedFrameAuthPrompt, nsIAuthPrompt2)
  658. NeckoParent::NestedFrameAuthPrompt::NestedFrameAuthPrompt(PNeckoParent* aParent,
  659. TabId aNestedFrameId)
  660. : mNeckoParent(aParent)
  661. , mNestedFrameId(aNestedFrameId)
  662. {}
  663. NS_IMETHODIMP
  664. NeckoParent::NestedFrameAuthPrompt::AsyncPromptAuth(
  665. nsIChannel* aChannel, nsIAuthPromptCallback* callback,
  666. nsISupports*, uint32_t,
  667. nsIAuthInformation* aInfo, nsICancelable**)
  668. {
  669. static uint64_t callbackId = 0;
  670. MOZ_ASSERT(XRE_IsParentProcess());
  671. nsCOMPtr<nsIURI> uri;
  672. nsresult rv = aChannel->GetURI(getter_AddRefs(uri));
  673. NS_ENSURE_SUCCESS(rv, rv);
  674. nsAutoCString spec;
  675. if (uri) {
  676. rv = uri->GetSpec(spec);
  677. NS_ENSURE_SUCCESS(rv, rv);
  678. }
  679. nsString realm;
  680. rv = aInfo->GetRealm(realm);
  681. NS_ENSURE_SUCCESS(rv, rv);
  682. callbackId++;
  683. if (mNeckoParent->SendAsyncAuthPromptForNestedFrame(mNestedFrameId,
  684. spec,
  685. realm,
  686. callbackId)) {
  687. CallbackMap()[callbackId] = callback;
  688. return NS_OK;
  689. }
  690. return NS_ERROR_FAILURE;
  691. }
  692. bool
  693. NeckoParent::RecvOnAuthAvailable(const uint64_t& aCallbackId,
  694. const nsString& aUser,
  695. const nsString& aPassword,
  696. const nsString& aDomain)
  697. {
  698. nsCOMPtr<nsIAuthPromptCallback> callback = CallbackMap()[aCallbackId];
  699. if (!callback) {
  700. return true;
  701. }
  702. CallbackMap().erase(aCallbackId);
  703. RefPtr<nsAuthInformationHolder> holder =
  704. new nsAuthInformationHolder(0, EmptyString(), EmptyCString());
  705. holder->SetUsername(aUser);
  706. holder->SetPassword(aPassword);
  707. holder->SetDomain(aDomain);
  708. callback->OnAuthAvailable(nullptr, holder);
  709. return true;
  710. }
  711. bool
  712. NeckoParent::RecvOnAuthCancelled(const uint64_t& aCallbackId,
  713. const bool& aUserCancel)
  714. {
  715. nsCOMPtr<nsIAuthPromptCallback> callback = CallbackMap()[aCallbackId];
  716. if (!callback) {
  717. return true;
  718. }
  719. CallbackMap().erase(aCallbackId);
  720. callback->OnAuthCancelled(nullptr, aUserCancel);
  721. return true;
  722. }
  723. /* Predictor Messages */
  724. bool
  725. NeckoParent::RecvPredPredict(const ipc::OptionalURIParams& aTargetURI,
  726. const ipc::OptionalURIParams& aSourceURI,
  727. const uint32_t& aReason,
  728. const SerializedLoadContext& aLoadContext,
  729. const bool& hasVerifier)
  730. {
  731. nsCOMPtr<nsIURI> targetURI = DeserializeURI(aTargetURI);
  732. nsCOMPtr<nsIURI> sourceURI = DeserializeURI(aSourceURI);
  733. // We only actually care about the loadContext.mPrivateBrowsing, so we'll just
  734. // pass dummy params for nestFrameId, and originAttributes.
  735. uint64_t nestedFrameId = 0;
  736. DocShellOriginAttributes attrs(NECKO_UNKNOWN_APP_ID, false);
  737. nsCOMPtr<nsILoadContext> loadContext;
  738. if (aLoadContext.IsNotNull()) {
  739. attrs.SyncAttributesWithPrivateBrowsing(aLoadContext.mOriginAttributes.mPrivateBrowsingId > 0);
  740. loadContext = new LoadContext(aLoadContext, nestedFrameId, attrs);
  741. }
  742. // Get the current predictor
  743. nsresult rv = NS_OK;
  744. nsCOMPtr<nsINetworkPredictor> predictor =
  745. do_GetService("@mozilla.org/network/predictor;1", &rv);
  746. NS_ENSURE_SUCCESS(rv, false);
  747. nsCOMPtr<nsINetworkPredictorVerifier> verifier;
  748. if (hasVerifier) {
  749. verifier = do_QueryInterface(predictor);
  750. }
  751. predictor->Predict(targetURI, sourceURI, aReason, loadContext, verifier);
  752. return true;
  753. }
  754. bool
  755. NeckoParent::RecvPredLearn(const ipc::URIParams& aTargetURI,
  756. const ipc::OptionalURIParams& aSourceURI,
  757. const uint32_t& aReason,
  758. const SerializedLoadContext& aLoadContext)
  759. {
  760. nsCOMPtr<nsIURI> targetURI = DeserializeURI(aTargetURI);
  761. nsCOMPtr<nsIURI> sourceURI = DeserializeURI(aSourceURI);
  762. // We only actually care about the loadContext.mPrivateBrowsing, so we'll just
  763. // pass dummy params for nestFrameId, and originAttributes;
  764. uint64_t nestedFrameId = 0;
  765. DocShellOriginAttributes attrs(NECKO_UNKNOWN_APP_ID, false);
  766. nsCOMPtr<nsILoadContext> loadContext;
  767. if (aLoadContext.IsNotNull()) {
  768. attrs.SyncAttributesWithPrivateBrowsing(aLoadContext.mOriginAttributes.mPrivateBrowsingId > 0);
  769. loadContext = new LoadContext(aLoadContext, nestedFrameId, attrs);
  770. }
  771. // Get the current predictor
  772. nsresult rv = NS_OK;
  773. nsCOMPtr<nsINetworkPredictor> predictor =
  774. do_GetService("@mozilla.org/network/predictor;1", &rv);
  775. NS_ENSURE_SUCCESS(rv, false);
  776. predictor->Learn(targetURI, sourceURI, aReason, loadContext);
  777. return true;
  778. }
  779. bool
  780. NeckoParent::RecvPredReset()
  781. {
  782. // Get the current predictor
  783. nsresult rv = NS_OK;
  784. nsCOMPtr<nsINetworkPredictor> predictor =
  785. do_GetService("@mozilla.org/network/predictor;1", &rv);
  786. NS_ENSURE_SUCCESS(rv, false);
  787. predictor->Reset();
  788. return true;
  789. }
  790. bool
  791. NeckoParent::RecvRemoveRequestContext(const nsCString& rcid)
  792. {
  793. nsCOMPtr<nsIRequestContextService> rcsvc =
  794. do_GetService("@mozilla.org/network/request-context-service;1");
  795. if (!rcsvc) {
  796. return true;
  797. }
  798. nsID id;
  799. id.Parse(rcid.BeginReading());
  800. rcsvc->RemoveRequestContext(id);
  801. return true;
  802. }
  803. } // namespace net
  804. } // namespace mozilla