| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_browser_impl.h" | 5 #include "headless/lib/browser/headless_browser_impl.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 58 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 59 return HeadlessWebContents::Builder(this); | 59 return HeadlessWebContents::Builder(this); |
| 60 } | 60 } |
| 61 | 61 |
| 62 HeadlessBrowserContext::Builder | 62 HeadlessBrowserContext::Builder |
| 63 HeadlessBrowserImpl::CreateBrowserContextBuilder() { | 63 HeadlessBrowserImpl::CreateBrowserContextBuilder() { |
| 64 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 64 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 65 return HeadlessBrowserContext::Builder(this); | 65 return HeadlessBrowserContext::Builder(this); |
| 66 } | 66 } |
| 67 | 67 |
| 68 void HeadlessBrowserImpl::AddObserver(Observer* observer) { |
| 69 observers_.insert(observer); |
| 70 } |
| 71 |
| 72 void HeadlessBrowserImpl::RemoveObserver(Observer* observer) { |
| 73 observers_.erase(observer); |
| 74 } |
| 75 |
| 68 HeadlessWebContents* HeadlessBrowserImpl::CreateWebContents( | 76 HeadlessWebContents* HeadlessBrowserImpl::CreateWebContents( |
| 69 HeadlessWebContents::Builder* builder) { | 77 HeadlessWebContents::Builder* builder) { |
| 70 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 78 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 71 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = | 79 std::unique_ptr<HeadlessWebContentsImpl> headless_web_contents = |
| 72 HeadlessWebContentsImpl::Create(builder, window_tree_host_->window(), | 80 HeadlessWebContentsImpl::Create(builder, window_tree_host_->window(), |
| 73 this); | 81 this); |
| 74 if (!headless_web_contents) | 82 if (!headless_web_contents) |
| 75 return nullptr; | 83 return nullptr; |
| 76 return RegisterWebContents(std::move(headless_web_contents)); | 84 return RegisterWebContents(std::move(headless_web_contents)); |
| 77 } | 85 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 101 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 109 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 102 BrowserMainThread()->PostTask(FROM_HERE, | 110 BrowserMainThread()->PostTask(FROM_HERE, |
| 103 base::MessageLoop::QuitWhenIdleClosure()); | 111 base::MessageLoop::QuitWhenIdleClosure()); |
| 104 } | 112 } |
| 105 | 113 |
| 106 std::vector<HeadlessWebContents*> HeadlessBrowserImpl::GetAllWebContents() { | 114 std::vector<HeadlessWebContents*> HeadlessBrowserImpl::GetAllWebContents() { |
| 107 std::vector<HeadlessWebContents*> result; | 115 std::vector<HeadlessWebContents*> result; |
| 108 result.reserve(web_contents_.size()); | 116 result.reserve(web_contents_.size()); |
| 109 | 117 |
| 110 for (const auto& web_contents_pair : web_contents_) { | 118 for (const auto& web_contents_pair : web_contents_) { |
| 111 result.push_back(web_contents_pair.first); | 119 result.push_back(web_contents_pair.second.get()); |
| 112 } | 120 } |
| 113 | 121 |
| 114 return result; | 122 return result; |
| 115 } | 123 } |
| 116 | 124 |
| 117 HeadlessBrowserMainParts* HeadlessBrowserImpl::browser_main_parts() const { | 125 HeadlessBrowserMainParts* HeadlessBrowserImpl::browser_main_parts() const { |
| 118 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); | 126 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 119 return browser_main_parts_; | 127 return browser_main_parts_; |
| 120 } | 128 } |
| 121 | 129 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 135 | 143 |
| 136 window_tree_client_.reset( | 144 window_tree_client_.reset( |
| 137 new HeadlessWindowTreeClient(window_tree_host_->window())); | 145 new HeadlessWindowTreeClient(window_tree_host_->window())); |
| 138 | 146 |
| 139 on_start_callback_.Run(this); | 147 on_start_callback_.Run(this); |
| 140 on_start_callback_ = base::Callback<void(HeadlessBrowser*)>(); | 148 on_start_callback_ = base::Callback<void(HeadlessBrowser*)>(); |
| 141 } | 149 } |
| 142 | 150 |
| 143 HeadlessWebContentsImpl* HeadlessBrowserImpl::RegisterWebContents( | 151 HeadlessWebContentsImpl* HeadlessBrowserImpl::RegisterWebContents( |
| 144 std::unique_ptr<HeadlessWebContentsImpl> web_contents) { | 152 std::unique_ptr<HeadlessWebContentsImpl> web_contents) { |
| 153 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 145 DCHECK(web_contents); | 154 DCHECK(web_contents); |
| 146 HeadlessWebContentsImpl* unowned_web_contents = web_contents.get(); | 155 HeadlessWebContentsImpl* unowned_web_contents = web_contents.get(); |
| 147 web_contents_[unowned_web_contents] = std::move(web_contents); | 156 web_contents_[unowned_web_contents] = std::move(web_contents); |
| 157 for (auto it = observers_.begin(); it != observers_.end();) { |
| 158 // Pre-increment it because the observer may unregister itself. |
| 159 Observer* observer = *it++; |
| 160 observer->HeadlessWebContentsCreated(unowned_web_contents); |
| 161 } |
| 148 return unowned_web_contents; | 162 return unowned_web_contents; |
| 149 } | 163 } |
| 150 | 164 |
| 151 void HeadlessBrowserImpl::DestroyWebContents( | 165 void HeadlessBrowserImpl::DestroyWebContents( |
| 152 HeadlessWebContentsImpl* web_contents) { | 166 HeadlessWebContentsImpl* web_contents) { |
| 153 auto it = web_contents_.find(web_contents); | 167 DCHECK(BrowserMainThread()->BelongsToCurrentThread()); |
| 154 DCHECK(it != web_contents_.end()); | 168 auto find_it = web_contents_.find(web_contents); |
| 155 web_contents_.erase(it); | 169 DCHECK(find_it != web_contents_.end()); |
| 170 HeadlessWebContents* headless_web_contents = find_it->second.get(); |
| 171 web_contents_.erase(find_it); |
| 172 for (auto it = observers_.begin(); it != observers_.end();) { |
| 173 // Pre-increment it because the observer may unregister itself. |
| 174 Observer* observer = *it++; |
| 175 observer->HeadlessWebContentsDestroyed(headless_web_contents); |
| 176 } |
| 156 } | 177 } |
| 157 | 178 |
| 158 void HeadlessBrowserImpl::SetOptionsForTesting( | 179 void HeadlessBrowserImpl::SetOptionsForTesting( |
| 159 HeadlessBrowser::Options options) { | 180 HeadlessBrowser::Options options) { |
| 160 options_ = std::move(options); | 181 options_ = std::move(options); |
| 161 browser_main_parts()->default_browser_context()->SetOptionsForTesting( | 182 browser_main_parts()->default_browser_context()->SetOptionsForTesting( |
| 162 &options_); | 183 &options_); |
| 163 } | 184 } |
| 164 | 185 |
| 165 void RunChildProcessIfNeeded(int argc, const char** argv) { | 186 void RunChildProcessIfNeeded(int argc, const char** argv) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 184 | 205 |
| 185 // Child processes should not end up here. | 206 // Child processes should not end up here. |
| 186 base::CommandLine command_line(options.argc, options.argv); | 207 base::CommandLine command_line(options.argc, options.argv); |
| 187 DCHECK(!command_line.HasSwitch(switches::kProcessType)); | 208 DCHECK(!command_line.HasSwitch(switches::kProcessType)); |
| 188 #endif | 209 #endif |
| 189 return RunContentMain(std::move(options), | 210 return RunContentMain(std::move(options), |
| 190 std::move(on_browser_start_callback)); | 211 std::move(on_browser_start_callback)); |
| 191 } | 212 } |
| 192 | 213 |
| 193 } // namespace headless | 214 } // namespace headless |
| OLD | NEW |