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_EMBEDDED_WORKER_DEVTOOLS_MANAGER_H_
6 #define CONTENT_BROWSER_DEVTOOLS_EMBEDDED_WORKER_DEVTOOLS_MANAGER_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/singleton.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string16.h"
16 #include "content/browser/shared_worker/shared_worker_instance.h"
17 #include "content/common/content_export.h"
21 class DevToolsAgentHost
;
22 class DevToolsAgentHostImpl
;
23 class EmbeddedWorkerDevToolsAgentHost
;
24 class ServiceWorkerContextCore
;
26 // EmbeddedWorkerDevToolsManager is used instead of WorkerDevToolsManager when
27 // "enable-embedded-shared-worker" flag is set.
28 // This class lives on UI thread.
29 class CONTENT_EXPORT EmbeddedWorkerDevToolsManager
{
31 typedef std::pair
<int, int> WorkerId
;
33 class ServiceWorkerIdentifier
{
35 ServiceWorkerIdentifier(
36 const ServiceWorkerContextCore
* context
,
37 base::WeakPtr
<ServiceWorkerContextCore
> context_weak
,
40 ServiceWorkerIdentifier(const ServiceWorkerIdentifier
& other
);
41 ~ServiceWorkerIdentifier();
43 bool Matches(const ServiceWorkerIdentifier
& other
) const;
45 const ServiceWorkerContextCore
* context() const;
46 base::WeakPtr
<ServiceWorkerContextCore
> context_weak() const;
47 int64
version_id() const;
51 const ServiceWorkerContextCore
* const context_
;
52 const base::WeakPtr
<ServiceWorkerContextCore
> context_weak_
;
53 const int64 version_id_
;
57 // Returns the EmbeddedWorkerDevToolsManager singleton.
58 static EmbeddedWorkerDevToolsManager
* GetInstance();
60 DevToolsAgentHostImpl
* GetDevToolsAgentHostForWorker(int worker_process_id
,
63 std::vector
<scoped_refptr
<DevToolsAgentHost
> > GetOrCreateAllAgentHosts();
65 // Returns true when the worker must be paused on start because a DevTool
66 // window for the same former SharedWorkerInstance is still opened.
67 bool SharedWorkerCreated(int worker_process_id
,
69 const SharedWorkerInstance
& instance
);
70 // Returns true when the worker must be paused on start because a DevTool
71 // window for the same former ServiceWorkerIdentifier is still opened or
72 // debug-on-start is enabled in chrome://serviceworker-internals.
73 bool ServiceWorkerCreated(int worker_process_id
,
75 const ServiceWorkerIdentifier
& service_worker_id
);
76 void WorkerReadyForInspection(int worker_process_id
, int worker_route_id
);
77 void WorkerDestroyed(int worker_process_id
, int worker_route_id
);
79 void set_debug_service_worker_on_start(bool debug_on_start
) {
80 debug_service_worker_on_start_
= debug_on_start
;
82 bool debug_service_worker_on_start() const {
83 return debug_service_worker_on_start_
;
87 friend struct DefaultSingletonTraits
<EmbeddedWorkerDevToolsManager
>;
88 friend class EmbeddedWorkerDevToolsAgentHost
;
89 friend class EmbeddedWorkerDevToolsManagerTest
;
90 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerDevToolsManagerTest
, BasicTest
);
91 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerDevToolsManagerTest
, AttachTest
);
93 typedef std::map
<WorkerId
, EmbeddedWorkerDevToolsAgentHost
*> AgentHostMap
;
95 EmbeddedWorkerDevToolsManager();
96 virtual ~EmbeddedWorkerDevToolsManager();
98 void RemoveInspectedWorkerData(WorkerId id
);
100 AgentHostMap::iterator
FindExistingSharedWorkerAgentHost(
101 const SharedWorkerInstance
& instance
);
102 AgentHostMap::iterator
FindExistingServiceWorkerAgentHost(
103 const ServiceWorkerIdentifier
& service_worker_id
);
105 void WorkerRestarted(const WorkerId
& id
, const AgentHostMap::iterator
& it
);
107 // Resets to its initial state as if newly created.
108 void ResetForTesting();
110 AgentHostMap workers_
;
112 bool debug_service_worker_on_start_
;
114 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerDevToolsManager
);
117 } // namespace content
119 #endif // CONTENT_BROWSER_DEVTOOLS_EMBEDDED_WORKER_DEVTOOLS_MANAGER_H_