1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_MAIN_RESOURCE_HANDLE_CORE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_MAIN_RESOURCE_HANDLE_CORE_H_
7 
8 #include <memory>
9 #include <utility>
10 
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/browser/service_worker/service_worker_container_host.h"
15 #include "content/browser/service_worker/service_worker_controllee_request_handler.h"
16 #include "content/common/content_export.h"
17 #include "services/network/public/mojom/network_context.mojom.h"
18 
19 namespace network {
20 struct CrossOriginEmbedderPolicy;
21 }
22 
23 namespace content {
24 
25 class ServiceWorkerContextWrapper;
26 class ServiceWorkerMainResourceHandle;
27 
28 // This class is created on the UI thread, but should only be accessed from the
29 // service worker core thread afterwards. It is the core thread pendant of
30 // ServiceWorkerMainResourceHandle. See the ServiceWorkerMainResourceHandle
31 // header for more details about the lifetime of both classes.
32 //
33 // TODO(crbug.com/824858): Merge this class into ServiceWorkerMainResourceHandle
34 // when the core thread moves to the UI thread.
35 class CONTENT_EXPORT ServiceWorkerMainResourceHandleCore {
36  public:
37   ServiceWorkerMainResourceHandleCore(
38       base::WeakPtr<ServiceWorkerMainResourceHandle> ui_handle,
39       ServiceWorkerContextWrapper* context_wrapper);
40   ~ServiceWorkerMainResourceHandleCore();
41 
42   // Called by corresponding methods in ServiceWorkerMainResourceHandle. See
43   // comments in the header of ServiceWorkerMainResourceHandle for details.
44   void OnBeginNavigationCommit(
45       int render_process_id,
46       int render_frame_id,
47       const network::CrossOriginEmbedderPolicy& cross_origin_embedder_policy,
48       mojo::PendingRemote<network::mojom::CrossOriginEmbedderPolicyReporter>
49           coep_reporter);
50   void OnBeginWorkerCommit(
51       const network::CrossOriginEmbedderPolicy& cross_origin_embedder_policy);
52 
context_wrapper()53   ServiceWorkerContextWrapper* context_wrapper() const {
54     return context_wrapper_.get();
55   }
56 
set_container_host(base::WeakPtr<ServiceWorkerContainerHost> container_host)57   void set_container_host(
58       base::WeakPtr<ServiceWorkerContainerHost> container_host) {
59     container_host_ = std::move(container_host);
60   }
61 
container_host()62   base::WeakPtr<ServiceWorkerContainerHost> container_host() {
63     return container_host_;
64   }
65 
set_interceptor(std::unique_ptr<ServiceWorkerControlleeRequestHandler> interceptor)66   void set_interceptor(
67       std::unique_ptr<ServiceWorkerControlleeRequestHandler> interceptor) {
68     interceptor_ = std::move(interceptor);
69   }
70 
interceptor()71   ServiceWorkerControlleeRequestHandler* interceptor() {
72     return interceptor_.get();
73   }
74 
AsWeakPtr()75   base::WeakPtr<ServiceWorkerMainResourceHandleCore> AsWeakPtr() {
76     return weak_factory_.GetWeakPtr();
77   }
78 
79  private:
80   scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_;
81   base::WeakPtr<ServiceWorkerMainResourceHandle> ui_handle_;
82   base::WeakPtr<ServiceWorkerContainerHost> container_host_;
83   std::unique_ptr<ServiceWorkerControlleeRequestHandler> interceptor_;
84   base::WeakPtrFactory<ServiceWorkerMainResourceHandleCore> weak_factory_{this};
85 
86   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerMainResourceHandleCore);
87 };
88 
89 }  // namespace content
90 
91 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_MAIN_RESOURCE_HANDLE_CORE_H_
92