dom/file/FileRequest.cpp

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:dac27c500d95
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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 "FileRequest.h"
8
9 #include "mozilla/EventDispatcher.h"
10 #include "mozilla/dom/FileRequestBinding.h"
11 #include "nsCxPusher.h"
12 #include "nsError.h"
13 #include "nsIDOMProgressEvent.h"
14 #include "nsDOMClassInfoID.h"
15 #include "FileHelper.h"
16 #include "LockedFile.h"
17
18 using namespace mozilla;
19
20 USING_FILE_NAMESPACE
21
22 FileRequest::FileRequest(nsPIDOMWindow* aWindow)
23 : DOMRequest(aWindow), mWrapAsDOMRequest(false)
24 {
25 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
26 }
27
28 FileRequest::~FileRequest()
29 {
30 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
31 }
32
33 // static
34 already_AddRefed<FileRequest>
35 FileRequest::Create(nsPIDOMWindow* aOwner, LockedFile* aLockedFile,
36 bool aWrapAsDOMRequest)
37 {
38 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
39
40 nsRefPtr<FileRequest> request = new FileRequest(aOwner);
41 request->mLockedFile = aLockedFile;
42 request->mWrapAsDOMRequest = aWrapAsDOMRequest;
43
44 return request.forget();
45 }
46
47 nsresult
48 FileRequest::PreHandleEvent(EventChainPreVisitor& aVisitor)
49 {
50 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
51
52 aVisitor.mCanHandle = true;
53 aVisitor.mParentTarget = mLockedFile;
54 return NS_OK;
55 }
56
57 nsresult
58 FileRequest::NotifyHelperCompleted(FileHelper* aFileHelper)
59 {
60 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
61
62 nsresult rv = aFileHelper->mResultCode;
63
64 // If the request failed then fire error event and return.
65 if (NS_FAILED(rv)) {
66 FireError(rv);
67 return NS_OK;
68 }
69
70 // Otherwise we need to get the result from the helper.
71 nsIScriptContext* sc = GetContextForEventHandlers(&rv);
72 NS_ENSURE_STATE(sc);
73
74 AutoJSContext cx;
75 NS_ASSERTION(cx, "Failed to get a context!");
76
77 JS::Rooted<JS::Value> result(cx);
78
79 JS::Rooted<JSObject*> global(cx, sc->GetWindowProxy());
80 NS_ASSERTION(global, "Failed to get global object!");
81
82 JSAutoCompartment ac(cx, global);
83
84 rv = aFileHelper->GetSuccessResult(cx, &result);
85 if (NS_FAILED(rv)) {
86 NS_WARNING("GetSuccessResult failed!");
87 }
88
89 if (NS_SUCCEEDED(rv)) {
90 FireSuccess(result);
91 }
92 else {
93 FireError(rv);
94 }
95
96 return NS_OK;
97 }
98
99 NS_IMPL_CYCLE_COLLECTION_INHERITED(FileRequest, DOMRequest,
100 mLockedFile)
101
102 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileRequest)
103 NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
104
105 NS_IMPL_ADDREF_INHERITED(FileRequest, DOMRequest)
106 NS_IMPL_RELEASE_INHERITED(FileRequest, DOMRequest)
107
108 // virtual
109 JSObject*
110 FileRequest::WrapObject(JSContext* aCx)
111 {
112 if (mWrapAsDOMRequest) {
113 return DOMRequest::WrapObject(aCx);
114 }
115 return FileRequestBinding::Wrap(aCx, this);
116 }
117
118 LockedFile*
119 FileRequest::GetLockedFile() const
120 {
121 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
122 return mLockedFile;
123 }
124
125 void
126 FileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal)
127 {
128 if (NS_FAILED(CheckInnerWindowCorrectness())) {
129 return;
130 }
131
132 nsCOMPtr<nsIDOMEvent> event;
133 nsresult rv = NS_NewDOMProgressEvent(getter_AddRefs(event), this,
134 nullptr, nullptr);
135 if (NS_FAILED(rv)) {
136 return;
137 }
138
139 nsCOMPtr<nsIDOMProgressEvent> progress = do_QueryInterface(event);
140 MOZ_ASSERT(progress);
141 rv = progress->InitProgressEvent(NS_LITERAL_STRING("progress"), false, false,
142 false, aLoaded, aTotal);
143 if (NS_FAILED(rv)) {
144 return;
145 }
146
147 DispatchTrustedEvent(event);
148 }

mercurial