Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Unified Diff: components/html_viewer/devtools_agent_impl.cc

Issue 1163953002: html_viewer: Add DevToolsAgentImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/html_viewer/devtools_agent_impl.cc
diff --git a/components/html_viewer/devtools_agent_impl.cc b/components/html_viewer/devtools_agent_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..82542c33a59d394e2bbc10120bcba42d0680d65d
--- /dev/null
+++ b/components/html_viewer/devtools_agent_impl.cc
@@ -0,0 +1,103 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/html_viewer/devtools_agent_impl.h"
+
+#include <string>
+
+#include "base/json/json_reader.h"
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/values.h"
+#include "mojo/application/public/cpp/connect.h"
+#include "mojo/application/public/interfaces/shell.mojom.h"
+#include "third_party/WebKit/public/platform/WebString.h"
+#include "third_party/WebKit/public/platform/WebURLRequest.h"
+#include "third_party/WebKit/public/web/WebDevToolsAgent.h"
+#include "third_party/WebKit/public/web/WebLocalFrame.h"
+
+namespace html_viewer {
+
+DevToolsAgentImpl::DevToolsAgentImpl(blink::WebLocalFrame* frame,
+ mojo::Shell* shell)
+ : frame_(frame), binding_(this) {
+ DCHECK(frame);
+ DCHECK(shell);
+
+ mojo::ServiceProviderPtr devtools_service_provider;
+ mojo::URLRequestPtr request(mojo::URLRequest::New());
+ request->url = "mojo:devtools_service";
+ shell->ConnectToApplication(
+ request.Pass(), mojo::GetProxy(&devtools_service_provider), nullptr);
+ devtools_service::DevToolsRegistryPtr devtools_registry;
+ mojo::ConnectToService(devtools_service_provider.get(), &devtools_registry);
+
+ devtools_service::DevToolsAgentPtr agent;
+ binding_.Bind(&agent);
+ devtools_registry->RegisterAgent(agent.Pass());
+
+ frame_->setDevToolsAgentClient(this);
+}
+
+DevToolsAgentImpl::~DevToolsAgentImpl() {
+ if (client_)
+ frame_->devToolsAgent()->detach();
+}
+
+void DevToolsAgentImpl::SetClient(
+ devtools_service::DevToolsAgentClientPtr client,
+ const mojo::String& client_id) {
+ if (client_)
+ frame_->devToolsAgent()->detach();
+
+ client_ = client.Pass();
+ client_.set_error_handler(this);
+
+ frame_->devToolsAgent()->attach(blink::WebString::fromUTF8(client_id));
+}
+
+void DevToolsAgentImpl::DispatchProtocolMessage(const mojo::String& message) {
+ // TODO(yzshen): (1) Eventually the handling code for Page.navigate (and some
sky 2015/06/02 23:28:32 We have that class now. browser is the one that ha
yzshen1 2015/06/05 17:29:29 I think currently we don't have the ability to do
sky 2015/06/05 19:13:29 Fair enough.
+ // other commands) should live with the code managing tabs and navigation.
+ // We will need a DevToolsAgent implementation there as well, which handles
+ // some of the commands and relays messages between the DevTools service and
+ // the HTML viewer.
+ // (2) Consider refactoring and reusing the existing DevTools protocol parsing
+ // code.
+ scoped_ptr<base::Value> value = base::JSONReader::Read(message.get());
+ if (!value || !value->IsType(base::Value::TYPE_DICTIONARY))
+ return;
+
+ scoped_ptr<base::DictionaryValue> command =
+ make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release()));
dgozman 2015/06/03 09:55:55 There is |Value::GetAsDictionary| for this purpose
yzshen1 2015/06/05 17:29:29 Done. That is much better.
+ std::string method;
+ command->GetString("method", &method);
+ if (method == "Page.navigate") {
+ std::string url;
+ if (!command->GetString("params.url", &url))
+ return;
+
+ blink::WebURLRequest request{GURL(url)};
sky 2015/06/02 23:28:32 Should you validate url?
yzshen1 2015/06/05 17:29:29 Done.
+ frame_->loadRequest(request);
+
+ // The command should fall through to be handled by frame_->devToolsAgent().
+ }
+
+ frame_->devToolsAgent()->dispatchOnInspectorBackend(
+ blink::WebString::fromUTF8(message));
+}
+
+void DevToolsAgentImpl::sendProtocolMessage(int call_id,
+ const blink::WebString& response,
+ const blink::WebString& state) {
+ if (client_)
+ client_->DispatchProtocolMessage(response.utf8());
+}
+
+void DevToolsAgentImpl::OnConnectionError() {
+ client_.reset();
+ frame_->devToolsAgent()->detach();
+}
+
+} // namespace html_viewer

Powered by Google App Engine
This is Rietveld 408576698