summaryrefslogtreecommitdiffstats
path: root/android_webview/browser/aw_dev_tools_discovery_provider.cc
blob: fe1e61e8cf87fca6cc21250314995b40583471c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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 "android_webview/browser/aw_dev_tools_discovery_provider.h"

#include "android_webview/browser/browser_view_renderer.h"
#include "base/json/json_writer.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "components/devtools_discovery/basic_target_descriptor.h"
#include "components/devtools_discovery/devtools_discovery_manager.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/web_contents.h"

using content::DevToolsAgentHost;
using content::WebContents;

namespace {

std::string GetViewDescription(WebContents* web_contents) {
  android_webview::BrowserViewRenderer* bvr =
      android_webview::BrowserViewRenderer::FromWebContents(web_contents);
  if (!bvr) return "";
  base::DictionaryValue description;
  description.SetBoolean("attached", bvr->attached_to_window());
  description.SetBoolean("visible", bvr->IsVisible());
  gfx::Rect screen_rect = bvr->GetScreenRect();
  description.SetInteger("screenX", screen_rect.x());
  description.SetInteger("screenY", screen_rect.y());
  description.SetBoolean("empty", screen_rect.size().IsEmpty());
  if (!screen_rect.size().IsEmpty()) {
    description.SetInteger("width", screen_rect.width());
    description.SetInteger("height", screen_rect.height());
  }
  std::string json;
  base::JSONWriter::Write(description, &json);
  return json;
}

class TargetDescriptor : public devtools_discovery::BasicTargetDescriptor {
 public:
  explicit TargetDescriptor(scoped_refptr<DevToolsAgentHost> agent_host);

  // devtools_discovery::BasicTargetDescriptor overrides.
  std::string GetDescription() const override { return description_; }

 private:
  std::string description_;

  DISALLOW_COPY_AND_ASSIGN(TargetDescriptor);
};

TargetDescriptor::TargetDescriptor(scoped_refptr<DevToolsAgentHost> agent_host)
    : BasicTargetDescriptor(agent_host) {
  if (WebContents* web_contents = agent_host->GetWebContents())
    description_ = GetViewDescription(web_contents);
}

}  // namespace

namespace android_webview {

// static
void AwDevToolsDiscoveryProvider::Install() {
  devtools_discovery::DevToolsDiscoveryManager* discovery_manager =
      devtools_discovery::DevToolsDiscoveryManager::GetInstance();
  discovery_manager->AddProvider(
      make_scoped_ptr(new AwDevToolsDiscoveryProvider()));
}

AwDevToolsDiscoveryProvider::AwDevToolsDiscoveryProvider() {
}

AwDevToolsDiscoveryProvider::~AwDevToolsDiscoveryProvider() {
}

devtools_discovery::DevToolsTargetDescriptor::List
AwDevToolsDiscoveryProvider::GetDescriptors() {
  DevToolsAgentHost::List agent_hosts = DevToolsAgentHost::GetOrCreateAll();
  devtools_discovery::DevToolsTargetDescriptor::List result;
  result.reserve(agent_hosts.size());
  for (const auto& agent_host : agent_hosts)
    result.push_back(new TargetDescriptor(agent_host));
  return result;
}

}  // namespace android_webview