nsJSProtocolHandler.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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 "nsCOMPtr.h"
  6. #include "jsapi.h"
  7. #include "jswrapper.h"
  8. #include "nsCRT.h"
  9. #include "nsError.h"
  10. #include "nsXPIDLString.h"
  11. #include "nsReadableUtils.h"
  12. #include "nsJSProtocolHandler.h"
  13. #include "nsStringStream.h"
  14. #include "nsNetUtil.h"
  15. #include "nsIStreamListener.h"
  16. #include "nsIComponentManager.h"
  17. #include "nsIServiceManager.h"
  18. #include "nsIURI.h"
  19. #include "nsIScriptContext.h"
  20. #include "nsIScriptGlobalObject.h"
  21. #include "nsIPrincipal.h"
  22. #include "nsIScriptSecurityManager.h"
  23. #include "nsIInterfaceRequestor.h"
  24. #include "nsIInterfaceRequestorUtils.h"
  25. #include "nsIWindowMediator.h"
  26. #include "nsPIDOMWindow.h"
  27. #include "nsIConsoleService.h"
  28. #include "nsXPIDLString.h"
  29. #include "prprf.h"
  30. #include "nsEscape.h"
  31. #include "nsIWebNavigation.h"
  32. #include "nsIDocShell.h"
  33. #include "nsIContentViewer.h"
  34. #include "nsIXPConnect.h"
  35. #include "nsContentUtils.h"
  36. #include "nsJSUtils.h"
  37. #include "nsThreadUtils.h"
  38. #include "nsIScriptChannel.h"
  39. #include "nsIDocument.h"
  40. #include "nsILoadInfo.h"
  41. #include "nsIObjectInputStream.h"
  42. #include "nsIObjectOutputStream.h"
  43. #include "nsIWritablePropertyBag2.h"
  44. #include "nsIContentSecurityPolicy.h"
  45. #include "nsSandboxFlags.h"
  46. #include "mozilla/CycleCollectedJSContext.h"
  47. #include "mozilla/dom/ScriptSettings.h"
  48. #include "nsILoadInfo.h"
  49. #include "nsContentSecurityManager.h"
  50. #include "mozilla/ipc/URIUtils.h"
  51. using mozilla::dom::AutoEntryScript;
  52. static NS_DEFINE_CID(kJSURICID, NS_JSURI_CID);
  53. class nsJSThunk : public nsIInputStream
  54. {
  55. public:
  56. nsJSThunk();
  57. NS_DECL_THREADSAFE_ISUPPORTS
  58. NS_FORWARD_SAFE_NSIINPUTSTREAM(mInnerStream)
  59. nsresult Init(nsIURI* uri);
  60. nsresult EvaluateScript(nsIChannel *aChannel,
  61. PopupControlState aPopupState,
  62. uint32_t aExecutionPolicy,
  63. nsPIDOMWindowInner *aOriginalInnerWindow);
  64. protected:
  65. virtual ~nsJSThunk();
  66. nsCOMPtr<nsIInputStream> mInnerStream;
  67. nsCString mScript;
  68. nsCString mURL;
  69. };
  70. //
  71. // nsISupports implementation...
  72. //
  73. NS_IMPL_ISUPPORTS(nsJSThunk, nsIInputStream)
  74. nsJSThunk::nsJSThunk()
  75. {
  76. }
  77. nsJSThunk::~nsJSThunk()
  78. {
  79. }
  80. nsresult nsJSThunk::Init(nsIURI* uri)
  81. {
  82. NS_ENSURE_ARG_POINTER(uri);
  83. // Get the script string to evaluate...
  84. nsresult rv = uri->GetPath(mScript);
  85. if (NS_FAILED(rv)) return rv;
  86. // Get the url.
  87. rv = uri->GetSpec(mURL);
  88. if (NS_FAILED(rv)) return rv;
  89. return NS_OK;
  90. }
  91. static bool
  92. IsISO88591(const nsString& aString)
  93. {
  94. for (nsString::const_char_iterator c = aString.BeginReading(),
  95. c_end = aString.EndReading();
  96. c < c_end; ++c) {
  97. if (*c > 255)
  98. return false;
  99. }
  100. return true;
  101. }
  102. static
  103. nsIScriptGlobalObject* GetGlobalObject(nsIChannel* aChannel)
  104. {
  105. // Get the global object owner from the channel
  106. nsCOMPtr<nsIDocShell> docShell;
  107. NS_QueryNotificationCallbacks(aChannel, docShell);
  108. if (!docShell) {
  109. NS_WARNING("Unable to get a docShell from the channel!");
  110. return nullptr;
  111. }
  112. // So far so good: get the script global from its docshell
  113. nsIScriptGlobalObject* global = docShell->GetScriptGlobalObject();
  114. NS_ASSERTION(global,
  115. "Unable to get an nsIScriptGlobalObject from the "
  116. "docShell!");
  117. return global;
  118. }
  119. nsresult nsJSThunk::EvaluateScript(nsIChannel *aChannel,
  120. PopupControlState aPopupState,
  121. uint32_t aExecutionPolicy,
  122. nsPIDOMWindowInner *aOriginalInnerWindow)
  123. {
  124. if (aExecutionPolicy == nsIScriptChannel::NO_EXECUTION) {
  125. // Nothing to do here.
  126. return NS_ERROR_DOM_RETVAL_UNDEFINED;
  127. }
  128. NS_ENSURE_ARG_POINTER(aChannel);
  129. // Get principal of code for execution
  130. nsCOMPtr<nsISupports> owner;
  131. aChannel->GetOwner(getter_AddRefs(owner));
  132. nsCOMPtr<nsIPrincipal> principal = do_QueryInterface(owner);
  133. if (!principal) {
  134. nsCOMPtr<nsILoadInfo> loadInfo;
  135. aChannel->GetLoadInfo(getter_AddRefs(loadInfo));
  136. if (loadInfo && loadInfo->GetForceInheritPrincipal()) {
  137. principal = loadInfo->PrincipalToInherit();
  138. if (!principal) {
  139. principal = loadInfo->TriggeringPrincipal();
  140. }
  141. } else {
  142. // No execution without a principal!
  143. NS_ASSERTION(!owner, "Non-principal owner?");
  144. NS_WARNING("No principal to execute JS with");
  145. return NS_ERROR_DOM_RETVAL_UNDEFINED;
  146. }
  147. }
  148. nsresult rv;
  149. // CSP check: javascript: URIs disabled unless "inline" scripts are
  150. // allowed.
  151. nsCOMPtr<nsIContentSecurityPolicy> csp;
  152. rv = principal->GetCsp(getter_AddRefs(csp));
  153. NS_ENSURE_SUCCESS(rv, rv);
  154. if (csp) {
  155. bool allowsInlineScript = true;
  156. rv = csp->GetAllowsInline(nsIContentPolicy::TYPE_SCRIPT,
  157. EmptyString(), // aNonce
  158. true, // aParserCreated
  159. EmptyString(), // aContent
  160. 0, // aLineNumber
  161. &allowsInlineScript);
  162. //return early if inline scripts are not allowed
  163. if (!allowsInlineScript) {
  164. return NS_ERROR_DOM_RETVAL_UNDEFINED;
  165. }
  166. }
  167. // Get the global object we should be running on.
  168. nsIScriptGlobalObject* global = GetGlobalObject(aChannel);
  169. if (!global) {
  170. return NS_ERROR_FAILURE;
  171. }
  172. // Sandboxed document check: javascript: URI's are disabled
  173. // in a sandboxed document unless 'allow-scripts' was specified.
  174. nsIDocument* doc = aOriginalInnerWindow->GetExtantDoc();
  175. if (doc && doc->HasScriptsBlockedBySandbox()) {
  176. return NS_ERROR_DOM_RETVAL_UNDEFINED;
  177. }
  178. // Push our popup control state
  179. nsAutoPopupStatePusher popupStatePusher(aPopupState);
  180. // Make sure we still have the same inner window as we used to.
  181. nsCOMPtr<nsPIDOMWindowOuter> win = do_QueryInterface(global);
  182. nsPIDOMWindowInner *innerWin = win->GetCurrentInnerWindow();
  183. if (innerWin != aOriginalInnerWindow) {
  184. return NS_ERROR_UNEXPECTED;
  185. }
  186. nsCOMPtr<nsIScriptGlobalObject> innerGlobal = do_QueryInterface(innerWin);
  187. mozilla::DebugOnly<nsCOMPtr<nsIDOMWindow>> domWindow(do_QueryInterface(global, &rv));
  188. if (NS_FAILED(rv)) {
  189. return NS_ERROR_FAILURE;
  190. }
  191. // So far so good: get the script context from its owner.
  192. nsCOMPtr<nsIScriptContext> scriptContext = global->GetContext();
  193. if (!scriptContext)
  194. return NS_ERROR_FAILURE;
  195. nsAutoCString script(mScript);
  196. // Unescape the script
  197. NS_UnescapeURL(script);
  198. nsCOMPtr<nsIScriptSecurityManager> securityManager;
  199. securityManager = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
  200. if (NS_FAILED(rv))
  201. return rv;
  202. // New script entry point required, due to the "Create a script" step of
  203. // http://www.whatwg.org/specs/web-apps/current-work/#javascript-protocol
  204. mozilla::nsAutoMicroTask mt;
  205. AutoEntryScript aes(innerGlobal, "javascript: URI", true);
  206. JSContext* cx = aes.cx();
  207. JS::Rooted<JSObject*> globalJSObject(cx, innerGlobal->GetGlobalJSObject());
  208. NS_ENSURE_TRUE(globalJSObject, NS_ERROR_UNEXPECTED);
  209. //-- Don't execute unless the script principal subsumes the
  210. // principal of the context.
  211. nsIPrincipal* objectPrincipal = nsContentUtils::ObjectPrincipal(globalJSObject);
  212. bool subsumes;
  213. rv = principal->Subsumes(objectPrincipal, &subsumes);
  214. if (NS_FAILED(rv))
  215. return rv;
  216. if (!subsumes) {
  217. return NS_ERROR_DOM_RETVAL_UNDEFINED;
  218. }
  219. // Fail if someone tries to execute in a global with system principal.
  220. if (nsContentUtils::IsSystemPrincipal(objectPrincipal)) {
  221. return NS_ERROR_DOM_SECURITY_ERR;
  222. }
  223. JS::Rooted<JS::Value> v (cx, JS::UndefinedValue());
  224. // Finally, we have everything needed to evaluate the expression.
  225. JS::CompileOptions options(cx);
  226. options.setFileAndLine(mURL.get(), 1)
  227. .setVersion(JSVERSION_DEFAULT);
  228. nsJSUtils::EvaluateOptions evalOptions(cx);
  229. evalOptions.setCoerceToString(true);
  230. rv = nsJSUtils::EvaluateString(cx, NS_ConvertUTF8toUTF16(script),
  231. globalJSObject, options, evalOptions, &v);
  232. if (NS_FAILED(rv) || !(v.isString() || v.isUndefined())) {
  233. return NS_ERROR_MALFORMED_URI;
  234. } else if (v.isUndefined()) {
  235. return NS_ERROR_DOM_RETVAL_UNDEFINED;
  236. } else {
  237. MOZ_ASSERT(rv != NS_SUCCESS_DOM_SCRIPT_EVALUATION_THREW,
  238. "How did we get a non-undefined return value?");
  239. nsAutoJSString result;
  240. if (!result.init(cx, v)) {
  241. return NS_ERROR_OUT_OF_MEMORY;
  242. }
  243. char *bytes;
  244. uint32_t bytesLen;
  245. NS_NAMED_LITERAL_CSTRING(isoCharset, "ISO-8859-1");
  246. NS_NAMED_LITERAL_CSTRING(utf8Charset, "UTF-8");
  247. const nsCString *charset;
  248. if (IsISO88591(result)) {
  249. // For compatibility, if the result is ISO-8859-1, we use
  250. // ISO-8859-1, so that people can compatibly create images
  251. // using javascript: URLs.
  252. bytes = ToNewCString(result);
  253. bytesLen = result.Length();
  254. charset = &isoCharset;
  255. }
  256. else {
  257. bytes = ToNewUTF8String(result, &bytesLen);
  258. charset = &utf8Charset;
  259. }
  260. aChannel->SetContentCharset(*charset);
  261. if (bytes)
  262. rv = NS_NewByteInputStream(getter_AddRefs(mInnerStream),
  263. bytes, bytesLen,
  264. NS_ASSIGNMENT_ADOPT);
  265. else
  266. rv = NS_ERROR_OUT_OF_MEMORY;
  267. }
  268. return rv;
  269. }
  270. ////////////////////////////////////////////////////////////////////////////////
  271. class nsJSChannel : public nsIChannel,
  272. public nsIStreamListener,
  273. public nsIScriptChannel,
  274. public nsIPropertyBag2
  275. {
  276. public:
  277. nsJSChannel();
  278. NS_DECL_ISUPPORTS
  279. NS_DECL_NSIREQUEST
  280. NS_DECL_NSICHANNEL
  281. NS_DECL_NSIREQUESTOBSERVER
  282. NS_DECL_NSISTREAMLISTENER
  283. NS_DECL_NSISCRIPTCHANNEL
  284. NS_FORWARD_SAFE_NSIPROPERTYBAG(mPropertyBag)
  285. NS_FORWARD_SAFE_NSIPROPERTYBAG2(mPropertyBag)
  286. nsresult Init(nsIURI *aURI, nsILoadInfo* aLoadInfo);
  287. // Actually evaluate the script.
  288. void EvaluateScript();
  289. protected:
  290. virtual ~nsJSChannel();
  291. nsresult StopAll();
  292. void NotifyListener();
  293. void CleanupStrongRefs();
  294. protected:
  295. nsCOMPtr<nsIChannel> mStreamChannel;
  296. nsCOMPtr<nsIPropertyBag2> mPropertyBag;
  297. nsCOMPtr<nsIStreamListener> mListener; // Our final listener
  298. nsCOMPtr<nsPIDOMWindowInner> mOriginalInnerWindow; // The inner window our load
  299. // started against.
  300. // If we blocked onload on a document in AsyncOpen2, this is the document we
  301. // did it on.
  302. nsCOMPtr<nsIDocument> mDocumentOnloadBlockedOn;
  303. nsresult mStatus; // Our status
  304. nsLoadFlags mLoadFlags;
  305. nsLoadFlags mActualLoadFlags; // See AsyncOpen2
  306. RefPtr<nsJSThunk> mIOThunk;
  307. PopupControlState mPopupState;
  308. uint32_t mExecutionPolicy;
  309. bool mIsAsync;
  310. bool mIsActive;
  311. bool mOpenedStreamChannel;
  312. };
  313. nsJSChannel::nsJSChannel() :
  314. mStatus(NS_OK),
  315. mLoadFlags(LOAD_NORMAL),
  316. mActualLoadFlags(LOAD_NORMAL),
  317. mPopupState(openOverridden),
  318. mExecutionPolicy(NO_EXECUTION),
  319. mIsAsync(true),
  320. mIsActive(false),
  321. mOpenedStreamChannel(false)
  322. {
  323. }
  324. nsJSChannel::~nsJSChannel()
  325. {
  326. }
  327. nsresult nsJSChannel::StopAll()
  328. {
  329. nsresult rv = NS_ERROR_UNEXPECTED;
  330. nsCOMPtr<nsIWebNavigation> webNav;
  331. NS_QueryNotificationCallbacks(mStreamChannel, webNav);
  332. NS_ASSERTION(webNav, "Can't get nsIWebNavigation from channel!");
  333. if (webNav) {
  334. rv = webNav->Stop(nsIWebNavigation::STOP_ALL);
  335. }
  336. return rv;
  337. }
  338. nsresult nsJSChannel::Init(nsIURI* aURI, nsILoadInfo* aLoadInfo)
  339. {
  340. RefPtr<nsJSURI> jsURI;
  341. nsresult rv = aURI->QueryInterface(kJSURICID,
  342. getter_AddRefs(jsURI));
  343. NS_ENSURE_SUCCESS(rv, rv);
  344. // Create the nsIStreamIO layer used by the nsIStreamIOChannel.
  345. mIOThunk = new nsJSThunk();
  346. // Create a stock input stream channel...
  347. // Remember, until AsyncOpen is called, the script will not be evaluated
  348. // and the underlying Input Stream will not be created...
  349. nsCOMPtr<nsIChannel> channel;
  350. rv = NS_NewInputStreamChannelInternal(getter_AddRefs(channel),
  351. aURI,
  352. mIOThunk,
  353. NS_LITERAL_CSTRING("text/html"),
  354. EmptyCString(),
  355. aLoadInfo);
  356. NS_ENSURE_SUCCESS(rv, rv);
  357. rv = mIOThunk->Init(aURI);
  358. if (NS_SUCCEEDED(rv)) {
  359. mStreamChannel = channel;
  360. mPropertyBag = do_QueryInterface(channel);
  361. nsCOMPtr<nsIWritablePropertyBag2> writableBag =
  362. do_QueryInterface(channel);
  363. if (writableBag && jsURI->GetBaseURI()) {
  364. writableBag->SetPropertyAsInterface(NS_LITERAL_STRING("baseURI"),
  365. jsURI->GetBaseURI());
  366. }
  367. }
  368. return rv;
  369. }
  370. //
  371. // nsISupports implementation...
  372. //
  373. NS_IMPL_ISUPPORTS(nsJSChannel, nsIChannel, nsIRequest, nsIRequestObserver,
  374. nsIStreamListener, nsIScriptChannel, nsIPropertyBag,
  375. nsIPropertyBag2)
  376. //
  377. // nsIRequest implementation...
  378. //
  379. NS_IMETHODIMP
  380. nsJSChannel::GetName(nsACString &aResult)
  381. {
  382. return mStreamChannel->GetName(aResult);
  383. }
  384. NS_IMETHODIMP
  385. nsJSChannel::IsPending(bool *aResult)
  386. {
  387. *aResult = mIsActive;
  388. return NS_OK;
  389. }
  390. NS_IMETHODIMP
  391. nsJSChannel::GetStatus(nsresult *aResult)
  392. {
  393. if (NS_SUCCEEDED(mStatus) && mOpenedStreamChannel) {
  394. return mStreamChannel->GetStatus(aResult);
  395. }
  396. *aResult = mStatus;
  397. return NS_OK;
  398. }
  399. NS_IMETHODIMP
  400. nsJSChannel::Cancel(nsresult aStatus)
  401. {
  402. mStatus = aStatus;
  403. if (mOpenedStreamChannel) {
  404. mStreamChannel->Cancel(aStatus);
  405. }
  406. return NS_OK;
  407. }
  408. NS_IMETHODIMP
  409. nsJSChannel::Suspend()
  410. {
  411. return mStreamChannel->Suspend();
  412. }
  413. NS_IMETHODIMP
  414. nsJSChannel::Resume()
  415. {
  416. return mStreamChannel->Resume();
  417. }
  418. //
  419. // nsIChannel implementation
  420. //
  421. NS_IMETHODIMP
  422. nsJSChannel::GetOriginalURI(nsIURI * *aURI)
  423. {
  424. return mStreamChannel->GetOriginalURI(aURI);
  425. }
  426. NS_IMETHODIMP
  427. nsJSChannel::SetOriginalURI(nsIURI *aURI)
  428. {
  429. return mStreamChannel->SetOriginalURI(aURI);
  430. }
  431. NS_IMETHODIMP
  432. nsJSChannel::GetURI(nsIURI * *aURI)
  433. {
  434. return mStreamChannel->GetURI(aURI);
  435. }
  436. NS_IMETHODIMP
  437. nsJSChannel::Open(nsIInputStream **aResult)
  438. {
  439. nsresult rv = mIOThunk->EvaluateScript(mStreamChannel, mPopupState,
  440. mExecutionPolicy,
  441. mOriginalInnerWindow);
  442. NS_ENSURE_SUCCESS(rv, rv);
  443. return mStreamChannel->Open(aResult);
  444. }
  445. NS_IMETHODIMP
  446. nsJSChannel::Open2(nsIInputStream** aStream)
  447. {
  448. nsCOMPtr<nsIStreamListener> listener;
  449. nsresult rv = nsContentSecurityManager::doContentSecurityCheck(this, listener);
  450. NS_ENSURE_SUCCESS(rv, rv);
  451. return Open(aStream);
  452. }
  453. NS_IMETHODIMP
  454. nsJSChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports *aContext)
  455. {
  456. #ifdef DEBUG
  457. {
  458. nsCOMPtr<nsILoadInfo> loadInfo = nsIChannel::GetLoadInfo();
  459. MOZ_ASSERT(!loadInfo || loadInfo->GetSecurityMode() == 0 ||
  460. loadInfo->GetInitialSecurityCheckDone(),
  461. "security flags in loadInfo but asyncOpen2() not called");
  462. }
  463. #endif
  464. MOZ_RELEASE_ASSERT(!aContext, "please call AsyncOpen2()");
  465. NS_ENSURE_ARG(aListener);
  466. // First make sure that we have a usable inner window; we'll want to make
  467. // sure that we execute against that inner and no other.
  468. nsIScriptGlobalObject* global = GetGlobalObject(this);
  469. if (!global) {
  470. return NS_ERROR_NOT_AVAILABLE;
  471. }
  472. nsCOMPtr<nsPIDOMWindowOuter> win(do_QueryInterface(global));
  473. NS_ASSERTION(win, "Our global is not a window??");
  474. // Make sure we create a new inner window if one doesn't already exist (see
  475. // bug 306630).
  476. mOriginalInnerWindow = win->EnsureInnerWindow();
  477. if (!mOriginalInnerWindow) {
  478. return NS_ERROR_NOT_AVAILABLE;
  479. }
  480. mListener = aListener;
  481. mIsActive = true;
  482. // Temporarily set the LOAD_BACKGROUND flag to suppress load group observer
  483. // notifications (and hence nsIWebProgressListener notifications) from
  484. // being dispatched. This is required since we suppress LOAD_DOCUMENT_URI,
  485. // which means that the DocLoader would not generate document start and
  486. // stop notifications (see bug 257875).
  487. mActualLoadFlags = mLoadFlags;
  488. mLoadFlags |= LOAD_BACKGROUND;
  489. // Add the javascript channel to its loadgroup so that we know if
  490. // network loads were canceled or not...
  491. nsCOMPtr<nsILoadGroup> loadGroup;
  492. mStreamChannel->GetLoadGroup(getter_AddRefs(loadGroup));
  493. if (loadGroup) {
  494. nsresult rv = loadGroup->AddRequest(this, nullptr);
  495. if (NS_FAILED(rv)) {
  496. mIsActive = false;
  497. CleanupStrongRefs();
  498. return rv;
  499. }
  500. }
  501. mDocumentOnloadBlockedOn = mOriginalInnerWindow->GetExtantDoc();
  502. if (mDocumentOnloadBlockedOn) {
  503. // If we're a document channel, we need to actually block onload on our
  504. // _parent_ document. This is because we don't actually set our
  505. // LOAD_DOCUMENT_URI flag, so a docloader we're loading in as the
  506. // document channel will claim to not be busy, and our parent's onload
  507. // could fire too early.
  508. nsLoadFlags loadFlags;
  509. mStreamChannel->GetLoadFlags(&loadFlags);
  510. if (loadFlags & LOAD_DOCUMENT_URI) {
  511. mDocumentOnloadBlockedOn =
  512. mDocumentOnloadBlockedOn->GetParentDocument();
  513. }
  514. }
  515. if (mDocumentOnloadBlockedOn) {
  516. mDocumentOnloadBlockedOn->BlockOnload();
  517. }
  518. mPopupState = win->GetPopupControlState();
  519. void (nsJSChannel::*method)();
  520. if (mIsAsync) {
  521. // post an event to do the rest
  522. method = &nsJSChannel::EvaluateScript;
  523. } else {
  524. EvaluateScript();
  525. if (mOpenedStreamChannel) {
  526. // That will handle notifying things
  527. return NS_OK;
  528. }
  529. NS_ASSERTION(NS_FAILED(mStatus), "We should have failed _somehow_");
  530. // mStatus is going to be NS_ERROR_DOM_RETVAL_UNDEFINED if we didn't
  531. // have any content resulting from the execution and NS_BINDING_ABORTED
  532. // if something we did causes our own load to be stopped. Return
  533. // success in those cases, and error out in all others.
  534. if (mStatus != NS_ERROR_DOM_RETVAL_UNDEFINED &&
  535. mStatus != NS_BINDING_ABORTED) {
  536. // Note that calling EvaluateScript() handled removing us from the
  537. // loadgroup and marking us as not active anymore.
  538. CleanupStrongRefs();
  539. return mStatus;
  540. }
  541. // We're returning success from asyncOpen2(), but we didn't open a
  542. // stream channel. We'll have to notify ourselves, but make sure to do
  543. // it asynchronously.
  544. method = &nsJSChannel::NotifyListener;
  545. }
  546. nsresult rv = NS_DispatchToCurrentThread(mozilla::NewRunnableMethod(this, method));
  547. if (NS_FAILED(rv)) {
  548. loadGroup->RemoveRequest(this, nullptr, rv);
  549. mIsActive = false;
  550. CleanupStrongRefs();
  551. }
  552. return rv;
  553. }
  554. NS_IMETHODIMP
  555. nsJSChannel::AsyncOpen2(nsIStreamListener *aListener)
  556. {
  557. nsCOMPtr<nsIStreamListener> listener = aListener;
  558. nsresult rv = nsContentSecurityManager::doContentSecurityCheck(this, listener);
  559. NS_ENSURE_SUCCESS(rv, rv);
  560. return AsyncOpen(listener, nullptr);
  561. }
  562. void
  563. nsJSChannel::EvaluateScript()
  564. {
  565. // Synchronously execute the script...
  566. // mIsActive is used to indicate the the request is 'busy' during the
  567. // the script evaluation phase. This means that IsPending() will
  568. // indicate the the request is busy while the script is executing...
  569. // Note that we want to be in the loadgroup and pending while we evaluate
  570. // the script, so that we find out if the loadgroup gets canceled by the
  571. // script execution (in which case we shouldn't pump out data even if the
  572. // script returns it).
  573. if (NS_SUCCEEDED(mStatus)) {
  574. nsresult rv = mIOThunk->EvaluateScript(mStreamChannel, mPopupState,
  575. mExecutionPolicy,
  576. mOriginalInnerWindow);
  577. // Note that evaluation may have canceled us, so recheck mStatus again
  578. if (NS_FAILED(rv) && NS_SUCCEEDED(mStatus)) {
  579. mStatus = rv;
  580. }
  581. }
  582. // Remove the javascript channel from its loadgroup...
  583. nsCOMPtr<nsILoadGroup> loadGroup;
  584. mStreamChannel->GetLoadGroup(getter_AddRefs(loadGroup));
  585. if (loadGroup) {
  586. loadGroup->RemoveRequest(this, nullptr, mStatus);
  587. }
  588. // Reset load flags to their original value...
  589. mLoadFlags = mActualLoadFlags;
  590. // We're no longer active, it's now up to the stream channel to do
  591. // the loading, if needed.
  592. mIsActive = false;
  593. if (NS_FAILED(mStatus)) {
  594. if (mIsAsync) {
  595. NotifyListener();
  596. }
  597. return;
  598. }
  599. // EvaluateScript() succeeded, and we were not canceled, that
  600. // means there's data to parse as a result of evaluating the
  601. // script.
  602. // Get the stream channels load flags (!= mLoadFlags).
  603. nsLoadFlags loadFlags;
  604. mStreamChannel->GetLoadFlags(&loadFlags);
  605. uint32_t disposition;
  606. if (NS_FAILED(mStreamChannel->GetContentDisposition(&disposition)))
  607. disposition = nsIChannel::DISPOSITION_INLINE;
  608. if (loadFlags & LOAD_DOCUMENT_URI && disposition != nsIChannel::DISPOSITION_ATTACHMENT) {
  609. // We're loaded as the document channel and not expecting to download
  610. // the result. If we go on, we'll blow away the current document. Make
  611. // sure that's ok. If so, stop all pending network loads.
  612. nsCOMPtr<nsIDocShell> docShell;
  613. NS_QueryNotificationCallbacks(mStreamChannel, docShell);
  614. if (docShell) {
  615. nsCOMPtr<nsIContentViewer> cv;
  616. docShell->GetContentViewer(getter_AddRefs(cv));
  617. if (cv) {
  618. bool okToUnload;
  619. if (NS_SUCCEEDED(cv->PermitUnload(&okToUnload)) &&
  620. !okToUnload) {
  621. // The user didn't want to unload the current
  622. // page, translate this into an undefined
  623. // return from the javascript: URL...
  624. mStatus = NS_ERROR_DOM_RETVAL_UNDEFINED;
  625. }
  626. }
  627. }
  628. if (NS_SUCCEEDED(mStatus)) {
  629. mStatus = StopAll();
  630. }
  631. }
  632. if (NS_FAILED(mStatus)) {
  633. if (mIsAsync) {
  634. NotifyListener();
  635. }
  636. return;
  637. }
  638. mStatus = mStreamChannel->AsyncOpen2(this);
  639. if (NS_SUCCEEDED(mStatus)) {
  640. // mStreamChannel will call OnStartRequest and OnStopRequest on
  641. // us, so we'll be sure to call them on our listener.
  642. mOpenedStreamChannel = true;
  643. // Now readd ourselves to the loadgroup so we can receive
  644. // cancellation notifications.
  645. mIsActive = true;
  646. if (loadGroup) {
  647. mStatus = loadGroup->AddRequest(this, nullptr);
  648. // If AddRequest failed, that's OK. The key is to make sure we get
  649. // cancelled if needed, and that call just canceled us if it
  650. // failed. We'll still get notified by the stream channel when it
  651. // finishes.
  652. }
  653. } else if (mIsAsync) {
  654. NotifyListener();
  655. }
  656. return;
  657. }
  658. void
  659. nsJSChannel::NotifyListener()
  660. {
  661. mListener->OnStartRequest(this, nullptr);
  662. mListener->OnStopRequest(this, nullptr, mStatus);
  663. CleanupStrongRefs();
  664. }
  665. void
  666. nsJSChannel::CleanupStrongRefs()
  667. {
  668. mListener = nullptr;
  669. mOriginalInnerWindow = nullptr;
  670. if (mDocumentOnloadBlockedOn) {
  671. mDocumentOnloadBlockedOn->UnblockOnload(false);
  672. mDocumentOnloadBlockedOn = nullptr;
  673. }
  674. }
  675. NS_IMETHODIMP
  676. nsJSChannel::GetLoadFlags(nsLoadFlags *aLoadFlags)
  677. {
  678. *aLoadFlags = mLoadFlags;
  679. return NS_OK;
  680. }
  681. NS_IMETHODIMP
  682. nsJSChannel::SetLoadFlags(nsLoadFlags aLoadFlags)
  683. {
  684. // Figure out whether the LOAD_BACKGROUND bit in aLoadFlags is
  685. // actually right.
  686. bool bogusLoadBackground = false;
  687. if (mIsActive && !(mActualLoadFlags & LOAD_BACKGROUND) &&
  688. (aLoadFlags & LOAD_BACKGROUND)) {
  689. // We're getting a LOAD_BACKGROUND, but it's probably just our own fake
  690. // flag being mirrored to us. The one exception is if our loadgroup is
  691. // LOAD_BACKGROUND.
  692. bool loadGroupIsBackground = false;
  693. nsCOMPtr<nsILoadGroup> loadGroup;
  694. mStreamChannel->GetLoadGroup(getter_AddRefs(loadGroup));
  695. if (loadGroup) {
  696. nsLoadFlags loadGroupFlags;
  697. loadGroup->GetLoadFlags(&loadGroupFlags);
  698. loadGroupIsBackground = ((loadGroupFlags & LOAD_BACKGROUND) != 0);
  699. }
  700. bogusLoadBackground = !loadGroupIsBackground;
  701. }
  702. // Classifying a javascript: URI doesn't help us, and requires
  703. // NSS to boot, which we don't have in content processes. See
  704. // https://bugzilla.mozilla.org/show_bug.cgi?id=617838.
  705. aLoadFlags &= ~LOAD_CLASSIFY_URI;
  706. // Since the javascript channel is never the actual channel that
  707. // any data is loaded through, don't ever set the
  708. // LOAD_DOCUMENT_URI flag on it, since that could lead to two
  709. // 'document channels' in the loadgroup if a javascript: URL is
  710. // loaded while a document is being loaded in the same window.
  711. // XXXbz this, and a whole lot of other hackery, could go away if we'd just
  712. // cancel the current document load on javascript: load start like IE does.
  713. mLoadFlags = aLoadFlags & ~LOAD_DOCUMENT_URI;
  714. if (bogusLoadBackground) {
  715. aLoadFlags = aLoadFlags & ~LOAD_BACKGROUND;
  716. }
  717. mActualLoadFlags = aLoadFlags;
  718. // ... but the underlying stream channel should get this bit, if
  719. // set, since that'll be the real document channel if the
  720. // javascript: URL generated data.
  721. return mStreamChannel->SetLoadFlags(aLoadFlags);
  722. }
  723. NS_IMETHODIMP
  724. nsJSChannel::GetLoadGroup(nsILoadGroup* *aLoadGroup)
  725. {
  726. return mStreamChannel->GetLoadGroup(aLoadGroup);
  727. }
  728. NS_IMETHODIMP
  729. nsJSChannel::SetLoadGroup(nsILoadGroup* aLoadGroup)
  730. {
  731. if (aLoadGroup) {
  732. bool streamPending;
  733. nsresult rv = mStreamChannel->IsPending(&streamPending);
  734. NS_ENSURE_SUCCESS(rv, rv);
  735. if (streamPending) {
  736. nsCOMPtr<nsILoadGroup> curLoadGroup;
  737. mStreamChannel->GetLoadGroup(getter_AddRefs(curLoadGroup));
  738. if (aLoadGroup != curLoadGroup) {
  739. // Move the stream channel to our new loadgroup. Make sure to
  740. // add it before removing it, so that we don't trigger onload
  741. // by accident.
  742. aLoadGroup->AddRequest(mStreamChannel, nullptr);
  743. if (curLoadGroup) {
  744. curLoadGroup->RemoveRequest(mStreamChannel, nullptr,
  745. NS_BINDING_RETARGETED);
  746. }
  747. }
  748. }
  749. }
  750. return mStreamChannel->SetLoadGroup(aLoadGroup);
  751. }
  752. NS_IMETHODIMP
  753. nsJSChannel::GetOwner(nsISupports* *aOwner)
  754. {
  755. return mStreamChannel->GetOwner(aOwner);
  756. }
  757. NS_IMETHODIMP
  758. nsJSChannel::SetOwner(nsISupports* aOwner)
  759. {
  760. return mStreamChannel->SetOwner(aOwner);
  761. }
  762. NS_IMETHODIMP
  763. nsJSChannel::GetLoadInfo(nsILoadInfo* *aLoadInfo)
  764. {
  765. return mStreamChannel->GetLoadInfo(aLoadInfo);
  766. }
  767. NS_IMETHODIMP
  768. nsJSChannel::SetLoadInfo(nsILoadInfo* aLoadInfo)
  769. {
  770. return mStreamChannel->SetLoadInfo(aLoadInfo);
  771. }
  772. NS_IMETHODIMP
  773. nsJSChannel::GetNotificationCallbacks(nsIInterfaceRequestor* *aCallbacks)
  774. {
  775. return mStreamChannel->GetNotificationCallbacks(aCallbacks);
  776. }
  777. NS_IMETHODIMP
  778. nsJSChannel::SetNotificationCallbacks(nsIInterfaceRequestor* aCallbacks)
  779. {
  780. return mStreamChannel->SetNotificationCallbacks(aCallbacks);
  781. }
  782. NS_IMETHODIMP
  783. nsJSChannel::GetSecurityInfo(nsISupports * *aSecurityInfo)
  784. {
  785. return mStreamChannel->GetSecurityInfo(aSecurityInfo);
  786. }
  787. NS_IMETHODIMP
  788. nsJSChannel::GetContentType(nsACString &aContentType)
  789. {
  790. return mStreamChannel->GetContentType(aContentType);
  791. }
  792. NS_IMETHODIMP
  793. nsJSChannel::SetContentType(const nsACString &aContentType)
  794. {
  795. return mStreamChannel->SetContentType(aContentType);
  796. }
  797. NS_IMETHODIMP
  798. nsJSChannel::GetContentCharset(nsACString &aContentCharset)
  799. {
  800. return mStreamChannel->GetContentCharset(aContentCharset);
  801. }
  802. NS_IMETHODIMP
  803. nsJSChannel::SetContentCharset(const nsACString &aContentCharset)
  804. {
  805. return mStreamChannel->SetContentCharset(aContentCharset);
  806. }
  807. NS_IMETHODIMP
  808. nsJSChannel::GetContentDisposition(uint32_t *aContentDisposition)
  809. {
  810. return mStreamChannel->GetContentDisposition(aContentDisposition);
  811. }
  812. NS_IMETHODIMP
  813. nsJSChannel::SetContentDisposition(uint32_t aContentDisposition)
  814. {
  815. return mStreamChannel->SetContentDisposition(aContentDisposition);
  816. }
  817. NS_IMETHODIMP
  818. nsJSChannel::GetContentDispositionFilename(nsAString &aContentDispositionFilename)
  819. {
  820. return mStreamChannel->GetContentDispositionFilename(aContentDispositionFilename);
  821. }
  822. NS_IMETHODIMP
  823. nsJSChannel::SetContentDispositionFilename(const nsAString &aContentDispositionFilename)
  824. {
  825. return mStreamChannel->SetContentDispositionFilename(aContentDispositionFilename);
  826. }
  827. NS_IMETHODIMP
  828. nsJSChannel::GetContentDispositionHeader(nsACString &aContentDispositionHeader)
  829. {
  830. return mStreamChannel->GetContentDispositionHeader(aContentDispositionHeader);
  831. }
  832. NS_IMETHODIMP
  833. nsJSChannel::GetContentLength(int64_t *aContentLength)
  834. {
  835. return mStreamChannel->GetContentLength(aContentLength);
  836. }
  837. NS_IMETHODIMP
  838. nsJSChannel::SetContentLength(int64_t aContentLength)
  839. {
  840. return mStreamChannel->SetContentLength(aContentLength);
  841. }
  842. NS_IMETHODIMP
  843. nsJSChannel::OnStartRequest(nsIRequest* aRequest,
  844. nsISupports* aContext)
  845. {
  846. NS_ENSURE_TRUE(aRequest == mStreamChannel, NS_ERROR_UNEXPECTED);
  847. return mListener->OnStartRequest(this, aContext);
  848. }
  849. NS_IMETHODIMP
  850. nsJSChannel::OnDataAvailable(nsIRequest* aRequest,
  851. nsISupports* aContext,
  852. nsIInputStream* aInputStream,
  853. uint64_t aOffset,
  854. uint32_t aCount)
  855. {
  856. NS_ENSURE_TRUE(aRequest == mStreamChannel, NS_ERROR_UNEXPECTED);
  857. return mListener->OnDataAvailable(this, aContext, aInputStream, aOffset,
  858. aCount);
  859. }
  860. NS_IMETHODIMP
  861. nsJSChannel::OnStopRequest(nsIRequest* aRequest,
  862. nsISupports* aContext,
  863. nsresult aStatus)
  864. {
  865. NS_ENSURE_TRUE(aRequest == mStreamChannel, NS_ERROR_UNEXPECTED);
  866. nsCOMPtr<nsIStreamListener> listener = mListener;
  867. CleanupStrongRefs();
  868. // Make sure aStatus matches what GetStatus() returns
  869. if (NS_FAILED(mStatus)) {
  870. aStatus = mStatus;
  871. }
  872. nsresult rv = listener->OnStopRequest(this, aContext, aStatus);
  873. nsCOMPtr<nsILoadGroup> loadGroup;
  874. mStreamChannel->GetLoadGroup(getter_AddRefs(loadGroup));
  875. if (loadGroup) {
  876. loadGroup->RemoveRequest(this, nullptr, mStatus);
  877. }
  878. mIsActive = false;
  879. return rv;
  880. }
  881. NS_IMETHODIMP
  882. nsJSChannel::SetExecutionPolicy(uint32_t aPolicy)
  883. {
  884. NS_ENSURE_ARG(aPolicy <= EXECUTE_NORMAL);
  885. mExecutionPolicy = aPolicy;
  886. return NS_OK;
  887. }
  888. NS_IMETHODIMP
  889. nsJSChannel::GetExecutionPolicy(uint32_t* aPolicy)
  890. {
  891. *aPolicy = mExecutionPolicy;
  892. return NS_OK;
  893. }
  894. NS_IMETHODIMP
  895. nsJSChannel::SetExecuteAsync(bool aIsAsync)
  896. {
  897. if (!mIsActive) {
  898. mIsAsync = aIsAsync;
  899. }
  900. // else ignore this call
  901. NS_WARNING_ASSERTION(!mIsActive,
  902. "Calling SetExecuteAsync on active channel?");
  903. return NS_OK;
  904. }
  905. NS_IMETHODIMP
  906. nsJSChannel::GetExecuteAsync(bool* aIsAsync)
  907. {
  908. *aIsAsync = mIsAsync;
  909. return NS_OK;
  910. }
  911. ////////////////////////////////////////////////////////////////////////////////
  912. nsJSProtocolHandler::nsJSProtocolHandler()
  913. {
  914. }
  915. nsresult
  916. nsJSProtocolHandler::Init()
  917. {
  918. return NS_OK;
  919. }
  920. nsJSProtocolHandler::~nsJSProtocolHandler()
  921. {
  922. }
  923. NS_IMPL_ISUPPORTS(nsJSProtocolHandler, nsIProtocolHandler)
  924. nsresult
  925. nsJSProtocolHandler::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
  926. {
  927. if (aOuter)
  928. return NS_ERROR_NO_AGGREGATION;
  929. nsJSProtocolHandler* ph = new nsJSProtocolHandler();
  930. NS_ADDREF(ph);
  931. nsresult rv = ph->Init();
  932. if (NS_SUCCEEDED(rv)) {
  933. rv = ph->QueryInterface(aIID, aResult);
  934. }
  935. NS_RELEASE(ph);
  936. return rv;
  937. }
  938. nsresult
  939. nsJSProtocolHandler::EnsureUTF8Spec(const nsAFlatCString &aSpec, const char *aCharset,
  940. nsACString &aUTF8Spec)
  941. {
  942. aUTF8Spec.Truncate();
  943. nsresult rv;
  944. if (!mTextToSubURI) {
  945. mTextToSubURI = do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
  946. NS_ENSURE_SUCCESS(rv, rv);
  947. }
  948. nsAutoString uStr;
  949. rv = mTextToSubURI->UnEscapeNonAsciiURI(nsDependentCString(aCharset), aSpec, uStr);
  950. NS_ENSURE_SUCCESS(rv, rv);
  951. if (!IsASCII(uStr)) {
  952. rv = NS_EscapeURL(NS_ConvertUTF16toUTF8(uStr),
  953. esc_AlwaysCopy | esc_OnlyNonASCII, aUTF8Spec,
  954. mozilla::fallible);
  955. NS_ENSURE_SUCCESS(rv, rv);
  956. }
  957. return NS_OK;
  958. }
  959. ////////////////////////////////////////////////////////////////////////////////
  960. // nsIProtocolHandler methods:
  961. NS_IMETHODIMP
  962. nsJSProtocolHandler::GetScheme(nsACString &result)
  963. {
  964. result = "javascript";
  965. return NS_OK;
  966. }
  967. NS_IMETHODIMP
  968. nsJSProtocolHandler::GetDefaultPort(int32_t *result)
  969. {
  970. *result = -1; // no port for javascript: URLs
  971. return NS_OK;
  972. }
  973. NS_IMETHODIMP
  974. nsJSProtocolHandler::GetProtocolFlags(uint32_t *result)
  975. {
  976. *result = URI_NORELATIVE | URI_NOAUTH | URI_INHERITS_SECURITY_CONTEXT |
  977. URI_LOADABLE_BY_ANYONE | URI_NON_PERSISTABLE | URI_OPENING_EXECUTES_SCRIPT;
  978. return NS_OK;
  979. }
  980. NS_IMETHODIMP
  981. nsJSProtocolHandler::NewURI(const nsACString &aSpec,
  982. const char *aCharset,
  983. nsIURI *aBaseURI,
  984. nsIURI **result)
  985. {
  986. nsresult rv;
  987. // javascript: URLs (currently) have no additional structure beyond that
  988. // provided by standard URLs, so there is no "outer" object given to
  989. // CreateInstance.
  990. nsCOMPtr<nsIURI> url = new nsJSURI(aBaseURI);
  991. if (!aCharset || !nsCRT::strcasecmp("UTF-8", aCharset))
  992. rv = url->SetSpec(aSpec);
  993. else {
  994. nsAutoCString utf8Spec;
  995. rv = EnsureUTF8Spec(PromiseFlatCString(aSpec), aCharset, utf8Spec);
  996. if (NS_SUCCEEDED(rv)) {
  997. if (utf8Spec.IsEmpty())
  998. rv = url->SetSpec(aSpec);
  999. else
  1000. rv = url->SetSpec(utf8Spec);
  1001. }
  1002. }
  1003. if (NS_FAILED(rv)) {
  1004. return rv;
  1005. }
  1006. url.forget(result);
  1007. return rv;
  1008. }
  1009. NS_IMETHODIMP
  1010. nsJSProtocolHandler::NewChannel2(nsIURI* uri,
  1011. nsILoadInfo* aLoadInfo,
  1012. nsIChannel** result)
  1013. {
  1014. nsresult rv;
  1015. NS_ENSURE_ARG_POINTER(uri);
  1016. RefPtr<nsJSChannel> channel = new nsJSChannel();
  1017. if (!channel) {
  1018. return NS_ERROR_OUT_OF_MEMORY;
  1019. }
  1020. rv = channel->Init(uri, aLoadInfo);
  1021. NS_ENSURE_SUCCESS(rv, rv);
  1022. if (NS_SUCCEEDED(rv)) {
  1023. channel.forget(result);
  1024. }
  1025. return rv;
  1026. }
  1027. NS_IMETHODIMP
  1028. nsJSProtocolHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
  1029. {
  1030. return NewChannel2(uri, nullptr, result);
  1031. }
  1032. NS_IMETHODIMP
  1033. nsJSProtocolHandler::AllowPort(int32_t port, const char *scheme, bool *_retval)
  1034. {
  1035. // don't override anything.
  1036. *_retval = false;
  1037. return NS_OK;
  1038. }
  1039. ////////////////////////////////////////////////////////////
  1040. // nsJSURI implementation
  1041. static NS_DEFINE_CID(kThisSimpleURIImplementationCID,
  1042. NS_THIS_SIMPLEURI_IMPLEMENTATION_CID);
  1043. NS_IMPL_ADDREF_INHERITED(nsJSURI, mozilla::net::nsSimpleURI)
  1044. NS_IMPL_RELEASE_INHERITED(nsJSURI, mozilla::net::nsSimpleURI)
  1045. NS_INTERFACE_MAP_BEGIN(nsJSURI)
  1046. if (aIID.Equals(kJSURICID))
  1047. foundInterface = static_cast<nsIURI*>(this);
  1048. else if (aIID.Equals(kThisSimpleURIImplementationCID)) {
  1049. // Need to return explicitly here, because if we just set foundInterface
  1050. // to null the NS_INTERFACE_MAP_END_INHERITING will end up calling into
  1051. // nsSimplURI::QueryInterface and finding something for this CID.
  1052. *aInstancePtr = nullptr;
  1053. return NS_NOINTERFACE;
  1054. }
  1055. else
  1056. NS_INTERFACE_MAP_END_INHERITING(mozilla::net::nsSimpleURI)
  1057. // nsISerializable methods:
  1058. NS_IMETHODIMP
  1059. nsJSURI::Read(nsIObjectInputStream* aStream)
  1060. {
  1061. nsresult rv = mozilla::net::nsSimpleURI::Read(aStream);
  1062. if (NS_FAILED(rv)) return rv;
  1063. bool haveBase;
  1064. rv = aStream->ReadBoolean(&haveBase);
  1065. if (NS_FAILED(rv)) return rv;
  1066. if (haveBase) {
  1067. nsCOMPtr<nsISupports> supports;
  1068. rv = aStream->ReadObject(true, getter_AddRefs(supports));
  1069. if (NS_FAILED(rv)) return rv;
  1070. mBaseURI = do_QueryInterface(supports);
  1071. }
  1072. return NS_OK;
  1073. }
  1074. NS_IMETHODIMP
  1075. nsJSURI::Write(nsIObjectOutputStream* aStream)
  1076. {
  1077. nsresult rv = mozilla::net::nsSimpleURI::Write(aStream);
  1078. if (NS_FAILED(rv)) return rv;
  1079. rv = aStream->WriteBoolean(mBaseURI != nullptr);
  1080. if (NS_FAILED(rv)) return rv;
  1081. if (mBaseURI) {
  1082. rv = aStream->WriteObject(mBaseURI, true);
  1083. if (NS_FAILED(rv)) return rv;
  1084. }
  1085. return NS_OK;
  1086. }
  1087. // nsIIPCSerializableURI
  1088. void
  1089. nsJSURI::Serialize(mozilla::ipc::URIParams& aParams)
  1090. {
  1091. using namespace mozilla::ipc;
  1092. JSURIParams jsParams;
  1093. URIParams simpleParams;
  1094. mozilla::net::nsSimpleURI::Serialize(simpleParams);
  1095. jsParams.simpleParams() = simpleParams;
  1096. if (mBaseURI) {
  1097. SerializeURI(mBaseURI, jsParams.baseURI());
  1098. } else {
  1099. jsParams.baseURI() = mozilla::void_t();
  1100. }
  1101. aParams = jsParams;
  1102. }
  1103. bool
  1104. nsJSURI::Deserialize(const mozilla::ipc::URIParams& aParams)
  1105. {
  1106. using namespace mozilla::ipc;
  1107. if (aParams.type() != URIParams::TJSURIParams) {
  1108. NS_ERROR("Received unknown parameters from the other process!");
  1109. return false;
  1110. }
  1111. const JSURIParams& jsParams = aParams.get_JSURIParams();
  1112. mozilla::net::nsSimpleURI::Deserialize(jsParams.simpleParams());
  1113. if (jsParams.baseURI().type() != OptionalURIParams::Tvoid_t) {
  1114. mBaseURI = DeserializeURI(jsParams.baseURI().get_URIParams());
  1115. } else {
  1116. mBaseURI = nullptr;
  1117. }
  1118. return true;
  1119. }
  1120. // nsSimpleURI methods:
  1121. /* virtual */ mozilla::net::nsSimpleURI*
  1122. nsJSURI::StartClone(mozilla::net::nsSimpleURI::RefHandlingEnum refHandlingMode,
  1123. const nsACString& newRef)
  1124. {
  1125. nsCOMPtr<nsIURI> baseClone;
  1126. if (mBaseURI) {
  1127. // Note: We preserve ref on *base* URI, regardless of ref handling mode.
  1128. nsresult rv = mBaseURI->Clone(getter_AddRefs(baseClone));
  1129. if (NS_FAILED(rv)) {
  1130. return nullptr;
  1131. }
  1132. }
  1133. nsJSURI* url = new nsJSURI(baseClone);
  1134. SetRefOnClone(url, refHandlingMode, newRef);
  1135. return url;
  1136. }
  1137. /* virtual */ nsresult
  1138. nsJSURI::EqualsInternal(nsIURI* aOther,
  1139. mozilla::net::nsSimpleURI::RefHandlingEnum aRefHandlingMode,
  1140. bool* aResult)
  1141. {
  1142. NS_ENSURE_ARG_POINTER(aOther);
  1143. NS_PRECONDITION(aResult, "null pointer for outparam");
  1144. RefPtr<nsJSURI> otherJSURI;
  1145. nsresult rv = aOther->QueryInterface(kJSURICID,
  1146. getter_AddRefs(otherJSURI));
  1147. if (NS_FAILED(rv)) {
  1148. *aResult = false; // aOther is not a nsJSURI --> not equal.
  1149. return NS_OK;
  1150. }
  1151. // Compare the member data that our base class knows about.
  1152. if (!mozilla::net::nsSimpleURI::EqualsInternal(otherJSURI, aRefHandlingMode)) {
  1153. *aResult = false;
  1154. return NS_OK;
  1155. }
  1156. // Compare the piece of additional member data that we add to base class.
  1157. nsIURI* otherBaseURI = otherJSURI->GetBaseURI();
  1158. if (mBaseURI) {
  1159. // (As noted in StartClone, we always honor refs on mBaseURI)
  1160. return mBaseURI->Equals(otherBaseURI, aResult);
  1161. }
  1162. *aResult = !otherBaseURI;
  1163. return NS_OK;
  1164. }
  1165. NS_IMETHODIMP
  1166. nsJSURI::GetClassIDNoAlloc(nsCID *aClassIDNoAlloc)
  1167. {
  1168. *aClassIDNoAlloc = kJSURICID;
  1169. return NS_OK;
  1170. }