1 // Copyright 2014 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.
5 #ifndef CONTENT_BROWSER_DEVTOOLS_SERVICE_WORKER_DEVTOOLS_MANAGER_H_
6 #define CONTENT_BROWSER_DEVTOOLS_SERVICE_WORKER_DEVTOOLS_MANAGER_H_
10 #include "base/basictypes.h"
11 #include "base/memory/singleton.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "content/public/browser/devtools_agent_host.h"
18 class DevToolsAgentHostImpl
;
19 class ServiceWorkerDevToolsAgentHost
;
20 class ServiceWorkerContextCore
;
22 // Manages WorkerDevToolsAgentHost's for Service Workers.
23 // This class lives on UI thread.
24 class CONTENT_EXPORT ServiceWorkerDevToolsManager
{
26 using WorkerId
= std::pair
<int, int>;
27 using AgentList
= std::vector
<scoped_refptr
<ServiceWorkerDevToolsAgentHost
>>;
31 virtual void WorkerCreated(ServiceWorkerDevToolsAgentHost
* host
) {}
32 virtual void WorkerReadyForInspection(
33 ServiceWorkerDevToolsAgentHost
* host
) {}
34 virtual void WorkerDestroyed(ServiceWorkerDevToolsAgentHost
* host
) {}
35 virtual void DebugOnStartUpdated(bool debug_on_start
) {}
38 virtual ~Observer() {}
41 class ServiceWorkerIdentifier
{
43 ServiceWorkerIdentifier(
44 const ServiceWorkerContextCore
* context
,
45 base::WeakPtr
<ServiceWorkerContextCore
> context_weak
,
48 ServiceWorkerIdentifier(const ServiceWorkerIdentifier
& other
);
49 ~ServiceWorkerIdentifier();
51 bool Matches(const ServiceWorkerIdentifier
& other
) const;
53 const ServiceWorkerContextCore
* context() const { return context_
; }
54 base::WeakPtr
<ServiceWorkerContextCore
> context_weak() const {
57 int64
version_id() const { return version_id_
; }
58 GURL
url() const { return url_
; }
61 const ServiceWorkerContextCore
* const context_
;
62 const base::WeakPtr
<ServiceWorkerContextCore
> context_weak_
;
63 const int64 version_id_
;
67 // Returns the ServiceWorkerDevToolsManager singleton.
68 static ServiceWorkerDevToolsManager
* GetInstance();
70 DevToolsAgentHostImpl
* GetDevToolsAgentHostForWorker(int worker_process_id
,
72 void AddAllAgentHosts(
73 std::vector
<scoped_refptr
<ServiceWorkerDevToolsAgentHost
>>* result
);
75 // Returns true when the worker must be paused on start because a DevTool
76 // window for the same former ServiceWorkerIdentifier is still opened or
77 // debug-on-start is enabled in chrome://serviceworker-internals.
78 bool WorkerCreated(int worker_process_id
,
80 const ServiceWorkerIdentifier
& service_worker_id
);
81 void WorkerReadyForInspection(int worker_process_id
, int worker_route_id
);
82 void WorkerStopIgnored(int worker_process_id
, int worker_route_id
);
83 void WorkerDestroyed(int worker_process_id
, int worker_route_id
);
84 void RemoveInspectedWorkerData(WorkerId id
);
86 void AddObserver(Observer
* observer
);
87 void RemoveObserver(Observer
* observer
);
89 void set_debug_service_worker_on_start(bool debug_on_start
);
90 bool debug_service_worker_on_start() const {
91 return debug_service_worker_on_start_
;
95 friend struct DefaultSingletonTraits
<ServiceWorkerDevToolsManager
>;
96 friend class ServiceWorkerDevToolsAgentHost
;
98 using AgentHostMap
= std::map
<WorkerId
, ServiceWorkerDevToolsAgentHost
*>;
100 ServiceWorkerDevToolsManager();
101 ~ServiceWorkerDevToolsManager();
103 AgentHostMap::iterator
FindExistingWorkerAgentHost(
104 const ServiceWorkerIdentifier
& service_worker_id
);
106 // Resets to its initial state as if newly created.
107 void ResetForTesting();
109 ObserverList
<Observer
> observer_list_
;
110 AgentHostMap workers_
;
111 bool debug_service_worker_on_start_
;
113 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDevToolsManager
);
116 } // namespace content
118 #endif // CONTENT_BROWSER_DEVTOOLS_SERVICE_WORKER_DEVTOOLS_MANAGER_H_