PushManager.h (4249B)
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 /** 8 * PushManager and PushSubscription are exposed on the main and worker threads. 9 * The main thread version is implemented in Push.js. The JS implementation 10 * makes it easier to use certain APIs like the permission prompt and Promises. 11 * 12 * Unfortunately, JS-implemented WebIDL is not supported off the main thread. 13 * To work around this, we use a chain of runnables to query the JS-implemented 14 * nsIPushService component for subscription information, and return the 15 * results to the worker. We don't have to deal with permission prompts, since 16 * we just reject calls if the principal does not have permission. 17 * 18 * On the main thread, PushManager wraps a JS-implemented PushManagerImpl 19 * instance. The C++ wrapper is necessary because our bindings code cannot 20 * accomodate "JS-implemented on the main thread, C++ on the worker" bindings. 21 * 22 * PushSubscription is in C++ on both threads since it isn't particularly 23 * verbose to implement in C++ compared to JS. 24 */ 25 26 #ifndef mozilla_dom_PushManager_h 27 #define mozilla_dom_PushManager_h 28 29 #include "mozilla/AlreadyAddRefed.h" 30 #include "mozilla/RefPtr.h" 31 #include "mozilla/dom/BindingDeclarations.h" 32 #include "mozilla/dom/TypedArray.h" 33 #include "nsCOMPtr.h" 34 #include "nsWrapperCache.h" 35 36 class nsIGlobalObject; 37 class nsIPrincipal; 38 class nsIPushSubscription; 39 40 namespace mozilla { 41 class ErrorResult; 42 43 namespace dom { 44 45 class OwningArrayBufferViewOrArrayBufferOrString; 46 class Promise; 47 class PushManagerImpl; 48 struct PushSubscriptionOptionsInit; 49 class WorkerPrivate; 50 51 nsresult GetSubscriptionParams(nsIPushSubscription* aSubscription, 52 nsAString& aEndpoint, 53 nsTArray<uint8_t>& aRawP256dhKey, 54 nsTArray<uint8_t>& aAuthSecret, 55 nsTArray<uint8_t>& aAppServerKey); 56 57 class PushManager final : public nsISupports, public nsWrapperCache { 58 public: 59 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 60 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(PushManager) 61 62 enum SubscriptionAction { 63 SubscribeAction, 64 GetSubscriptionAction, 65 }; 66 67 // The main thread constructor. 68 PushManager(nsIGlobalObject* aGlobal, PushManagerImpl* aImpl); 69 70 // The worker thread constructor. 71 explicit PushManager(const nsAString& aScope); 72 73 nsIGlobalObject* GetParentObject() const { return mGlobal; } 74 75 JSObject* WrapObject(JSContext* aCx, 76 JS::Handle<JSObject*> aGivenProto) override; 77 78 static already_AddRefed<PushManager> Constructor(GlobalObject& aGlobal, 79 const nsAString& aScope, 80 ErrorResult& aRv); 81 82 static bool IsEnabled(JSContext* aCx, JSObject* aGlobal); 83 84 already_AddRefed<Promise> PerformSubscriptionActionFromWorker( 85 SubscriptionAction aAction, ErrorResult& aRv); 86 87 already_AddRefed<Promise> PerformSubscriptionActionFromWorker( 88 SubscriptionAction aAction, const PushSubscriptionOptionsInit& aOptions, 89 ErrorResult& aRv); 90 91 // Web IDL members: 92 93 static void GetSupportedContentEncodings( 94 GlobalObject& aGlobal, JS::MutableHandle<JSObject*> aEncodings, 95 ErrorResult& aRv); 96 97 already_AddRefed<Promise> Subscribe( 98 const PushSubscriptionOptionsInit& aOptions, ErrorResult& aRv); 99 100 already_AddRefed<Promise> GetSubscription(ErrorResult& aRv); 101 102 already_AddRefed<Promise> PermissionState( 103 const PushSubscriptionOptionsInit& aOptions, ErrorResult& aRv); 104 105 private: 106 ~PushManager(); 107 108 nsresult NormalizeAppServerKey( 109 const OwningArrayBufferViewOrArrayBufferOrString& aSource, 110 nsTArray<uint8_t>& aAppServerKey); 111 112 // The following are only set and accessed on the main thread. 113 nsCOMPtr<nsIGlobalObject> mGlobal; 114 RefPtr<PushManagerImpl> mImpl; 115 116 // Only used on the worker thread. 117 nsString mScope; 118 }; 119 } // namespace dom 120 } // namespace mozilla 121 122 #endif // mozilla_dom_PushManager_h