Line data Source code
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 : #include "ServiceWorker.h"
8 :
9 : #include "nsIDocument.h"
10 : #include "nsPIDOMWindow.h"
11 : #include "ServiceWorkerClient.h"
12 : #include "ServiceWorkerManager.h"
13 : #include "ServiceWorkerPrivate.h"
14 : #include "WorkerPrivate.h"
15 :
16 : #include "mozilla/Preferences.h"
17 : #include "mozilla/dom/Promise.h"
18 : #include "mozilla/dom/ServiceWorkerGlobalScopeBinding.h"
19 :
20 : #ifdef XP_WIN
21 : #undef PostMessage
22 : #endif
23 :
24 : using mozilla::ErrorResult;
25 : using namespace mozilla::dom;
26 :
27 : namespace mozilla {
28 : namespace dom {
29 : namespace workers {
30 :
31 : bool
32 1 : ServiceWorkerVisible(JSContext* aCx, JSObject* aObj)
33 : {
34 1 : if (NS_IsMainThread()) {
35 0 : return Preferences::GetBool("dom.serviceWorkers.enabled", false);
36 : }
37 :
38 1 : return IS_INSTANCE_OF(ServiceWorkerGlobalScope, aObj);
39 : }
40 :
41 0 : ServiceWorker::ServiceWorker(nsPIDOMWindowInner* aWindow,
42 0 : ServiceWorkerInfo* aInfo)
43 : : DOMEventTargetHelper(aWindow),
44 0 : mInfo(aInfo)
45 : {
46 0 : AssertIsOnMainThread();
47 0 : MOZ_ASSERT(aInfo);
48 :
49 : // This will update our state too.
50 0 : mInfo->AppendWorker(this);
51 0 : }
52 :
53 0 : ServiceWorker::~ServiceWorker()
54 : {
55 0 : AssertIsOnMainThread();
56 0 : mInfo->RemoveWorker(this);
57 0 : }
58 :
59 0 : NS_IMPL_ADDREF_INHERITED(ServiceWorker, DOMEventTargetHelper)
60 0 : NS_IMPL_RELEASE_INHERITED(ServiceWorker, DOMEventTargetHelper)
61 :
62 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ServiceWorker)
63 0 : NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
64 :
65 : JSObject*
66 0 : ServiceWorker::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
67 : {
68 0 : AssertIsOnMainThread();
69 :
70 0 : return ServiceWorkerBinding::Wrap(aCx, this, aGivenProto);
71 : }
72 :
73 : void
74 0 : ServiceWorker::GetScriptURL(nsString& aURL) const
75 : {
76 0 : CopyUTF8toUTF16(mInfo->ScriptSpec(), aURL);
77 0 : }
78 :
79 : void
80 0 : ServiceWorker::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
81 : const Sequence<JSObject*>& aTransferable,
82 : ErrorResult& aRv)
83 : {
84 0 : if (State() == ServiceWorkerState::Redundant) {
85 0 : aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
86 0 : return;
87 : }
88 :
89 0 : nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(GetParentObject());
90 0 : if (!window || !window->GetExtantDoc()) {
91 0 : NS_WARNING("Trying to call post message from an invalid dom object.");
92 0 : aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
93 0 : return;
94 : }
95 :
96 0 : UniquePtr<ServiceWorkerClientInfo> clientInfo(new ServiceWorkerClientInfo(window->GetExtantDoc()));
97 0 : ServiceWorkerPrivate* workerPrivate = mInfo->WorkerPrivate();
98 0 : aRv = workerPrivate->SendMessageEvent(aCx, aMessage, aTransferable, Move(clientInfo));
99 : }
100 :
101 : } // namespace workers
102 : } // namespace dom
103 : } // namespace mozilla
|