| 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" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 base::DictionaryValue* HeadlessDevToolsManagerDelegate::HandleCommand( | 78 base::DictionaryValue* HeadlessDevToolsManagerDelegate::HandleCommand( |
| 79 content::DevToolsAgentHost* agent_host, | 79 content::DevToolsAgentHost* agent_host, |
| 80 base::DictionaryValue* command) { | 80 base::DictionaryValue* command) { |
| 81 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 81 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 82 | 82 |
| 83 if (!browser_) | 83 if (!browser_) |
| 84 return nullptr; | 84 return nullptr; |
| 85 | 85 |
| 86 int id; | 86 int id; |
| 87 std::string method; | 87 std::string method; |
| 88 const base::DictionaryValue* params = nullptr; | |
| 89 if (!command->GetInteger("id", &id) || | 88 if (!command->GetInteger("id", &id) || |
| 90 !command->GetString("method", &method) || | 89 !command->GetString("method", &method)) { |
| 91 !command->GetDictionary("params", ¶ms)) { | |
| 92 return nullptr; | 90 return nullptr; |
| 93 } | 91 } |
| 94 auto find_it = command_map_.find(method); | 92 auto find_it = command_map_.find(method); |
| 95 if (find_it == command_map_.end()) | 93 if (find_it == command_map_.end()) |
| 96 return nullptr; | 94 return nullptr; |
| 97 CommandMemberFnPtr command_fn_ptr = find_it->second; | 95 CommandMemberFnPtr command_fn_ptr = find_it->second; |
| 96 const base::DictionaryValue* params = nullptr; |
| 97 command->GetDictionary("params", ¶ms); |
| 98 std::unique_ptr<base::DictionaryValue> cmd_result( | 98 std::unique_ptr<base::DictionaryValue> cmd_result( |
| 99 ((this)->*command_fn_ptr)(id, params)); | 99 ((this)->*command_fn_ptr)(id, params)); |
| 100 return cmd_result.release(); | 100 return cmd_result.release(); |
| 101 } | 101 } |
| 102 | 102 |
| 103 std::string HeadlessDevToolsManagerDelegate::GetDiscoveryPageHTML() { | 103 std::string HeadlessDevToolsManagerDelegate::GetDiscoveryPageHTML() { |
| 104 return ResourceBundle::GetSharedInstance() | 104 return ResourceBundle::GetSharedInstance() |
| 105 .GetRawDataResource(IDR_HEADLESS_LIB_DEVTOOLS_DISCOVERY_PAGE) | 105 .GetRawDataResource(IDR_HEADLESS_LIB_DEVTOOLS_DISCOVERY_PAGE) |
| 106 .as_string(); | 106 .as_string(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 std::string HeadlessDevToolsManagerDelegate::GetFrontendResource( | 109 std::string HeadlessDevToolsManagerDelegate::GetFrontendResource( |
| 110 const std::string& path) { | 110 const std::string& path) { |
| 111 return content::DevToolsFrontendHost::GetFrontendResource(path).as_string(); | 111 return content::DevToolsFrontendHost::GetFrontendResource(path).as_string(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 std::unique_ptr<base::DictionaryValue> | 114 std::unique_ptr<base::DictionaryValue> |
| 115 HeadlessDevToolsManagerDelegate::CreateTarget( | 115 HeadlessDevToolsManagerDelegate::CreateTarget( |
| 116 int command_id, | 116 int command_id, |
| 117 const base::DictionaryValue* params) { | 117 const base::DictionaryValue* params) { |
| 118 std::string url; | 118 std::string url; |
| 119 std::string browser_context_id; | 119 std::string browser_context_id; |
| 120 int width = browser_->options()->window_size.width(); | 120 int width = browser_->options()->window_size.width(); |
| 121 int height = browser_->options()->window_size.height(); | 121 int height = browser_->options()->window_size.height(); |
| 122 params->GetString("url", &url); | 122 if (!params || !params->GetString("url", &url)) |
| 123 return CreateInvalidParamResponse(command_id, "url"); |
| 123 params->GetString("browserContextId", &browser_context_id); | 124 params->GetString("browserContextId", &browser_context_id); |
| 124 params->GetInteger("width", &width); | 125 params->GetInteger("width", &width); |
| 125 params->GetInteger("height", &height); | 126 params->GetInteger("height", &height); |
| 126 | 127 |
| 127 HeadlessBrowserContext* context = | 128 HeadlessBrowserContext* context = |
| 128 browser_->GetBrowserContextForId(browser_context_id); | 129 browser_->GetBrowserContextForId(browser_context_id); |
| 129 if (!browser_context_id.empty()) { | 130 if (!browser_context_id.empty()) { |
| 130 context = browser_->GetBrowserContextForId(browser_context_id); | 131 context = browser_->GetBrowserContextForId(browser_context_id); |
| 131 } else { | 132 } else { |
| 132 context = browser_->GetDefaultBrowserContext(); | 133 context = browser_->GetDefaultBrowserContext(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 147 .Build() | 148 .Build() |
| 148 ->Serialize()); | 149 ->Serialize()); |
| 149 return CreateSuccessResponse(command_id, std::move(result)); | 150 return CreateSuccessResponse(command_id, std::move(result)); |
| 150 } | 151 } |
| 151 | 152 |
| 152 std::unique_ptr<base::DictionaryValue> | 153 std::unique_ptr<base::DictionaryValue> |
| 153 HeadlessDevToolsManagerDelegate::CloseTarget( | 154 HeadlessDevToolsManagerDelegate::CloseTarget( |
| 154 int command_id, | 155 int command_id, |
| 155 const base::DictionaryValue* params) { | 156 const base::DictionaryValue* params) { |
| 156 std::string target_id; | 157 std::string target_id; |
| 157 if (!params->GetString("targetId", &target_id)) | 158 if (!params || !params->GetString("targetId", &target_id)) |
| 158 return CreateInvalidParamResponse(command_id, "targetId"); | 159 return CreateInvalidParamResponse(command_id, "targetId"); |
| 159 HeadlessWebContents* web_contents = | 160 HeadlessWebContents* web_contents = |
| 160 browser_->GetWebContentsForDevToolsAgentHostId(target_id); | 161 browser_->GetWebContentsForDevToolsAgentHostId(target_id); |
| 161 bool success = false; | 162 bool success = false; |
| 162 if (web_contents) { | 163 if (web_contents) { |
| 163 web_contents->Close(); | 164 web_contents->Close(); |
| 164 success = true; | 165 success = true; |
| 165 } | 166 } |
| 166 std::unique_ptr<base::Value> result(target::CloseTargetResult::Builder() | 167 std::unique_ptr<base::Value> result(target::CloseTargetResult::Builder() |
| 167 .SetSuccess(success) | 168 .SetSuccess(success) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 183 .Build() | 184 .Build() |
| 184 ->Serialize()); | 185 ->Serialize()); |
| 185 return CreateSuccessResponse(command_id, std::move(result)); | 186 return CreateSuccessResponse(command_id, std::move(result)); |
| 186 } | 187 } |
| 187 | 188 |
| 188 std::unique_ptr<base::DictionaryValue> | 189 std::unique_ptr<base::DictionaryValue> |
| 189 HeadlessDevToolsManagerDelegate::DisposeBrowserContext( | 190 HeadlessDevToolsManagerDelegate::DisposeBrowserContext( |
| 190 int command_id, | 191 int command_id, |
| 191 const base::DictionaryValue* params) { | 192 const base::DictionaryValue* params) { |
| 192 std::string browser_context_id; | 193 std::string browser_context_id; |
| 193 if (!params->GetString("browserContextId", &browser_context_id)) | 194 if (!params || !params->GetString("browserContextId", &browser_context_id)) |
| 194 return CreateInvalidParamResponse(command_id, "browserContextId"); | 195 return CreateInvalidParamResponse(command_id, "browserContextId"); |
| 195 HeadlessBrowserContext* context = | 196 HeadlessBrowserContext* context = |
| 196 browser_->GetBrowserContextForId(browser_context_id); | 197 browser_->GetBrowserContextForId(browser_context_id); |
| 197 | 198 |
| 198 bool success = false; | 199 bool success = false; |
| 199 if (context && context != browser_->GetDefaultBrowserContext() && | 200 if (context && context != browser_->GetDefaultBrowserContext() && |
| 200 context->GetAllWebContents().empty()) { | 201 context->GetAllWebContents().empty()) { |
| 201 success = true; | 202 success = true; |
| 202 context->Close(); | 203 context->Close(); |
| 203 } | 204 } |
| 204 | 205 |
| 205 std::unique_ptr<base::Value> result( | 206 std::unique_ptr<base::Value> result( |
| 206 target::DisposeBrowserContextResult::Builder() | 207 target::DisposeBrowserContextResult::Builder() |
| 207 .SetSuccess(success) | 208 .SetSuccess(success) |
| 208 .Build() | 209 .Build() |
| 209 ->Serialize()); | 210 ->Serialize()); |
| 210 return CreateSuccessResponse(command_id, std::move(result)); | 211 return CreateSuccessResponse(command_id, std::move(result)); |
| 211 } | 212 } |
| 212 | 213 |
| 213 } // namespace headless | 214 } // namespace headless |
| OLD | NEW |