Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: headless/lib/browser/headless_devtools_manager_delegate.cc

Issue 2181413002: [headless] Remove default browser context. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More fixes Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 9
9 #include "base/guid.h" 10 #include "base/guid.h"
10 #include "content/public/browser/web_contents.h" 11 #include "content/public/browser/web_contents.h"
11 #include "headless/lib/browser/headless_browser_context_impl.h" 12 #include "headless/lib/browser/headless_browser_context_impl.h"
12 #include "headless/lib/browser/headless_browser_impl.h" 13 #include "headless/lib/browser/headless_browser_impl.h"
13 #include "headless/lib/browser/headless_web_contents_impl.h" 14 #include "headless/lib/browser/headless_web_contents_impl.h"
14 #include "headless/public/domains/browser.h" 15 #include "headless/public/domains/browser.h"
15 16
16 namespace headless { 17 namespace headless {
17 18
18 HeadlessDevToolsManagerDelegate::HeadlessDevToolsManagerDelegate( 19 HeadlessDevToolsManagerDelegate::HeadlessDevToolsManagerDelegate(
19 HeadlessBrowserImpl* browser) 20 HeadlessBrowserImpl* browser)
20 : browser_(browser) { 21 : browser_(browser), weak_ptr_factory_(this) {
21 command_map_["Browser.createTarget"] = 22 command_map_["Browser.createTarget"] =
22 &HeadlessDevToolsManagerDelegate::CreateTarget; 23 &HeadlessDevToolsManagerDelegate::CreateTarget;
23 command_map_["Browser.closeTarget"] = 24 command_map_["Browser.closeTarget"] =
24 &HeadlessDevToolsManagerDelegate::CloseTarget; 25 &HeadlessDevToolsManagerDelegate::CloseTarget;
25 command_map_["Browser.createBrowserContext"] = 26 command_map_["Browser.createBrowserContext"] =
26 &HeadlessDevToolsManagerDelegate::CreateBrowserContext; 27 &HeadlessDevToolsManagerDelegate::CreateBrowserContext;
27 command_map_["Browser.disposeBrowserContext"] = 28 command_map_["Browser.disposeBrowserContext"] =
28 &HeadlessDevToolsManagerDelegate::DisposeBrowserContext; 29 &HeadlessDevToolsManagerDelegate::DisposeBrowserContext;
29 } 30 }
30 31
(...skipping 17 matching lines...) Expand all
48 std::unique_ptr<base::Value> cmd_result(((this)->*command_fn_ptr)(params)); 49 std::unique_ptr<base::Value> cmd_result(((this)->*command_fn_ptr)(params));
49 if (!cmd_result) 50 if (!cmd_result)
50 return nullptr; 51 return nullptr;
51 52
52 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); 53 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
53 result->SetInteger("id", id); 54 result->SetInteger("id", id);
54 result->Set("result", std::move(cmd_result)); 55 result->Set("result", std::move(cmd_result));
55 return result.release(); 56 return result.release();
56 } 57 }
57 58
59 void HeadlessDevToolsManagerDelegate::Shutdown() {
60 default_browser_context_.reset();
61 browser_context_map_.clear();
62 }
63
64 base::WeakPtr<HeadlessDevToolsManagerDelegate>
65 HeadlessDevToolsManagerDelegate::GetWeakPtr() {
66 return weak_ptr_factory_.GetWeakPtr();
67 }
68
58 std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::CreateTarget( 69 std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::CreateTarget(
59 const base::DictionaryValue* params) { 70 const base::DictionaryValue* params) {
60 std::string url; 71 std::string url;
61 std::string browser_context_id; 72 std::string browser_context_id;
62 int width = browser_->options()->window_size.width(); 73 int width = browser_->options()->window_size.width();
63 int height = browser_->options()->window_size.height(); 74 int height = browser_->options()->window_size.height();
64 params->GetString("url", &url); 75 params->GetString("url", &url);
65 params->GetString("browserContextId", &browser_context_id); 76 params->GetString("browserContextId", &browser_context_id);
66 params->GetInteger("width", &width); 77 params->GetInteger("width", &width);
67 params->GetInteger("height", &height); 78 params->GetInteger("height", &height);
68 HeadlessWebContentsImpl* web_contents_impl; 79
80 HeadlessBrowserContext* context;
69 auto find_it = browser_context_map_.find(browser_context_id); 81 auto find_it = browser_context_map_.find(browser_context_id);
70 if (find_it != browser_context_map_.end()) { 82 if (find_it != browser_context_map_.end()) {
71 web_contents_impl = HeadlessWebContentsImpl::From( 83 context = find_it->second.get();
72 browser_->CreateWebContentsBuilder()
73 .SetInitialURL(GURL(url))
74 .SetWindowSize(gfx::Size(width, height))
75 .SetBrowserContext(find_it->second.get())
76 .Build());
77 } else { 84 } else {
78 web_contents_impl = HeadlessWebContentsImpl::From( 85 if (!default_browser_context_) {
79 browser_->CreateWebContentsBuilder() 86 default_browser_context_ =
80 .SetInitialURL(GURL(url)) 87 browser_->CreateBrowserContextBuilder().Build();
81 .SetWindowSize(gfx::Size(width, height)) 88 }
82 .Build()); 89 context = default_browser_context_.get();
83 } 90 }
91
92 HeadlessWebContentsImpl* web_contents_impl =
93 HeadlessWebContentsImpl::From(context->CreateWebContentsBuilder()
94 .SetInitialURL(GURL(url))
95 .SetWindowSize(gfx::Size(width, height))
96 .Build());
97
84 return browser::CreateTargetResult::Builder() 98 return browser::CreateTargetResult::Builder()
85 .SetTargetId(web_contents_impl->GetDevtoolsAgentHostId()) 99 .SetTargetId(web_contents_impl->GetDevtoolsAgentHostId())
86 .Build() 100 .Build()
87 ->Serialize(); 101 ->Serialize();
88 } 102 }
89 103
90 std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::CloseTarget( 104 std::unique_ptr<base::Value> HeadlessDevToolsManagerDelegate::CloseTarget(
91 const base::DictionaryValue* params) { 105 const base::DictionaryValue* params) {
92 std::string target_id; 106 std::string target_id;
93 if (!params->GetString("targetId", &target_id)) { 107 if (!params->GetString("targetId", &target_id)) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 if (success) 159 if (success)
146 browser_context_map_.erase(find_it); 160 browser_context_map_.erase(find_it);
147 } 161 }
148 return browser::DisposeBrowserContextResult::Builder() 162 return browser::DisposeBrowserContextResult::Builder()
149 .SetSuccess(success) 163 .SetSuccess(success)
150 .Build() 164 .Build()
151 ->Serialize(); 165 ->Serialize();
152 } 166 }
153 167
154 } // namespace headless 168 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698