OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "headless/lib/browser/headless_devtools_manager_delegate.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/guid.h" |
| 10 #include "content/public/browser/web_contents.h" |
| 11 #include "headless/lib/browser/headless_browser_context_impl.h" |
| 12 #include "headless/lib/browser/headless_browser_impl.h" |
| 13 #include "headless/lib/browser/headless_web_contents_impl.h" |
| 14 #include "headless/public/domains/browser.h" |
| 15 |
| 16 namespace headless { |
| 17 |
| 18 HeadlessDevToolsManagerDelegate::HeadlessDevToolsManagerDelegate( |
| 19 HeadlessBrowserImpl* browser) |
| 20 : browser_(browser) { |
| 21 command_map_["Browser.createTarget"] = |
| 22 &HeadlessDevToolsManagerDelegate::CreateTarget; |
| 23 command_map_["Browser.closeTarget"] = |
| 24 &HeadlessDevToolsManagerDelegate::CloseTarget; |
| 25 command_map_["Browser.createBrowserContext"] = |
| 26 &HeadlessDevToolsManagerDelegate::CreateBrowserContext; |
| 27 command_map_["Browser.disposeBrowserContext"] = |
| 28 &HeadlessDevToolsManagerDelegate::DisposeBrowserContext; |
| 29 } |
| 30 |
| 31 HeadlessDevToolsManagerDelegate::~HeadlessDevToolsManagerDelegate() {} |
| 32 |
| 33 base::DictionaryValue* HeadlessDevToolsManagerDelegate::HandleCommand( |
| 34 content::DevToolsAgentHost* agent_host, |
| 35 base::DictionaryValue* command) { |
| 36 int id; |
| 37 std::string method; |
| 38 const base::DictionaryValue* params = nullptr; |
| 39 if (!command->GetInteger("id", &id) || |
| 40 !command->GetString("method", &method) || |
| 41 !command->GetDictionary("params", ¶ms)) { |
| 42 return nullptr; |
| 43 } |
| 44 auto find_it = command_map_.find(method); |
| 45 if (find_it == command_map_.end()) |
| 46 return nullptr; |
| 47 CommandMemberFnPtr command_fn_ptr = find_it->second; |
| 48 std::unique_ptr<base::Value> cmd_result(((this)->*command_fn_ptr)(params)); |
| 49 if (!cmd_result) |
| 50 return nullptr; |
| 51 |
| 52 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
| 53 result->SetInteger("id", id); |
| 54 result->Set("result", std::move(cmd_result)); |
| 55 return result.release(); |
| 56 } |
| 57 |
| 58 std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::CreateTarget( |
| 59 const base::DictionaryValue* params) { |
| 60 std::string initial_url; |
| 61 std::string browser_context_id; |
| 62 int width = 800; |
| 63 int height = 600; |
| 64 params->GetString("initialUrl", &initial_url); |
| 65 params->GetString("browserContextId", &browser_context_id); |
| 66 params->GetInteger("width", &width); |
| 67 params->GetInteger("height", &height); |
| 68 HeadlessWebContentsImpl* web_contents_impl; |
| 69 auto find_it = browser_context_map_.find(browser_context_id); |
| 70 if (find_it != browser_context_map_.end()) { |
| 71 web_contents_impl = HeadlessWebContentsImpl::From( |
| 72 browser_->CreateWebContentsBuilder() |
| 73 .SetInitialURL(GURL(initial_url)) |
| 74 .SetWindowSize(gfx::Size(width, height)) |
| 75 .SetBrowserContext(find_it->second.get()) |
| 76 .Build()); |
| 77 } else { |
| 78 web_contents_impl = HeadlessWebContentsImpl::From( |
| 79 browser_->CreateWebContentsBuilder() |
| 80 .SetInitialURL(GURL(initial_url)) |
| 81 .SetWindowSize(gfx::Size(width, height)) |
| 82 .Build()); |
| 83 } |
| 84 return browser::CreateTargetResult::Builder() |
| 85 .SetTargetId(web_contents_impl->GetDevtoolsAgentHostId()) |
| 86 .Build() |
| 87 ->Serialize(); |
| 88 } |
| 89 |
| 90 std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::CloseTarget( |
| 91 const base::DictionaryValue* params) { |
| 92 std::string target_id; |
| 93 if (!params->GetString("targetId", &target_id)) { |
| 94 return nullptr; |
| 95 } |
| 96 HeadlessWebContents* web_contents = |
| 97 browser_->GetWebContentsForDevtoolsAgentHostId(target_id); |
| 98 bool success = false; |
| 99 if (web_contents) { |
| 100 web_contents->Close(); |
| 101 success = true; |
| 102 } |
| 103 return browser::CloseTargetResult::Builder() |
| 104 .SetSuccess(success) |
| 105 .Build() |
| 106 ->Serialize(); |
| 107 } |
| 108 |
| 109 std::unique_ptr<base::Value> |
| 110 HeadlessDevToolsManagerDelegate::CreateBrowserContext( |
| 111 const base::DictionaryValue* params) { |
| 112 std::string browser_context_id = base::GenerateGUID(); |
| 113 browser_context_map_[browser_context_id] = |
| 114 browser_->CreateBrowserContextBuilder().Build(); |
| 115 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
| 116 return browser::CreateBrowserContextResult::Builder() |
| 117 .SetBrowserContextId(browser_context_id) |
| 118 .Build() |
| 119 ->Serialize(); |
| 120 } |
| 121 |
| 122 std::unique_ptr<base::Value> |
| 123 HeadlessDevToolsManagerDelegate::DisposeBrowserContext( |
| 124 const base::DictionaryValue* params) { |
| 125 std::string browser_context_id; |
| 126 if (!params->GetString("browserContextId", &browser_context_id)) { |
| 127 return nullptr; |
| 128 } |
| 129 auto find_it = browser_context_map_.find(browser_context_id); |
| 130 bool success = false; |
| 131 if (find_it != browser_context_map_.end()) { |
| 132 success = true; |
| 133 HeadlessBrowserContextImpl* headless_browser_context = |
| 134 HeadlessBrowserContextImpl::From(find_it->second.get()); |
| 135 // Make sure |headless_browser_context| isn't in use! |
| 136 for (HeadlessWebContents* headless_web_contents : |
| 137 browser_->GetAllWebContents()) { |
| 138 content::WebContents* web_contents = |
| 139 HeadlessWebContentsImpl::From(headless_web_contents)->web_contents(); |
| 140 if (web_contents->GetBrowserContext() == headless_browser_context) { |
| 141 success = false; |
| 142 break; |
| 143 } |
| 144 } |
| 145 if (success) |
| 146 browser_context_map_.erase(find_it); |
| 147 } |
| 148 return browser::DisposeBrowserContextResult::Builder() |
| 149 .SetSuccess(success) |
| 150 .Build() |
| 151 ->Serialize(); |
| 152 } |
| 153 |
| 154 } // namespace headless |
OLD | NEW |