summaryrefslogtreecommitdiffstats
path: root/mandoline/ui/browser/browser_manager.cc
blob: e98a0fc87e2d9c061b13490f0829b5a383d19d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mandoline/ui/browser/browser_manager.h"

#include "base/command_line.h"
#include "base/time/time.h"
#include "components/view_manager/public/cpp/view.h"
#include "components/view_manager/public/cpp/view_observer.h"
#include "mandoline/ui/browser/browser_ui.h"
#include "mojo/services/tracing/public/cpp/switches.h"
#include "mojo/services/tracing/public/interfaces/tracing.mojom.h"

namespace mandoline {

namespace {

const char kGoogleURL[] = "http://www.google.com";

}  // namespace

BrowserManager::BrowserManager()
    : app_(nullptr) {
}

BrowserManager::~BrowserManager() {
  DCHECK(browsers_.empty());
}

BrowserUI* BrowserManager::CreateBrowser(const GURL& default_url) {
  BrowserUI* browser = BrowserUI::Create(app_, this);
  browsers_.insert(browser);
  browser->LoadURL(default_url);
  return browser;
}

void BrowserManager::BrowserUIClosed(BrowserUI* browser) {
  scoped_ptr<BrowserUI> browser_owner(browser);
  DCHECK_GT(browsers_.count(browser), 0u);
  browsers_.erase(browser);
  if (browsers_.empty())
    app_->Quit();
}

void BrowserManager::LaunchURL(const mojo::String& url) {
  DCHECK(!browsers_.empty());
  // TODO(fsamuel): Create a new Browser once we support multiple browser
  // windows.
  (*browsers_.begin())->LoadURL(GURL(url));
}

void BrowserManager::Initialize(mojo::ApplicationImpl* app) {
  app_ = app;
  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  // Create a Browser for each valid URL in the command line.
  for (const auto& arg : command_line->GetArgs()) {
    GURL url(arg);
    if (url.is_valid())
      CreateBrowser(url);
  }
  // If there were no valid URLs in the command line create a Browser with the
  // default URL.
  if (browsers_.empty())
    CreateBrowser(GURL(kGoogleURL));

  // Record the browser startup time metrics for performance testing.
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
          tracing::kEnableStatsCollectionBindings)) {
    mojo::URLRequestPtr request(mojo::URLRequest::New());
    request->url = mojo::String::From("mojo:tracing");
    tracing::StartupPerformanceDataCollectorPtr collector;
    app_->ConnectToService(request.Pass(), &collector);
    // TODO(msw): When to record the browser message loop start time?
    const base::Time startup_time = base::Time::Now();
    collector->SetBrowserMessageLoopStartTime(startup_time.ToInternalValue());
    // TODO(msw): When to record the browser window display time?
    const base::Time display_time = base::Time::Now();
    collector->SetBrowserWindowDisplayTime(display_time.ToInternalValue());
    // TODO(msw): When to record the browser open tabs time?
    const base::Time open_tabs_time = base::Time::Now();
    collector->SetBrowserOpenTabsTime(open_tabs_time.ToInternalValue());
  }
}

bool BrowserManager::ConfigureIncomingConnection(
    mojo::ApplicationConnection* connection) {
  connection->AddService<LaunchHandler>(this);
  return true;
}

void BrowserManager::Create(mojo::ApplicationConnection* connection,
                            mojo::InterfaceRequest<LaunchHandler> request) {
  launch_handler_bindings_.AddBinding(this, request.Pass());
}

}  // namespace mandoline