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"
9 #include "base/json/json_reader.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h"
13 #include "mojo/application/public/cpp/connect.h"
14 #include "mojo/application/public/interfaces/shell.mojom.h"
15 #include "third_party/WebKit/public/platform/WebString.h"
16 #include "third_party/WebKit/public/platform/WebURLRequest.h"
17 #include "third_party/WebKit/public/web/WebDevToolsAgent.h"
18 #include "third_party/WebKit/public/web/WebLocalFrame.h"
20 namespace html_viewer
{
22 DevToolsAgentImpl::DevToolsAgentImpl(blink::WebLocalFrame
* frame
,
24 : frame_(frame
), binding_(this), handling_page_navigate_request_(false) {
28 mojo::ServiceProviderPtr devtools_service_provider
;
29 mojo::URLRequestPtr
request(mojo::URLRequest::New());
30 request
->url
= "mojo:devtools_service";
31 shell
->ConnectToApplication(request
.Pass(),
32 mojo::GetProxy(&devtools_service_provider
),
35 devtools_service::DevToolsRegistryPtr devtools_registry
;
36 mojo::ConnectToService(devtools_service_provider
.get(), &devtools_registry
);
38 devtools_service::DevToolsAgentPtr agent
;
39 binding_
.Bind(&agent
);
40 devtools_registry
->RegisterAgent(agent
.Pass());
42 frame_
->setDevToolsAgentClient(this);
45 DevToolsAgentImpl::~DevToolsAgentImpl() {
47 frame_
->devToolsAgent()->detach();
50 void DevToolsAgentImpl::SetClient(
51 devtools_service::DevToolsAgentClientPtr client
,
52 const mojo::String
& client_id
) {
54 frame_
->devToolsAgent()->detach();
56 client_
= client
.Pass();
57 client_
.set_connection_error_handler([this]() { OnConnectionError(); });
59 frame_
->devToolsAgent()->attach(blink::WebString::fromUTF8(client_id
));
62 void DevToolsAgentImpl::DispatchProtocolMessage(const mojo::String
& message
) {
63 // TODO(yzshen): (1) Eventually the handling code for Page.navigate (and some
64 // other commands) should live with the code managing tabs and navigation.
65 // We will need a DevToolsAgent implementation there as well, which handles
66 // some of the commands and relays messages between the DevTools service and
68 // (2) Consider refactoring and reusing the existing DevTools protocol parsing
71 scoped_ptr
<base::Value
> value
= base::JSONReader::Read(message
.get());
72 base::DictionaryValue
* command
= nullptr;
73 if (!value
|| !value
->GetAsDictionary(&command
))
77 if (!command
->GetString("method", &method
) || method
!= "Page.navigate")
80 std::string url_string
;
81 if (!command
->GetString("params.url", &url_string
))
88 handling_page_navigate_request_
= true;
89 frame_
->loadRequest(blink::WebURLRequest(url
));
90 handling_page_navigate_request_
= false;
92 // The command should fall through to be handled by frame_->devToolsAgent().
95 frame_
->devToolsAgent()->dispatchOnInspectorBackend(
96 blink::WebString::fromUTF8(message
));
99 void DevToolsAgentImpl::sendProtocolMessage(int call_id
,
100 const blink::WebString
& response
,
101 const blink::WebString
& state
) {
103 client_
->DispatchProtocolMessage(response
.utf8());
106 void DevToolsAgentImpl::OnConnectionError() {
108 frame_
->devToolsAgent()->detach();
111 } // namespace html_viewer