Fix a couple bugs in the settings_page_header wrt breadcrumbs.
[chromium-blink-merge.git] / android_webview / browser / aw_dev_tools_discovery_provider.cc
blobfe1e61e8cf87fca6cc21250314995b40583471c3
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 "android_webview/browser/aw_dev_tools_discovery_provider.h"
7 #include "android_webview/browser/browser_view_renderer.h"
8 #include "base/json/json_writer.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "components/devtools_discovery/basic_target_descriptor.h"
13 #include "components/devtools_discovery/devtools_discovery_manager.h"
14 #include "content/public/browser/devtools_agent_host.h"
15 #include "content/public/browser/web_contents.h"
17 using content::DevToolsAgentHost;
18 using content::WebContents;
20 namespace {
22 std::string GetViewDescription(WebContents* web_contents) {
23 android_webview::BrowserViewRenderer* bvr =
24 android_webview::BrowserViewRenderer::FromWebContents(web_contents);
25 if (!bvr) return "";
26 base::DictionaryValue description;
27 description.SetBoolean("attached", bvr->attached_to_window());
28 description.SetBoolean("visible", bvr->IsVisible());
29 gfx::Rect screen_rect = bvr->GetScreenRect();
30 description.SetInteger("screenX", screen_rect.x());
31 description.SetInteger("screenY", screen_rect.y());
32 description.SetBoolean("empty", screen_rect.size().IsEmpty());
33 if (!screen_rect.size().IsEmpty()) {
34 description.SetInteger("width", screen_rect.width());
35 description.SetInteger("height", screen_rect.height());
37 std::string json;
38 base::JSONWriter::Write(description, &json);
39 return json;
42 class TargetDescriptor : public devtools_discovery::BasicTargetDescriptor {
43 public:
44 explicit TargetDescriptor(scoped_refptr<DevToolsAgentHost> agent_host);
46 // devtools_discovery::BasicTargetDescriptor overrides.
47 std::string GetDescription() const override { return description_; }
49 private:
50 std::string description_;
52 DISALLOW_COPY_AND_ASSIGN(TargetDescriptor);
55 TargetDescriptor::TargetDescriptor(scoped_refptr<DevToolsAgentHost> agent_host)
56 : BasicTargetDescriptor(agent_host) {
57 if (WebContents* web_contents = agent_host->GetWebContents())
58 description_ = GetViewDescription(web_contents);
61 } // namespace
63 namespace android_webview {
65 // static
66 void AwDevToolsDiscoveryProvider::Install() {
67 devtools_discovery::DevToolsDiscoveryManager* discovery_manager =
68 devtools_discovery::DevToolsDiscoveryManager::GetInstance();
69 discovery_manager->AddProvider(
70 make_scoped_ptr(new AwDevToolsDiscoveryProvider()));
73 AwDevToolsDiscoveryProvider::AwDevToolsDiscoveryProvider() {
76 AwDevToolsDiscoveryProvider::~AwDevToolsDiscoveryProvider() {
79 devtools_discovery::DevToolsTargetDescriptor::List
80 AwDevToolsDiscoveryProvider::GetDescriptors() {
81 DevToolsAgentHost::List agent_hosts = DevToolsAgentHost::GetOrCreateAll();
82 devtools_discovery::DevToolsTargetDescriptor::List result;
83 result.reserve(agent_hosts.size());
84 for (const auto& agent_host : agent_hosts)
85 result.push_back(new TargetDescriptor(agent_host));
86 return result;
89 } // namespace android_webview