Eliminate ViewTreeHostConnection and replace with a factory function.
[chromium-blink-merge.git] / mandoline / ui / desktop_ui / browser_manager.cc
blob1fb7b6dd1e0c2c5059feed09e0b7fc911ea64764
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 scoped_ptr<BrowserWindow> browser_owner(browser);
36 DCHECK_GT(browsers_.count(browser), 0u);
37 browsers_.erase(browser);
38 if (browsers_.empty())
39 app_->Quit();
42 void BrowserManager::LaunchURL(const mojo::String& url) {
43 DCHECK(!browsers_.empty());
44 // TODO(fsamuel): Create a new Browser once we support multiple browser
45 // windows.
46 (*browsers_.begin())->LoadURL(GURL(url));
49 void BrowserManager::Initialize(mojo::ApplicationImpl* app) {
50 app_ = app;
52 mojo::URLRequestPtr request(mojo::URLRequest::New());
53 request->url = "mojo:view_manager";
54 app_->ConnectToService(request.Pass(), &host_factory_);
56 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
57 // Create a Browser for each valid URL in the command line.
58 for (const auto& arg : command_line->GetArgs()) {
59 GURL url(arg);
60 if (url.is_valid())
61 CreateBrowser(url);
63 // If there were no valid URLs in the command line create a Browser with the
64 // default URL.
65 if (browsers_.empty())
66 CreateBrowser(GURL(kGoogleURL));
69 bool BrowserManager::ConfigureIncomingConnection(
70 mojo::ApplicationConnection* connection) {
71 connection->AddService<LaunchHandler>(this);
72 return true;
75 void BrowserManager::Create(mojo::ApplicationConnection* connection,
76 mojo::InterfaceRequest<LaunchHandler> request) {
77 launch_handler_bindings_.AddBinding(this, request.Pass());
80 } // namespace mandoline