Chromium Code Reviews| 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 17 matching lines...) Expand all Loading... | |
| 44 std::unique_ptr<base::Value> result) { | 45 std::unique_ptr<base::Value> result) { |
| 45 if (!result) | 46 if (!result) |
| 46 result = base::MakeUnique<base::DictionaryValue>(); | 47 result = base::MakeUnique<base::DictionaryValue>(); |
| 47 | 48 |
| 48 auto response = base::MakeUnique<base::DictionaryValue>(); | 49 auto response = base::MakeUnique<base::DictionaryValue>(); |
| 49 response->SetInteger(kIdParam, command_id); | 50 response->SetInteger(kIdParam, command_id); |
| 50 response->Set(kResultParam, std::move(result)); | 51 response->Set(kResultParam, std::move(result)); |
| 51 return response; | 52 return response; |
| 52 } | 53 } |
| 53 | 54 |
| 55 std::unique_ptr<base::DictionaryValue> CreateSuccessResponse(int command_id) { | |
| 56 return CreateSuccessResponse(command_id, nullptr); | |
| 57 } | |
| 58 | |
| 54 std::unique_ptr<base::DictionaryValue> CreateErrorResponse( | 59 std::unique_ptr<base::DictionaryValue> CreateErrorResponse( |
| 55 int command_id, | 60 int command_id, |
| 56 int error_code, | 61 int error_code, |
| 57 const std::string& error_message) { | 62 const std::string& error_message) { |
| 58 auto error_object = base::MakeUnique<base::DictionaryValue>(); | 63 auto error_object = base::MakeUnique<base::DictionaryValue>(); |
| 59 error_object->SetInteger(kErrorCodeParam, error_code); | 64 error_object->SetInteger(kErrorCodeParam, error_code); |
| 60 error_object->SetString(kErrorMessageParam, error_message); | 65 error_object->SetString(kErrorMessageParam, error_message); |
| 61 | 66 |
| 62 auto response = base::MakeUnique<base::DictionaryValue>(); | 67 auto response = base::MakeUnique<base::DictionaryValue>(); |
| 63 response->SetInteger(kIdParam, command_id); | 68 response->SetInteger(kIdParam, command_id); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 command_map_["Target.createTarget"] = base::Bind( | 165 command_map_["Target.createTarget"] = base::Bind( |
| 161 &HeadlessDevToolsManagerDelegate::CreateTarget, base::Unretained(this)); | 166 &HeadlessDevToolsManagerDelegate::CreateTarget, base::Unretained(this)); |
| 162 command_map_["Target.closeTarget"] = base::Bind( | 167 command_map_["Target.closeTarget"] = base::Bind( |
| 163 &HeadlessDevToolsManagerDelegate::CloseTarget, base::Unretained(this)); | 168 &HeadlessDevToolsManagerDelegate::CloseTarget, base::Unretained(this)); |
| 164 command_map_["Target.createBrowserContext"] = | 169 command_map_["Target.createBrowserContext"] = |
| 165 base::Bind(&HeadlessDevToolsManagerDelegate::CreateBrowserContext, | 170 base::Bind(&HeadlessDevToolsManagerDelegate::CreateBrowserContext, |
| 166 base::Unretained(this)); | 171 base::Unretained(this)); |
| 167 command_map_["Target.disposeBrowserContext"] = | 172 command_map_["Target.disposeBrowserContext"] = |
| 168 base::Bind(&HeadlessDevToolsManagerDelegate::DisposeBrowserContext, | 173 base::Bind(&HeadlessDevToolsManagerDelegate::DisposeBrowserContext, |
| 169 base::Unretained(this)); | 174 base::Unretained(this)); |
| 175 command_map_["Browser.setDownloadBehavior"] = | |
| 176 base::Bind(&HeadlessDevToolsManagerDelegate::SetDownloadBehavior, | |
| 177 base::Unretained(this)); | |
| 178 command_map_["Browser.setDownloadDirectory"] = | |
| 179 base::Bind(&HeadlessDevToolsManagerDelegate::SetDownloadDirectory, | |
| 180 base::Unretained(this)); | |
| 170 | 181 |
| 171 async_command_map_["Page.printToPDF"] = base::Bind( | 182 async_command_map_["Page.printToPDF"] = base::Bind( |
| 172 &HeadlessDevToolsManagerDelegate::PrintToPDF, base::Unretained(this)); | 183 &HeadlessDevToolsManagerDelegate::PrintToPDF, base::Unretained(this)); |
| 173 } | 184 } |
| 174 | 185 |
| 175 HeadlessDevToolsManagerDelegate::~HeadlessDevToolsManagerDelegate() {} | 186 HeadlessDevToolsManagerDelegate::~HeadlessDevToolsManagerDelegate() {} |
| 176 | 187 |
| 177 base::DictionaryValue* HeadlessDevToolsManagerDelegate::HandleCommand( | 188 base::DictionaryValue* HeadlessDevToolsManagerDelegate::HandleCommand( |
| 178 content::DevToolsAgentHost* agent_host, | 189 content::DevToolsAgentHost* agent_host, |
| 179 base::DictionaryValue* command) { | 190 base::DictionaryValue* command) { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 369 } | 380 } |
| 370 | 381 |
| 371 std::unique_ptr<base::Value> result( | 382 std::unique_ptr<base::Value> result( |
| 372 target::DisposeBrowserContextResult::Builder() | 383 target::DisposeBrowserContextResult::Builder() |
| 373 .SetSuccess(success) | 384 .SetSuccess(success) |
| 374 .Build() | 385 .Build() |
| 375 ->Serialize()); | 386 ->Serialize()); |
| 376 return CreateSuccessResponse(command_id, std::move(result)); | 387 return CreateSuccessResponse(command_id, std::move(result)); |
| 377 } | 388 } |
| 378 | 389 |
| 390 std::unique_ptr<base::DictionaryValue> | |
| 391 HeadlessDevToolsManagerDelegate::SetDownloadBehavior( | |
| 392 int command_id, | |
| 393 const base::DictionaryValue* params) { | |
| 394 std::string behavior; | |
| 395 if (!params || !params->GetString("behavior", &behavior)) | |
| 396 return CreateInvalidParamResponse(command_id, "behavior"); | |
| 397 | |
| 398 for (HeadlessBrowserContext* hbc : browser_->GetAllBrowserContexts()) { | |
| 399 HeadlessBrowserContextImpl* hbci = | |
| 400 static_cast<HeadlessBrowserContextImpl*>(hbc); | |
| 401 | |
| 402 HeadlessDownloadManagerDelegate* download_delegate = | |
| 403 static_cast<HeadlessDownloadManagerDelegate*>( | |
| 404 hbci->GetDownloadManagerDelegate()); | |
| 405 | |
| 406 download_delegate->SetDownloadBehavior(behavior); | |
|
Sami
2017/05/17 13:20:40
Do we also need to apply this to contexts that get
Sami
2017/05/19 13:57:26
We could store the setting somewhere in |browser_|
| |
| 407 } | |
| 408 | |
| 409 return CreateSuccessResponse(command_id); | |
| 410 } | |
| 411 | |
| 412 std::unique_ptr<base::DictionaryValue> | |
| 413 HeadlessDevToolsManagerDelegate::SetDownloadDirectory( | |
| 414 int command_id, | |
| 415 const base::DictionaryValue* params) { | |
| 416 std::string path; | |
| 417 if (!params || !params->GetString("path", &path)) | |
| 418 return CreateInvalidParamResponse(command_id, "path"); | |
| 419 | |
| 420 for (HeadlessBrowserContext* hbc : browser_->GetAllBrowserContexts()) { | |
| 421 HeadlessBrowserContextImpl* hbci = | |
| 422 static_cast<HeadlessBrowserContextImpl*>(hbc); | |
| 423 | |
| 424 HeadlessDownloadManagerDelegate* download_delegate = | |
| 425 static_cast<HeadlessDownloadManagerDelegate*>( | |
| 426 hbci->GetDownloadManagerDelegate()); | |
| 427 | |
| 428 download_delegate->SetDownloadDirectory(path); | |
|
Sami
2017/05/17 13:20:40
Same question here.
| |
| 429 } | |
| 430 return CreateSuccessResponse(command_id); | |
| 431 } | |
| 432 | |
| 379 } // namespace headless | 433 } // namespace headless |
| OLD | NEW |