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