Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / devtools / service_worker_devtools_manager.h
blob2c8e997ea9f355eacdcb07a98c4613adb27b34f9
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_
8 #include <map>
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"
16 namespace content {
18 class BrowserContext;
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 {
26 public:
27 using WorkerId = std::pair<int, int>;
28 using AgentList = std::vector<scoped_refptr<ServiceWorkerDevToolsAgentHost>>;
30 class Observer {
31 public:
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) {}
38 protected:
39 virtual ~Observer() {}
42 class ServiceWorkerIdentifier {
43 public:
44 ServiceWorkerIdentifier(
45 const ServiceWorkerContextCore* context,
46 base::WeakPtr<ServiceWorkerContextCore> context_weak,
47 int64 version_id,
48 const GURL& url);
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 {
56 return context_weak_;
58 int64 version_id() const { return version_id_; }
59 GURL url() const { return url_; }
61 private:
62 const ServiceWorkerContextCore* const context_;
63 const base::WeakPtr<ServiceWorkerContextCore> context_weak_;
64 const int64 version_id_;
65 const GURL url_;
68 // Returns the ServiceWorkerDevToolsManager singleton.
69 static ServiceWorkerDevToolsManager* GetInstance();
71 DevToolsAgentHostImpl* GetDevToolsAgentHostForWorker(int worker_process_id,
72 int worker_route_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,
83 int worker_route_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_;
98 private:
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_