summaryrefslogtreecommitdiffstats
path: root/base/metrics
diff options
context:
space:
mode:
authorben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-13 21:24:39 +0000
committerben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-13 21:24:39 +0000
commite6e30ac7da61cf09099d861344b3c22e5d5d158c (patch)
tree3d6df38aeec8737b3aac899ab2079d77511a96e6 /base/metrics
parent662e95081cfe6aa20be3a367d7e12b71f9d790b3 (diff)
downloadchromium_src-e6e30ac7da61cf09099d861344b3c22e5d5d158c.zip
chromium_src-e6e30ac7da61cf09099d861344b3c22e5d5d158c.tar.gz
chromium_src-e6e30ac7da61cf09099d861344b3c22e5d5d158c.tar.bz2
Move the guts of user_metrics to a new static lib in //base/metrics.
I retained a couple of wrapper functions in content that post back to the UI thread if they are called from another thread. All existing code will continue to use these wrappers. The intent is that some code that I don't want to depend on content that only lives on the UI thread can now call //base/metrics code directly. R=isherman@chromium.org, jam@chromium.org http://crbug.com/332504 Review URL: https://codereview.chromium.org/129223004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@244580 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/metrics')
-rw-r--r--base/metrics/user_metrics.cc45
-rw-r--r--base/metrics/user_metrics.h57
-rw-r--r--base/metrics/user_metrics_action.h28
3 files changed, 130 insertions, 0 deletions
diff --git a/base/metrics/user_metrics.cc b/base/metrics/user_metrics.cc
new file mode 100644
index 0000000..a3b122a
--- /dev/null
+++ b/base/metrics/user_metrics.cc
@@ -0,0 +1,45 @@
+// Copyright 2014 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 "base/metrics/user_metrics.h"
+
+#include <vector>
+
+#include "base/lazy_instance.h"
+
+namespace base {
+namespace {
+
+base::LazyInstance<std::vector<ActionCallback> > g_action_callbacks =
+ LAZY_INSTANCE_INITIALIZER;
+
+void Record(const char *action) {
+ for (size_t i = 0; i < g_action_callbacks.Get().size(); i++)
+ g_action_callbacks.Get()[i].Run(action);
+}
+
+} // namespace
+
+void RecordAction(const UserMetricsAction& action) {
+ Record(action.str_);
+}
+
+void RecordComputedAction(const std::string& action) {
+ Record(action.c_str());
+}
+
+void AddActionCallback(const ActionCallback& callback) {
+ g_action_callbacks.Get().push_back(callback);
+}
+
+void RemoveActionCallback(const ActionCallback& callback) {
+ for (size_t i = 0; i < g_action_callbacks.Get().size(); i++) {
+ if (g_action_callbacks.Get()[i].Equals(callback)) {
+ g_action_callbacks.Get().erase(g_action_callbacks.Get().begin() + i);
+ return;
+ }
+ }
+}
+
+} // namespace base
diff --git a/base/metrics/user_metrics.h b/base/metrics/user_metrics.h
new file mode 100644
index 0000000..10e7d3c
--- /dev/null
+++ b/base/metrics/user_metrics.h
@@ -0,0 +1,57 @@
+// Copyright 2014 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.
+
+#ifndef BASE_METRICS_USER_METRICS_H_
+#define BASE_METRICS_USER_METRICS_H_
+
+#include <string>
+
+#include "base/base_export.h"
+#include "base/callback.h"
+#include "base/metrics/user_metrics_action.h"
+
+namespace base {
+
+// This module provides some helper functions for logging actions tracked by
+// the user metrics system.
+
+// Record that the user performed an action.
+// "Action" here means a user-generated event:
+// good: "Reload", "CloseTab", and "IMEInvoked"
+// not good: "SSLDialogShown", "PageLoaded", "DiskFull"
+// We use this to gather anonymized information about how users are
+// interacting with the browser.
+// WARNING: In calls to this function, UserMetricsAction and a
+// string literal parameter must be on the same line, e.g.
+// RecordAction(UserMetricsAction("my extremely long action name"));
+// This ensures that our processing scripts can associate this action's hash
+// with its metric name. Therefore, it will be possible to retrieve the metric
+// name from the hash later on.
+//
+// Once a new recorded action is added, run
+// tools/metrics/actions/extract_actions.py --hash
+// to generate a new mapping of [action hashes -> metric names] and send it
+// out for review to be updated.
+//
+// For more complicated situations (like when there are many different
+// possible actions), see RecordComputedAction.
+BASE_EXPORT void RecordAction(const UserMetricsAction& action);
+
+// This function has identical input and behavior to RecordAction, but is
+// not automatically found by the action-processing scripts. It can be used
+// when it's a pain to enumerate all possible actions, but if you use this
+// you need to also update the rules for extracting known actions in
+// tools/metrics/actions/extract_actions.py.
+BASE_EXPORT void RecordComputedAction(const std::string& action);
+
+// Called with the action string.
+typedef base::Callback<void(const std::string&)> ActionCallback;
+
+// Add/remove action callbacks (see above).
+BASE_EXPORT void AddActionCallback(const ActionCallback& callback);
+BASE_EXPORT void RemoveActionCallback(const ActionCallback& callback);
+
+} // namespace base
+
+#endif // BASE_METRICS_USER_METRICS_H_
diff --git a/base/metrics/user_metrics_action.h b/base/metrics/user_metrics_action.h
new file mode 100644
index 0000000..8c195b3
--- /dev/null
+++ b/base/metrics/user_metrics_action.h
@@ -0,0 +1,28 @@
+// Copyright 2014 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.
+
+#ifndef BASE_METRICS_USER_METRICS_ACTION_H_
+#define BASE_METRICS_USER_METRICS_ACTION_H_
+
+namespace base {
+
+// UserMetricsAction exists purely to standardize on the parameters passed to
+// UserMetrics. That way, our toolset can scan the source code reliable for
+// constructors and extract the associated string constants.
+// WARNING: When using UserMetricsAction, UserMetricsAction and a string literal
+// parameter must be on the same line, e.g.
+// RecordAction(UserMetricsAction("my extremely long action name"));
+// or
+// RenderThread::Get()->RecordAction(
+// UserMetricsAction("my extremely long action name"));
+// because otherwise our processing scripts won't pick up on new actions.
+// Please see tools/metrics/actions/extract_actions.py for details.
+struct UserMetricsAction {
+ const char* str_;
+ explicit UserMetricsAction(const char* str) : str_(str) {}
+};
+
+} // namespace base
+
+#endif // BASE_METRICS_USER_METRICS_ACTION_H_