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
|
// Copyright 2015 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 "components/html_viewer/devtools_agent_impl.h"
#include "base/bind.h"
#include "base/logging.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/web/WebDevToolsAgent.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
namespace html_viewer {
DevToolsAgentImpl::DevToolsAgentImpl(blink::WebLocalFrame* frame,
const std::string& id,
const std::string* state)
: frame_(frame), id_(id), binding_(this), cache_until_client_ready_(false) {
DCHECK(frame);
frame_->setDevToolsAgentClient(this);
if (state) {
cache_until_client_ready_ = true;
frame_->devToolsAgent()->reattach(blink::WebString::fromUTF8(id_), 0,
blink::WebString::fromUTF8(*state));
}
}
DevToolsAgentImpl::~DevToolsAgentImpl() {
if (cache_until_client_ready_ || client_)
frame_->devToolsAgent()->detach();
}
void DevToolsAgentImpl::BindToRequest(
mojo::InterfaceRequest<DevToolsAgent> request) {
binding_.Bind(request.Pass());
}
void DevToolsAgentImpl::SetClient(
devtools_service::DevToolsAgentClientPtr client) {
if (client_)
frame_->devToolsAgent()->detach();
client_ = client.Pass();
client_.set_connection_error_handler(base::Bind(
&DevToolsAgentImpl::OnConnectionError, base::Unretained(this)));
if (cache_until_client_ready_) {
cache_until_client_ready_ = false;
for (const auto& message : cached_client_messages_)
client_->DispatchProtocolMessage(message.call_id, message.response,
message.state);
cached_client_messages_.clear();
} else {
frame_->devToolsAgent()->attach(blink::WebString::fromUTF8(id_), 0);
}
}
void DevToolsAgentImpl::DispatchProtocolMessage(const mojo::String& message) {
frame_->devToolsAgent()->dispatchOnInspectorBackend(
0, blink::WebString::fromUTF8(message));
}
void DevToolsAgentImpl::sendProtocolMessage(int session_id,
int call_id,
const blink::WebString& response,
const blink::WebString& state) {
DCHECK(!response.isNull());
mojo::String response_str = response.utf8();
mojo::String state_str;
if (!state.isNull())
state_str = state.utf8();
if (client_) {
client_->DispatchProtocolMessage(call_id, response_str, state_str);
} else if (cache_until_client_ready_) {
cached_client_messages_.resize(cached_client_messages_.size() + 1);
CachedClientMessage& message = cached_client_messages_.back();
message.call_id = call_id;
message.response.Swap(&response_str);
message.state.Swap(&state_str);
}
}
void DevToolsAgentImpl::OnConnectionError() {
client_.reset();
frame_->devToolsAgent()->detach();
}
} // namespace html_viewer
|