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"
19 class DevToolsAgentHostImpl
;
20 class ServiceWorkerDevToolsAgentHost
;
21 class ServiceWorkerContextCore
;
23 // Manages WorkerDevToolsAgentHost's for Service Workers.
24 // This class lives on UI thread.
25 class CONTENT_EXPORT ServiceWorkerDevToolsManager
{
27 using WorkerId
= std::pair
<int, int>;
28 using AgentList
= std::vector
<scoped_refptr
<ServiceWorkerDevToolsAgentHost
>>;
32 virtual void WorkerCreated(ServiceWorkerDevToolsAgentHost
* host
) {}
33 virtual void WorkerReadyForInspection(
34 ServiceWorkerDevToolsAgentHost
* host
) {}
35 virtual void WorkerDestroyed(ServiceWorkerDevToolsAgentHost
* host
) {}
36 virtual void DebugOnStartUpdated(bool debug_on_start
) {}
39 virtual ~Observer() {}
42 class ServiceWorkerIdentifier
{
44 ServiceWorkerIdentifier(
45 const ServiceWorkerContextCore
* context
,
46 base::WeakPtr
<ServiceWorkerContextCore
> context_weak
,
49 ServiceWorkerIdentifier(const ServiceWorkerIdentifier
& other
);
50 ~ServiceWorkerIdentifier();
52 bool Matches(const ServiceWorkerIdentifier
& other
) const;
54 const ServiceWorkerContextCore
* context() const { return context_
; }
55 base::WeakPtr
<ServiceWorkerContextCore
> context_weak() const {
58 int64
version_id() const { return version_id_
; }
59 GURL
url() const { return url_
; }
62 const ServiceWorkerContextCore
* const context_
;
63 const base::WeakPtr
<ServiceWorkerContextCore
> context_weak_
;
64 const int64 version_id_
;
68 // Returns the ServiceWorkerDevToolsManager singleton.
69 static ServiceWorkerDevToolsManager
* GetInstance();
71 DevToolsAgentHostImpl
* GetDevToolsAgentHostForWorker(int worker_process_id
,
73 void AddAllAgentHosts(
74 std::vector
<scoped_refptr
<ServiceWorkerDevToolsAgentHost
>>* result
);
75 void AddAllAgentHostsForBrowserContext(
76 BrowserContext
* browser_context
,
77 std::vector
<scoped_refptr
<ServiceWorkerDevToolsAgentHost
>>* result
);
79 // Returns true when the worker must be paused on start because a DevTool
80 // window for the same former ServiceWorkerIdentifier is still opened or
81 // debug-on-start is enabled in chrome://serviceworker-internals.
82 bool WorkerCreated(int worker_process_id
,
84 const ServiceWorkerIdentifier
& service_worker_id
);
85 void WorkerReadyForInspection(int worker_process_id
, int worker_route_id
);
86 void WorkerStopIgnored(int worker_process_id
, int worker_route_id
);
87 void WorkerDestroyed(int worker_process_id
, int worker_route_id
);
88 void RemoveInspectedWorkerData(WorkerId id
);
90 void AddObserver(Observer
* observer
);
91 void RemoveObserver(Observer
* observer
);
93 void set_debug_service_worker_on_start(bool debug_on_start
);
94 bool debug_service_worker_on_start() const {
95 return debug_service_worker_on_start_
;
99 friend struct base::DefaultSingletonTraits
<ServiceWorkerDevToolsManager
>;
100 friend class ServiceWorkerDevToolsAgentHost
;
102 using AgentHostMap
= std::map
<WorkerId
, ServiceWorkerDevToolsAgentHost
*>;
104 ServiceWorkerDevToolsManager();
105 ~ServiceWorkerDevToolsManager();
107 AgentHostMap::iterator
FindExistingWorkerAgentHost(
108 const ServiceWorkerIdentifier
& service_worker_id
);
110 // Resets to its initial state as if newly created.
111 void ResetForTesting();
113 base::ObserverList
<Observer
> observer_list_
;
114 AgentHostMap workers_
;
115 bool debug_service_worker_on_start_
;
117 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDevToolsManager
);
120 } // namespace content
122 #endif // CONTENT_BROWSER_DEVTOOLS_SERVICE_WORKER_DEVTOOLS_MANAGER_H_