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

android_webview / browser / aw_devtools_manager_delegate.cc [blame]

// Copyright 2013 The Chromium Authors
// 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_devtools_manager_delegate.h"

#include "android_webview/browser/gfx/browser_view_renderer.h"
#include "android_webview/common/aw_content_client.h"
#include "base/json/json_writer.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/web_contents.h"

using content::DevToolsAgentHost;

namespace android_webview {

AwDevToolsManagerDelegate::AwDevToolsManagerDelegate() = default;

AwDevToolsManagerDelegate::~AwDevToolsManagerDelegate() = default;

std::string AwDevToolsManagerDelegate::GetTargetDescription(
    content::WebContents* web_contents) {
  android_webview::BrowserViewRenderer* bvr =
      android_webview::BrowserViewRenderer::FromWebContents(web_contents);
  if (!bvr)
    return "";
  base::Value::Dict description;
  description.Set("attached", bvr->attached_to_window());
  description.Set("never_attached", !bvr->was_attached());
  description.Set("visible", bvr->IsVisible());
  gfx::Rect screen_rect = bvr->GetScreenRect();
  description.Set("screenX", screen_rect.x());
  description.Set("screenY", screen_rect.y());
  description.Set("empty", screen_rect.size().IsEmpty());
  if (!screen_rect.size().IsEmpty()) {
    description.Set("width", screen_rect.width());
    description.Set("height", screen_rect.height());
  }
  std::string json;
  base::JSONWriter::Write(description, &json);
  return json;
}

std::string AwDevToolsManagerDelegate::GetDiscoveryPageHTML() {
  const char html[] =
      "<html>"
      "<head><title>WebView remote debugging</title></head>"
      "<body>Please use <a href=\'chrome://inspect\'>chrome://inspect</a>"
      "</body>"
      "</html>";
  return html;
}

bool AwDevToolsManagerDelegate::IsBrowserTargetDiscoverable() {
  return true;
}

content::DevToolsAgentHost::List
AwDevToolsManagerDelegate::RemoteDebuggingTargets(TargetType target_type) {
  DevToolsAgentHost::List result;
  std::set<content::WebContents*> targets_web_contents;
  DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll();
  for (DevToolsAgentHost::List::iterator it = agents.begin();
       it != agents.end(); ++it) {
    if (content::WebContents* web_contents = (*it)->GetWebContents()) {
      if (targets_web_contents.find(web_contents) !=
          targets_web_contents.end()) {
        continue;
      }
      targets_web_contents.insert(web_contents);
    }
    result.push_back(*it);
  }
  return result;
}
}  // namespace android_webview