Don't preload rarely seen large images
[chromium-blink-merge.git] / components / html_viewer / devtools_agent_impl.cc
blob8430cfec484a0220f004fd3e561b00508baf353c
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(
32 request.Pass(), mojo::GetProxy(&devtools_service_provider), nullptr);
33 devtools_service::DevToolsRegistryPtr devtools_registry;
34 mojo::ConnectToService(devtools_service_provider.get(), &devtools_registry);
36 devtools_service::DevToolsAgentPtr agent;
37 binding_.Bind(&agent);
38 devtools_registry->RegisterAgent(agent.Pass());
40 frame_->setDevToolsAgentClient(this);
43 DevToolsAgentImpl::~DevToolsAgentImpl() {
44 if (client_)
45 frame_->devToolsAgent()->detach();
48 void DevToolsAgentImpl::SetClient(
49 devtools_service::DevToolsAgentClientPtr client,
50 const mojo::String& client_id) {
51 if (client_)
52 frame_->devToolsAgent()->detach();
54 client_ = client.Pass();
55 client_.set_error_handler(this);
57 frame_->devToolsAgent()->attach(blink::WebString::fromUTF8(client_id));
60 void DevToolsAgentImpl::DispatchProtocolMessage(const mojo::String& message) {
61 // TODO(yzshen): (1) Eventually the handling code for Page.navigate (and some
62 // other commands) should live with the code managing tabs and navigation.
63 // We will need a DevToolsAgent implementation there as well, which handles
64 // some of the commands and relays messages between the DevTools service and
65 // the HTML viewer.
66 // (2) Consider refactoring and reusing the existing DevTools protocol parsing
67 // code.
68 do {
69 scoped_ptr<base::Value> value = base::JSONReader::Read(message.get());
70 base::DictionaryValue* command = nullptr;
71 if (!value || !value->GetAsDictionary(&command))
72 break;
74 std::string method;
75 if (!command->GetString("method", &method) || method != "Page.navigate")
76 break;
78 std::string url_string;
79 if (!command->GetString("params.url", &url_string))
80 break;
82 GURL url(url_string);
83 if (!url.is_valid())
84 break;
86 handling_page_navigate_request_ = true;
87 frame_->loadRequest(blink::WebURLRequest(url));
88 handling_page_navigate_request_ = false;
90 // The command should fall through to be handled by frame_->devToolsAgent().
91 } while (false);
93 frame_->devToolsAgent()->dispatchOnInspectorBackend(
94 blink::WebString::fromUTF8(message));
97 void DevToolsAgentImpl::sendProtocolMessage(int call_id,
98 const blink::WebString& response,
99 const blink::WebString& state) {
100 if (client_)
101 client_->DispatchProtocolMessage(response.utf8());
104 void DevToolsAgentImpl::OnConnectionError() {
105 client_.reset();
106 frame_->devToolsAgent()->detach();
109 } // namespace html_viewer