|
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 #ifndef mozilla_dom_domrequest_h__ |
|
8 #define mozilla_dom_domrequest_h__ |
|
9 |
|
10 #include "nsIDOMDOMRequest.h" |
|
11 #include "mozilla/Attributes.h" |
|
12 #include "mozilla/DOMEventTargetHelper.h" |
|
13 #include "mozilla/dom/DOMRequestBinding.h" |
|
14 |
|
15 #include "nsCOMPtr.h" |
|
16 |
|
17 namespace mozilla { |
|
18 namespace dom { |
|
19 |
|
20 class DOMRequest : public DOMEventTargetHelper, |
|
21 public nsIDOMDOMRequest |
|
22 { |
|
23 protected: |
|
24 JS::Heap<JS::Value> mResult; |
|
25 nsCOMPtr<nsISupports> mError; |
|
26 bool mDone; |
|
27 |
|
28 public: |
|
29 NS_DECL_ISUPPORTS_INHERITED |
|
30 NS_DECL_NSIDOMDOMREQUEST |
|
31 NS_REALLY_FORWARD_NSIDOMEVENTTARGET(DOMEventTargetHelper) |
|
32 |
|
33 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(DOMRequest, |
|
34 DOMEventTargetHelper) |
|
35 |
|
36 // WrapperCache |
|
37 nsPIDOMWindow* GetParentObject() const |
|
38 { |
|
39 return GetOwner(); |
|
40 } |
|
41 |
|
42 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
43 |
|
44 // WebIDL Interface |
|
45 DOMRequestReadyState ReadyState() const |
|
46 { |
|
47 return mDone ? DOMRequestReadyState::Done |
|
48 : DOMRequestReadyState::Pending; |
|
49 } |
|
50 |
|
51 void GetResult(JSContext*, JS::MutableHandle<JS::Value> aRetval) const |
|
52 { |
|
53 NS_ASSERTION(mDone || mResult == JSVAL_VOID, |
|
54 "Result should be undefined when pending"); |
|
55 JS::ExposeValueToActiveJS(mResult); |
|
56 aRetval.set(mResult); |
|
57 } |
|
58 |
|
59 nsISupports* GetError() const |
|
60 { |
|
61 NS_ASSERTION(mDone || !mError, |
|
62 "Error should be null when pending"); |
|
63 return mError; |
|
64 } |
|
65 |
|
66 IMPL_EVENT_HANDLER(success) |
|
67 IMPL_EVENT_HANDLER(error) |
|
68 |
|
69 |
|
70 void FireSuccess(JS::Handle<JS::Value> aResult); |
|
71 void FireError(const nsAString& aError); |
|
72 void FireError(nsresult aError); |
|
73 void FireDetailedError(nsISupports* aError); |
|
74 |
|
75 DOMRequest(nsPIDOMWindow* aWindow); |
|
76 |
|
77 virtual ~DOMRequest() |
|
78 { |
|
79 mResult = JSVAL_VOID; |
|
80 mozilla::DropJSObjects(this); |
|
81 } |
|
82 |
|
83 protected: |
|
84 void FireEvent(const nsAString& aType, bool aBubble, bool aCancelable); |
|
85 |
|
86 void RootResultVal(); |
|
87 }; |
|
88 |
|
89 class DOMRequestService MOZ_FINAL : public nsIDOMRequestService |
|
90 { |
|
91 public: |
|
92 NS_DECL_ISUPPORTS |
|
93 NS_DECL_NSIDOMREQUESTSERVICE |
|
94 |
|
95 // Returns an owning reference! No one should call this but the factory. |
|
96 static DOMRequestService* FactoryCreate() |
|
97 { |
|
98 DOMRequestService* res = new DOMRequestService; |
|
99 NS_ADDREF(res); |
|
100 return res; |
|
101 } |
|
102 }; |
|
103 |
|
104 } // namespace dom |
|
105 } // namespace mozilla |
|
106 |
|
107 #define DOMREQUEST_SERVICE_CONTRACTID "@mozilla.org/dom/dom-request-service;1" |
|
108 |
|
109 #endif // mozilla_dom_domrequest_h__ |