summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/browser_notification_observers.cc
blob: 366e6543a6f1d4858ad98e47f808737198c65b46 (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
// Copyright (c) 2010 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 "chrome/browser/chromeos/browser_notification_observers.h"

#include <string>

#include "base/file_util.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/chromeos/login/authentication_notification_details.h"
#include "chrome/common/notification_service.h"

namespace {

// Static function that records uptime in /proc/uptime to tmp for metrics use.
void RecordUptime(const std::string& filename) {
  std::string uptime;
  const FilePath proc_uptime = FilePath("/proc/uptime");
  const FilePath uptime_output = FilePath(filename);

  if (file_util::ReadFileToString(proc_uptime, &uptime))
    file_util::WriteFile(uptime_output, uptime.data(), uptime.size());
}

}  // namespace

namespace chromeos {

InitialTabNotificationObserver::InitialTabNotificationObserver() {
  registrar_.Add(this, NotificationType::LOAD_START,
                 NotificationService::AllSources());
}

InitialTabNotificationObserver::~InitialTabNotificationObserver() {
}

void InitialTabNotificationObserver::Observe(
    NotificationType type,
    const NotificationSource& source,
    const NotificationDetails& details) {
  // Only log for first tab to render.  Make sure this is only done once.
  if (type == NotificationType::LOAD_START &&
      num_tabs_.GetNext() == 0) {
    // If we can't post it, it doesn't matter.
    ChromeThread::PostTask(
        ChromeThread::FILE, FROM_HERE,
        NewRunnableFunction(RecordUptime,
                            std::string("/tmp/uptime-chrome-first-render")));
    registrar_.Remove(this, NotificationType::LOAD_START,
                      NotificationService::AllSources());
  }
}

LogLoginSuccessObserver::LogLoginSuccessObserver() {
  registrar_.Add(this, NotificationType::LOGIN_AUTHENTICATION,
                 NotificationService::AllSources());
}

LogLoginSuccessObserver::~LogLoginSuccessObserver() {
}

void LogLoginSuccessObserver::Observe(NotificationType type,
                                   const NotificationSource& source,
                                   const NotificationDetails& details) {
  DCHECK(type == NotificationType::LOGIN_AUTHENTICATION);
  Details<AuthenticationNotificationDetails> auth_details(details);
  if (auth_details->success()) {
    // If we can't post it, it doesn't matter.
    ChromeThread::PostTask(ChromeThread::FILE, FROM_HERE,
                           NewRunnableFunction(RecordUptime,
                               std::string("/tmp/uptime-login-successful")));
    registrar_.Remove(this, NotificationType::LOGIN_AUTHENTICATION,
                      NotificationService::AllSources());
  }
}

}  // namespace chromeos