OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "headless/lib/browser/headless_devtools_manager_delegate.h" | 5 #include "headless/lib/browser/headless_devtools_manager_delegate.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/devtools_agent_host.h" | 11 #include "content/public/browser/devtools_agent_host.h" |
12 #include "content/public/browser/devtools_frontend_host.h" | 12 #include "content/public/browser/devtools_frontend_host.h" |
13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
14 #include "headless/grit/headless_lib_resources.h" | 14 #include "headless/grit/headless_lib_resources.h" |
15 #include "headless/lib/browser/headless_browser_context_impl.h" | 15 #include "headless/lib/browser/headless_browser_context_impl.h" |
16 #include "headless/lib/browser/headless_browser_impl.h" | 16 #include "headless/lib/browser/headless_browser_impl.h" |
| 17 #include "headless/lib/browser/headless_download_manager_delegate.h" |
17 #include "headless/lib/browser/headless_web_contents_impl.h" | 18 #include "headless/lib/browser/headless_web_contents_impl.h" |
18 #include "headless/public/devtools/domains/target.h" | 19 #include "headless/public/devtools/domains/target.h" |
19 #include "printing/units.h" | 20 #include "printing/units.h" |
20 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
21 | 22 |
22 namespace headless { | 23 namespace headless { |
23 | 24 |
24 namespace { | 25 namespace { |
25 const char kIdParam[] = "id"; | 26 const char kIdParam[] = "id"; |
26 const char kResultParam[] = "result"; | 27 const char kResultParam[] = "result"; |
(...skipping 12 matching lines...) Expand all Loading... |
39 std::unique_ptr<base::Value> result) { | 40 std::unique_ptr<base::Value> result) { |
40 if (!result) | 41 if (!result) |
41 result = base::MakeUnique<base::DictionaryValue>(); | 42 result = base::MakeUnique<base::DictionaryValue>(); |
42 | 43 |
43 auto response = base::MakeUnique<base::DictionaryValue>(); | 44 auto response = base::MakeUnique<base::DictionaryValue>(); |
44 response->SetInteger(kIdParam, command_id); | 45 response->SetInteger(kIdParam, command_id); |
45 response->Set(kResultParam, std::move(result)); | 46 response->Set(kResultParam, std::move(result)); |
46 return response; | 47 return response; |
47 } | 48 } |
48 | 49 |
| 50 std::unique_ptr<base::DictionaryValue> CreateSuccessResponse(int command_id) { |
| 51 return CreateSuccessResponse(command_id, nullptr); |
| 52 } |
| 53 |
49 std::unique_ptr<base::DictionaryValue> CreateErrorResponse( | 54 std::unique_ptr<base::DictionaryValue> CreateErrorResponse( |
50 int command_id, | 55 int command_id, |
51 int error_code, | 56 int error_code, |
52 const std::string& error_message) { | 57 const std::string& error_message) { |
53 auto error_object = base::MakeUnique<base::DictionaryValue>(); | 58 auto error_object = base::MakeUnique<base::DictionaryValue>(); |
54 error_object->SetInteger(kErrorCodeParam, error_code); | 59 error_object->SetInteger(kErrorCodeParam, error_code); |
55 error_object->SetString(kErrorMessageParam, error_message); | 60 error_object->SetString(kErrorMessageParam, error_message); |
56 | 61 |
57 auto response = base::MakeUnique<base::DictionaryValue>(); | 62 auto response = base::MakeUnique<base::DictionaryValue>(); |
58 response->SetInteger(kIdParam, command_id); | 63 response->SetInteger(kIdParam, command_id); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 command_map_["Target.createTarget"] = base::Bind( | 167 command_map_["Target.createTarget"] = base::Bind( |
163 &HeadlessDevToolsManagerDelegate::CreateTarget, base::Unretained(this)); | 168 &HeadlessDevToolsManagerDelegate::CreateTarget, base::Unretained(this)); |
164 command_map_["Target.closeTarget"] = base::Bind( | 169 command_map_["Target.closeTarget"] = base::Bind( |
165 &HeadlessDevToolsManagerDelegate::CloseTarget, base::Unretained(this)); | 170 &HeadlessDevToolsManagerDelegate::CloseTarget, base::Unretained(this)); |
166 command_map_["Target.createBrowserContext"] = | 171 command_map_["Target.createBrowserContext"] = |
167 base::Bind(&HeadlessDevToolsManagerDelegate::CreateBrowserContext, | 172 base::Bind(&HeadlessDevToolsManagerDelegate::CreateBrowserContext, |
168 base::Unretained(this)); | 173 base::Unretained(this)); |
169 command_map_["Target.disposeBrowserContext"] = | 174 command_map_["Target.disposeBrowserContext"] = |
170 base::Bind(&HeadlessDevToolsManagerDelegate::DisposeBrowserContext, | 175 base::Bind(&HeadlessDevToolsManagerDelegate::DisposeBrowserContext, |
171 base::Unretained(this)); | 176 base::Unretained(this)); |
| 177 command_map_["Browser.setDownloadBehavior"] = |
| 178 base::Bind(&HeadlessDevToolsManagerDelegate::SetDownloadBehavior, |
| 179 base::Unretained(this)); |
| 180 command_map_["Browser.setDownloadDirectory"] = |
| 181 base::Bind(&HeadlessDevToolsManagerDelegate::SetDownloadDirectory, |
| 182 base::Unretained(this)); |
172 | 183 |
173 async_command_map_["Page.printToPDF"] = base::Bind( | 184 async_command_map_["Page.printToPDF"] = base::Bind( |
174 &HeadlessDevToolsManagerDelegate::PrintToPDF, base::Unretained(this)); | 185 &HeadlessDevToolsManagerDelegate::PrintToPDF, base::Unretained(this)); |
175 } | 186 } |
176 | 187 |
177 HeadlessDevToolsManagerDelegate::~HeadlessDevToolsManagerDelegate() {} | 188 HeadlessDevToolsManagerDelegate::~HeadlessDevToolsManagerDelegate() {} |
178 | 189 |
179 base::DictionaryValue* HeadlessDevToolsManagerDelegate::HandleCommand( | 190 base::DictionaryValue* HeadlessDevToolsManagerDelegate::HandleCommand( |
180 content::DevToolsAgentHost* agent_host, | 191 content::DevToolsAgentHost* agent_host, |
181 base::DictionaryValue* command) { | 192 base::DictionaryValue* command) { |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 } | 382 } |
372 | 383 |
373 std::unique_ptr<base::Value> result( | 384 std::unique_ptr<base::Value> result( |
374 target::DisposeBrowserContextResult::Builder() | 385 target::DisposeBrowserContextResult::Builder() |
375 .SetSuccess(success) | 386 .SetSuccess(success) |
376 .Build() | 387 .Build() |
377 ->Serialize()); | 388 ->Serialize()); |
378 return CreateSuccessResponse(command_id, std::move(result)); | 389 return CreateSuccessResponse(command_id, std::move(result)); |
379 } | 390 } |
380 | 391 |
| 392 std::unique_ptr<base::DictionaryValue> |
| 393 HeadlessDevToolsManagerDelegate::SetDownloadBehavior( |
| 394 int command_id, |
| 395 const base::DictionaryValue* params) { |
| 396 std::string behavior; |
| 397 if (!params || !params->GetString("behavior", &behavior)) |
| 398 return CreateInvalidParamResponse(command_id, "behavior"); |
| 399 |
| 400 HeadlessDownloadManagerDelegate::DownloadBehavior download_behaviour; |
| 401 if (behavior == "allow") { |
| 402 download_behaviour = |
| 403 HeadlessDownloadManagerDelegate::DownloadBehavior::ALLOW; |
| 404 } else if (behavior == "deny") { |
| 405 download_behaviour = |
| 406 HeadlessDownloadManagerDelegate::DownloadBehavior::DENY; |
| 407 } |
| 408 |
| 409 for (HeadlessBrowserContext* hbc : browser_->GetAllBrowserContexts()) { |
| 410 HeadlessBrowserContextImpl* hbci = |
| 411 static_cast<HeadlessBrowserContextImpl*>(hbc); |
| 412 |
| 413 HeadlessDownloadManagerDelegate* download_delegate = |
| 414 static_cast<HeadlessDownloadManagerDelegate*>( |
| 415 hbci->GetDownloadManagerDelegate()); |
| 416 |
| 417 download_delegate->SetDownloadBehavior(download_behaviour); |
| 418 } |
| 419 |
| 420 return CreateSuccessResponse(command_id); |
| 421 } |
| 422 |
| 423 std::unique_ptr<base::DictionaryValue> |
| 424 HeadlessDevToolsManagerDelegate::SetDownloadDirectory( |
| 425 int command_id, |
| 426 const base::DictionaryValue* params) { |
| 427 std::string path; |
| 428 if (!params || !params->GetString("path", &path)) |
| 429 return CreateInvalidParamResponse(command_id, "path"); |
| 430 |
| 431 for (HeadlessBrowserContext* hbc : browser_->GetAllBrowserContexts()) { |
| 432 HeadlessBrowserContextImpl* hbci = |
| 433 static_cast<HeadlessBrowserContextImpl*>(hbc); |
| 434 |
| 435 HeadlessDownloadManagerDelegate* download_delegate = |
| 436 static_cast<HeadlessDownloadManagerDelegate*>( |
| 437 hbci->GetDownloadManagerDelegate()); |
| 438 |
| 439 download_delegate->SetDownloadDirectory(base::FilePath(path)); |
| 440 } |
| 441 return CreateSuccessResponse(command_id); |
| 442 } |
| 443 |
381 } // namespace headless | 444 } // namespace headless |
OLD | NEW |