Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
7 #include "FileRequest.h"
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"
18 using namespace mozilla;
20 USING_FILE_NAMESPACE
22 FileRequest::FileRequest(nsPIDOMWindow* aWindow)
23 : DOMRequest(aWindow), mWrapAsDOMRequest(false)
24 {
25 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
26 }
28 FileRequest::~FileRequest()
29 {
30 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
31 }
33 // static
34 already_AddRefed<FileRequest>
35 FileRequest::Create(nsPIDOMWindow* aOwner, LockedFile* aLockedFile,
36 bool aWrapAsDOMRequest)
37 {
38 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
40 nsRefPtr<FileRequest> request = new FileRequest(aOwner);
41 request->mLockedFile = aLockedFile;
42 request->mWrapAsDOMRequest = aWrapAsDOMRequest;
44 return request.forget();
45 }
47 nsresult
48 FileRequest::PreHandleEvent(EventChainPreVisitor& aVisitor)
49 {
50 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
52 aVisitor.mCanHandle = true;
53 aVisitor.mParentTarget = mLockedFile;
54 return NS_OK;
55 }
57 nsresult
58 FileRequest::NotifyHelperCompleted(FileHelper* aFileHelper)
59 {
60 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
62 nsresult rv = aFileHelper->mResultCode;
64 // If the request failed then fire error event and return.
65 if (NS_FAILED(rv)) {
66 FireError(rv);
67 return NS_OK;
68 }
70 // Otherwise we need to get the result from the helper.
71 nsIScriptContext* sc = GetContextForEventHandlers(&rv);
72 NS_ENSURE_STATE(sc);
74 AutoJSContext cx;
75 NS_ASSERTION(cx, "Failed to get a context!");
77 JS::Rooted<JS::Value> result(cx);
79 JS::Rooted<JSObject*> global(cx, sc->GetWindowProxy());
80 NS_ASSERTION(global, "Failed to get global object!");
82 JSAutoCompartment ac(cx, global);
84 rv = aFileHelper->GetSuccessResult(cx, &result);
85 if (NS_FAILED(rv)) {
86 NS_WARNING("GetSuccessResult failed!");
87 }
89 if (NS_SUCCEEDED(rv)) {
90 FireSuccess(result);
91 }
92 else {
93 FireError(rv);
94 }
96 return NS_OK;
97 }
99 NS_IMPL_CYCLE_COLLECTION_INHERITED(FileRequest, DOMRequest,
100 mLockedFile)
102 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FileRequest)
103 NS_INTERFACE_MAP_END_INHERITING(DOMRequest)
105 NS_IMPL_ADDREF_INHERITED(FileRequest, DOMRequest)
106 NS_IMPL_RELEASE_INHERITED(FileRequest, DOMRequest)
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 }
118 LockedFile*
119 FileRequest::GetLockedFile() const
120 {
121 MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
122 return mLockedFile;
123 }
125 void
126 FileRequest::FireProgressEvent(uint64_t aLoaded, uint64_t aTotal)
127 {
128 if (NS_FAILED(CheckInnerWindowCorrectness())) {
129 return;
130 }
132 nsCOMPtr<nsIDOMEvent> event;
133 nsresult rv = NS_NewDOMProgressEvent(getter_AddRefs(event), this,
134 nullptr, nullptr);
135 if (NS_FAILED(rv)) {
136 return;
137 }
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 }
147 DispatchTrustedEvent(event);
148 }