Chromium Code Reviews| 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.newBrowserContext"] = | |
| 25 &HeadlessDevToolsManagerDelegate::newBrowserContext; | |
| 26 command_map_["Browser.closeBrowserContext"] = | |
| 27 &HeadlessDevToolsManagerDelegate::closeBrowserContext; | |
| 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 browser_context_id; | |
| 61 int width = 800; | |
| 62 int height = 800; | |
|
Sami
2016/07/05 09:41:35
600?
alex clarke (OOO till 29th)
2016/07/05 11:54:00
Done.
| |
| 63 params->GetString("initialUrl", &initial_url); | |
| 64 params->GetString("browserContextId", &browser_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(browser_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::newBrowserContext( | |
| 110 const base::DictionaryValue* params) { | |
| 111 std::string browser_context_id = base::GenerateGUID(); | |
| 112 browser_context_map_[browser_context_id] = | |
| 113 browser_->CreateBrowserContextBuilder().Build(); | |
| 114 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | |
| 115 return browser::NewBrowserContextResult::Builder() | |
| 116 .SetBrowserContextId(browser_context_id) | |
| 117 .Build() | |
| 118 ->Serialize(); | |
| 119 } | |
| 120 | |
| 121 std::unique_ptr<base::Value> | |
| 122 HeadlessDevToolsManagerDelegate::closeBrowserContext( | |
| 123 const base::DictionaryValue* params) { | |
| 124 std::string browser_context_id; | |
| 125 if (!params->GetString("browserContextId", &browser_context_id)) { | |
| 126 return nullptr; | |
| 127 } | |
| 128 auto find_it = browser_context_map_.find(browser_context_id); | |
| 129 bool success = false; | |
| 130 if (find_it != browser_context_map_.end()) { | |
| 131 success = true; | |
| 132 HeadlessBrowserContextImpl* headless_browser_context = | |
| 133 HeadlessBrowserContextImpl::From(find_it->second.get()); | |
| 134 // Make sure |headless_browser_context| isn't in use! | |
| 135 for (HeadlessWebContents* headless_web_contents : | |
| 136 browser_->GetAllWebContents()) { | |
| 137 content::WebContents* web_contents = | |
| 138 HeadlessWebContentsImpl::From(headless_web_contents)->web_contents(); | |
| 139 if (web_contents->GetBrowserContext() == headless_browser_context) { | |
| 140 success = false; | |
| 141 break; | |
| 142 } | |
| 143 } | |
| 144 if (success) | |
| 145 browser_context_map_.erase(find_it); | |
| 146 } | |
| 147 return browser::CloseBrowserContextResult::Builder() | |
| 148 .SetSuccess(success) | |
| 149 .Build() | |
| 150 ->Serialize(); | |
| 151 } | |
| 152 | |
| 153 } // namespace headless | |
| OLD | NEW |