IDBFileRequest.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "IDBFileRequest.h"
  6. #include "IDBFileHandle.h"
  7. #include "js/RootingAPI.h"
  8. #include "jsapi.h"
  9. #include "mozilla/Assertions.h"
  10. #include "mozilla/dom/IDBFileRequestBinding.h"
  11. #include "mozilla/dom/ProgressEvent.h"
  12. #include "mozilla/dom/ScriptSettings.h"
  13. #include "mozilla/EventDispatcher.h"
  14. #include "nsCOMPtr.h"
  15. #include "nsDebug.h"
  16. #include "nsError.h"
  17. #include "nsLiteralString.h"
  18. namespace mozilla {
  19. namespace dom {
  20. using namespace mozilla::dom::indexedDB;
  21. IDBFileRequest::IDBFileRequest(nsPIDOMWindowInner* aWindow,
  22. IDBFileHandle* aFileHandle,
  23. bool aWrapAsDOMRequest)
  24. : DOMRequest(aWindow)
  25. , FileRequestBase(DEBUGONLY(aFileHandle->OwningThread()))
  26. , mFileHandle(aFileHandle)
  27. , mWrapAsDOMRequest(aWrapAsDOMRequest)
  28. {
  29. AssertIsOnOwningThread();
  30. }
  31. IDBFileRequest::~IDBFileRequest()
  32. {
  33. AssertIsOnOwningThread();
  34. }
  35. // static
  36. already_AddRefed<IDBFileRequest>
  37. IDBFileRequest::Create(nsPIDOMWindowInner* aOwner, IDBFileHandle* aFileHandle,
  38. bool aWrapAsDOMRequest)
  39. {
  40. MOZ_ASSERT(aFileHandle);
  41. aFileHandle->AssertIsOnOwningThread();
  42. RefPtr<IDBFileRequest> request =
  43. new IDBFileRequest(aOwner, aFileHandle, aWrapAsDOMRequest);
  44. return request.forget();
  45. }
  46. NS_IMPL_ADDREF_INHERITED(IDBFileRequest, DOMRequest)
  47. NS_IMPL_RELEASE_INHERITED(IDBFileRequest, DOMRequest)
  48. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(IDBFileRequest)
  49. NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
  50. NS_IMPL_CYCLE_COLLECTION_INHERITED(IDBFileRequest, DOMRequest,
  51. mFileHandle)
  52. nsresult
  53. IDBFileRequest::GetEventTargetParent(EventChainPreVisitor& aVisitor)
  54. {
  55. AssertIsOnOwningThread();
  56. aVisitor.mCanHandle = true;
  57. aVisitor.SetParentTarget(mFileHandle, false);
  58. return NS_OK;
  59. }
  60. // virtual
  61. JSObject*
  62. IDBFileRequest::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  63. {
  64. AssertIsOnOwningThread();
  65. if (mWrapAsDOMRequest) {
  66. return DOMRequest::WrapObject(aCx, aGivenProto);
  67. }
  68. return IDBFileRequestBinding::Wrap(aCx, this, aGivenProto);
  69. }
  70. mozilla::dom::FileHandleBase*
  71. IDBFileRequest::FileHandle() const
  72. {
  73. AssertIsOnOwningThread();
  74. return mFileHandle;
  75. }
  76. void
  77. IDBFileRequest::OnProgress(uint64_t aProgress, uint64_t aProgressMax)
  78. {
  79. AssertIsOnOwningThread();
  80. FireProgressEvent(aProgress, aProgressMax);
  81. }
  82. void
  83. IDBFileRequest::SetResultCallback(ResultCallback* aCallback)
  84. {
  85. AssertIsOnOwningThread();
  86. MOZ_ASSERT(aCallback);
  87. AutoJSAPI autoJS;
  88. if (NS_WARN_IF(!autoJS.Init(GetOwner()))) {
  89. FireError(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR);
  90. return;
  91. }
  92. JSContext* cx = autoJS.cx();
  93. JS::Rooted<JS::Value> result(cx);
  94. nsresult rv = aCallback->GetResult(cx, &result);
  95. if (NS_WARN_IF(NS_FAILED(rv))) {
  96. FireError(rv);
  97. } else {
  98. FireSuccess(result);
  99. }
  100. }
  101. void
  102. IDBFileRequest::SetError(nsresult aError)
  103. {
  104. AssertIsOnOwningThread();
  105. FireError(aError);
  106. }
  107. void
  108. IDBFileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal)
  109. {
  110. AssertIsOnOwningThread();
  111. if (NS_FAILED(CheckInnerWindowCorrectness())) {
  112. return;
  113. }
  114. ProgressEventInit init;
  115. init.mBubbles = false;
  116. init.mCancelable = false;
  117. init.mLengthComputable = false;
  118. init.mLoaded = aLoaded;
  119. init.mTotal = aTotal;
  120. RefPtr<ProgressEvent> event =
  121. ProgressEvent::Constructor(this, NS_LITERAL_STRING("progress"), init);
  122. DispatchTrustedEvent(event);
  123. }
  124. } // namespace dom
  125. } // namespace mozilla