1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_SERVICE_WORKER_SERVICE_WORKER_H_
32 #define THIRD_PARTY_BLINK_RENDERER_MODULES_SERVICE_WORKER_SERVICE_WORKER_H_
33 
34 #include <memory>
35 #include "base/memory/scoped_refptr.h"
36 #include "third_party/blink/public/mojom/service_worker/service_worker_object.mojom-blink.h"
37 #include "third_party/blink/public/platform/modules/service_worker/web_service_worker_object_info.h"
38 #include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
39 #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
40 #include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h"
41 #include "third_party/blink/renderer/core/workers/abstract_worker.h"
42 #include "third_party/blink/renderer/modules/modules_export.h"
43 #include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_receiver.h"
44 #include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_remote.h"
45 #include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
46 
47 namespace blink {
48 
49 class PostMessageOptions;
50 class ScriptState;
51 
52 class MODULES_EXPORT ServiceWorker final
53     : public AbstractWorker,
54       public ActiveScriptWrappable<ServiceWorker>,
55       public mojom::blink::ServiceWorkerObject {
56   DEFINE_WRAPPERTYPEINFO();
57 
58  public:
59   static ServiceWorker* From(ExecutionContext*,
60                              mojom::blink::ServiceWorkerObjectInfoPtr);
61   // TODO(crbug.com/879019): Eventually we'll remove WebServiceWorkerObjectInfo
62   // and use the above From() everywhere instead of this one.
63   static ServiceWorker* From(ExecutionContext*, WebServiceWorkerObjectInfo);
64 
Create(ExecutionContext * context,WebServiceWorkerObjectInfo info)65   static ServiceWorker* Create(ExecutionContext* context,
66                                WebServiceWorkerObjectInfo info) {
67     ServiceWorker* worker =
68         MakeGarbageCollected<ServiceWorker>(context, std::move(info));
69     worker->UpdateStateIfNeeded();
70     return worker;
71   }
72 
73   ServiceWorker(ExecutionContext*, WebServiceWorkerObjectInfo);
74   ~ServiceWorker() override;
75   void Trace(Visitor*) const override;
76 
77   void postMessage(ScriptState*,
78                    const ScriptValue& message,
79                    HeapVector<ScriptValue>& transfer,
80                    ExceptionState&);
81   void postMessage(ScriptState*,
82                    const ScriptValue& message,
83                    const PostMessageOptions*,
84                    ExceptionState&);
85 
86   String scriptURL() const;
87   String state() const;
DEFINE_ATTRIBUTE_EVENT_LISTENER(statechange,kStatechange)88   DEFINE_ATTRIBUTE_EVENT_LISTENER(statechange, kStatechange)
89 
90   ServiceWorker* ToServiceWorker() override { return this; }
91 
92   // ScriptWrappable overrides.
93   bool HasPendingActivity() const final;
94 
95   // AbstractWorker overrides.
96   const AtomicString& InterfaceName() const override;
97 
98   // Implements mojom::blink::ServiceWorkerObject.
99   void StateChanged(mojom::blink::ServiceWorkerState new_state) override;
100 
101   ScriptPromise InternalsTerminate(ScriptState*);
102 
103  private:
104   // ExecutionContextLifecycleStateObserver overrides.
105   void ContextLifecycleStateChanged(mojom::FrameLifecycleState state) override;
106   void ContextDestroyed() override;
107 
108   bool was_stopped_;
109   const KURL url_;
110   mojom::blink::ServiceWorkerState state_;
111   // Both |host_| and |receiver_| are associated with
112   // content.mojom.ServiceWorkerContainer interface for a Document, and
113   // content.mojom.ServiceWorker interface for a ServiceWorkerGlobalScope.
114   //
115   // |host_| keeps the Mojo connection to the
116   // browser-side ServiceWorkerObjectHost, whose lifetime is bound
117   // to |host_| via the Mojo connection.
118   HeapMojoAssociatedRemote<mojom::blink::ServiceWorkerObjectHost,
119                            HeapMojoWrapperMode::kWithoutContextObserver>
120       host_;
121   // Receives messages from the content::ServiceWorkerObjectHost in the browser
122   // process.
123   HeapMojoAssociatedReceiver<mojom::blink::ServiceWorkerObject,
124                              ServiceWorker,
125                              HeapMojoWrapperMode::kWithoutContextObserver>
126       receiver_;
127 };
128 
129 }  // namespace blink
130 
131 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_SERVICE_WORKER_SERVICE_WORKER_H_
132