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