| 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_web_contents_impl.h" | 17 #include "headless/lib/browser/headless_web_contents_impl.h" |
| 18 #include "headless/public/devtools/domains/target.h" | 18 #include "headless/public/devtools/domains/target.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 20 | 20 |
| 21 namespace headless { | 21 namespace headless { |
| 22 | 22 |
| 23 namespace { |
| 24 const char kIdParam[] = "id"; |
| 25 const char kResultParam[] = "result"; |
| 26 const char kErrorParam[] = "error"; |
| 27 const char kErrorCodeParam[] = "code"; |
| 28 const char kErrorMessageParam[] = "message"; |
| 29 |
| 30 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object |
| 31 enum Error { |
| 32 kErrorInvalidParam = -32602, |
| 33 kErrorServerError = -32000 |
| 34 }; |
| 35 |
| 36 std::unique_ptr<base::DictionaryValue> CreateSuccessResponse( |
| 37 int command_id, |
| 38 std::unique_ptr<base::Value> result) { |
| 39 if (!result) |
| 40 result.reset(new base::DictionaryValue()); |
| 41 |
| 42 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
| 43 response->SetInteger(kIdParam, command_id); |
| 44 response->Set(kResultParam, std::move(result)); |
| 45 return response; |
| 46 } |
| 47 |
| 48 std::unique_ptr<base::DictionaryValue> CreateErrorResponse( |
| 49 int command_id, |
| 50 int error_code, |
| 51 const std::string& error_message) { |
| 52 std::unique_ptr<base::DictionaryValue> error_object( |
| 53 new base::DictionaryValue()); |
| 54 error_object->SetInteger(kErrorCodeParam, error_code); |
| 55 error_object->SetString(kErrorMessageParam, error_message); |
| 56 |
| 57 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
| 58 response->Set(kErrorParam, std::move(error_object)); |
| 59 return response; |
| 60 } |
| 61 |
| 62 std::unique_ptr<base::DictionaryValue> CreateInvalidParamResponse( |
| 63 int command_id, |
| 64 const std::string& param) { |
| 65 return CreateErrorResponse( |
| 66 command_id, kErrorInvalidParam, |
| 67 base::StringPrintf("Missing or invalid '%s' parameter", param.c_str())); |
| 68 } |
| 69 } // namespace |
| 70 |
| 23 HeadlessDevToolsManagerDelegate::HeadlessDevToolsManagerDelegate( | 71 HeadlessDevToolsManagerDelegate::HeadlessDevToolsManagerDelegate( |
| 24 base::WeakPtr<HeadlessBrowserImpl> browser) | 72 base::WeakPtr<HeadlessBrowserImpl> browser) |
| 25 : browser_(std::move(browser)), default_browser_context_(nullptr) { | 73 : browser_(std::move(browser)) { |
| 26 command_map_["Target.createTarget"] = | 74 command_map_["Target.createTarget"] = |
| 27 &HeadlessDevToolsManagerDelegate::CreateTarget; | 75 &HeadlessDevToolsManagerDelegate::CreateTarget; |
| 28 command_map_["Target.closeTarget"] = | 76 command_map_["Target.closeTarget"] = |
| 29 &HeadlessDevToolsManagerDelegate::CloseTarget; | 77 &HeadlessDevToolsManagerDelegate::CloseTarget; |
| 30 command_map_["Target.createBrowserContext"] = | 78 command_map_["Target.createBrowserContext"] = |
| 31 &HeadlessDevToolsManagerDelegate::CreateBrowserContext; | 79 &HeadlessDevToolsManagerDelegate::CreateBrowserContext; |
| 32 command_map_["Target.disposeBrowserContext"] = | 80 command_map_["Target.disposeBrowserContext"] = |
| 33 &HeadlessDevToolsManagerDelegate::DisposeBrowserContext; | 81 &HeadlessDevToolsManagerDelegate::DisposeBrowserContext; |
| 34 } | 82 } |
| 35 | 83 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 48 const base::DictionaryValue* params = nullptr; | 96 const base::DictionaryValue* params = nullptr; |
| 49 if (!command->GetInteger("id", &id) || | 97 if (!command->GetInteger("id", &id) || |
| 50 !command->GetString("method", &method) || | 98 !command->GetString("method", &method) || |
| 51 !command->GetDictionary("params", ¶ms)) { | 99 !command->GetDictionary("params", ¶ms)) { |
| 52 return nullptr; | 100 return nullptr; |
| 53 } | 101 } |
| 54 auto find_it = command_map_.find(method); | 102 auto find_it = command_map_.find(method); |
| 55 if (find_it == command_map_.end()) | 103 if (find_it == command_map_.end()) |
| 56 return nullptr; | 104 return nullptr; |
| 57 CommandMemberFnPtr command_fn_ptr = find_it->second; | 105 CommandMemberFnPtr command_fn_ptr = find_it->second; |
| 58 std::unique_ptr<base::Value> cmd_result(((this)->*command_fn_ptr)(params)); | 106 std::unique_ptr<base::DictionaryValue> cmd_result( |
| 59 if (!cmd_result) | 107 ((this)->*command_fn_ptr)(id, params)); |
| 60 return nullptr; | 108 return cmd_result.release(); |
| 61 | |
| 62 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | |
| 63 result->SetInteger("id", id); | |
| 64 result->Set("result", std::move(cmd_result)); | |
| 65 return result.release(); | |
| 66 } | 109 } |
| 67 | 110 |
| 68 std::string HeadlessDevToolsManagerDelegate::GetDiscoveryPageHTML() { | 111 std::string HeadlessDevToolsManagerDelegate::GetDiscoveryPageHTML() { |
| 69 return ResourceBundle::GetSharedInstance().GetRawDataResource( | 112 return ResourceBundle::GetSharedInstance() |
| 70 IDR_HEADLESS_LIB_DEVTOOLS_DISCOVERY_PAGE).as_string(); | 113 .GetRawDataResource(IDR_HEADLESS_LIB_DEVTOOLS_DISCOVERY_PAGE) |
| 114 .as_string(); |
| 71 } | 115 } |
| 72 | 116 |
| 73 std::string HeadlessDevToolsManagerDelegate::GetFrontendResource( | 117 std::string HeadlessDevToolsManagerDelegate::GetFrontendResource( |
| 74 const std::string& path) { | 118 const std::string& path) { |
| 75 return content::DevToolsFrontendHost::GetFrontendResource(path).as_string(); | 119 return content::DevToolsFrontendHost::GetFrontendResource(path).as_string(); |
| 76 } | 120 } |
| 77 | 121 |
| 78 std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::CreateTarget( | 122 std::unique_ptr<base::DictionaryValue> |
| 123 HeadlessDevToolsManagerDelegate::CreateTarget( |
| 124 int command_id, |
| 79 const base::DictionaryValue* params) { | 125 const base::DictionaryValue* params) { |
| 80 std::string url; | 126 std::string url; |
| 81 std::string browser_context_id; | 127 std::string browser_context_id; |
| 82 int width = browser_->options()->window_size.width(); | 128 int width = browser_->options()->window_size.width(); |
| 83 int height = browser_->options()->window_size.height(); | 129 int height = browser_->options()->window_size.height(); |
| 84 params->GetString("url", &url); | 130 params->GetString("url", &url); |
| 85 params->GetString("browserContextId", &browser_context_id); | 131 params->GetString("browserContextId", &browser_context_id); |
| 86 params->GetInteger("width", &width); | 132 params->GetInteger("width", &width); |
| 87 params->GetInteger("height", &height); | 133 params->GetInteger("height", &height); |
| 88 | 134 |
| 89 // TODO(alexclarke): Should we fail when user passes incorrect id? | |
| 90 HeadlessBrowserContext* context = | 135 HeadlessBrowserContext* context = |
| 91 browser_->GetBrowserContextForId(browser_context_id); | 136 browser_->GetBrowserContextForId(browser_context_id); |
| 92 if (!context) { | 137 if (!browser_context_id.empty()) { |
| 93 if (!default_browser_context_) { | 138 context = browser_->GetBrowserContextForId(browser_context_id); |
| 94 default_browser_context_ = | 139 if (!context) |
| 95 browser_->CreateBrowserContextBuilder().Build(); | 140 return CreateInvalidParamResponse(command_id, "browserContextId"); |
| 141 } else { |
| 142 context = browser_->GetDefaultBrowserContext(); |
| 143 if (!context) { |
| 144 return CreateErrorResponse(command_id, kErrorServerError, |
| 145 "You specified no |browserContextId|, but " |
| 146 "there is no default browser context set on " |
| 147 "HeadlessBrowser"); |
| 96 } | 148 } |
| 97 context = default_browser_context_; | |
| 98 } | 149 } |
| 99 | 150 |
| 100 HeadlessWebContentsImpl* web_contents_impl = | 151 HeadlessWebContentsImpl* web_contents_impl = |
| 101 HeadlessWebContentsImpl::From(context->CreateWebContentsBuilder() | 152 HeadlessWebContentsImpl::From(context->CreateWebContentsBuilder() |
| 102 .SetInitialURL(GURL(url)) | 153 .SetInitialURL(GURL(url)) |
| 103 .SetWindowSize(gfx::Size(width, height)) | 154 .SetWindowSize(gfx::Size(width, height)) |
| 104 .Build()); | 155 .Build()); |
| 105 | 156 |
| 106 return target::CreateTargetResult::Builder() | 157 std::unique_ptr<base::Value> result( |
| 107 .SetTargetId(web_contents_impl->GetDevToolsAgentHostId()) | 158 target::CreateTargetResult::Builder() |
| 108 .Build() | 159 .SetTargetId(web_contents_impl->GetDevToolsAgentHostId()) |
| 109 ->Serialize(); | 160 .Build() |
| 161 ->Serialize()); |
| 162 return CreateSuccessResponse(command_id, std::move(result)); |
| 110 } | 163 } |
| 111 | 164 |
| 112 std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::CloseTarget( | 165 std::unique_ptr<base::DictionaryValue> |
| 166 HeadlessDevToolsManagerDelegate::CloseTarget( |
| 167 int command_id, |
| 113 const base::DictionaryValue* params) { | 168 const base::DictionaryValue* params) { |
| 114 std::string target_id; | 169 std::string target_id; |
| 115 if (!params->GetString("targetId", &target_id)) { | 170 if (!params->GetString("targetId", &target_id)) |
| 116 return nullptr; | 171 return CreateInvalidParamResponse(command_id, "targetId"); |
| 117 } | |
| 118 HeadlessWebContents* web_contents = | 172 HeadlessWebContents* web_contents = |
| 119 browser_->GetWebContentsForDevToolsAgentHostId(target_id); | 173 browser_->GetWebContentsForDevToolsAgentHostId(target_id); |
| 120 bool success = false; | 174 bool success = false; |
| 121 if (web_contents) { | 175 if (web_contents) { |
| 122 web_contents->Close(); | 176 web_contents->Close(); |
| 123 success = true; | 177 success = true; |
| 124 } | 178 } |
| 125 return target::CloseTargetResult::Builder() | 179 std::unique_ptr<base::Value> result(target::CloseTargetResult::Builder() |
| 126 .SetSuccess(success) | 180 .SetSuccess(success) |
| 127 .Build() | 181 .Build() |
| 128 ->Serialize(); | 182 ->Serialize()); |
| 183 return CreateSuccessResponse(command_id, std::move(result)); |
| 129 } | 184 } |
| 130 | 185 |
| 131 std::unique_ptr<base::Value> | 186 std::unique_ptr<base::DictionaryValue> |
| 132 HeadlessDevToolsManagerDelegate::CreateBrowserContext( | 187 HeadlessDevToolsManagerDelegate::CreateBrowserContext( |
| 188 int command_id, |
| 133 const base::DictionaryValue* params) { | 189 const base::DictionaryValue* params) { |
| 134 HeadlessBrowserContext* browser_context = | 190 HeadlessBrowserContext* browser_context = |
| 135 browser_->CreateBrowserContextBuilder().Build(); | 191 browser_->CreateBrowserContextBuilder().Build(); |
| 136 | 192 |
| 137 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 193 std::unique_ptr<base::Value> result( |
| 138 return target::CreateBrowserContextResult::Builder() | 194 target::CreateBrowserContextResult::Builder() |
| 139 .SetBrowserContextId(browser_context->Id()) | 195 .SetBrowserContextId(browser_context->Id()) |
| 140 .Build() | 196 .Build() |
| 141 ->Serialize(); | 197 ->Serialize()); |
| 198 return CreateSuccessResponse(command_id, std::move(result)); |
| 142 } | 199 } |
| 143 | 200 |
| 144 std::unique_ptr<base::Value> | 201 std::unique_ptr<base::DictionaryValue> |
| 145 HeadlessDevToolsManagerDelegate::DisposeBrowserContext( | 202 HeadlessDevToolsManagerDelegate::DisposeBrowserContext( |
| 203 int command_id, |
| 146 const base::DictionaryValue* params) { | 204 const base::DictionaryValue* params) { |
| 147 std::string browser_context_id; | 205 std::string browser_context_id; |
| 148 if (!params->GetString("browserContextId", &browser_context_id)) { | 206 if (!params->GetString("browserContextId", &browser_context_id)) |
| 149 return nullptr; | 207 return CreateInvalidParamResponse(command_id, "browserContextId"); |
| 150 } | |
| 151 HeadlessBrowserContext* context = | 208 HeadlessBrowserContext* context = |
| 152 browser_->GetBrowserContextForId(browser_context_id); | 209 browser_->GetBrowserContextForId(browser_context_id); |
| 153 | 210 |
| 154 bool success = false; | 211 bool success = false; |
| 155 if (context && context != default_browser_context_ && | 212 if (context && context != browser_->GetDefaultBrowserContext() && |
| 156 context->GetAllWebContents().empty()) { | 213 context->GetAllWebContents().empty()) { |
| 157 success = true; | 214 success = true; |
| 158 context->Close(); | 215 context->Close(); |
| 159 } | 216 } |
| 160 | 217 |
| 161 return target::DisposeBrowserContextResult::Builder() | 218 std::unique_ptr<base::Value> result( |
| 162 .SetSuccess(success) | 219 target::DisposeBrowserContextResult::Builder() |
| 163 .Build() | 220 .SetSuccess(success) |
| 164 ->Serialize(); | 221 .Build() |
| 222 ->Serialize()); |
| 223 return CreateSuccessResponse(command_id, std::move(result)); |
| 165 } | 224 } |
| 166 | 225 |
| 167 } // namespace headless | 226 } // namespace headless |
| OLD | NEW |