OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/html_viewer/devtools_agent_impl.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/logging.h" | |
11 #include "third_party/WebKit/public/platform/WebString.h" | |
12 #include "third_party/WebKit/public/web/WebDevToolsAgent.h" | |
13 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
14 | |
15 namespace html_viewer { | |
16 | |
17 DevToolsAgentImpl::DevToolsAgentImpl(blink::WebLocalFrame* frame, | |
18 const std::string& id, | |
19 const std::string* state) | |
20 : frame_(frame), id_(id), binding_(this), cache_until_client_ready_(false) { | |
21 DCHECK(frame); | |
22 frame_->setDevToolsAgentClient(this); | |
23 | |
24 if (state) { | |
25 cache_until_client_ready_ = true; | |
26 frame_->devToolsAgent()->reattach(blink::WebString::fromUTF8(id_), 0, | |
27 blink::WebString::fromUTF8(*state)); | |
28 } | |
29 } | |
30 | |
31 DevToolsAgentImpl::~DevToolsAgentImpl() { | |
32 if (cache_until_client_ready_ || client_) | |
33 frame_->devToolsAgent()->detach(); | |
34 } | |
35 | |
36 void DevToolsAgentImpl::BindToRequest( | |
37 mojo::InterfaceRequest<DevToolsAgent> request) { | |
38 binding_.Bind(std::move(request)); | |
39 } | |
40 | |
41 void DevToolsAgentImpl::SetClient( | |
42 devtools_service::DevToolsAgentClientPtr client) { | |
43 if (client_) | |
44 frame_->devToolsAgent()->detach(); | |
45 | |
46 client_ = std::move(client); | |
47 client_.set_connection_error_handler(base::Bind( | |
48 &DevToolsAgentImpl::OnConnectionError, base::Unretained(this))); | |
49 | |
50 if (cache_until_client_ready_) { | |
51 cache_until_client_ready_ = false; | |
52 for (const auto& message : cached_client_messages_) | |
53 client_->DispatchProtocolMessage(message.call_id, message.response, | |
54 message.state); | |
55 cached_client_messages_.clear(); | |
56 } else { | |
57 frame_->devToolsAgent()->attach(blink::WebString::fromUTF8(id_), 0); | |
58 } | |
59 } | |
60 | |
61 void DevToolsAgentImpl::DispatchProtocolMessage(const mojo::String& message) { | |
62 frame_->devToolsAgent()->dispatchOnInspectorBackend( | |
63 0, blink::WebString::fromUTF8(message)); | |
64 } | |
65 | |
66 void DevToolsAgentImpl::sendProtocolMessage(int session_id, | |
67 int call_id, | |
68 const blink::WebString& response, | |
69 const blink::WebString& state) { | |
70 DCHECK(!response.isNull()); | |
71 | |
72 mojo::String response_str = response.utf8(); | |
73 mojo::String state_str; | |
74 if (!state.isNull()) | |
75 state_str = state.utf8(); | |
76 | |
77 if (client_) { | |
78 client_->DispatchProtocolMessage(call_id, response_str, state_str); | |
79 } else if (cache_until_client_ready_) { | |
80 cached_client_messages_.resize(cached_client_messages_.size() + 1); | |
81 CachedClientMessage& message = cached_client_messages_.back(); | |
82 message.call_id = call_id; | |
83 message.response.Swap(&response_str); | |
84 message.state.Swap(&state_str); | |
85 } | |
86 } | |
87 | |
88 void DevToolsAgentImpl::OnConnectionError() { | |
89 client_.reset(); | |
90 frame_->devToolsAgent()->detach(); | |
91 } | |
92 | |
93 } // namespace html_viewer | |
OLD | NEW |