Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / html_viewer / devtools_agent_impl.cc
blob26de27a63378b28169e95e58257dd488f2f511fa
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"
7 #include <string>
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,
23 mojo::Shell* shell)
24 : frame_(frame), binding_(this), handling_page_navigate_request_(false) {
25 DCHECK(frame);
26 DCHECK(shell);
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),
33 nullptr,
34 nullptr);
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() {
46 if (client_)
47 frame_->devToolsAgent()->detach();
50 void DevToolsAgentImpl::SetClient(
51 devtools_service::DevToolsAgentClientPtr client,
52 const mojo::String& client_id) {
53 if (client_)
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
67 // the HTML viewer.
68 // (2) Consider refactoring and reusing the existing DevTools protocol parsing
69 // code.
70 do {
71 scoped_ptr<base::Value> value = base::JSONReader::Read(message.get());
72 base::DictionaryValue* command = nullptr;
73 if (!value || !value->GetAsDictionary(&command))
74 break;
76 std::string method;
77 if (!command->GetString("method", &method) || method != "Page.navigate")
78 break;
80 std::string url_string;
81 if (!command->GetString("params.url", &url_string))
82 break;
84 GURL url(url_string);
85 if (!url.is_valid())
86 break;
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().
93 } while (false);
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) {
102 if (client_)
103 client_->DispatchProtocolMessage(response.utf8());
106 void DevToolsAgentImpl::OnConnectionError() {
107 client_.reset();
108 frame_->devToolsAgent()->detach();
111 } // namespace html_viewer