imgRequestProxy.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. *
  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/. */
  6. #include "ImageLogging.h"
  7. #include "imgRequestProxy.h"
  8. #include "imgIOnloadBlocker.h"
  9. #include "imgLoader.h"
  10. #include "Image.h"
  11. #include "ImageOps.h"
  12. #include "nsError.h"
  13. #include "nsCRTGlue.h"
  14. #include "imgINotificationObserver.h"
  15. using namespace mozilla::image;
  16. // The split of imgRequestProxy and imgRequestProxyStatic means that
  17. // certain overridden functions need to be usable in the destructor.
  18. // Since virtual functions can't be used in that way, this class
  19. // provides a behavioural trait for each class to use instead.
  20. class ProxyBehaviour
  21. {
  22. public:
  23. virtual ~ProxyBehaviour() {}
  24. virtual already_AddRefed<mozilla::image::Image> GetImage() const = 0;
  25. virtual bool HasImage() const = 0;
  26. virtual already_AddRefed<ProgressTracker> GetProgressTracker() const = 0;
  27. virtual imgRequest* GetOwner() const = 0;
  28. virtual void SetOwner(imgRequest* aOwner) = 0;
  29. };
  30. class RequestBehaviour : public ProxyBehaviour
  31. {
  32. public:
  33. RequestBehaviour() : mOwner(nullptr), mOwnerHasImage(false) {}
  34. virtual already_AddRefed<mozilla::image::Image>GetImage() const override;
  35. virtual bool HasImage() const override;
  36. virtual already_AddRefed<ProgressTracker> GetProgressTracker() const override;
  37. virtual imgRequest* GetOwner() const override {
  38. return mOwner;
  39. }
  40. virtual void SetOwner(imgRequest* aOwner) override {
  41. mOwner = aOwner;
  42. if (mOwner) {
  43. RefPtr<ProgressTracker> ownerProgressTracker = GetProgressTracker();
  44. mOwnerHasImage = ownerProgressTracker && ownerProgressTracker->HasImage();
  45. } else {
  46. mOwnerHasImage = false;
  47. }
  48. }
  49. private:
  50. // We maintain the following invariant:
  51. // The proxy is registered at most with a single imgRequest as an observer,
  52. // and whenever it is, mOwner points to that object. This helps ensure that
  53. // imgRequestProxy::~imgRequestProxy unregisters the proxy as an observer
  54. // from whatever request it was registered with (if any). This, in turn,
  55. // means that imgRequest::mObservers will not have any stale pointers in it.
  56. RefPtr<imgRequest> mOwner;
  57. bool mOwnerHasImage;
  58. };
  59. already_AddRefed<mozilla::image::Image>
  60. RequestBehaviour::GetImage() const
  61. {
  62. if (!mOwnerHasImage) {
  63. return nullptr;
  64. }
  65. RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
  66. return progressTracker->GetImage();
  67. }
  68. already_AddRefed<ProgressTracker>
  69. RequestBehaviour::GetProgressTracker() const
  70. {
  71. // NOTE: It's possible that our mOwner has an Image that it didn't notify
  72. // us about, if we were Canceled before its Image was constructed.
  73. // (Canceling removes us as an observer, so mOwner has no way to notify us).
  74. // That's why this method uses mOwner->GetProgressTracker() instead of just
  75. // mOwner->mProgressTracker -- we might have a null mImage and yet have an
  76. // mOwner with a non-null mImage (and a null mProgressTracker pointer).
  77. return mOwner->GetProgressTracker();
  78. }
  79. NS_IMPL_ADDREF(imgRequestProxy)
  80. NS_IMPL_RELEASE(imgRequestProxy)
  81. NS_INTERFACE_MAP_BEGIN(imgRequestProxy)
  82. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, imgIRequest)
  83. NS_INTERFACE_MAP_ENTRY(imgIRequest)
  84. NS_INTERFACE_MAP_ENTRY(nsIRequest)
  85. NS_INTERFACE_MAP_ENTRY(nsISupportsPriority)
  86. NS_INTERFACE_MAP_ENTRY(nsISecurityInfoProvider)
  87. NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsITimedChannel,
  88. TimedChannel() != nullptr)
  89. NS_INTERFACE_MAP_END
  90. imgRequestProxy::imgRequestProxy() :
  91. mBehaviour(new RequestBehaviour),
  92. mURI(nullptr),
  93. mListener(nullptr),
  94. mLoadFlags(nsIRequest::LOAD_NORMAL),
  95. mLockCount(0),
  96. mAnimationConsumers(0),
  97. mCanceled(false),
  98. mIsInLoadGroup(false),
  99. mListenerIsStrongRef(false),
  100. mDecodeRequested(false),
  101. mDeferNotifications(false)
  102. {
  103. /* member initializers and constructor code */
  104. }
  105. imgRequestProxy::~imgRequestProxy()
  106. {
  107. /* destructor code */
  108. NS_PRECONDITION(!mListener,
  109. "Someone forgot to properly cancel this request!");
  110. // Unlock the image the proper number of times if we're holding locks on
  111. // it. Note that UnlockImage() decrements mLockCount each time it's called.
  112. while (mLockCount) {
  113. UnlockImage();
  114. }
  115. ClearAnimationConsumers();
  116. // Explicitly set mListener to null to ensure that the RemoveProxy
  117. // call below can't send |this| to an arbitrary listener while |this|
  118. // is being destroyed. This is all belt-and-suspenders in view of the
  119. // above assert.
  120. NullOutListener();
  121. if (GetOwner()) {
  122. /* Call RemoveProxy with a successful status. This will keep the
  123. channel, if still downloading data, from being canceled if 'this' is
  124. the last observer. This allows the image to continue to download and
  125. be cached even if no one is using it currently.
  126. */
  127. mCanceled = true;
  128. GetOwner()->RemoveProxy(this, NS_OK);
  129. }
  130. }
  131. nsresult
  132. imgRequestProxy::Init(imgRequest* aOwner,
  133. nsILoadGroup* aLoadGroup,
  134. ImageURL* aURI,
  135. imgINotificationObserver* aObserver)
  136. {
  137. NS_PRECONDITION(!GetOwner() && !mListener,
  138. "imgRequestProxy is already initialized");
  139. LOG_SCOPE_WITH_PARAM(gImgLog, "imgRequestProxy::Init", "request",
  140. aOwner);
  141. MOZ_ASSERT(mAnimationConsumers == 0, "Cannot have animation before Init");
  142. mBehaviour->SetOwner(aOwner);
  143. mListener = aObserver;
  144. // Make sure to addref mListener before the AddProxy call below, since
  145. // that call might well want to release it if the imgRequest has
  146. // already seen OnStopRequest.
  147. if (mListener) {
  148. mListenerIsStrongRef = true;
  149. NS_ADDREF(mListener);
  150. }
  151. mLoadGroup = aLoadGroup;
  152. mURI = aURI;
  153. // Note: AddProxy won't send all the On* notifications immediately
  154. if (GetOwner()) {
  155. GetOwner()->AddProxy(this);
  156. }
  157. return NS_OK;
  158. }
  159. nsresult
  160. imgRequestProxy::ChangeOwner(imgRequest* aNewOwner)
  161. {
  162. NS_PRECONDITION(GetOwner(),
  163. "Cannot ChangeOwner on a proxy without an owner!");
  164. if (mCanceled) {
  165. // Ensure that this proxy has received all notifications to date
  166. // before we clean it up when removing it from the old owner below.
  167. SyncNotifyListener();
  168. }
  169. // If we're holding locks, unlock the old image.
  170. // Note that UnlockImage decrements mLockCount each time it's called.
  171. uint32_t oldLockCount = mLockCount;
  172. while (mLockCount) {
  173. UnlockImage();
  174. }
  175. // If we're holding animation requests, undo them.
  176. uint32_t oldAnimationConsumers = mAnimationConsumers;
  177. ClearAnimationConsumers();
  178. GetOwner()->RemoveProxy(this, NS_IMAGELIB_CHANGING_OWNER);
  179. mBehaviour->SetOwner(aNewOwner);
  180. // If we were locked, apply the locks here
  181. for (uint32_t i = 0; i < oldLockCount; i++) {
  182. LockImage();
  183. }
  184. // If we had animation requests, restore them here. Note that we
  185. // do this *after* RemoveProxy, which clears out animation consumers
  186. // (see bug 601723).
  187. for (uint32_t i = 0; i < oldAnimationConsumers; i++) {
  188. IncrementAnimationConsumers();
  189. }
  190. GetOwner()->AddProxy(this);
  191. // If we'd previously requested a synchronous decode, request a decode on the
  192. // new image.
  193. if (mDecodeRequested) {
  194. StartDecoding();
  195. }
  196. return NS_OK;
  197. }
  198. void
  199. imgRequestProxy::AddToLoadGroup()
  200. {
  201. NS_ASSERTION(!mIsInLoadGroup, "Whaa, we're already in the loadgroup!");
  202. if (!mIsInLoadGroup && mLoadGroup) {
  203. mLoadGroup->AddRequest(this, nullptr);
  204. mIsInLoadGroup = true;
  205. }
  206. }
  207. void
  208. imgRequestProxy::RemoveFromLoadGroup(bool releaseLoadGroup)
  209. {
  210. if (!mIsInLoadGroup) {
  211. return;
  212. }
  213. /* calling RemoveFromLoadGroup may cause the document to finish
  214. loading, which could result in our death. We need to make sure
  215. that we stay alive long enough to fight another battle... at
  216. least until we exit this function.
  217. */
  218. nsCOMPtr<imgIRequest> kungFuDeathGrip(this);
  219. mLoadGroup->RemoveRequest(this, nullptr, NS_OK);
  220. mIsInLoadGroup = false;
  221. if (releaseLoadGroup) {
  222. // We're done with the loadgroup, release it.
  223. mLoadGroup = nullptr;
  224. }
  225. }
  226. /** nsIRequest / imgIRequest methods **/
  227. NS_IMETHODIMP
  228. imgRequestProxy::GetName(nsACString& aName)
  229. {
  230. aName.Truncate();
  231. if (mURI) {
  232. mURI->GetSpec(aName);
  233. }
  234. return NS_OK;
  235. }
  236. NS_IMETHODIMP
  237. imgRequestProxy::IsPending(bool* _retval)
  238. {
  239. return NS_ERROR_NOT_IMPLEMENTED;
  240. }
  241. NS_IMETHODIMP
  242. imgRequestProxy::GetStatus(nsresult* aStatus)
  243. {
  244. return NS_ERROR_NOT_IMPLEMENTED;
  245. }
  246. NS_IMETHODIMP
  247. imgRequestProxy::Cancel(nsresult status)
  248. {
  249. if (mCanceled) {
  250. return NS_ERROR_FAILURE;
  251. }
  252. LOG_SCOPE(gImgLog, "imgRequestProxy::Cancel");
  253. mCanceled = true;
  254. nsCOMPtr<nsIRunnable> ev = new imgCancelRunnable(this, status);
  255. return NS_DispatchToCurrentThread(ev);
  256. }
  257. void
  258. imgRequestProxy::DoCancel(nsresult status)
  259. {
  260. if (GetOwner()) {
  261. GetOwner()->RemoveProxy(this, status);
  262. }
  263. NullOutListener();
  264. }
  265. NS_IMETHODIMP
  266. imgRequestProxy::CancelAndForgetObserver(nsresult aStatus)
  267. {
  268. // If mCanceled is true but mListener is non-null, that means
  269. // someone called Cancel() on us but the imgCancelRunnable is still
  270. // pending. We still need to null out mListener before returning
  271. // from this function in this case. That means we want to do the
  272. // RemoveProxy call right now, because we need to deliver the
  273. // onStopRequest.
  274. if (mCanceled && !mListener) {
  275. return NS_ERROR_FAILURE;
  276. }
  277. LOG_SCOPE(gImgLog, "imgRequestProxy::CancelAndForgetObserver");
  278. mCanceled = true;
  279. // Now cheat and make sure our removal from loadgroup happens async
  280. bool oldIsInLoadGroup = mIsInLoadGroup;
  281. mIsInLoadGroup = false;
  282. if (GetOwner()) {
  283. GetOwner()->RemoveProxy(this, aStatus);
  284. }
  285. mIsInLoadGroup = oldIsInLoadGroup;
  286. if (mIsInLoadGroup) {
  287. NS_DispatchToCurrentThread(NewRunnableMethod(this, &imgRequestProxy::DoRemoveFromLoadGroup));
  288. }
  289. NullOutListener();
  290. return NS_OK;
  291. }
  292. NS_IMETHODIMP
  293. imgRequestProxy::StartDecoding()
  294. {
  295. // Flag this, so we know to transfer the request if our owner changes
  296. mDecodeRequested = true;
  297. RefPtr<Image> image = GetImage();
  298. if (image) {
  299. return image->StartDecoding();
  300. }
  301. if (GetOwner()) {
  302. GetOwner()->StartDecoding();
  303. }
  304. return NS_OK;
  305. }
  306. NS_IMETHODIMP
  307. imgRequestProxy::LockImage()
  308. {
  309. mLockCount++;
  310. RefPtr<Image> image = GetImage();
  311. if (image) {
  312. return image->LockImage();
  313. }
  314. return NS_OK;
  315. }
  316. NS_IMETHODIMP
  317. imgRequestProxy::UnlockImage()
  318. {
  319. MOZ_ASSERT(mLockCount > 0, "calling unlock but no locks!");
  320. mLockCount--;
  321. RefPtr<Image> image = GetImage();
  322. if (image) {
  323. return image->UnlockImage();
  324. }
  325. return NS_OK;
  326. }
  327. NS_IMETHODIMP
  328. imgRequestProxy::RequestDiscard()
  329. {
  330. RefPtr<Image> image = GetImage();
  331. if (image) {
  332. return image->RequestDiscard();
  333. }
  334. return NS_OK;
  335. }
  336. NS_IMETHODIMP
  337. imgRequestProxy::IncrementAnimationConsumers()
  338. {
  339. mAnimationConsumers++;
  340. RefPtr<Image> image = GetImage();
  341. if (image) {
  342. image->IncrementAnimationConsumers();
  343. }
  344. return NS_OK;
  345. }
  346. NS_IMETHODIMP
  347. imgRequestProxy::DecrementAnimationConsumers()
  348. {
  349. // We may get here if some responsible code called Increment,
  350. // then called us, but we have meanwhile called ClearAnimationConsumers
  351. // because we needed to get rid of them earlier (see
  352. // imgRequest::RemoveProxy), and hence have nothing left to
  353. // decrement. (In such a case we got rid of the animation consumers
  354. // early, but not the observer.)
  355. if (mAnimationConsumers > 0) {
  356. mAnimationConsumers--;
  357. RefPtr<Image> image = GetImage();
  358. if (image) {
  359. image->DecrementAnimationConsumers();
  360. }
  361. }
  362. return NS_OK;
  363. }
  364. void
  365. imgRequestProxy::ClearAnimationConsumers()
  366. {
  367. while (mAnimationConsumers > 0) {
  368. DecrementAnimationConsumers();
  369. }
  370. }
  371. NS_IMETHODIMP
  372. imgRequestProxy::Suspend()
  373. {
  374. return NS_ERROR_NOT_IMPLEMENTED;
  375. }
  376. NS_IMETHODIMP
  377. imgRequestProxy::Resume()
  378. {
  379. return NS_ERROR_NOT_IMPLEMENTED;
  380. }
  381. NS_IMETHODIMP
  382. imgRequestProxy::GetLoadGroup(nsILoadGroup** loadGroup)
  383. {
  384. NS_IF_ADDREF(*loadGroup = mLoadGroup.get());
  385. return NS_OK;
  386. }
  387. NS_IMETHODIMP
  388. imgRequestProxy::SetLoadGroup(nsILoadGroup* loadGroup)
  389. {
  390. mLoadGroup = loadGroup;
  391. return NS_OK;
  392. }
  393. NS_IMETHODIMP
  394. imgRequestProxy::GetLoadFlags(nsLoadFlags* flags)
  395. {
  396. *flags = mLoadFlags;
  397. return NS_OK;
  398. }
  399. NS_IMETHODIMP
  400. imgRequestProxy::SetLoadFlags(nsLoadFlags flags)
  401. {
  402. mLoadFlags = flags;
  403. return NS_OK;
  404. }
  405. /** imgIRequest methods **/
  406. NS_IMETHODIMP
  407. imgRequestProxy::GetImage(imgIContainer** aImage)
  408. {
  409. NS_ENSURE_TRUE(aImage, NS_ERROR_NULL_POINTER);
  410. // It's possible that our owner has an image but hasn't notified us of it -
  411. // that'll happen if we get Canceled before the owner instantiates its image
  412. // (because Canceling unregisters us as a listener on mOwner). If we're
  413. // in that situation, just grab the image off of mOwner.
  414. RefPtr<Image> image = GetImage();
  415. nsCOMPtr<imgIContainer> imageToReturn;
  416. if (image) {
  417. imageToReturn = do_QueryInterface(image);
  418. }
  419. if (!imageToReturn && GetOwner()) {
  420. imageToReturn = GetOwner()->GetImage();
  421. }
  422. if (!imageToReturn) {
  423. return NS_ERROR_FAILURE;
  424. }
  425. imageToReturn.swap(*aImage);
  426. return NS_OK;
  427. }
  428. NS_IMETHODIMP
  429. imgRequestProxy::GetImageStatus(uint32_t* aStatus)
  430. {
  431. RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
  432. *aStatus = progressTracker->GetImageStatus();
  433. return NS_OK;
  434. }
  435. NS_IMETHODIMP
  436. imgRequestProxy::GetImageErrorCode(nsresult* aStatus)
  437. {
  438. if (!GetOwner()) {
  439. return NS_ERROR_FAILURE;
  440. }
  441. *aStatus = GetOwner()->GetImageErrorCode();
  442. return NS_OK;
  443. }
  444. NS_IMETHODIMP
  445. imgRequestProxy::GetURI(nsIURI** aURI)
  446. {
  447. MOZ_ASSERT(NS_IsMainThread(), "Must be on main thread to convert URI");
  448. nsCOMPtr<nsIURI> uri = mURI->ToIURI();
  449. uri.forget(aURI);
  450. return NS_OK;
  451. }
  452. nsresult
  453. imgRequestProxy::GetCurrentURI(nsIURI** aURI)
  454. {
  455. if (!GetOwner()) {
  456. return NS_ERROR_FAILURE;
  457. }
  458. return GetOwner()->GetCurrentURI(aURI);
  459. }
  460. nsresult
  461. imgRequestProxy::GetURI(ImageURL** aURI)
  462. {
  463. if (!mURI) {
  464. return NS_ERROR_FAILURE;
  465. }
  466. NS_ADDREF(*aURI = mURI);
  467. return NS_OK;
  468. }
  469. NS_IMETHODIMP
  470. imgRequestProxy::GetNotificationObserver(imgINotificationObserver** aObserver)
  471. {
  472. *aObserver = mListener;
  473. NS_IF_ADDREF(*aObserver);
  474. return NS_OK;
  475. }
  476. NS_IMETHODIMP
  477. imgRequestProxy::GetMimeType(char** aMimeType)
  478. {
  479. if (!GetOwner()) {
  480. return NS_ERROR_FAILURE;
  481. }
  482. const char* type = GetOwner()->GetMimeType();
  483. if (!type) {
  484. return NS_ERROR_FAILURE;
  485. }
  486. *aMimeType = NS_strdup(type);
  487. return NS_OK;
  488. }
  489. static imgRequestProxy* NewProxy(imgRequestProxy* /*aThis*/)
  490. {
  491. return new imgRequestProxy();
  492. }
  493. imgRequestProxy* NewStaticProxy(imgRequestProxy* aThis)
  494. {
  495. nsCOMPtr<nsIPrincipal> currentPrincipal;
  496. aThis->GetImagePrincipal(getter_AddRefs(currentPrincipal));
  497. RefPtr<Image> image = aThis->GetImage();
  498. return new imgRequestProxyStatic(image, currentPrincipal);
  499. }
  500. NS_IMETHODIMP
  501. imgRequestProxy::Clone(imgINotificationObserver* aObserver,
  502. imgIRequest** aClone)
  503. {
  504. nsresult result;
  505. imgRequestProxy* proxy;
  506. result = Clone(aObserver, &proxy);
  507. *aClone = proxy;
  508. return result;
  509. }
  510. nsresult imgRequestProxy::Clone(imgINotificationObserver* aObserver,
  511. imgRequestProxy** aClone)
  512. {
  513. return PerformClone(aObserver, NewProxy, aClone);
  514. }
  515. nsresult
  516. imgRequestProxy::PerformClone(imgINotificationObserver* aObserver,
  517. imgRequestProxy* (aAllocFn)(imgRequestProxy*),
  518. imgRequestProxy** aClone)
  519. {
  520. NS_PRECONDITION(aClone, "Null out param");
  521. LOG_SCOPE(gImgLog, "imgRequestProxy::Clone");
  522. *aClone = nullptr;
  523. RefPtr<imgRequestProxy> clone = aAllocFn(this);
  524. // It is important to call |SetLoadFlags()| before calling |Init()| because
  525. // |Init()| adds the request to the loadgroup.
  526. // When a request is added to a loadgroup, its load flags are merged
  527. // with the load flags of the loadgroup.
  528. // XXXldb That's not true anymore. Stuff from imgLoader adds the
  529. // request to the loadgroup.
  530. clone->SetLoadFlags(mLoadFlags);
  531. nsresult rv = clone->Init(mBehaviour->GetOwner(), mLoadGroup,
  532. mURI, aObserver);
  533. if (NS_FAILED(rv)) {
  534. return rv;
  535. }
  536. if (GetOwner() && GetOwner()->GetValidator()) {
  537. clone->SetNotificationsDeferred(true);
  538. GetOwner()->GetValidator()->AddProxy(clone);
  539. }
  540. // Assign to *aClone before calling Notify so that if the caller expects to
  541. // only be notified for requests it's already holding pointers to it won't be
  542. // surprised.
  543. NS_ADDREF(*aClone = clone);
  544. // This is wrong!!! We need to notify asynchronously, but there's code that
  545. // assumes that we don't. This will be fixed in bug 580466.
  546. clone->SyncNotifyListener();
  547. return NS_OK;
  548. }
  549. NS_IMETHODIMP
  550. imgRequestProxy::GetImagePrincipal(nsIPrincipal** aPrincipal)
  551. {
  552. if (!GetOwner()) {
  553. return NS_ERROR_FAILURE;
  554. }
  555. nsCOMPtr<nsIPrincipal> principal = GetOwner()->GetPrincipal();
  556. principal.forget(aPrincipal);
  557. return NS_OK;
  558. }
  559. NS_IMETHODIMP
  560. imgRequestProxy::GetMultipart(bool* aMultipart)
  561. {
  562. if (!GetOwner()) {
  563. return NS_ERROR_FAILURE;
  564. }
  565. *aMultipart = GetOwner()->GetMultipart();
  566. return NS_OK;
  567. }
  568. NS_IMETHODIMP
  569. imgRequestProxy::GetCORSMode(int32_t* aCorsMode)
  570. {
  571. if (!GetOwner()) {
  572. return NS_ERROR_FAILURE;
  573. }
  574. *aCorsMode = GetOwner()->GetCORSMode();
  575. return NS_OK;
  576. }
  577. /** nsISupportsPriority methods **/
  578. NS_IMETHODIMP
  579. imgRequestProxy::GetPriority(int32_t* priority)
  580. {
  581. NS_ENSURE_STATE(GetOwner());
  582. *priority = GetOwner()->Priority();
  583. return NS_OK;
  584. }
  585. NS_IMETHODIMP
  586. imgRequestProxy::SetPriority(int32_t priority)
  587. {
  588. NS_ENSURE_STATE(GetOwner() && !mCanceled);
  589. GetOwner()->AdjustPriority(this, priority - GetOwner()->Priority());
  590. return NS_OK;
  591. }
  592. NS_IMETHODIMP
  593. imgRequestProxy::AdjustPriority(int32_t priority)
  594. {
  595. // We don't require |!mCanceled| here. This may be called even if we're
  596. // cancelled, because it's invoked as part of the process of removing an image
  597. // from the load group.
  598. NS_ENSURE_STATE(GetOwner());
  599. GetOwner()->AdjustPriority(this, priority);
  600. return NS_OK;
  601. }
  602. /** nsISecurityInfoProvider methods **/
  603. NS_IMETHODIMP
  604. imgRequestProxy::GetSecurityInfo(nsISupports** _retval)
  605. {
  606. if (GetOwner()) {
  607. return GetOwner()->GetSecurityInfo(_retval);
  608. }
  609. *_retval = nullptr;
  610. return NS_OK;
  611. }
  612. NS_IMETHODIMP
  613. imgRequestProxy::GetHasTransferredData(bool* hasData)
  614. {
  615. if (GetOwner()) {
  616. *hasData = GetOwner()->HasTransferredData();
  617. } else {
  618. // The safe thing to do is to claim we have data
  619. *hasData = true;
  620. }
  621. return NS_OK;
  622. }
  623. static const char*
  624. NotificationTypeToString(int32_t aType)
  625. {
  626. switch(aType)
  627. {
  628. case imgINotificationObserver::SIZE_AVAILABLE: return "SIZE_AVAILABLE";
  629. case imgINotificationObserver::FRAME_UPDATE: return "FRAME_UPDATE";
  630. case imgINotificationObserver::FRAME_COMPLETE: return "FRAME_COMPLETE";
  631. case imgINotificationObserver::LOAD_COMPLETE: return "LOAD_COMPLETE";
  632. case imgINotificationObserver::DECODE_COMPLETE: return "DECODE_COMPLETE";
  633. case imgINotificationObserver::DISCARD: return "DISCARD";
  634. case imgINotificationObserver::UNLOCKED_DRAW: return "UNLOCKED_DRAW";
  635. case imgINotificationObserver::IS_ANIMATED: return "IS_ANIMATED";
  636. case imgINotificationObserver::HAS_TRANSPARENCY: return "HAS_TRANSPARENCY";
  637. default:
  638. NS_NOTREACHED("Notification list should be exhaustive");
  639. return "(unknown notification)";
  640. }
  641. }
  642. void
  643. imgRequestProxy::Notify(int32_t aType, const mozilla::gfx::IntRect* aRect)
  644. {
  645. MOZ_ASSERT(aType != imgINotificationObserver::LOAD_COMPLETE,
  646. "Should call OnLoadComplete");
  647. LOG_FUNC_WITH_PARAM(gImgLog, "imgRequestProxy::Notify", "type",
  648. NotificationTypeToString(aType));
  649. if (!mListener || mCanceled) {
  650. return;
  651. }
  652. // Make sure the listener stays alive while we notify.
  653. nsCOMPtr<imgINotificationObserver> listener(mListener);
  654. listener->Notify(this, aType, aRect);
  655. }
  656. void
  657. imgRequestProxy::OnLoadComplete(bool aLastPart)
  658. {
  659. if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
  660. nsAutoCString name;
  661. GetName(name);
  662. LOG_FUNC_WITH_PARAM(gImgLog, "imgRequestProxy::OnLoadComplete",
  663. "name", name.get());
  664. }
  665. // There's all sorts of stuff here that could kill us (the OnStopRequest call
  666. // on the listener, the removal from the loadgroup, the release of the
  667. // listener, etc). Don't let them do it.
  668. nsCOMPtr<imgIRequest> kungFuDeathGrip(this);
  669. if (mListener && !mCanceled) {
  670. // Hold a ref to the listener while we call it, just in case.
  671. nsCOMPtr<imgINotificationObserver> listener(mListener);
  672. listener->Notify(this, imgINotificationObserver::LOAD_COMPLETE, nullptr);
  673. }
  674. // If we're expecting more data from a multipart channel, re-add ourself
  675. // to the loadgroup so that the document doesn't lose track of the load.
  676. // If the request is already a background request and there's more data
  677. // coming, we can just leave the request in the loadgroup as-is.
  678. if (aLastPart || (mLoadFlags & nsIRequest::LOAD_BACKGROUND) == 0) {
  679. RemoveFromLoadGroup(aLastPart);
  680. // More data is coming, so change the request to be a background request
  681. // and put it back in the loadgroup.
  682. if (!aLastPart) {
  683. mLoadFlags |= nsIRequest::LOAD_BACKGROUND;
  684. AddToLoadGroup();
  685. }
  686. }
  687. if (mListenerIsStrongRef && aLastPart) {
  688. NS_PRECONDITION(mListener, "How did that happen?");
  689. // Drop our strong ref to the listener now that we're done with
  690. // everything. Note that this can cancel us and other fun things
  691. // like that. Don't add anything in this method after this point.
  692. imgINotificationObserver* obs = mListener;
  693. mListenerIsStrongRef = false;
  694. NS_RELEASE(obs);
  695. }
  696. }
  697. void
  698. imgRequestProxy::BlockOnload()
  699. {
  700. if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
  701. nsAutoCString name;
  702. GetName(name);
  703. LOG_FUNC_WITH_PARAM(gImgLog, "imgRequestProxy::BlockOnload",
  704. "name", name.get());
  705. }
  706. nsCOMPtr<imgIOnloadBlocker> blocker = do_QueryInterface(mListener);
  707. if (blocker) {
  708. blocker->BlockOnload(this);
  709. }
  710. }
  711. void
  712. imgRequestProxy::UnblockOnload()
  713. {
  714. if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
  715. nsAutoCString name;
  716. GetName(name);
  717. LOG_FUNC_WITH_PARAM(gImgLog, "imgRequestProxy::UnblockOnload",
  718. "name", name.get());
  719. }
  720. nsCOMPtr<imgIOnloadBlocker> blocker = do_QueryInterface(mListener);
  721. if (blocker) {
  722. blocker->UnblockOnload(this);
  723. }
  724. }
  725. void
  726. imgRequestProxy::NullOutListener()
  727. {
  728. // If we have animation consumers, then they don't matter anymore
  729. if (mListener) {
  730. ClearAnimationConsumers();
  731. }
  732. if (mListenerIsStrongRef) {
  733. // Releasing could do weird reentery stuff, so just play it super-safe
  734. nsCOMPtr<imgINotificationObserver> obs;
  735. obs.swap(mListener);
  736. mListenerIsStrongRef = false;
  737. } else {
  738. mListener = nullptr;
  739. }
  740. }
  741. NS_IMETHODIMP
  742. imgRequestProxy::GetStaticRequest(imgIRequest** aReturn)
  743. {
  744. imgRequestProxy* proxy;
  745. nsresult result = GetStaticRequest(&proxy);
  746. *aReturn = proxy;
  747. return result;
  748. }
  749. nsresult
  750. imgRequestProxy::GetStaticRequest(imgRequestProxy** aReturn)
  751. {
  752. *aReturn = nullptr;
  753. RefPtr<Image> image = GetImage();
  754. bool animated;
  755. if (!image || (NS_SUCCEEDED(image->GetAnimated(&animated)) && !animated)) {
  756. // Early exit - we're not animated, so we don't have to do anything.
  757. NS_ADDREF(*aReturn = this);
  758. return NS_OK;
  759. }
  760. // Check for errors in the image. Callers code rely on GetStaticRequest
  761. // failing in this case, though with FrozenImage there's no technical reason
  762. // for it anymore.
  763. if (image->HasError()) {
  764. return NS_ERROR_FAILURE;
  765. }
  766. // We are animated. We need to create a frozen version of this image.
  767. RefPtr<Image> frozenImage = ImageOps::Freeze(image);
  768. // Create a static imgRequestProxy with our new extracted frame.
  769. nsCOMPtr<nsIPrincipal> currentPrincipal;
  770. GetImagePrincipal(getter_AddRefs(currentPrincipal));
  771. RefPtr<imgRequestProxy> req = new imgRequestProxyStatic(frozenImage,
  772. currentPrincipal);
  773. req->Init(nullptr, nullptr, mURI, nullptr);
  774. NS_ADDREF(*aReturn = req);
  775. return NS_OK;
  776. }
  777. void
  778. imgRequestProxy::NotifyListener()
  779. {
  780. // It would be nice to notify the observer directly in the status tracker
  781. // instead of through the proxy, but there are several places we do extra
  782. // processing when we receive notifications (like OnStopRequest()), and we
  783. // need to check mCanceled everywhere too.
  784. RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
  785. if (GetOwner()) {
  786. // Send the notifications to our listener asynchronously.
  787. progressTracker->Notify(this);
  788. } else {
  789. // We don't have an imgRequest, so we can only notify the clone of our
  790. // current state, but we still have to do that asynchronously.
  791. MOZ_ASSERT(HasImage(),
  792. "if we have no imgRequest, we should have an Image");
  793. progressTracker->NotifyCurrentState(this);
  794. }
  795. }
  796. void
  797. imgRequestProxy::SyncNotifyListener()
  798. {
  799. // It would be nice to notify the observer directly in the status tracker
  800. // instead of through the proxy, but there are several places we do extra
  801. // processing when we receive notifications (like OnStopRequest()), and we
  802. // need to check mCanceled everywhere too.
  803. RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
  804. progressTracker->SyncNotify(this);
  805. }
  806. void
  807. imgRequestProxy::SetHasImage()
  808. {
  809. RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
  810. MOZ_ASSERT(progressTracker);
  811. RefPtr<Image> image = progressTracker->GetImage();
  812. MOZ_ASSERT(image);
  813. // Force any private status related to the owner to reflect
  814. // the presence of an image;
  815. mBehaviour->SetOwner(mBehaviour->GetOwner());
  816. // Apply any locks we have
  817. for (uint32_t i = 0; i < mLockCount; ++i) {
  818. image->LockImage();
  819. }
  820. // Apply any animation consumers we have
  821. for (uint32_t i = 0; i < mAnimationConsumers; i++) {
  822. image->IncrementAnimationConsumers();
  823. }
  824. }
  825. already_AddRefed<ProgressTracker>
  826. imgRequestProxy::GetProgressTracker() const
  827. {
  828. return mBehaviour->GetProgressTracker();
  829. }
  830. already_AddRefed<mozilla::image::Image>
  831. imgRequestProxy::GetImage() const
  832. {
  833. return mBehaviour->GetImage();
  834. }
  835. bool
  836. RequestBehaviour::HasImage() const
  837. {
  838. if (!mOwnerHasImage) {
  839. return false;
  840. }
  841. RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
  842. return progressTracker ? progressTracker->HasImage() : false;
  843. }
  844. bool
  845. imgRequestProxy::HasImage() const
  846. {
  847. return mBehaviour->HasImage();
  848. }
  849. imgRequest*
  850. imgRequestProxy::GetOwner() const
  851. {
  852. return mBehaviour->GetOwner();
  853. }
  854. ////////////////// imgRequestProxyStatic methods
  855. class StaticBehaviour : public ProxyBehaviour
  856. {
  857. public:
  858. explicit StaticBehaviour(mozilla::image::Image* aImage) : mImage(aImage) {}
  859. virtual already_AddRefed<mozilla::image::Image>
  860. GetImage() const override {
  861. RefPtr<mozilla::image::Image> image = mImage;
  862. return image.forget();
  863. }
  864. virtual bool HasImage() const override {
  865. return mImage;
  866. }
  867. virtual already_AddRefed<ProgressTracker> GetProgressTracker()
  868. const override {
  869. return mImage->GetProgressTracker();
  870. }
  871. virtual imgRequest* GetOwner() const override {
  872. return nullptr;
  873. }
  874. virtual void SetOwner(imgRequest* aOwner) override {
  875. MOZ_ASSERT(!aOwner,
  876. "We shouldn't be giving static requests a non-null owner.");
  877. }
  878. private:
  879. // Our image. We have to hold a strong reference here, because that's normally
  880. // the job of the underlying request.
  881. RefPtr<mozilla::image::Image> mImage;
  882. };
  883. imgRequestProxyStatic::imgRequestProxyStatic(mozilla::image::Image* aImage,
  884. nsIPrincipal* aPrincipal)
  885. : mPrincipal(aPrincipal)
  886. {
  887. mBehaviour = mozilla::MakeUnique<StaticBehaviour>(aImage);
  888. }
  889. NS_IMETHODIMP
  890. imgRequestProxyStatic::GetImagePrincipal(nsIPrincipal** aPrincipal)
  891. {
  892. if (!mPrincipal) {
  893. return NS_ERROR_FAILURE;
  894. }
  895. NS_ADDREF(*aPrincipal = mPrincipal);
  896. return NS_OK;
  897. }
  898. nsresult
  899. imgRequestProxyStatic::Clone(imgINotificationObserver* aObserver,
  900. imgRequestProxy** aClone)
  901. {
  902. return PerformClone(aObserver, NewStaticProxy, aClone);
  903. }