1 // Copyright 2014 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_METRICS_USER_METRICS_H_ 6 #define BASE_METRICS_USER_METRICS_H_ 7 8 #include <string> 9 10 #include "base/base_export.h" 11 #include "base/functional/callback.h" 12 #include "base/metrics/user_metrics_action.h" 13 #include "base/task/single_thread_task_runner.h" 14 15 namespace base { 16 17 class TimeTicks; 18 19 // This module provides some helper functions for logging actions tracked by 20 // the user metrics system. 21 22 // For best practices on deciding when to emit a user action, see 23 // https://chromium.googlesource.com/chromium/src.git/+/HEAD/tools/metrics/actions/README.md 24 25 // Record that the user performed an action. 26 // This function must be called after the task runner has been set with 27 // SetRecordActionTaskRunner(). 28 // 29 // "Action" here means a user-generated event: 30 // good: "Reload", "CloseTab", and "IMEInvoked" 31 // not good: "SSLDialogShown", "PageLoaded", "DiskFull" 32 // We use this to gather anonymized information about how users are 33 // interacting with the browser. 34 // WARNING: In calls to this function, UserMetricsAction should be followed by a 35 // string literal parameter and not a variable e.g. 36 // RecordAction(UserMetricsAction("my action name")); 37 // This ensures that our processing scripts can associate this action's hash 38 // with its metric name. Therefore, it will be possible to retrieve the metric 39 // name from the hash later on. 40 // 41 // Once a new recorded action is added, run 42 // tools/metrics/actions/extract_actions.py 43 // to add the metric to actions.xml, then update the <owner>s and <description> 44 // sections. Make sure to include the actions.xml file when you upload your code 45 // for review! 46 // 47 // For more complicated situations (like when there are many different 48 // possible actions), see RecordComputedAction(). 49 BASE_EXPORT void RecordAction(const UserMetricsAction& action); 50 51 // This function has identical input and behavior to RecordAction(), but is 52 // not automatically found by the action-processing scripts. It can be used 53 // when it's a pain to enumerate all possible actions, but if you use this 54 // you need to also update the rules for extracting known actions in 55 // tools/metrics/actions/extract_actions.py. 56 // This function must be called after the task runner has been set with 57 // SetRecordActionTaskRunner(). 58 BASE_EXPORT void RecordComputedAction(const std::string& action); 59 60 // Similar to RecordComputedAction, but also takes the time at which the action 61 // was observed. 62 BASE_EXPORT void RecordComputedActionAt(const std::string& action, 63 TimeTicks action_time); 64 65 // Similar to RecordComputedActionAt, but takes the amount of time elasped since 66 // the action was observed. 67 BASE_EXPORT void RecordComputedActionSince(const std::string& action, 68 TimeDelta time_since); 69 70 // Called with the action string. 71 using ActionCallback = RepeatingCallback<void(const std::string&, TimeTicks)>; 72 73 // Add/remove action callbacks (see above). 74 // These functions must be called after the task runner has been set with 75 // SetRecordActionTaskRunner(). 76 BASE_EXPORT void AddActionCallback(const ActionCallback& callback); 77 BASE_EXPORT void RemoveActionCallback(const ActionCallback& callback); 78 79 // Set the task runner on which to record actions. 80 BASE_EXPORT void SetRecordActionTaskRunner( 81 scoped_refptr<SingleThreadTaskRunner> task_runner); 82 83 // Returns the task runner used to record actions. Returns null when not set. 84 // This function is thread safe. 85 BASE_EXPORT scoped_refptr<SingleThreadTaskRunner> GetRecordActionTaskRunner(); 86 87 } // namespace base 88 89 #endif // BASE_METRICS_USER_METRICS_H_ 90