1// Copyright 2014 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 "android_webview/browser/aw_dev_tools_manager_delegate.h"
6
7#include "android_webview/native/aw_contents.h"
8#include "base/bind.h"
9#include "base/json/json_writer.h"
10#include "base/strings/stringprintf.h"
11#include "base/strings/utf_string_conversions.h"
12#include "base/values.h"
13#include "content/public/browser/devtools_agent_host.h"
14#include "content/public/browser/devtools_target.h"
15#include "content/public/browser/web_contents.h"
16
17using content::DevToolsAgentHost;
18using content::RenderViewHost;
19using content::WebContents;
20
21namespace {
22
23const char kTargetTypePage[] = "page";
24const char kTargetTypeServiceWorker[] = "service_worker";
25const char kTargetTypeOther[] = "other";
26
27std::string GetViewDescription(WebContents* web_contents);
28
29class Target : public content::DevToolsTarget {
30 public:
31  explicit Target(scoped_refptr<DevToolsAgentHost> agent_host);
32
33  virtual std::string GetId() const OVERRIDE { return agent_host_->GetId(); }
34  virtual std::string GetParentId() const OVERRIDE { return std::string(); }
35  virtual std::string GetType() const OVERRIDE {
36    switch (agent_host_->GetType()) {
37      case DevToolsAgentHost::TYPE_WEB_CONTENTS:
38        return kTargetTypePage;
39      case DevToolsAgentHost::TYPE_SERVICE_WORKER:
40        return kTargetTypeServiceWorker;
41      default:
42        break;
43    }
44    return kTargetTypeOther;
45  }
46  virtual std::string GetTitle() const OVERRIDE {
47    return agent_host_->GetTitle();
48  }
49  virtual std::string GetDescription() const OVERRIDE { return description_; }
50  virtual GURL GetURL() const OVERRIDE { return agent_host_->GetURL(); }
51  virtual GURL GetFaviconURL() const OVERRIDE { return GURL(); }
52  virtual base::TimeTicks GetLastActivityTime() const OVERRIDE {
53    return last_activity_time_;
54  }
55  virtual bool IsAttached() const OVERRIDE {
56    return agent_host_->IsAttached();
57  }
58  virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE {
59    return agent_host_;
60  }
61  virtual bool Activate() const OVERRIDE { return agent_host_->Activate(); }
62  virtual bool Close() const OVERRIDE { return agent_host_->Close(); }
63
64 private:
65  scoped_refptr<DevToolsAgentHost> agent_host_;
66  std::string description_;
67  base::TimeTicks last_activity_time_;
68};
69
70Target::Target(scoped_refptr<DevToolsAgentHost> agent_host)
71    : agent_host_(agent_host) {
72  if (WebContents* web_contents = agent_host->GetWebContents()) {
73    description_ = GetViewDescription(web_contents);
74    last_activity_time_ = web_contents->GetLastActiveTime();
75  }
76}
77
78std::string GetViewDescription(WebContents* web_contents) {
79  const android_webview::BrowserViewRenderer* bvr =
80      android_webview::AwContents::FromWebContents(web_contents)
81          ->GetBrowserViewRenderer();
82  if (!bvr) return "";
83  base::DictionaryValue description;
84  description.SetBoolean("attached", bvr->attached_to_window());
85  description.SetBoolean("visible", bvr->IsVisible());
86  gfx::Rect screen_rect = bvr->GetScreenRect();
87  description.SetInteger("screenX", screen_rect.x());
88  description.SetInteger("screenY", screen_rect.y());
89  description.SetBoolean("empty", screen_rect.size().IsEmpty());
90  if (!screen_rect.size().IsEmpty()) {
91    description.SetInteger("width", screen_rect.width());
92    description.SetInteger("height", screen_rect.height());
93  }
94  std::string json;
95  base::JSONWriter::Write(&description, &json);
96  return json;
97}
98
99}  // namespace
100
101namespace android_webview {
102
103AwDevToolsManagerDelegate::AwDevToolsManagerDelegate() {
104}
105
106AwDevToolsManagerDelegate::~AwDevToolsManagerDelegate() {
107}
108
109base::DictionaryValue* AwDevToolsManagerDelegate::HandleCommand(
110    content::DevToolsAgentHost* agent_host,
111    base::DictionaryValue* command_dict) {
112  return NULL;
113}
114
115void AwDevToolsManagerDelegate::EnumerateTargets(TargetCallback callback) {
116  TargetList targets;
117  DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll();
118  for (DevToolsAgentHost::List::iterator it = agents.begin();
119      it != agents.end(); ++it) {
120    targets.push_back(new Target(*it));
121  }
122  callback.Run(targets);
123}
124
125std::string AwDevToolsManagerDelegate::GetPageThumbnailData(const GURL&) {
126  return "";
127}
128
129scoped_ptr<content::DevToolsTarget> AwDevToolsManagerDelegate::CreateNewTarget(
130    const GURL&) {
131  return scoped_ptr<content::DevToolsTarget>();
132}
133
134}  // namespace android_webview
135