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 "base/base64.h" | |
|
Eric Seckler
2017/03/29 11:21:19
nit: unnecessary?
jzfeng
2017/03/30 03:04:57
Done.
| |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/devtools_agent_host.h" | 12 #include "content/public/browser/devtools_agent_host.h" |
| 12 #include "content/public/browser/devtools_frontend_host.h" | 13 #include "content/public/browser/devtools_frontend_host.h" |
| 13 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 14 #include "headless/grit/headless_lib_resources.h" | 15 #include "headless/grit/headless_lib_resources.h" |
| 15 #include "headless/lib/browser/headless_browser_context_impl.h" | 16 #include "headless/lib/browser/headless_browser_context_impl.h" |
| 16 #include "headless/lib/browser/headless_browser_impl.h" | 17 #include "headless/lib/browser/headless_browser_impl.h" |
| 18 #include "headless/lib/browser/headless_print_manager.h" | |
| 17 #include "headless/lib/browser/headless_web_contents_impl.h" | 19 #include "headless/lib/browser/headless_web_contents_impl.h" |
| 18 #include "headless/public/devtools/domains/target.h" | 20 #include "headless/public/devtools/domains/target.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 20 | 22 |
| 21 namespace headless { | 23 namespace headless { |
| 22 | 24 |
| 23 namespace { | 25 namespace { |
| 24 const char kIdParam[] = "id"; | 26 const char kIdParam[] = "id"; |
| 25 const char kResultParam[] = "result"; | 27 const char kResultParam[] = "result"; |
| 26 const char kErrorParam[] = "error"; | 28 const char kErrorParam[] = "error"; |
| 27 const char kErrorCodeParam[] = "code"; | 29 const char kErrorCodeParam[] = "code"; |
| 28 const char kErrorMessageParam[] = "message"; | 30 const char kErrorMessageParam[] = "message"; |
| 29 | 31 |
| 30 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object | 32 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object |
| 31 enum Error { | 33 enum Error { |
| 32 kErrorInvalidParam = -32602, | 34 kErrorInvalidParam = -32602, |
| 33 kErrorServerError = -32000 | 35 kErrorServerError = -32000 |
| 34 }; | 36 }; |
| 35 | 37 |
| 36 std::unique_ptr<base::DictionaryValue> CreateSuccessResponse( | 38 std::unique_ptr<base::DictionaryValue> CreateSuccessResponse( |
| 37 int command_id, | 39 int command_id, |
| 38 std::unique_ptr<base::Value> result) { | 40 std::unique_ptr<base::Value> result) { |
| 39 if (!result) | 41 if (!result) |
| 40 result.reset(new base::DictionaryValue()); | 42 result = base::MakeUnique<base::DictionaryValue>(); |
| 41 | 43 |
| 42 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 44 auto response = base::MakeUnique<base::DictionaryValue>(); |
| 43 response->SetInteger(kIdParam, command_id); | 45 response->SetInteger(kIdParam, command_id); |
| 44 response->Set(kResultParam, std::move(result)); | 46 response->Set(kResultParam, std::move(result)); |
| 45 return response; | 47 return response; |
| 46 } | 48 } |
| 47 | 49 |
| 48 std::unique_ptr<base::DictionaryValue> CreateErrorResponse( | 50 std::unique_ptr<base::DictionaryValue> CreateErrorResponse( |
| 49 int command_id, | 51 int command_id, |
| 50 int error_code, | 52 int error_code, |
| 51 const std::string& error_message) { | 53 const std::string& error_message) { |
| 52 std::unique_ptr<base::DictionaryValue> error_object( | 54 auto error_object = base::MakeUnique<base::DictionaryValue>(); |
| 53 new base::DictionaryValue()); | |
| 54 error_object->SetInteger(kErrorCodeParam, error_code); | 55 error_object->SetInteger(kErrorCodeParam, error_code); |
| 55 error_object->SetString(kErrorMessageParam, error_message); | 56 error_object->SetString(kErrorMessageParam, error_message); |
| 56 | 57 |
| 57 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 58 auto response = base::MakeUnique<base::DictionaryValue>(); |
| 59 response->SetInteger(kIdParam, command_id); | |
| 58 response->Set(kErrorParam, std::move(error_object)); | 60 response->Set(kErrorParam, std::move(error_object)); |
| 59 return response; | 61 return response; |
| 60 } | 62 } |
| 61 | 63 |
| 62 std::unique_ptr<base::DictionaryValue> CreateInvalidParamResponse( | 64 std::unique_ptr<base::DictionaryValue> CreateInvalidParamResponse( |
| 63 int command_id, | 65 int command_id, |
| 64 const std::string& param) { | 66 const std::string& param) { |
| 65 return CreateErrorResponse( | 67 return CreateErrorResponse( |
| 66 command_id, kErrorInvalidParam, | 68 command_id, kErrorInvalidParam, |
| 67 base::StringPrintf("Missing or invalid '%s' parameter", param.c_str())); | 69 base::StringPrintf("Missing or invalid '%s' parameter", param.c_str())); |
| 68 } | 70 } |
| 71 | |
| 72 void PDFCreated(content::DevToolsManagerDelegate::CommandCallback callback, | |
| 73 int command_id, | |
| 74 printing::HeadlessPrintManager::PrintResult print_result, | |
| 75 const std::string& data) { | |
| 76 std::unique_ptr<base::DictionaryValue> response; | |
| 77 if (print_result == printing::HeadlessPrintManager::kPrintSuccess) { | |
| 78 response = CreateSuccessResponse( | |
| 79 command_id, | |
| 80 printing::HeadlessPrintManager::PDFContentsToDictionaryValue(data)); | |
| 81 } else { | |
| 82 response = CreateErrorResponse( | |
| 83 command_id, kErrorServerError, | |
| 84 printing::HeadlessPrintManager::PrintResultToErrMsg(print_result)); | |
| 85 } | |
| 86 callback.Run(std::move(response)); | |
| 87 } | |
| 88 | |
| 69 } // namespace | 89 } // namespace |
| 70 | 90 |
| 71 HeadlessDevToolsManagerDelegate::HeadlessDevToolsManagerDelegate( | 91 HeadlessDevToolsManagerDelegate::HeadlessDevToolsManagerDelegate( |
| 72 base::WeakPtr<HeadlessBrowserImpl> browser) | 92 base::WeakPtr<HeadlessBrowserImpl> browser) |
| 73 : browser_(std::move(browser)) { | 93 : browser_(std::move(browser)) { |
| 74 command_map_["Target.createTarget"] = | 94 command_map_["Target.createTarget"] = |
| 75 &HeadlessDevToolsManagerDelegate::CreateTarget; | 95 &HeadlessDevToolsManagerDelegate::CreateTarget; |
| 76 command_map_["Target.closeTarget"] = | 96 command_map_["Target.closeTarget"] = |
| 77 &HeadlessDevToolsManagerDelegate::CloseTarget; | 97 &HeadlessDevToolsManagerDelegate::CloseTarget; |
| 78 command_map_["Target.createBrowserContext"] = | 98 command_map_["Target.createBrowserContext"] = |
| 79 &HeadlessDevToolsManagerDelegate::CreateBrowserContext; | 99 &HeadlessDevToolsManagerDelegate::CreateBrowserContext; |
| 80 command_map_["Target.disposeBrowserContext"] = | 100 command_map_["Target.disposeBrowserContext"] = |
| 81 &HeadlessDevToolsManagerDelegate::DisposeBrowserContext; | 101 &HeadlessDevToolsManagerDelegate::DisposeBrowserContext; |
| 102 | |
| 103 async_command_map_["Page.printToPDF"] = | |
| 104 &HeadlessDevToolsManagerDelegate::PrintToPDF; | |
| 82 } | 105 } |
| 83 | 106 |
| 84 HeadlessDevToolsManagerDelegate::~HeadlessDevToolsManagerDelegate() {} | 107 HeadlessDevToolsManagerDelegate::~HeadlessDevToolsManagerDelegate() {} |
| 85 | 108 |
| 86 base::DictionaryValue* HeadlessDevToolsManagerDelegate::HandleCommand( | 109 base::DictionaryValue* HeadlessDevToolsManagerDelegate::HandleCommand( |
| 87 content::DevToolsAgentHost* agent_host, | 110 content::DevToolsAgentHost* agent_host, |
| 88 base::DictionaryValue* command) { | 111 base::DictionaryValue* command) { |
| 89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 90 | 113 |
| 91 if (!browser_) | 114 if (!browser_) |
| 92 return nullptr; | 115 return nullptr; |
| 93 | 116 |
| 94 int id; | 117 int id; |
| 95 std::string method; | 118 std::string method; |
| 96 if (!command->GetInteger("id", &id) || | 119 if (!command->GetInteger("id", &id) || |
| 97 !command->GetString("method", &method)) { | 120 !command->GetString("method", &method)) { |
| 98 return nullptr; | 121 return nullptr; |
| 99 } | 122 } |
| 100 auto find_it = command_map_.find(method); | 123 auto find_it = command_map_.find(method); |
| 101 if (find_it == command_map_.end()) | 124 if (find_it == command_map_.end()) |
| 102 return nullptr; | 125 return nullptr; |
| 103 CommandMemberFnPtr command_fn_ptr = find_it->second; | 126 CommandMemberFnPtr command_fn_ptr = find_it->second; |
| 104 const base::DictionaryValue* params = nullptr; | 127 const base::DictionaryValue* params = nullptr; |
| 105 command->GetDictionary("params", ¶ms); | 128 command->GetDictionary("params", ¶ms); |
| 106 std::unique_ptr<base::DictionaryValue> cmd_result( | 129 std::unique_ptr<base::DictionaryValue> cmd_result( |
| 107 ((this)->*command_fn_ptr)(id, params)); | 130 (this->*command_fn_ptr)(id, params)); |
| 108 return cmd_result.release(); | 131 return cmd_result.release(); |
| 109 } | 132 } |
| 110 | 133 |
| 134 bool HeadlessDevToolsManagerDelegate::HandleAsyncCommand( | |
| 135 content::DevToolsAgentHost* agent_host, | |
| 136 base::DictionaryValue* command, | |
| 137 const CommandCallback& callback) { | |
| 138 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 139 | |
| 140 if (!browser_) | |
| 141 return false; | |
| 142 | |
| 143 int id; | |
| 144 std::string method; | |
| 145 if (!command->GetInteger("id", &id) || | |
| 146 !command->GetString("method", &method)) { | |
| 147 return false; | |
| 148 } | |
| 149 auto find_it = async_command_map_.find(method); | |
| 150 if (find_it == async_command_map_.end()) | |
| 151 return false; | |
| 152 AsyncCommandMemberFnPtr async_command_fn_ptr = find_it->second; | |
| 153 const base::DictionaryValue* params = nullptr; | |
| 154 command->GetDictionary("params", ¶ms); | |
| 155 (this->*async_command_fn_ptr)(agent_host, id, params, callback); | |
| 156 return true; | |
| 157 } | |
| 158 | |
| 111 scoped_refptr<content::DevToolsAgentHost> | 159 scoped_refptr<content::DevToolsAgentHost> |
| 112 HeadlessDevToolsManagerDelegate::CreateNewTarget(const GURL& url) { | 160 HeadlessDevToolsManagerDelegate::CreateNewTarget(const GURL& url) { |
| 113 HeadlessBrowserContext* context = browser_->GetDefaultBrowserContext(); | 161 HeadlessBrowserContext* context = browser_->GetDefaultBrowserContext(); |
| 114 HeadlessWebContentsImpl* web_contents_impl = HeadlessWebContentsImpl::From( | 162 HeadlessWebContentsImpl* web_contents_impl = HeadlessWebContentsImpl::From( |
| 115 context->CreateWebContentsBuilder() | 163 context->CreateWebContentsBuilder() |
| 116 .SetInitialURL(url) | 164 .SetInitialURL(url) |
| 117 .SetWindowSize(browser_->options()->window_size) | 165 .SetWindowSize(browser_->options()->window_size) |
| 118 .Build()); | 166 .Build()); |
| 119 return content::DevToolsAgentHost::GetOrCreateFor( | 167 return content::DevToolsAgentHost::GetOrCreateFor( |
| 120 web_contents_impl->web_contents()); | 168 web_contents_impl->web_contents()); |
| 121 } | 169 } |
| 122 | 170 |
| 123 std::string HeadlessDevToolsManagerDelegate::GetDiscoveryPageHTML() { | 171 std::string HeadlessDevToolsManagerDelegate::GetDiscoveryPageHTML() { |
| 124 return ResourceBundle::GetSharedInstance() | 172 return ResourceBundle::GetSharedInstance() |
| 125 .GetRawDataResource(IDR_HEADLESS_LIB_DEVTOOLS_DISCOVERY_PAGE) | 173 .GetRawDataResource(IDR_HEADLESS_LIB_DEVTOOLS_DISCOVERY_PAGE) |
| 126 .as_string(); | 174 .as_string(); |
| 127 } | 175 } |
| 128 | 176 |
| 129 std::string HeadlessDevToolsManagerDelegate::GetFrontendResource( | 177 std::string HeadlessDevToolsManagerDelegate::GetFrontendResource( |
| 130 const std::string& path) { | 178 const std::string& path) { |
| 131 return content::DevToolsFrontendHost::GetFrontendResource(path).as_string(); | 179 return content::DevToolsFrontendHost::GetFrontendResource(path).as_string(); |
| 132 } | 180 } |
| 133 | 181 |
| 182 void HeadlessDevToolsManagerDelegate::PrintToPDF( | |
| 183 content::DevToolsAgentHost* agent_host, | |
| 184 int command_id, | |
| 185 const base::DictionaryValue* params, | |
| 186 CommandCallback callback) { | |
| 187 content::WebContents* web_contents = agent_host->GetWebContents(); | |
| 188 content::RenderFrameHost* rfh = web_contents->GetMainFrame(); | |
| 189 | |
| 190 printing::HeadlessPrintManager::FromWebContents(web_contents) | |
| 191 ->GetPDFContents(rfh, base::Bind(&PDFCreated, callback, command_id)); | |
| 192 } | |
| 193 | |
| 134 std::unique_ptr<base::DictionaryValue> | 194 std::unique_ptr<base::DictionaryValue> |
| 135 HeadlessDevToolsManagerDelegate::CreateTarget( | 195 HeadlessDevToolsManagerDelegate::CreateTarget( |
| 136 int command_id, | 196 int command_id, |
| 137 const base::DictionaryValue* params) { | 197 const base::DictionaryValue* params) { |
| 138 std::string url; | 198 std::string url; |
| 139 std::string browser_context_id; | 199 std::string browser_context_id; |
| 140 int width = browser_->options()->window_size.width(); | 200 int width = browser_->options()->window_size.width(); |
| 141 int height = browser_->options()->window_size.height(); | 201 int height = browser_->options()->window_size.height(); |
| 142 if (!params || !params->GetString("url", &url)) | 202 if (!params || !params->GetString("url", &url)) |
| 143 return CreateInvalidParamResponse(command_id, "url"); | 203 return CreateInvalidParamResponse(command_id, "url"); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 | 290 |
| 231 std::unique_ptr<base::Value> result( | 291 std::unique_ptr<base::Value> result( |
| 232 target::DisposeBrowserContextResult::Builder() | 292 target::DisposeBrowserContextResult::Builder() |
| 233 .SetSuccess(success) | 293 .SetSuccess(success) |
| 234 .Build() | 294 .Build() |
| 235 ->Serialize()); | 295 ->Serialize()); |
| 236 return CreateSuccessResponse(command_id, std::move(result)); | 296 return CreateSuccessResponse(command_id, std::move(result)); |
| 237 } | 297 } |
| 238 | 298 |
| 239 } // namespace headless | 299 } // namespace headless |
| OLD | NEW |