1 // Copyright 2015 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 #include "components/html_viewer/devtools_agent_impl.h"
8 #include "base/logging.h"
9 #include "third_party/WebKit/public/platform/WebString.h"
10 #include "third_party/WebKit/public/web/WebDevToolsAgent.h"
11 #include "third_party/WebKit/public/web/WebLocalFrame.h"
13 namespace html_viewer
{
15 DevToolsAgentImpl::DevToolsAgentImpl(blink::WebLocalFrame
* frame
,
16 const std::string
& id
,
17 const std::string
* state
)
18 : frame_(frame
), id_(id
), binding_(this), cache_until_client_ready_(false) {
20 frame_
->setDevToolsAgentClient(this);
23 cache_until_client_ready_
= true;
24 frame_
->devToolsAgent()->reattach(blink::WebString::fromUTF8(id_
),
25 blink::WebString::fromUTF8(*state
));
29 DevToolsAgentImpl::~DevToolsAgentImpl() {
30 if (cache_until_client_ready_
|| client_
)
31 frame_
->devToolsAgent()->detach();
34 void DevToolsAgentImpl::BindToRequest(
35 mojo::InterfaceRequest
<DevToolsAgent
> request
) {
36 binding_
.Bind(request
.Pass());
39 void DevToolsAgentImpl::SetClient(
40 devtools_service::DevToolsAgentClientPtr client
) {
42 frame_
->devToolsAgent()->detach();
44 client_
= client
.Pass();
45 client_
.set_connection_error_handler(base::Bind(
46 &DevToolsAgentImpl::OnConnectionError
, base::Unretained(this)));
48 if (cache_until_client_ready_
) {
49 cache_until_client_ready_
= false;
50 for (const auto& message
: cached_client_messages_
)
51 client_
->DispatchProtocolMessage(message
.call_id
, message
.response
,
53 cached_client_messages_
.clear();
55 frame_
->devToolsAgent()->attach(blink::WebString::fromUTF8(id_
));
59 void DevToolsAgentImpl::DispatchProtocolMessage(const mojo::String
& message
) {
60 frame_
->devToolsAgent()->dispatchOnInspectorBackend(
61 blink::WebString::fromUTF8(message
));
64 void DevToolsAgentImpl::sendProtocolMessage(int call_id
,
65 const blink::WebString
& response
,
66 const blink::WebString
& state
) {
67 DCHECK(!response
.isNull());
69 mojo::String response_str
= response
.utf8();
70 mojo::String state_str
;
72 state_str
= state
.utf8();
75 client_
->DispatchProtocolMessage(call_id
, response_str
, state_str
);
76 } else if (cache_until_client_ready_
) {
77 cached_client_messages_
.resize(cached_client_messages_
.size() + 1);
78 CachedClientMessage
& message
= cached_client_messages_
.back();
79 message
.call_id
= call_id
;
80 message
.response
.Swap(&response_str
);
81 message
.state
.Swap(&state_str
);
85 void DevToolsAgentImpl::OnConnectionError() {
87 frame_
->devToolsAgent()->detach();
90 } // namespace html_viewer