summaryrefslogtreecommitdiffstats
path: root/content/browser/devtools/renderer_overrides_handler.cc
blob: f7db2e89e9528bb567d4a03695aa750821d32f52 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2013 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 "content/browser/devtools/renderer_overrides_handler.h"

#include <string>

#include "base/base64.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/file_path.h"
#include "base/string16.h"
#include "base/stringprintf.h"
#include "base/values.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/devtools/devtools_protocol_constants.h"
#include "content/browser/renderer_host/render_view_host_delegate.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/javascript_dialog_manager.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/page_transition_types.h"
#include "content/public/common/referrer.h"
#include "googleurl/src/gurl.h"
#include "ui/snapshot/snapshot.h"

namespace content {

RendererOverridesHandler::RendererOverridesHandler(DevToolsAgentHost* agent)
    : agent_(agent) {
  RegisterCommandHandler(
      devtools::DOM::setFileInputFiles::kName,
      base::Bind(
          &RendererOverridesHandler::GrantPermissionsForSetFileInputFiles,
          base::Unretained(this)));
  RegisterCommandHandler(
      devtools::Page::handleJavaScriptDialog::kName,
      base::Bind(
          &RendererOverridesHandler::PageHandleJavaScriptDialog,
          base::Unretained(this)));
  RegisterCommandHandler(
      devtools::Page::navigate::kName,
      base::Bind(
          &RendererOverridesHandler::PageNavigate,
          base::Unretained(this)));
  RegisterCommandHandler(
      devtools::Page::captureScreenshot::kName,
      base::Bind(
          &RendererOverridesHandler::PageCaptureScreenshot,
          base::Unretained(this)));
}

RendererOverridesHandler::~RendererOverridesHandler() {}

scoped_ptr<DevToolsProtocol::Response>
RendererOverridesHandler::GrantPermissionsForSetFileInputFiles(
    DevToolsProtocol::Command* command) {
  base::DictionaryValue* params = command->params();
  base::ListValue* file_list = NULL;
  const char* param =
      devtools::DOM::setFileInputFiles::kParamFiles;
  if (!params || !params->GetList(param, &file_list)) {
    return command->ErrorResponse(
        DevToolsProtocol::kErrorInvalidParams,
        base::StringPrintf("Missing or invalid '%s' parameter", param));
  }
  RenderViewHost* host = agent_->GetRenderViewHost();
  if (!host)
    return scoped_ptr<DevToolsProtocol::Response>();

  for (size_t i = 0; i < file_list->GetSize(); ++i) {
    base::FilePath::StringType file;
    if (!file_list->GetString(i, &file)) {
      return command->ErrorResponse(
          DevToolsProtocol::kErrorInvalidParams,
          base::StringPrintf("'%s' must be a list of strings", param));
    }
    ChildProcessSecurityPolicyImpl::GetInstance()->GrantReadFile(
        host->GetProcess()->GetID(), base::FilePath(file));
  }
  return scoped_ptr<DevToolsProtocol::Response>();
}

scoped_ptr<DevToolsProtocol::Response>
RendererOverridesHandler::PageHandleJavaScriptDialog(
    DevToolsProtocol::Command* command) {
  base::DictionaryValue* params = command->params();
  const char* paramAccept =
      devtools::Page::handleJavaScriptDialog::kParamAccept;
  bool accept;
  if (!params || !params->GetBoolean(paramAccept, &accept)) {
    return command->ErrorResponse(
        DevToolsProtocol::kErrorInvalidParams,
        base::StringPrintf("Missing or invalid '%s' parameter", paramAccept));
  }
  string16 prompt_override;
  string16* prompt_override_ptr = &prompt_override;
  if (!params || !params->GetString(
      devtools::Page::handleJavaScriptDialog::kParamPromptText,
      prompt_override_ptr)) {
    prompt_override_ptr = NULL;
  }

  RenderViewHost* host = agent_->GetRenderViewHost();
  if (host) {
    WebContents* web_contents = host->GetDelegate()->GetAsWebContents();
    if (web_contents) {
      JavaScriptDialogManager* manager =
          web_contents->GetDelegate()->GetJavaScriptDialogManager();
      if (manager && manager->HandleJavaScriptDialog(
              web_contents, accept, prompt_override_ptr)) {
        return scoped_ptr<DevToolsProtocol::Response>();
      }
    }
  }
  return command->ErrorResponse(
      DevToolsProtocol::kErrorInternalError,
      "No JavaScript dialog to handle");
}

scoped_ptr<DevToolsProtocol::Response>
RendererOverridesHandler::PageNavigate(
    DevToolsProtocol::Command* command) {
  base::DictionaryValue* params = command->params();
  std::string url;
  const char* param = devtools::Page::navigate::kParamUrl;
  if (!params || !params->GetString(param, &url)) {
    return command->ErrorResponse(
        DevToolsProtocol::kErrorInvalidParams,
        base::StringPrintf("Missing or invalid '%s' parameter",
                           param));
  }
  GURL gurl(url);
  if (!gurl.is_valid()) {
    return command->ErrorResponse(
        DevToolsProtocol::kErrorInternalError,
        "Cannot navigate to invalid URL");
  }
  RenderViewHost* host = agent_->GetRenderViewHost();
  if (host) {
    WebContents* web_contents = host->GetDelegate()->GetAsWebContents();
    if (web_contents) {
      web_contents->GetController()
          .LoadURL(gurl, Referrer(), PAGE_TRANSITION_TYPED, std::string());
      return command->SuccessResponse(new base::DictionaryValue());
    }
  }
  return command->ErrorResponse(
      DevToolsProtocol::kErrorInternalError,
      "No WebContents to navigate");
}

scoped_ptr<DevToolsProtocol::Response>
RendererOverridesHandler::PageCaptureScreenshot(
    DevToolsProtocol::Command* command) {
  std::string base_64_data;
  if (!CaptureScreenshot(&base_64_data))
    return command->ErrorResponse(DevToolsProtocol::kErrorInternalError,
                                  "Unable to capture a screenshot");

  base::DictionaryValue* response = new base::DictionaryValue();
  response->SetString(
      devtools::Page::captureScreenshot::kResponseData, base_64_data);
  return command->SuccessResponse(response);
}

bool RendererOverridesHandler::CaptureScreenshot(std::string* base_64_data) {
  RenderViewHost* host = agent_->GetRenderViewHost();
  gfx::Rect view_bounds = host->GetView()->GetViewBounds();
  gfx::Rect snapshot_bounds(view_bounds.size());
  gfx::Size snapshot_size = snapshot_bounds.size();
  std::vector<unsigned char> png;
  if (!ui::GrabViewSnapshot(host->GetView()->GetNativeView(),
                            &png,
                            snapshot_bounds))
    return false;

  return base::Base64Encode(base::StringPiece(
                                reinterpret_cast<char*>(&*png.begin()),
                                png.size()),
                            base_64_data);
}

}  // namespace content