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 <string> |
| 8 |
| 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" |
| 19 |
| 20 namespace html_viewer { |
| 21 |
| 22 DevToolsAgentImpl::DevToolsAgentImpl(blink::WebLocalFrame* frame, |
| 23 mojo::Shell* shell) |
| 24 : frame_(frame), binding_(this) { |
| 25 DCHECK(frame); |
| 26 DCHECK(shell); |
| 27 |
| 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); |
| 35 |
| 36 devtools_service::DevToolsAgentPtr agent; |
| 37 binding_.Bind(&agent); |
| 38 devtools_registry->RegisterAgent(agent.Pass()); |
| 39 |
| 40 frame_->setDevToolsAgentClient(this); |
| 41 } |
| 42 |
| 43 DevToolsAgentImpl::~DevToolsAgentImpl() { |
| 44 if (client_) |
| 45 frame_->devToolsAgent()->detach(); |
| 46 } |
| 47 |
| 48 void DevToolsAgentImpl::SetClient( |
| 49 devtools_service::DevToolsAgentClientPtr client, |
| 50 const mojo::String& client_id) { |
| 51 if (client_) |
| 52 frame_->devToolsAgent()->detach(); |
| 53 |
| 54 client_ = client.Pass(); |
| 55 client_.set_error_handler(this); |
| 56 |
| 57 frame_->devToolsAgent()->attach(blink::WebString::fromUTF8(client_id)); |
| 58 } |
| 59 |
| 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; |
| 73 |
| 74 std::string method; |
| 75 if (!command->GetString("method", &method) || method != "Page.navigate") |
| 76 break; |
| 77 |
| 78 std::string url_string; |
| 79 if (!command->GetString("params.url", &url_string)) |
| 80 break; |
| 81 |
| 82 GURL url(url_string); |
| 83 if (!url.is_valid()) |
| 84 break; |
| 85 |
| 86 frame_->loadRequest(blink::WebURLRequest(url)); |
| 87 |
| 88 // The command should fall through to be handled by frame_->devToolsAgent(). |
| 89 } while (false); |
| 90 |
| 91 frame_->devToolsAgent()->dispatchOnInspectorBackend( |
| 92 blink::WebString::fromUTF8(message)); |
| 93 } |
| 94 |
| 95 void DevToolsAgentImpl::sendProtocolMessage(int call_id, |
| 96 const blink::WebString& response, |
| 97 const blink::WebString& state) { |
| 98 if (client_) |
| 99 client_->DispatchProtocolMessage(response.utf8()); |
| 100 } |
| 101 |
| 102 void DevToolsAgentImpl::OnConnectionError() { |
| 103 client_.reset(); |
| 104 frame_->devToolsAgent()->detach(); |
| 105 } |
| 106 |
| 107 } // namespace html_viewer |
OLD | NEW |