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.newPage"] = &HeadlessDevToolsManagerDelegate::newPage; |
| 22 command_map_["Browser.closePage"] = |
| 23 &HeadlessDevToolsManagerDelegate::closePage; |
| 24 command_map_["Browser.newContext"] = |
| 25 &HeadlessDevToolsManagerDelegate::newContext; |
| 26 command_map_["Browser.closeContext"] = |
| 27 &HeadlessDevToolsManagerDelegate::closeContext; |
| 28 } |
| 29 |
| 30 HeadlessDevToolsManagerDelegate::~HeadlessDevToolsManagerDelegate() {} |
| 31 |
| 32 base::DictionaryValue* HeadlessDevToolsManagerDelegate::HandleCommand( |
| 33 content::DevToolsAgentHost* agent_host, |
| 34 base::DictionaryValue* command) { |
| 35 int id; |
| 36 std::string method; |
| 37 const base::DictionaryValue* params = nullptr; |
| 38 if (!command->GetInteger("id", &id) || |
| 39 !command->GetString("method", &method) || |
| 40 !command->GetDictionary("params", ¶ms)) { |
| 41 return nullptr; |
| 42 } |
| 43 auto find_it = command_map_.find(method); |
| 44 if (find_it == command_map_.end()) |
| 45 return nullptr; |
| 46 CommandMemberFnPtr command_fn_ptr = find_it->second; |
| 47 std::unique_ptr<base::Value> cmd_result(((this)->*command_fn_ptr)(params)); |
| 48 if (!cmd_result) |
| 49 return nullptr; |
| 50 |
| 51 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
| 52 result->SetInteger("id", id); |
| 53 result->Set("result", std::move(cmd_result)); |
| 54 return result.release(); |
| 55 } |
| 56 |
| 57 std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::newPage( |
| 58 const base::DictionaryValue* params) { |
| 59 std::string initial_url; |
| 60 std::string context_id; |
| 61 int width = 800; |
| 62 int height = 800; |
| 63 params->GetString("initialUrl", &initial_url); |
| 64 params->GetString("contextId", &context_id); |
| 65 params->GetInteger("width", &width); |
| 66 params->GetInteger("height", &height); |
| 67 HeadlessWebContentsImpl* web_contents_impl; |
| 68 auto find_it = browser_context_map_.find(context_id); |
| 69 if (find_it != browser_context_map_.end()) { |
| 70 web_contents_impl = HeadlessWebContentsImpl::From( |
| 71 browser_->CreateWebContentsBuilder() |
| 72 .SetInitialURL(GURL(initial_url)) |
| 73 .SetWindowSize(gfx::Size(width, height)) |
| 74 .SetBrowserContext(find_it->second.get()) |
| 75 .Build()); |
| 76 } else { |
| 77 web_contents_impl = HeadlessWebContentsImpl::From( |
| 78 browser_->CreateWebContentsBuilder() |
| 79 .SetInitialURL(GURL(initial_url)) |
| 80 .SetWindowSize(gfx::Size(width, height)) |
| 81 .Build()); |
| 82 } |
| 83 web_contents_map_[web_contents_impl->GetAgentHostId()] = web_contents_impl; |
| 84 return browser::NewPageResult::Builder() |
| 85 .SetPageId(web_contents_impl->GetAgentHostId()) |
| 86 .Build() |
| 87 ->Serialize(); |
| 88 } |
| 89 |
| 90 std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::closePage( |
| 91 const base::DictionaryValue* params) { |
| 92 std::string page_id; |
| 93 if (!params->GetString("pageId", &page_id)) { |
| 94 return nullptr; |
| 95 } |
| 96 auto find_it = web_contents_map_.find(page_id); |
| 97 bool success = false; |
| 98 if (find_it != web_contents_map_.end()) { |
| 99 find_it->second->Close(); |
| 100 web_contents_map_.erase(find_it); |
| 101 success = true; |
| 102 } |
| 103 return browser::ClosePageResult::Builder() |
| 104 .SetSuccess(success) |
| 105 .Build() |
| 106 ->Serialize(); |
| 107 } |
| 108 |
| 109 std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::newContext( |
| 110 const base::DictionaryValue* params) { |
| 111 std::string context_id = base::GenerateGUID(); |
| 112 browser_context_map_[context_id] = |
| 113 browser_->CreateBrowserContextBuilder().Build(); |
| 114 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
| 115 return browser::NewContextResult::Builder() |
| 116 .SetContextId(context_id) |
| 117 .Build() |
| 118 ->Serialize(); |
| 119 } |
| 120 |
| 121 std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::closeContext( |
| 122 const base::DictionaryValue* params) { |
| 123 std::string context_id; |
| 124 if (!params->GetString("contextId", &context_id)) { |
| 125 return nullptr; |
| 126 } |
| 127 auto find_it = browser_context_map_.find(context_id); |
| 128 bool success = false; |
| 129 if (find_it != browser_context_map_.end()) { |
| 130 success = true; |
| 131 HeadlessBrowserContextImpl* headless_browser_context = |
| 132 HeadlessBrowserContextImpl::From(find_it->second.get()); |
| 133 // Make sure |headless_browser_context| isn't in use! |
| 134 for (HeadlessWebContents* headless_web_contents : |
| 135 browser_->GetAllWebContents()) { |
| 136 content::WebContents* web_contents = |
| 137 HeadlessWebContentsImpl::From(headless_web_contents)->web_contents(); |
| 138 if (web_contents->GetBrowserContext() == headless_browser_context) { |
| 139 success = false; |
| 140 break; |
| 141 } |
| 142 } |
| 143 if (success) |
| 144 browser_context_map_.erase(find_it); |
| 145 } |
| 146 return browser::CloseContextResult::Builder() |
| 147 .SetSuccess(success) |
| 148 .Build() |
| 149 ->Serialize(); |
| 150 } |
| 151 |
| 152 } // namespace headless |
OLD | NEW |