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.
4
5#ifndef CONTENT_BROWSER_DEVTOOLS_EMBEDDED_WORKER_DEVTOOLS_MANAGER_H_
6#define CONTENT_BROWSER_DEVTOOLS_EMBEDDED_WORKER_DEVTOOLS_MANAGER_H_
7
8#include <map>
9
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"
18
19namespace content {
20
21class DevToolsAgentHost;
22class DevToolsAgentHostImpl;
23class EmbeddedWorkerDevToolsAgentHost;
24class ServiceWorkerContextCore;
25
26// EmbeddedWorkerDevToolsManager is used instead of WorkerDevToolsManager when
27// "enable-embedded-shared-worker" flag is set.
28// This class lives on UI thread.
29class CONTENT_EXPORT EmbeddedWorkerDevToolsManager {
30 public:
31  typedef std::pair<int, int> WorkerId;
32
33  class ServiceWorkerIdentifier {
34   public:
35    ServiceWorkerIdentifier(
36        const ServiceWorkerContextCore* context,
37        base::WeakPtr<ServiceWorkerContextCore> context_weak,
38        int64 version_id,
39        const GURL& url);
40    ServiceWorkerIdentifier(const ServiceWorkerIdentifier& other);
41    ~ServiceWorkerIdentifier();
42
43    bool Matches(const ServiceWorkerIdentifier& other) const;
44
45    const ServiceWorkerContextCore* context() const;
46    base::WeakPtr<ServiceWorkerContextCore> context_weak() const;
47    int64 version_id() const;
48    GURL url() const;
49
50   private:
51    const ServiceWorkerContextCore* const context_;
52    const base::WeakPtr<ServiceWorkerContextCore> context_weak_;
53    const int64 version_id_;
54    const GURL url_;
55  };
56
57  // Returns the EmbeddedWorkerDevToolsManager singleton.
58  static EmbeddedWorkerDevToolsManager* GetInstance();
59
60  DevToolsAgentHostImpl* GetDevToolsAgentHostForWorker(int worker_process_id,
61                                                   int worker_route_id);
62
63  std::vector<scoped_refptr<DevToolsAgentHost> > GetOrCreateAllAgentHosts();
64
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,
68                           int worker_route_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,
74                            int worker_route_id,
75                            const ServiceWorkerIdentifier& service_worker_id);
76  void WorkerReadyForInspection(int worker_process_id, int worker_route_id);
77  void WorkerContextStarted(int worker_process_id, int worker_route_id);
78  void WorkerDestroyed(int worker_process_id, int worker_route_id);
79
80  void set_debug_service_worker_on_start(bool debug_on_start) {
81    debug_service_worker_on_start_ = debug_on_start;
82  }
83  bool debug_service_worker_on_start() const {
84    return debug_service_worker_on_start_;
85  }
86
87 private:
88  friend struct DefaultSingletonTraits<EmbeddedWorkerDevToolsManager>;
89  friend class EmbeddedWorkerDevToolsAgentHost;
90  friend class EmbeddedWorkerDevToolsManagerTest;
91  FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerDevToolsManagerTest, BasicTest);
92  FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerDevToolsManagerTest, AttachTest);
93
94  typedef std::map<WorkerId, EmbeddedWorkerDevToolsAgentHost*> AgentHostMap;
95
96  EmbeddedWorkerDevToolsManager();
97  virtual ~EmbeddedWorkerDevToolsManager();
98
99  void RemoveInspectedWorkerData(WorkerId id);
100
101  AgentHostMap::iterator FindExistingSharedWorkerAgentHost(
102      const SharedWorkerInstance& instance);
103  AgentHostMap::iterator FindExistingServiceWorkerAgentHost(
104      const ServiceWorkerIdentifier& service_worker_id);
105
106  void WorkerRestarted(const WorkerId& id, const AgentHostMap::iterator& it);
107
108  // Resets to its initial state as if newly created.
109  void ResetForTesting();
110
111  AgentHostMap workers_;
112
113  bool debug_service_worker_on_start_;
114
115  DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerDevToolsManager);
116};
117
118}  // namespace content
119
120#endif  // CONTENT_BROWSER_DEVTOOLS_EMBEDDED_WORKER_DEVTOOLS_MANAGER_H_
121