1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/base/DOMRequest.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,109 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla_dom_domrequest_h__ 1.11 +#define mozilla_dom_domrequest_h__ 1.12 + 1.13 +#include "nsIDOMDOMRequest.h" 1.14 +#include "mozilla/Attributes.h" 1.15 +#include "mozilla/DOMEventTargetHelper.h" 1.16 +#include "mozilla/dom/DOMRequestBinding.h" 1.17 + 1.18 +#include "nsCOMPtr.h" 1.19 + 1.20 +namespace mozilla { 1.21 +namespace dom { 1.22 + 1.23 +class DOMRequest : public DOMEventTargetHelper, 1.24 + public nsIDOMDOMRequest 1.25 +{ 1.26 +protected: 1.27 + JS::Heap<JS::Value> mResult; 1.28 + nsCOMPtr<nsISupports> mError; 1.29 + bool mDone; 1.30 + 1.31 +public: 1.32 + NS_DECL_ISUPPORTS_INHERITED 1.33 + NS_DECL_NSIDOMDOMREQUEST 1.34 + NS_REALLY_FORWARD_NSIDOMEVENTTARGET(DOMEventTargetHelper) 1.35 + 1.36 + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(DOMRequest, 1.37 + DOMEventTargetHelper) 1.38 + 1.39 + // WrapperCache 1.40 + nsPIDOMWindow* GetParentObject() const 1.41 + { 1.42 + return GetOwner(); 1.43 + } 1.44 + 1.45 + virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; 1.46 + 1.47 + // WebIDL Interface 1.48 + DOMRequestReadyState ReadyState() const 1.49 + { 1.50 + return mDone ? DOMRequestReadyState::Done 1.51 + : DOMRequestReadyState::Pending; 1.52 + } 1.53 + 1.54 + void GetResult(JSContext*, JS::MutableHandle<JS::Value> aRetval) const 1.55 + { 1.56 + NS_ASSERTION(mDone || mResult == JSVAL_VOID, 1.57 + "Result should be undefined when pending"); 1.58 + JS::ExposeValueToActiveJS(mResult); 1.59 + aRetval.set(mResult); 1.60 + } 1.61 + 1.62 + nsISupports* GetError() const 1.63 + { 1.64 + NS_ASSERTION(mDone || !mError, 1.65 + "Error should be null when pending"); 1.66 + return mError; 1.67 + } 1.68 + 1.69 + IMPL_EVENT_HANDLER(success) 1.70 + IMPL_EVENT_HANDLER(error) 1.71 + 1.72 + 1.73 + void FireSuccess(JS::Handle<JS::Value> aResult); 1.74 + void FireError(const nsAString& aError); 1.75 + void FireError(nsresult aError); 1.76 + void FireDetailedError(nsISupports* aError); 1.77 + 1.78 + DOMRequest(nsPIDOMWindow* aWindow); 1.79 + 1.80 + virtual ~DOMRequest() 1.81 + { 1.82 + mResult = JSVAL_VOID; 1.83 + mozilla::DropJSObjects(this); 1.84 + } 1.85 + 1.86 +protected: 1.87 + void FireEvent(const nsAString& aType, bool aBubble, bool aCancelable); 1.88 + 1.89 + void RootResultVal(); 1.90 +}; 1.91 + 1.92 +class DOMRequestService MOZ_FINAL : public nsIDOMRequestService 1.93 +{ 1.94 +public: 1.95 + NS_DECL_ISUPPORTS 1.96 + NS_DECL_NSIDOMREQUESTSERVICE 1.97 + 1.98 + // Returns an owning reference! No one should call this but the factory. 1.99 + static DOMRequestService* FactoryCreate() 1.100 + { 1.101 + DOMRequestService* res = new DOMRequestService; 1.102 + NS_ADDREF(res); 1.103 + return res; 1.104 + } 1.105 +}; 1.106 + 1.107 +} // namespace dom 1.108 +} // namespace mozilla 1.109 + 1.110 +#define DOMREQUEST_SERVICE_CONTRACTID "@mozilla.org/dom/dom-request-service;1" 1.111 + 1.112 +#endif // mozilla_dom_domrequest_h__