Roll src/third_party/WebKit a452221:9ff6d11 (svn 202117:202119)
[chromium-blink-merge.git] / mandoline / ui / desktop_ui / browser_manager.cc
blob2c53dc4c4d501beb42725ccf495cac64ad3a115e
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
3 // found in the LICENSE file.
5 #include "mandoline/ui/desktop_ui/browser_manager.h"
7 #include "base/command_line.h"
8 #include "components/view_manager/public/cpp/view.h"
9 #include "components/view_manager/public/cpp/view_observer.h"
10 #include "mandoline/ui/desktop_ui/browser_window.h"
12 namespace mandoline {
14 namespace {
16 const char kGoogleURL[] = "http://www.google.com";
18 } // namespace
20 BrowserManager::BrowserManager()
21 : app_(nullptr), startup_time_(base::Time::Now()) {}
23 BrowserManager::~BrowserManager() {
24 DCHECK(browsers_.empty());
27 BrowserWindow* BrowserManager::CreateBrowser(const GURL& default_url) {
28 BrowserWindow* browser = new BrowserWindow(app_, host_factory_.get(), this);
29 browsers_.insert(browser);
30 browser->LoadURL(default_url);
31 return browser;
34 void BrowserManager::BrowserWindowClosed(BrowserWindow* browser) {
35 DCHECK_GT(browsers_.count(browser), 0u);
36 browsers_.erase(browser);
37 if (browsers_.empty())
38 app_->Quit();
41 void BrowserManager::LaunchURL(const mojo::String& url) {
42 DCHECK(!browsers_.empty());
43 // TODO(fsamuel): Create a new Browser once we support multiple browser
44 // windows.
45 (*browsers_.begin())->LoadURL(GURL(url));
48 void BrowserManager::Initialize(mojo::ApplicationImpl* app) {
49 app_ = app;
51 mojo::URLRequestPtr request(mojo::URLRequest::New());
52 request->url = "mojo:view_manager";
53 app_->ConnectToService(request.Pass(), &host_factory_);
55 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
56 // Create a Browser for each valid URL in the command line.
57 for (const auto& arg : command_line->GetArgs()) {
58 GURL url(arg);
59 if (url.is_valid())
60 CreateBrowser(url);
62 // If there were no valid URLs in the command line create a Browser with the
63 // default URL.
64 if (browsers_.empty())
65 CreateBrowser(GURL(kGoogleURL));
68 bool BrowserManager::ConfigureIncomingConnection(
69 mojo::ApplicationConnection* connection) {
70 connection->AddService<LaunchHandler>(this);
71 return true;
74 void BrowserManager::Create(mojo::ApplicationConnection* connection,
75 mojo::InterfaceRequest<LaunchHandler> request) {
76 launch_handler_bindings_.AddBinding(this, request.Pass());
79 } // namespace mandoline