This source file includes following definitions.
- AddToPerfHistogram
- incognito_launched
- OnBrowserAdded
- weak_factory_
- GetPerfData
- ScheduleCollection
- CollectIfNecessary
- CollectIfNecessaryAndReschedule
- ParseProtoIfValid
#include <string>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/metrics/histogram.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/metrics/perf_provider_chromeos.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/common/chrome_switches.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/debug_daemon_client.h"
namespace {
const size_t kPerfCommandIntervalDefaultSeconds = 23093;
const size_t kPerfCommandStartIntervalLowerBoundMinutes = 10;
const size_t kPerfCommandStartIntervalUpperBoundMinutes = 20;
const size_t kPerfCommandDurationDefaultSeconds = 2;
enum GetPerfDataOutcome {
SUCCESS,
NOT_READY_TO_UPLOAD,
NOT_READY_TO_COLLECT,
INCOGNITO_ACTIVE,
INCOGNITO_LAUNCHED,
PROTOBUF_NOT_PARSED,
NUM_OUTCOMES
};
const char kGetPerfDataOutcomeHistogram[] = "UMA.Perf.GetData";
void AddToPerfHistogram(GetPerfDataOutcome outcome) {
UMA_HISTOGRAM_ENUMERATION(kGetPerfDataOutcomeHistogram,
outcome,
NUM_OUTCOMES);
}
}
namespace metrics {
class WindowedIncognitoObserver : public chrome::BrowserListObserver {
public:
WindowedIncognitoObserver() : incognito_launched_(false) {
BrowserList::AddObserver(this);
}
virtual ~WindowedIncognitoObserver() {
BrowserList::RemoveObserver(this);
}
bool incognito_launched() {
return incognito_launched_;
}
private:
virtual void OnBrowserAdded(Browser* browser) OVERRIDE {
if (browser->profile()->IsOffTheRecord())
incognito_launched_ = true;
}
bool incognito_launched_;
};
PerfProvider::PerfProvider()
: state_(READY_TO_COLLECT),
weak_factory_(this) {
size_t collection_interval_minutes = base::RandInt(
kPerfCommandStartIntervalLowerBoundMinutes,
kPerfCommandStartIntervalUpperBoundMinutes);
ScheduleCollection(base::TimeDelta::FromMinutes(collection_interval_minutes));
}
PerfProvider::~PerfProvider() {}
bool PerfProvider::GetPerfData(PerfDataProto* perf_data_proto) {
DCHECK(CalledOnValidThread());
if (state_ != READY_TO_UPLOAD) {
AddToPerfHistogram(NOT_READY_TO_UPLOAD);
return false;
}
*perf_data_proto = perf_data_proto_;
state_ = READY_TO_COLLECT;
AddToPerfHistogram(SUCCESS);
return true;
}
void PerfProvider::ScheduleCollection(const base::TimeDelta& interval) {
DCHECK(CalledOnValidThread());
if (timer_.IsRunning())
return;
timer_.Start(FROM_HERE, interval, this,
&PerfProvider::CollectIfNecessaryAndReschedule);
}
void PerfProvider::CollectIfNecessary() {
DCHECK(CalledOnValidThread());
if (state_ != READY_TO_COLLECT) {
AddToPerfHistogram(NOT_READY_TO_COLLECT);
return;
}
if (BrowserList::IsOffTheRecordSessionActive()) {
AddToPerfHistogram(INCOGNITO_ACTIVE);
return;
}
scoped_ptr<WindowedIncognitoObserver> incognito_observer(
new WindowedIncognitoObserver);
chromeos::DebugDaemonClient* client =
chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
base::TimeDelta collection_duration = base::TimeDelta::FromSeconds(
kPerfCommandDurationDefaultSeconds);
client->GetPerfData(collection_duration.InSeconds(),
base::Bind(&PerfProvider::ParseProtoIfValid,
weak_factory_.GetWeakPtr(),
base::Passed(&incognito_observer)));
}
void PerfProvider::CollectIfNecessaryAndReschedule() {
CollectIfNecessary();
ScheduleCollection(
base::TimeDelta::FromSeconds(kPerfCommandIntervalDefaultSeconds));
}
void PerfProvider::ParseProtoIfValid(
scoped_ptr<WindowedIncognitoObserver> incognito_observer,
const std::vector<uint8>& data) {
DCHECK(CalledOnValidThread());
if (incognito_observer->incognito_launched()) {
AddToPerfHistogram(INCOGNITO_LAUNCHED);
return;
}
if (!perf_data_proto_.ParseFromArray(data.data(), data.size())) {
AddToPerfHistogram(PROTOBUF_NOT_PARSED);
perf_data_proto_.Clear();
return;
}
state_ = READY_TO_UPLOAD;
}
}