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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/metrics/perf/windowed_incognito_observer.h"
#include <tuple>
#include "base/no_destructor.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "content/public/browser/browser_thread.h"
namespace metrics {
WindowedIncognitoObserver::WindowedIncognitoObserver(
WindowedIncognitoMonitor* monitor,
uint64_t num_incognito_window_opened)
: windowed_incognito_monitor_(monitor),
num_incognito_window_opened_(num_incognito_window_opened) {}
bool WindowedIncognitoObserver::IncognitoLaunched() const {
return windowed_incognito_monitor_->IncognitoLaunched(
num_incognito_window_opened_);
}
bool WindowedIncognitoObserver::IncognitoActive() const {
return windowed_incognito_monitor_->IncognitoActive();
}
// static
void WindowedIncognitoMonitor::Init() {
std::ignore = WindowedIncognitoMonitor::Get();
}
// static
std::unique_ptr<WindowedIncognitoObserver>
WindowedIncognitoMonitor::CreateObserver() {
WindowedIncognitoMonitor* instance = WindowedIncognitoMonitor::Get();
return instance->CreateIncognitoObserver();
}
// static
WindowedIncognitoMonitor* WindowedIncognitoMonitor::Get() {
static base::NoDestructor<WindowedIncognitoMonitor> instance;
return instance.get();
}
WindowedIncognitoMonitor::WindowedIncognitoMonitor()
: num_active_incognito_windows_(0), num_incognito_window_opened_(0) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
RegisterInstance();
}
WindowedIncognitoMonitor::~WindowedIncognitoMonitor() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UnregisterInstance();
}
void WindowedIncognitoMonitor::RegisterInstance() {
// |running_sessions_| is only accessed on the UI thread in RegisterInstance
// and UnregisterInstance. Therefore, we don't need to explicitly synchronize
// access to it.
if (running_sessions_++)
return;
BrowserList::AddObserver(this);
for (Browser* window : *BrowserList::GetInstance()) {
if (window->profile()->IsOffTheRecord())
num_active_incognito_windows_++;
}
}
void WindowedIncognitoMonitor::UnregisterInstance() {
// |running_sessions_| is only accessed on the UI thread in RegisterInstance
// and UnregisterInstance. Therefore, we don't need to explicitly synchronize
// access to it.
DCHECK_GT(running_sessions_, 0);
if (!--running_sessions_)
BrowserList::RemoveObserver(this);
}
std::unique_ptr<WindowedIncognitoObserver>
WindowedIncognitoMonitor::CreateIncognitoObserver() {
base::AutoLock lock(lock_);
return std::make_unique<WindowedIncognitoObserver>(
this, num_incognito_window_opened_);
}
bool WindowedIncognitoMonitor::IncognitoActive() const {
base::AutoLock lock(lock_);
return num_active_incognito_windows_ > 0;
}
bool WindowedIncognitoMonitor::IncognitoLaunched(
uint64_t prev_num_incognito_opened) const {
base::AutoLock lock(lock_);
// Whether there is any incognito window opened after the observer was
// created.
return prev_num_incognito_opened < num_incognito_window_opened_;
}
void WindowedIncognitoMonitor::OnBrowserAdded(Browser* browser) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!browser->profile()->IsOffTheRecord())
return;
base::AutoLock lock(lock_);
num_active_incognito_windows_++;
num_incognito_window_opened_++;
}
void WindowedIncognitoMonitor::OnBrowserRemoved(Browser* browser) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!browser->profile()->IsOffTheRecord())
return;
base::AutoLock lock(lock_);
DCHECK(num_active_incognito_windows_ > 0);
num_active_incognito_windows_--;
}
} // namespace metrics
|