/src/mozilla-central/dom/indexedDB/IDBFileRequest.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
5 | | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "IDBFileRequest.h" |
8 | | |
9 | | #include "IDBFileHandle.h" |
10 | | #include "js/RootingAPI.h" |
11 | | #include "jsapi.h" |
12 | | #include "mozilla/Assertions.h" |
13 | | #include "mozilla/dom/IDBFileRequestBinding.h" |
14 | | #include "mozilla/dom/ProgressEvent.h" |
15 | | #include "mozilla/dom/ScriptSettings.h" |
16 | | #include "mozilla/EventDispatcher.h" |
17 | | #include "nsCOMPtr.h" |
18 | | #include "nsDebug.h" |
19 | | #include "nsError.h" |
20 | | #include "nsLiteralString.h" |
21 | | |
22 | | namespace mozilla { |
23 | | namespace dom { |
24 | | |
25 | | using namespace mozilla::dom::indexedDB; |
26 | | |
27 | | IDBFileRequest::IDBFileRequest(IDBFileHandle* aFileHandle, |
28 | | bool aWrapAsDOMRequest) |
29 | | : DOMRequest(aFileHandle->GetOwner()) |
30 | | , mFileHandle(aFileHandle) |
31 | | , mWrapAsDOMRequest(aWrapAsDOMRequest) |
32 | | , mHasEncoding(false) |
33 | 0 | { |
34 | 0 | MOZ_ASSERT(aFileHandle); |
35 | 0 | aFileHandle->AssertIsOnOwningThread(); |
36 | 0 | } |
37 | | |
38 | | IDBFileRequest::~IDBFileRequest() |
39 | 0 | { |
40 | 0 | AssertIsOnOwningThread(); |
41 | 0 | } |
42 | | |
43 | | // static |
44 | | already_AddRefed<IDBFileRequest> |
45 | | IDBFileRequest::Create(IDBFileHandle* aFileHandle, |
46 | | bool aWrapAsDOMRequest) |
47 | 0 | { |
48 | 0 | MOZ_ASSERT(aFileHandle); |
49 | 0 | aFileHandle->AssertIsOnOwningThread(); |
50 | 0 |
|
51 | 0 | RefPtr<IDBFileRequest> request = |
52 | 0 | new IDBFileRequest(aFileHandle, aWrapAsDOMRequest); |
53 | 0 |
|
54 | 0 | return request.forget(); |
55 | 0 | } |
56 | | |
57 | | void |
58 | | IDBFileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal) |
59 | 0 | { |
60 | 0 | AssertIsOnOwningThread(); |
61 | 0 |
|
62 | 0 | if (NS_FAILED(CheckInnerWindowCorrectness())) { |
63 | 0 | return; |
64 | 0 | } |
65 | 0 | |
66 | 0 | ProgressEventInit init; |
67 | 0 | init.mBubbles = false; |
68 | 0 | init.mCancelable = false; |
69 | 0 | init.mLengthComputable = false; |
70 | 0 | init.mLoaded = aLoaded; |
71 | 0 | init.mTotal = aTotal; |
72 | 0 |
|
73 | 0 | RefPtr<ProgressEvent> event = |
74 | 0 | ProgressEvent::Constructor(this, NS_LITERAL_STRING("progress"), init); |
75 | 0 | DispatchTrustedEvent(event); |
76 | 0 | } |
77 | | |
78 | | void |
79 | | IDBFileRequest::SetResultCallback(ResultCallback* aCallback) |
80 | 0 | { |
81 | 0 | AssertIsOnOwningThread(); |
82 | 0 | MOZ_ASSERT(aCallback); |
83 | 0 |
|
84 | 0 | AutoJSAPI autoJS; |
85 | 0 | if (NS_WARN_IF(!autoJS.Init(GetOwner()))) { |
86 | 0 | FireError(NS_ERROR_DOM_FILEHANDLE_UNKNOWN_ERR); |
87 | 0 | return; |
88 | 0 | } |
89 | 0 | |
90 | 0 | JSContext* cx = autoJS.cx(); |
91 | 0 |
|
92 | 0 | JS::Rooted<JS::Value> result(cx); |
93 | 0 | nsresult rv = aCallback->GetResult(cx, &result); |
94 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
95 | 0 | FireError(rv); |
96 | 0 | } else { |
97 | 0 | FireSuccess(result); |
98 | 0 | } |
99 | 0 | } |
100 | | |
101 | | NS_IMPL_ADDREF_INHERITED(IDBFileRequest, DOMRequest) |
102 | | NS_IMPL_RELEASE_INHERITED(IDBFileRequest, DOMRequest) |
103 | | |
104 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IDBFileRequest) |
105 | 0 | NS_INTERFACE_MAP_END_INHERITING(DOMRequest) |
106 | | |
107 | | NS_IMPL_CYCLE_COLLECTION_INHERITED(IDBFileRequest, DOMRequest, |
108 | | mFileHandle) |
109 | | |
110 | | void |
111 | | IDBFileRequest::GetEventTargetParent(EventChainPreVisitor& aVisitor) |
112 | 0 | { |
113 | 0 | AssertIsOnOwningThread(); |
114 | 0 |
|
115 | 0 | aVisitor.mCanHandle = true; |
116 | 0 | aVisitor.SetParentTarget(mFileHandle, false); |
117 | 0 | } |
118 | | |
119 | | // virtual |
120 | | JSObject* |
121 | | IDBFileRequest::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
122 | 0 | { |
123 | 0 | AssertIsOnOwningThread(); |
124 | 0 |
|
125 | 0 | if (mWrapAsDOMRequest) { |
126 | 0 | return DOMRequest::WrapObject(aCx, aGivenProto); |
127 | 0 | } |
128 | 0 | return IDBFileRequest_Binding::Wrap(aCx, this, aGivenProto); |
129 | 0 | } |
130 | | |
131 | | } // namespace dom |
132 | | } // namespace mozilla |