summaryrefslogtreecommitdiffstats
path: root/content/browser/devtools/embedded_worker_devtools_manager.cc
blob: fb583554278d536244e154bc8b51eedbff7c9b0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/devtools/embedded_worker_devtools_manager.h"

#include "content/browser/devtools/devtools_manager_impl.h"
#include "content/browser/devtools/devtools_protocol.h"
#include "content/browser/devtools/devtools_protocol_constants.h"
#include "content/browser/devtools/ipc_devtools_agent_host.h"
#include "content/browser/shared_worker/shared_worker_instance.h"
#include "content/common/devtools_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/worker_service.h"
#include "ipc/ipc_listener.h"

namespace content {

namespace {

bool SendMessageToWorker(
    const EmbeddedWorkerDevToolsManager::WorkerId& worker_id,
    IPC::Message* message) {
  RenderProcessHost* host = RenderProcessHost::FromID(worker_id.first);
  if (!host) {
    delete message;
    return false;
  }
  message->set_routing_id(worker_id.second);
  host->Send(message);
  return true;
}

}  // namespace

EmbeddedWorkerDevToolsManager::WorkerInfo::WorkerInfo(
    const SharedWorkerInstance& instance)
    : shared_worker_instance_(new SharedWorkerInstance(instance)),
      state_(WORKER_UNINSPECTED),
      agent_host_(NULL) {
}

EmbeddedWorkerDevToolsManager::WorkerInfo::WorkerInfo(
    const base::FilePath& storage_partition_path,
    const GURL& service_worker_scope)
    : storage_partition_path_(new base::FilePath(storage_partition_path)),
      service_worker_scope_(new GURL(service_worker_scope)),
      state_(WORKER_UNINSPECTED),
      agent_host_(NULL) {
}

bool EmbeddedWorkerDevToolsManager::WorkerInfo::Matches(
    const SharedWorkerInstance& other) {
  if (!shared_worker_instance_)
    return false;
  return shared_worker_instance_->Matches(other);
}

bool EmbeddedWorkerDevToolsManager::WorkerInfo::Matches(
    const base::FilePath& other_storage_partition_path,
    const GURL& other_service_worker_scope) {
  if (!storage_partition_path_ || !service_worker_scope_)
    return false;
  return *storage_partition_path_ == other_storage_partition_path &&
         *service_worker_scope_ == other_service_worker_scope;
}

EmbeddedWorkerDevToolsManager::WorkerInfo::~WorkerInfo() {
}

class EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsAgentHost
    : public IPCDevToolsAgentHost,
      public IPC::Listener {
 public:
  explicit EmbeddedWorkerDevToolsAgentHost(WorkerId worker_id)
      : worker_id_(worker_id), worker_attached_(true) {
    AddRef();
    if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first))
      host->AddRoute(worker_id_.second, this);
  }

  // IPCDevToolsAgentHost implementation.
  virtual void SendMessageToAgent(IPC::Message* message) OVERRIDE {
    if (worker_attached_)
      SendMessageToWorker(worker_id_, message);
    else
      delete message;
  }
  virtual void OnClientAttached() OVERRIDE {}
  virtual void OnClientDetached() OVERRIDE {}

  // IPC::Listener implementation.
  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    bool handled = true;
    IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerDevToolsAgentHost, msg)
    IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend,
                        OnDispatchOnInspectorFrontend)
    IPC_MESSAGE_HANDLER(DevToolsHostMsg_SaveAgentRuntimeState,
                        OnSaveAgentRuntimeState)
    IPC_MESSAGE_UNHANDLED(handled = false)
    IPC_END_MESSAGE_MAP()
    return handled;
  }

  void ReattachToWorker(WorkerId worker_id) {
    CHECK(!worker_attached_);
    worker_attached_ = true;
    worker_id_ = worker_id;
    AddRef();
    if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first))
      host->AddRoute(worker_id_.second, this);
    Reattach(state_);
  }

  void DetachFromWorker() {
    CHECK(worker_attached_);
    worker_attached_ = false;
    if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first))
      host->RemoveRoute(worker_id_.second);
    Release();
  }

  WorkerId worker_id() const { return worker_id_; }

 private:
  virtual ~EmbeddedWorkerDevToolsAgentHost() {
    CHECK(!worker_attached_);
    EmbeddedWorkerDevToolsManager::GetInstance()->RemoveInspectedWorkerData(
        this);
  }

  void OnDispatchOnInspectorFrontend(const std::string& message) {
    DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend(this,
                                                                    message);
  }

  void OnSaveAgentRuntimeState(const std::string& state) { state_ = state; }

  WorkerId worker_id_;
  bool worker_attached_;
  std::string state_;
  DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerDevToolsAgentHost);
};

// static
EmbeddedWorkerDevToolsManager* EmbeddedWorkerDevToolsManager::GetInstance() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  return Singleton<EmbeddedWorkerDevToolsManager>::get();
}

DevToolsAgentHost* EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForWorker(
    int worker_process_id,
    int worker_route_id) {
  WorkerId id(worker_process_id, worker_route_id);

  WorkerInfoMap::iterator it = workers_.find(id);
  if (it == workers_.end())
    return NULL;

  WorkerInfo* info = it->second;
  if (info->state() != WORKER_UNINSPECTED)
    return info->agent_host();

  EmbeddedWorkerDevToolsAgentHost* agent_host =
      new EmbeddedWorkerDevToolsAgentHost(id);
  info->set_agent_host(agent_host);
  info->set_state(WORKER_INSPECTED);
  return agent_host;
}

DevToolsAgentHost*
EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForServiceWorker(
    const base::FilePath& storage_partition_path,
    const GURL& service_worker_scope) {
  WorkerInfoMap::iterator it = FindExistingServiceWorkerInfo(
      storage_partition_path, service_worker_scope);
  if (it == workers_.end())
    return NULL;
  return GetDevToolsAgentHostForWorker(it->first.first, it->first.second);
}

EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsManager() {
}

EmbeddedWorkerDevToolsManager::~EmbeddedWorkerDevToolsManager() {
}

bool EmbeddedWorkerDevToolsManager::SharedWorkerCreated(
    int worker_process_id,
    int worker_route_id,
    const SharedWorkerInstance& instance) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  const WorkerId id(worker_process_id, worker_route_id);
  WorkerInfoMap::iterator it = FindExistingSharedWorkerInfo(instance);
  if (it == workers_.end()) {
    scoped_ptr<WorkerInfo> info(new WorkerInfo(instance));
    workers_.set(id, info.Pass());
    return false;
  }
  MoveToPausedState(id, it);
  return true;
}

bool EmbeddedWorkerDevToolsManager::ServiceWorkerCreated(
    int worker_process_id,
    int worker_route_id,
    const base::FilePath& storage_partition_path,
    const GURL& service_worker_scope) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  const WorkerId id(worker_process_id, worker_route_id);
  WorkerInfoMap::iterator it = FindExistingServiceWorkerInfo(
      storage_partition_path, service_worker_scope);
  if (it == workers_.end()) {
    scoped_ptr<WorkerInfo> info(
        new WorkerInfo(storage_partition_path, service_worker_scope));
    workers_.set(id, info.Pass());
    return false;
  }
  MoveToPausedState(id, it);
  return true;
}

void EmbeddedWorkerDevToolsManager::WorkerDestroyed(int worker_process_id,
                                                    int worker_route_id) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  const WorkerId id(worker_process_id, worker_route_id);
  WorkerInfoMap::iterator it = workers_.find(id);
  DCHECK(it != workers_.end());
  WorkerInfo* info = it->second;
  switch (info->state()) {
    case WORKER_UNINSPECTED:
      workers_.erase(it);
      break;
    case WORKER_INSPECTED: {
      EmbeddedWorkerDevToolsAgentHost* agent_host = info->agent_host();
      if (!agent_host->IsAttached()) {
        scoped_ptr<WorkerInfo> worker_info = workers_.take_and_erase(it);
        agent_host->DetachFromWorker();
        return;
      }
      info->set_state(WORKER_TERMINATED);
      // Client host is debugging this worker agent host.
      std::string notification =
          DevToolsProtocol::CreateNotification(
              devtools::Worker::disconnectedFromWorker::kName, NULL)
              ->Serialize();
      DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend(
          agent_host, notification);
      agent_host->DetachFromWorker();
      break;
    }
    case WORKER_TERMINATED:
      NOTREACHED();
      break;
    case WORKER_PAUSED: {
      scoped_ptr<WorkerInfo> worker_info = workers_.take_and_erase(it);
      worker_info->set_state(WORKER_TERMINATED);
      const WorkerId old_id = worker_info->agent_host()->worker_id();
      workers_.set(old_id, worker_info.Pass());
      break;
    }
  }
}

void EmbeddedWorkerDevToolsManager::WorkerContextStarted(int worker_process_id,
                                                         int worker_route_id) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  const WorkerId id(worker_process_id, worker_route_id);
  WorkerInfoMap::iterator it = workers_.find(id);
  DCHECK(it != workers_.end());
  WorkerInfo* info = it->second;
  if (info->state() != WORKER_PAUSED)
    return;
  info->agent_host()->ReattachToWorker(id);
  info->set_state(WORKER_INSPECTED);
}

void EmbeddedWorkerDevToolsManager::RemoveInspectedWorkerData(
    EmbeddedWorkerDevToolsAgentHost* agent_host) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  const WorkerId id(agent_host->worker_id());
  scoped_ptr<WorkerInfo> worker_info = workers_.take_and_erase(id);
  if (worker_info) {
    DCHECK_EQ(WORKER_TERMINATED, worker_info->state());
    return;
  }
  for (WorkerInfoMap::iterator it = workers_.begin(); it != workers_.end();
       ++it) {
    if (it->second->agent_host() == agent_host) {
      DCHECK_EQ(WORKER_PAUSED, it->second->state());
      SendMessageToWorker(
          it->first,
          new DevToolsAgentMsg_ResumeWorkerContext(it->first.second));
      it->second->set_agent_host(NULL);
      it->second->set_state(WORKER_UNINSPECTED);
      return;
    }
  }
}

EmbeddedWorkerDevToolsManager::WorkerInfoMap::iterator
EmbeddedWorkerDevToolsManager::FindExistingSharedWorkerInfo(
    const SharedWorkerInstance& instance) {
  WorkerInfoMap::iterator it = workers_.begin();
  for (; it != workers_.end(); ++it) {
    if (it->second->Matches(instance))
      break;
  }
  return it;
}

EmbeddedWorkerDevToolsManager::WorkerInfoMap::iterator
EmbeddedWorkerDevToolsManager::FindExistingServiceWorkerInfo(
    const base::FilePath& storage_partition_path,
    const GURL& service_worker_scope) {
  WorkerInfoMap::iterator it = workers_.begin();
  for (; it != workers_.end(); ++it) {
    if (it->second->Matches(storage_partition_path, service_worker_scope))
      break;
  }
  return it;
}

void EmbeddedWorkerDevToolsManager::MoveToPausedState(
    const WorkerId& id,
    const WorkerInfoMap::iterator& it) {
  DCHECK_EQ(WORKER_TERMINATED, it->second->state());
  scoped_ptr<WorkerInfo> info = workers_.take_and_erase(it);
  info->set_state(WORKER_PAUSED);
  workers_.set(id, info.Pass());
}

void EmbeddedWorkerDevToolsManager::ResetForTesting() {
  workers_.clear();
}

}  // namespace content