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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
// Copyright (c) 2009 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 "chrome/browser/debugger/devtools_manager.h"
#include <vector>
#include "base/auto_reset.h"
#include "base/message_loop.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browsing_instance.h"
#include "chrome/browser/child_process_security_policy.h"
#include "chrome/browser/debugger/devtools_client_host.h"
#include "chrome/browser/debugger/devtools_netlog_observer.h"
#include "chrome/browser/debugger/devtools_window.h"
#include "chrome/browser/io_thread.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/renderer_host/site_instance.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/pref_names.h"
#include "googleurl/src/gurl.h"
// static
DevToolsManager* DevToolsManager::GetInstance() {
// http://crbug.com/47806 this method may be called when BrowserProcess
// has already been destroyed.
if (!g_browser_process)
return NULL;
return g_browser_process->devtools_manager();
}
// static
void DevToolsManager::RegisterUserPrefs(PrefService* prefs) {
prefs->RegisterBooleanPref(prefs::kDevToolsOpenDocked, true);
}
DevToolsManager::DevToolsManager()
: inspected_rvh_for_reopen_(NULL),
in_initial_show_(false),
last_orphan_cookie_(0) {
}
DevToolsManager::~DevToolsManager() {
DCHECK(inspected_rvh_to_client_host_.empty());
DCHECK(client_host_to_inspected_rvh_.empty());
// By the time we destroy devtools manager, all orphan client hosts should
// have been delelted, no need to notify them upon tab closing.
DCHECK(orphan_client_hosts_.empty());
}
DevToolsClientHost* DevToolsManager::GetDevToolsClientHostFor(
RenderViewHost* inspected_rvh) {
InspectedRvhToClientHostMap::iterator it =
inspected_rvh_to_client_host_.find(inspected_rvh);
if (it != inspected_rvh_to_client_host_.end())
return it->second;
return NULL;
}
void DevToolsManager::RegisterDevToolsClientHostFor(
RenderViewHost* inspected_rvh,
DevToolsClientHost* client_host) {
DCHECK(!GetDevToolsClientHostFor(inspected_rvh));
DevToolsRuntimeProperties initial_properties;
BindClientHost(inspected_rvh, client_host, initial_properties);
client_host->set_close_listener(this);
SendAttachToAgent(inspected_rvh);
}
void DevToolsManager::ForwardToDevToolsAgent(
RenderViewHost* client_rvh,
const IPC::Message& message) {
DevToolsClientHost* client_host = FindOwnerDevToolsClientHost(client_rvh);
if (client_host)
ForwardToDevToolsAgent(client_host, message);
}
void DevToolsManager::ForwardToDevToolsAgent(DevToolsClientHost* from,
const IPC::Message& message) {
RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(from);
if (!inspected_rvh) {
// TODO(yurys): notify client that the agent is no longer available
NOTREACHED();
return;
}
IPC::Message* m = new IPC::Message(message);
m->set_routing_id(inspected_rvh->routing_id());
inspected_rvh->Send(m);
}
void DevToolsManager::ForwardToDevToolsClient(RenderViewHost* inspected_rvh,
const IPC::Message& message) {
DevToolsClientHost* client_host = GetDevToolsClientHostFor(inspected_rvh);
if (!client_host) {
// Client window was closed while there were messages
// being sent to it.
return;
}
client_host->SendMessageToClient(message);
}
void DevToolsManager::ActivateWindow(RenderViewHost* client_rvh) {
DevToolsClientHost* client_host = FindOwnerDevToolsClientHost(client_rvh);
if (!client_host)
return;
DevToolsWindow* window = client_host->AsDevToolsWindow();
DCHECK(window);
window->Activate();
}
void DevToolsManager::CloseWindow(RenderViewHost* client_rvh) {
DevToolsClientHost* client_host = FindOwnerDevToolsClientHost(client_rvh);
if (client_host) {
RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(client_host);
DCHECK(inspected_rvh);
UnregisterDevToolsClientHostFor(inspected_rvh);
}
}
void DevToolsManager::RequestDockWindow(RenderViewHost* client_rvh) {
ReopenWindow(client_rvh, true);
}
void DevToolsManager::RequestUndockWindow(RenderViewHost* client_rvh) {
ReopenWindow(client_rvh, false);
}
void DevToolsManager::OpenDevToolsWindow(RenderViewHost* inspected_rvh) {
ToggleDevToolsWindow(
inspected_rvh,
true,
DEVTOOLS_TOGGLE_ACTION_NONE);
}
void DevToolsManager::ToggleDevToolsWindow(
RenderViewHost* inspected_rvh,
DevToolsToggleAction action) {
ToggleDevToolsWindow(inspected_rvh, false, action);
}
void DevToolsManager::RuntimePropertyChanged(RenderViewHost* inspected_rvh,
const std::string& name,
const std::string& value) {
RuntimePropertiesMap::iterator it =
runtime_properties_map_.find(inspected_rvh);
if (it == runtime_properties_map_.end()) {
std::pair<RenderViewHost*, DevToolsRuntimeProperties> value(
inspected_rvh,
DevToolsRuntimeProperties());
it = runtime_properties_map_.insert(value).first;
}
it->second[name] = value;
}
void DevToolsManager::InspectElement(RenderViewHost* inspected_rvh,
int x,
int y) {
IPC::Message* m = new DevToolsAgentMsg_InspectElement(x, y);
m->set_routing_id(inspected_rvh->routing_id());
inspected_rvh->Send(m);
// TODO(loislo): we should initiate DevTools window opening from within
// renderer. Otherwise, we still can hit a race condition here.
OpenDevToolsWindow(inspected_rvh);
}
void DevToolsManager::ClientHostClosing(DevToolsClientHost* host) {
RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(host);
if (!inspected_rvh) {
// It might be in the list of orphan client hosts, remove it from there.
for (OrphanClientHosts::iterator it = orphan_client_hosts_.begin();
it != orphan_client_hosts_.end(); ++it) {
if (it->second.first == host) {
orphan_client_hosts_.erase(it->first);
return;
}
}
return;
}
NotificationService::current()->Notify(
NotificationType::DEVTOOLS_WINDOW_CLOSING,
Source<Profile>(inspected_rvh->site_instance()->GetProcess()->profile()),
Details<RenderViewHost>(inspected_rvh));
UnbindClientHost(inspected_rvh, host);
}
RenderViewHost* DevToolsManager::GetInspectedRenderViewHost(
DevToolsClientHost* client_host) {
ClientHostToInspectedRvhMap::iterator it =
client_host_to_inspected_rvh_.find(client_host);
if (it != client_host_to_inspected_rvh_.end())
return it->second;
return NULL;
}
void DevToolsManager::UnregisterDevToolsClientHostFor(
RenderViewHost* inspected_rvh) {
DevToolsClientHost* host = GetDevToolsClientHostFor(inspected_rvh);
if (!host)
return;
UnbindClientHost(inspected_rvh, host);
host->InspectedTabClosing();
}
void DevToolsManager::OnNavigatingToPendingEntry(RenderViewHost* rvh,
RenderViewHost* dest_rvh,
const GURL& gurl) {
if (in_initial_show_) {
// Mute this even in case it is caused by the initial show routines.
return;
}
int cookie = DetachClientHost(rvh);
if (cookie != -1) {
// Navigating to URL in the inspected window.
AttachClientHost(cookie, dest_rvh);
return;
}
// Iterate over client hosts and if there is one that has render view host
// changing, reopen entire client window (this must be caused by the user
// manually refreshing its content).
for (ClientHostToInspectedRvhMap::iterator it =
client_host_to_inspected_rvh_.begin();
it != client_host_to_inspected_rvh_.end(); ++it) {
DevToolsWindow* window = it->first->AsDevToolsWindow();
if (window && window->GetRenderViewHost() == rvh) {
inspected_rvh_for_reopen_ = it->second;
MessageLoop::current()->PostTask(FROM_HERE,
NewRunnableMethod(this,
&DevToolsManager::ForceReopenWindow));
return;
}
}
}
int DevToolsManager::DetachClientHost(RenderViewHost* from_rvh) {
DevToolsClientHost* client_host = GetDevToolsClientHostFor(from_rvh);
if (!client_host)
return -1;
int cookie = last_orphan_cookie_++;
orphan_client_hosts_[cookie] =
std::pair<DevToolsClientHost*, DevToolsRuntimeProperties>(
client_host, runtime_properties_map_[from_rvh]);
UnbindClientHost(from_rvh, client_host);
return cookie;
}
void DevToolsManager::AttachClientHost(int client_host_cookie,
RenderViewHost* to_rvh) {
OrphanClientHosts::iterator it = orphan_client_hosts_.find(
client_host_cookie);
if (it == orphan_client_hosts_.end())
return;
DevToolsClientHost* client_host = (*it).second.first;
BindClientHost(to_rvh, client_host, (*it).second.second);
SendAttachToAgent(to_rvh);
orphan_client_hosts_.erase(client_host_cookie);
}
void DevToolsManager::SendAttachToAgent(RenderViewHost* inspected_rvh) {
if (inspected_rvh) {
ChildProcessSecurityPolicy::GetInstance()->GrantReadRawCookies(
inspected_rvh->process()->id());
DevToolsRuntimeProperties properties;
RuntimePropertiesMap::iterator it =
runtime_properties_map_.find(inspected_rvh);
if (it != runtime_properties_map_.end()) {
properties = DevToolsRuntimeProperties(it->second.begin(),
it->second.end());
}
IPC::Message* m = new DevToolsAgentMsg_Attach(properties);
m->set_routing_id(inspected_rvh->routing_id());
inspected_rvh->Send(m);
}
}
void DevToolsManager::SendDetachToAgent(RenderViewHost* inspected_rvh) {
if (inspected_rvh) {
IPC::Message* m = new DevToolsAgentMsg_Detach();
m->set_routing_id(inspected_rvh->routing_id());
inspected_rvh->Send(m);
}
}
void DevToolsManager::ForceReopenWindow() {
if (inspected_rvh_for_reopen_) {
RenderViewHost* inspected_rvn = inspected_rvh_for_reopen_;
UnregisterDevToolsClientHostFor(inspected_rvn);
OpenDevToolsWindow(inspected_rvn);
}
}
DevToolsClientHost* DevToolsManager::FindOwnerDevToolsClientHost(
RenderViewHost* client_rvh) {
for (InspectedRvhToClientHostMap::iterator it =
inspected_rvh_to_client_host_.begin();
it != inspected_rvh_to_client_host_.end();
++it) {
DevToolsWindow* win = it->second->AsDevToolsWindow();
if (!win)
continue;
if (client_rvh == win->GetRenderViewHost())
return it->second;
}
return NULL;
}
void DevToolsManager::ReopenWindow(RenderViewHost* client_rvh, bool docked) {
DevToolsClientHost* client_host = FindOwnerDevToolsClientHost(client_rvh);
if (!client_host)
return;
RenderViewHost* inspected_rvh = GetInspectedRenderViewHost(client_host);
DCHECK(inspected_rvh);
inspected_rvh->process()->profile()->GetPrefs()->SetBoolean(
prefs::kDevToolsOpenDocked, docked);
DevToolsWindow* window = client_host->AsDevToolsWindow();
DCHECK(window);
window->SetDocked(docked);
}
void DevToolsManager::ToggleDevToolsWindow(
RenderViewHost* inspected_rvh,
bool force_open,
DevToolsToggleAction action) {
bool do_open = force_open;
DevToolsClientHost* host = GetDevToolsClientHostFor(inspected_rvh);
if (!host) {
bool docked = inspected_rvh->process()->profile()->GetPrefs()->
GetBoolean(prefs::kDevToolsOpenDocked);
host = new DevToolsWindow(
inspected_rvh->site_instance()->browsing_instance()->profile(),
inspected_rvh,
docked);
RegisterDevToolsClientHostFor(inspected_rvh, host);
do_open = true;
}
DevToolsWindow* window = host->AsDevToolsWindow();
if (!window)
return;
// If window is docked and visible, we hide it on toggle. If window is
// undocked, we show (activate) it.
if (!window->is_docked() || do_open) {
AutoReset<bool> auto_reset_in_initial_show(&in_initial_show_, true);
window->Show(action);
} else {
UnregisterDevToolsClientHostFor(inspected_rvh);
}
}
void DevToolsManager::BindClientHost(
RenderViewHost* inspected_rvh,
DevToolsClientHost* client_host,
const DevToolsRuntimeProperties& runtime_properties) {
DCHECK(inspected_rvh_to_client_host_.find(inspected_rvh) ==
inspected_rvh_to_client_host_.end());
DCHECK(client_host_to_inspected_rvh_.find(client_host) ==
client_host_to_inspected_rvh_.end());
if (client_host_to_inspected_rvh_.empty()) {
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
NewRunnableFunction(&DevToolsNetLogObserver::Attach,
g_browser_process->io_thread()));
}
inspected_rvh_to_client_host_[inspected_rvh] = client_host;
client_host_to_inspected_rvh_[client_host] = inspected_rvh;
runtime_properties_map_[inspected_rvh] = runtime_properties;
}
void DevToolsManager::UnbindClientHost(RenderViewHost* inspected_rvh,
DevToolsClientHost* client_host) {
DCHECK(inspected_rvh_to_client_host_.find(inspected_rvh)->second ==
client_host);
DCHECK(client_host_to_inspected_rvh_.find(client_host)->second ==
inspected_rvh);
inspected_rvh_to_client_host_.erase(inspected_rvh);
client_host_to_inspected_rvh_.erase(client_host);
runtime_properties_map_.erase(inspected_rvh);
if (client_host_to_inspected_rvh_.empty()) {
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
NewRunnableFunction(&DevToolsNetLogObserver::Detach));
}
SendDetachToAgent(inspected_rvh);
if (inspected_rvh_for_reopen_ == inspected_rvh)
inspected_rvh_for_reopen_ = NULL;
int process_id = inspected_rvh->process()->id();
for (InspectedRvhToClientHostMap::iterator it =
inspected_rvh_to_client_host_.begin();
it != inspected_rvh_to_client_host_.end();
++it) {
if (it->first->process()->id() == process_id)
return;
}
// We've disconnected from the last renderer -> revoke cookie permissions.
ChildProcessSecurityPolicy::GetInstance()->RevokeReadRawCookies(process_id);
}
void DevToolsManager::CloseAllClientHosts() {
std::vector<RenderViewHost*> rhvs;
for (InspectedRvhToClientHostMap::iterator it =
inspected_rvh_to_client_host_.begin();
it != inspected_rvh_to_client_host_.end(); ++it) {
rhvs.push_back(it->first);
}
for (std::vector<RenderViewHost*>::iterator it = rhvs.begin();
it != rhvs.end(); ++it) {
UnregisterDevToolsClientHostFor(*it);
}
}
|