Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / mandoline / ui / browser / browser_manager.cc
blob527e2652a951823f0cadebfe05dbe08478d68b50
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/browser/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/browser/browser.h"
12 namespace mandoline {
14 namespace {
16 const char kGoogleURL[] = "http://www.google.com";
18 } // namespace
20 BrowserManager::BrowserManager()
21 : app_(nullptr) {
24 BrowserManager::~BrowserManager() {
25 DCHECK(browsers_.empty());
28 Browser* BrowserManager::CreateBrowser(const GURL& default_url) {
29 DCHECK(app_);
30 Browser* browser = new Browser(app_, this, default_url);
31 browsers_.insert(browser);
32 return browser;
35 void BrowserManager::LaunchURL(const mojo::String& url) {
36 DCHECK(!browsers_.empty());
37 mojo::URLRequestPtr request(mojo::URLRequest::New());
38 request->url = url;
39 // TODO(fsamuel): Create a new Browser once we support multiple browser
40 // windows.
41 (*browsers_.begin())->ReplaceContentWithRequest(request.Pass());
44 void BrowserManager::Initialize(mojo::ApplicationImpl* app) {
45 app_ = app;
46 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
47 // Create a Browser for each valid URL in the command line.
48 for (const auto& arg : command_line->GetArgs()) {
49 GURL url(arg);
50 if (url.is_valid())
51 CreateBrowser(url);
53 // If there were no valid URLs in the command line create a Browser with the
54 // default URL.
55 if (browsers_.empty())
56 CreateBrowser(GURL(kGoogleURL));
59 bool BrowserManager::ConfigureIncomingConnection(
60 mojo::ApplicationConnection* connection) {
61 connection->AddService<LaunchHandler>(this);
62 return true;
65 void BrowserManager::BrowserClosed(Browser* browser) {
66 scoped_ptr<Browser> browser_owner(browser);
67 DCHECK_GT(browsers_.count(browser), 0u);
68 browsers_.erase(browser);
69 if (browsers_.empty())
70 app_->Terminate();
73 void BrowserManager::InitUIIfNecessary(Browser* browser, mojo::View* view) {
74 DCHECK_GT(view->viewport_metrics().device_pixel_ratio, 0);
75 #if defined(USE_AURA)
76 if (!aura_init_)
77 aura_init_.reset(new AuraInit(view, app_->shell()));
78 #endif
81 void BrowserManager::Create(mojo::ApplicationConnection* connection,
82 mojo::InterfaceRequest<LaunchHandler> request) {
83 launch_handler_bindings_.AddBinding(this, request.Pass());
86 } // namespace mandoline