tfarina@chromium.org | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 5 | //------------------------------------------------------------------------------ |
| 6 | // Description of the life cycle of a instance of MetricsService. |
| 7 | // |
| 8 | // OVERVIEW |
| 9 | // |
| 10 | // A MetricsService instance is typically created at application startup. It |
| 11 | // is the central controller for the acquisition of log data, and the automatic |
| 12 | // transmission of that log data to an external server. Its major job is to |
| 13 | // manage logs, grouping them for transmission, and transmitting them. As part |
| 14 | // of its grouping, MS finalizes logs by including some just-in-time gathered |
| 15 | // memory statistics, snapshotting the current stats of numerous histograms, |
| 16 | // closing the logs, translating to XML text, and compressing the results for |
| 17 | // transmission. Transmission includes submitting a compressed log as data in a |
jar@chromium.org | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 18 | // URL-post, and retransmitting (or retaining at process termination) if the |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | // attempted transmission failed. Retention across process terminations is done |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 20 | // using the the PrefServices facilities. The retained logs (the ones that never |
| 21 | // got transmitted) are compressed and base64-encoded before being persisted. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | // |
jar@chromium.org | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 23 | // Logs fall into one of two categories: "initial logs," and "ongoing logs." |
| 24 | // There is at most one initial log sent for each complete run of the chromium |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | // product (from startup, to browser shutdown). An initial log is generally |
| 26 | // transmitted some short time (1 minute?) after startup, and includes stats |
| 27 | // such as recent crash info, the number and types of plugins, etc. The |
jar@chromium.org | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 28 | // external server's response to the initial log conceptually tells this MS if |
| 29 | // it should continue transmitting logs (during this session). The server |
| 30 | // response can actually be much more detailed, and always includes (at a |
| 31 | // minimum) how often additional ongoing logs should be sent. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | // |
| 33 | // After the above initial log, a series of ongoing logs will be transmitted. |
| 34 | // The first ongoing log actually begins to accumulate information stating when |
| 35 | // the MS was first constructed. Note that even though the initial log is |
| 36 | // commonly sent a full minute after startup, the initial log does not include |
| 37 | // much in the way of user stats. The most common interlog period (delay) |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 38 | // is 20 minutes. That time period starts when the first user action causes a |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | // logging event. This means that if there is no user action, there may be long |
jar@chromium.org | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 40 | // periods without any (ongoing) log transmissions. Ongoing logs typically |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | // contain very detailed records of user activities (ex: opened tab, closed |
| 42 | // tab, fetched URL, maximized window, etc.) In addition, just before an |
| 43 | // ongoing log is closed out, a call is made to gather memory statistics. Those |
| 44 | // memory statistics are deposited into a histogram, and the log finalization |
| 45 | // code is then called. In the finalization, a call to a Histogram server |
| 46 | // acquires a list of all local histograms that have been flagged for upload |
jar@chromium.org | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 47 | // to the UMA server. The finalization also acquires a the most recent number |
| 48 | // of page loads, along with any counts of renderer or plugin crashes. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | // |
| 50 | // When the browser shuts down, there will typically be a fragment of an ongoing |
| 51 | // log that has not yet been transmitted. At shutdown time, that fragment |
| 52 | // is closed (including snapshotting histograms), and converted to text. Note |
| 53 | // that memory stats are not gathered during shutdown, as gathering *might* be |
| 54 | // too time consuming. The textual representation of the fragment of the |
| 55 | // ongoing log is then stored persistently as a string in the PrefServices, for |
| 56 | // potential transmission during a future run of the product. |
| 57 | // |
| 58 | // There are two slightly abnormal shutdown conditions. There is a |
| 59 | // "disconnected scenario," and a "really fast startup and shutdown" scenario. |
| 60 | // In the "never connected" situation, the user has (during the running of the |
| 61 | // process) never established an internet connection. As a result, attempts to |
| 62 | // transmit the initial log have failed, and a lot(?) of data has accumulated in |
| 63 | // the ongoing log (which didn't yet get closed, because there was never even a |
| 64 | // contemplation of sending it). There is also a kindred "lost connection" |
| 65 | // situation, where a loss of connection prevented an ongoing log from being |
| 66 | // transmitted, and a (still open) log was stuck accumulating a lot(?) of data, |
| 67 | // while the earlier log retried its transmission. In both of these |
| 68 | // disconnected situations, two logs need to be, and are, persistently stored |
| 69 | // for future transmission. |
| 70 | // |
| 71 | // The other unusual shutdown condition, termed "really fast startup and |
| 72 | // shutdown," involves the deliberate user termination of the process before |
| 73 | // the initial log is even formed or transmitted. In that situation, no logging |
| 74 | // is done, but the historical crash statistics remain (unlogged) for inclusion |
| 75 | // in a future run's initial log. (i.e., we don't lose crash stats). |
| 76 | // |
| 77 | // With the above overview, we can now describe the state machine's various |
| 78 | // stats, based on the State enum specified in the state_ member. Those states |
| 79 | // are: |
| 80 | // |
| 81 | // INITIALIZED, // Constructor was called. |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 82 | // INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to complete. |
| 83 | // INIT_TASK_DONE, // Waiting for timer to send initial log. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | // INITIAL_LOG_READY, // Initial log generated, and waiting for reply. |
| 85 | // SEND_OLD_INITIAL_LOGS, // Sending unsent logs from previous session. |
| 86 | // SENDING_OLD_LOGS, // Sending unsent logs from previous session. |
| 87 | // SENDING_CURRENT_LOGS, // Sending standard current logs as they accrue. |
| 88 | // |
| 89 | // In more detail, we have: |
| 90 | // |
| 91 | // INITIALIZED, // Constructor was called. |
| 92 | // The MS has been constructed, but has taken no actions to compose the |
| 93 | // initial log. |
| 94 | // |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 95 | // INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to complete. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | // Typically about 30 seconds after startup, a task is sent to a second thread |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 97 | // (the file thread) to perform deferred (lower priority and slower) |
| 98 | // initialization steps such as getting the list of plugins. That task will |
| 99 | // (when complete) make an async callback (via a Task) to indicate the |
| 100 | // completion. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | // |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 102 | // INIT_TASK_DONE, // Waiting for timer to send initial log. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | // The callback has arrived, and it is now possible for an initial log to be |
| 104 | // created. This callback typically arrives back less than one second after |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 105 | // the deferred init task is dispatched. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | // |
| 107 | // INITIAL_LOG_READY, // Initial log generated, and waiting for reply. |
| 108 | // This state is entered only after an initial log has been composed, and |
| 109 | // prepared for transmission. It is also the case that any previously unsent |
| 110 | // logs have been loaded into instance variables for possible transmission. |
| 111 | // |
| 112 | // SEND_OLD_INITIAL_LOGS, // Sending unsent logs from previous session. |
| 113 | // This state indicates that the initial log for this session has been |
| 114 | // successfully sent and it is now time to send any "initial logs" that were |
| 115 | // saved from previous sessions. Most commonly, there are none, but all old |
| 116 | // logs that were "initial logs" must be sent before this state is exited. |
| 117 | // |
| 118 | // SENDING_OLD_LOGS, // Sending unsent logs from previous session. |
| 119 | // This state indicates that there are no more unsent initial logs, and now any |
| 120 | // ongoing logs from previous sessions should be transmitted. All such logs |
| 121 | // will be transmitted before exiting this state, and proceeding with ongoing |
| 122 | // logs from the current session (see next state). |
| 123 | // |
| 124 | // SENDING_CURRENT_LOGS, // Sending standard current logs as they accrue. |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 125 | // Current logs are being accumulated. Typically every 20 minutes a log is |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | // closed and finalized for transmission, at the same time as a new log is |
| 127 | // started. |
| 128 | // |
| 129 | // The progression through the above states is simple, and sequential, in the |
| 130 | // most common use cases. States proceed from INITIAL to SENDING_CURRENT_LOGS, |
| 131 | // and remain in the latter until shutdown. |
| 132 | // |
| 133 | // The one unusual case is when the user asks that we stop logging. When that |
| 134 | // happens, any pending (transmission in progress) log is pushed into the list |
| 135 | // of old unsent logs (the appropriate list, depending on whether it is an |
| 136 | // initial log, or an ongoing log). An addition, any log that is currently |
| 137 | // accumulating is also finalized, and pushed into the unsent log list. With |
jar@chromium.org | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 138 | // those pushes performed, we regress back to the SEND_OLD_INITIAL_LOGS state in |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | // case the user enables log recording again during this session. This way |
| 140 | // anything we have "pushed back" will be sent automatically if/when we progress |
| 141 | // back to SENDING_CURRENT_LOG state. |
| 142 | // |
| 143 | // Also note that whenever the member variables containing unsent logs are |
| 144 | // modified (i.e., when we send an old log), we mirror the list of logs into |
| 145 | // the PrefServices. This ensures that IF we crash, we won't start up and |
| 146 | // retransmit our old logs again. |
| 147 | // |
| 148 | // Due to race conditions, it is always possible that a log file could be sent |
| 149 | // twice. For example, if a log file is sent, but not yet acknowledged by |
| 150 | // the external server, and the user shuts down, then a copy of the log may be |
| 151 | // saved for re-transmission. These duplicates could be filtered out server |
jar@chromium.org | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 152 | // side, but are not expected to be a significant problem. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | // |
| 154 | // |
| 155 | //------------------------------------------------------------------------------ |
| 156 | |
maruel@chromium.org | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 157 | #include "chrome/browser/metrics/metrics_service.h" |
| 158 | |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 159 | #include "base/base64.h" |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 160 | #include "base/bind.h" |
| 161 | #include "base/callback.h" |
erg@google.com | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 162 | #include "base/command_line.h" |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 163 | #include "base/md5.h" |
brettw@chromium.org | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 164 | #include "base/metrics/histogram.h" |
brettw@chromium.org | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 165 | #include "base/string_number_conversions.h" |
rohitrao@chromium.org | e257d5b | 2011-05-25 14:10:16 | [diff] [blame] | 166 | #include "base/string_util.h" // For WriteInto(). |
brettw@chromium.org | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 167 | #include "base/threading/platform_thread.h" |
tfarina@chromium.org | b3841c50 | 2011-03-09 01:21:31 | [diff] [blame] | 168 | #include "base/threading/thread.h" |
viettrungluu@chromium.org | 440b37b2 | 2010-08-30 05:31:40 | [diff] [blame] | 169 | #include "base/utf_string_conversions.h" |
erg@google.com | 67908205 | 2010-07-21 21:30:13 | [diff] [blame] | 170 | #include "base/values.h" |
sky@google.com | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 171 | #include "chrome/browser/bookmarks/bookmark_model.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | #include "chrome/browser/browser_process.h" |
sail@chromium.org | 84c988a | 2011-04-19 17:56:33 | [diff] [blame] | 173 | #include "chrome/browser/memory_details.h" |
phajdan.jr@chromium.org | 7c927b6 | 2010-02-24 09:54:13 | [diff] [blame] | 174 | #include "chrome/browser/metrics/histogram_synchronizer.h" |
erg@google.com | 67908205 | 2010-07-21 21:30:13 | [diff] [blame] | 175 | #include "chrome/browser/metrics/metrics_log.h" |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 176 | #include "chrome/browser/metrics/metrics_reporting_scheduler.h" |
rtenneti@chromium.org | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 177 | #include "chrome/browser/net/network_stats.h" |
evan@chromium.org | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 178 | #include "chrome/browser/prefs/pref_service.h" |
battre@chromium.org | f8628c2 | 2011-04-05 12:10:18 | [diff] [blame] | 179 | #include "chrome/browser/prefs/scoped_user_pref_update.h" |
ben@chromium.org | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 180 | #include "chrome/browser/profiles/profile.h" |
erg@google.com | 8e5c89a | 2011-06-07 18:13:33 | [diff] [blame] | 181 | #include "chrome/browser/search_engines/template_url_service.h" |
tfarina@chromium.org | 71b73f0 | 2011-04-06 15:57:29 | [diff] [blame] | 182 | #include "chrome/browser/ui/browser_list.h" |
kuchhal@chromium.org | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 183 | #include "chrome/common/child_process_logging.h" |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 184 | #include "chrome/common/chrome_notification_types.h" |
jar@chromium.org | 9274524 | 2009-06-12 16:52:21 | [diff] [blame] | 185 | #include "chrome/common/chrome_switches.h" |
sergeyu@chromium.org | 3eb0d8f7 | 2010-12-15 23:38:25 | [diff] [blame] | 186 | #include "chrome/common/guid.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | #include "chrome/common/pref_names.h" |
jam@chromium.org | e09ba55 | 2009-02-05 03:26:29 | [diff] [blame] | 188 | #include "chrome/common/render_messages.h" |
avi@chromium.org | 35e251d | 2011-05-24 21:01:04 | [diff] [blame] | 189 | #include "content/browser/load_notification_details.h" |
jam@chromium.org | 5de63471 | 2011-03-02 00:20:19 | [diff] [blame] | 190 | #include "content/browser/renderer_host/render_process_host.h" |
jam@chromium.org | 6faf7b1 | 2011-03-09 19:24:14 | [diff] [blame] | 191 | #include "content/common/child_process_info.h" |
tfarina@chromium.org | b3841c50 | 2011-03-09 01:21:31 | [diff] [blame] | 192 | #include "content/common/notification_service.h" |
brettw@chromium.org | 191eb3f7 | 2010-12-21 06:27:50 | [diff] [blame] | 193 | #include "webkit/plugins/npapi/plugin_list.h" |
cpu@chromium.org | 91d9f3d | 2011-08-14 05:24:44 | [diff] [blame] | 194 | #include "webkit/plugins/webplugininfo.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 195 | |
phajdan.jr@chromium.org | e06131d | 2010-02-10 18:40:33 | [diff] [blame] | 196 | // TODO(port): port browser_distribution.h. |
| 197 | #if !defined(OS_POSIX) |
kuchhal@chromium.org | 79bf0b7 | 2009-04-27 21:30:55 | [diff] [blame] | 198 | #include "chrome/installer/util/browser_distribution.h" |
paul@chromium.org | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 199 | #endif |
| 200 | |
rvargas@google.com | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 201 | #if defined(OS_CHROMEOS) |
stevenjb@chromium.org | db342d5 | 2010-08-09 21:19:37 | [diff] [blame] | 202 | #include "chrome/browser/chromeos/cros/cros_library.h" |
rvargas@google.com | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 203 | #include "chrome/browser/chromeos/external_metrics.h" |
satorux@chromium.org | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 204 | #include "chrome/browser/chromeos/system/statistics_provider.h" |
rvargas@google.com | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 205 | #endif |
| 206 | |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 207 | namespace { |
| 208 | MetricsService::LogRecallStatus MakeRecallStatusHistogram( |
| 209 | MetricsService::LogRecallStatus status) { |
| 210 | UMA_HISTOGRAM_ENUMERATION("PrefService.PersistentLogRecall", status, |
| 211 | MetricsService::END_RECALL_STATUS); |
| 212 | return status; |
| 213 | } |
| 214 | |
| 215 | // TODO(ziadh): Remove this when done with experiment. |
| 216 | void MakeStoreStatusHistogram(MetricsService::LogStoreStatus status) { |
ziadh@chromium.org | 4e95d20 | 2010-07-24 01:47:56 | [diff] [blame] | 217 | UMA_HISTOGRAM_ENUMERATION("PrefService.PersistentLogStore2", status, |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 218 | MetricsService::END_STORE_STATUS); |
| 219 | } |
| 220 | } // namespace |
| 221 | |
dsh@google.com | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 222 | using base::Time; |
dsh@google.com | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 223 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 224 | // Check to see that we're being called on only one thread. |
| 225 | static bool IsSingleThreaded(); |
| 226 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | static const char kMetricsType[] = "application/vnd.mozilla.metrics.bz2"; |
| 228 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 229 | // The delay, in seconds, after starting recording before doing expensive |
| 230 | // initialization work. |
| 231 | static const int kInitializationDelaySeconds = 30; |
petersont@google.com | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 232 | |
jar@chromium.org | c9a3ef8 | 2009-05-28 22:02:46 | [diff] [blame] | 233 | // This specifies the amount of time to wait for all renderers to send their |
| 234 | // data. |
| 235 | static const int kMaxHistogramGatheringWaitDuration = 60000; // 60 seconds. |
| 236 | |
stuartmorgan@chromium.org | 54702c9 | 2011-04-15 15:06:43 | [diff] [blame] | 237 | // The maximum number of events in a log uploaded to the UMA server. |
| 238 | static const int kEventLimit = 2400; |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 239 | |
| 240 | // If an upload fails, and the transmission was over this byte count, then we |
| 241 | // will discard the log, and not try to retransmit it. We also don't persist |
| 242 | // the log to the prefs for transmission during the next chrome session if this |
| 243 | // limit is exceeded. |
| 244 | static const int kUploadLogAvoidRetransmitSize = 50000; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 246 | // Interval, in seconds, between state saves. |
| 247 | static const int kSaveStateInterval = 5 * 60; // five minutes |
| 248 | |
| 249 | // The number of "initial" logs we're willing to save, and hope to send during |
| 250 | // a future Chrome session. Initial logs contain crash stats, and are pretty |
| 251 | // small. |
| 252 | static const size_t kMaxInitialLogsPersisted = 20; |
| 253 | |
| 254 | // The number of ongoing logs we're willing to save persistently, and hope to |
jar@chromium.org | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 255 | // send during a this or future sessions. Note that each log may be pretty |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 256 | // large, as presumably the related "initial" log wasn't sent (probably nothing |
| 257 | // was, as the user was probably off-line). As a result, the log probably kept |
| 258 | // accumulating while the "initial" log was stalled (pending_), and couldn't be |
| 259 | // sent. As a result, we don't want to save too many of these mega-logs. |
| 260 | // A "standard shutdown" will create a small log, including just the data that |
| 261 | // was not yet been transmitted, and that is normal (to have exactly one |
| 262 | // ongoing_log_ at startup). |
jar@chromium.org | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 263 | static const size_t kMaxOngoingLogsPersisted = 8; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 264 | |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 265 | // We append (2) more elements to persisted lists: the size of the list and a |
| 266 | // checksum of the elements. |
| 267 | static const size_t kChecksumEntryCount = 2; |
| 268 | |
jar@chromium.org | c0c55e9 | 2011-09-10 18:47:30 | [diff] [blame^] | 269 | // static |
| 270 | MetricsService::ShutdownCleanliness MetricsService::clean_shutdown_status_ = |
| 271 | MetricsService::CLEANLY_SHUTDOWN; |
| 272 | |
erg@google.com | 67908205 | 2010-07-21 21:30:13 | [diff] [blame] | 273 | // This is used to quickly log stats from child process related notifications in |
| 274 | // MetricsService::child_stats_buffer_. The buffer's contents are transferred |
| 275 | // out when Local State is periodically saved. The information is then |
| 276 | // reported to the UMA server on next launch. |
| 277 | struct MetricsService::ChildProcessStats { |
| 278 | public: |
| 279 | explicit ChildProcessStats(ChildProcessInfo::ProcessType type) |
| 280 | : process_launches(0), |
| 281 | process_crashes(0), |
| 282 | instances(0), |
| 283 | process_type(type) {} |
| 284 | |
| 285 | // This constructor is only used by the map to return some default value for |
| 286 | // an index for which no value has been assigned. |
| 287 | ChildProcessStats() |
| 288 | : process_launches(0), |
pkasting@chromium.org | d88bf0a | 2011-08-30 23:55:57 | [diff] [blame] | 289 | process_crashes(0), |
| 290 | instances(0), |
| 291 | process_type(ChildProcessInfo::UNKNOWN_PROCESS) {} |
erg@google.com | 67908205 | 2010-07-21 21:30:13 | [diff] [blame] | 292 | |
| 293 | // The number of times that the given child process has been launched |
| 294 | int process_launches; |
| 295 | |
| 296 | // The number of times that the given child process has crashed |
| 297 | int process_crashes; |
| 298 | |
| 299 | // The number of instances of this child process that have been created. |
| 300 | // An instance is a DOM object rendered by this child process during a page |
| 301 | // load. |
| 302 | int instances; |
| 303 | |
| 304 | ChildProcessInfo::ProcessType process_type; |
| 305 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 306 | |
sail@chromium.org | 84c988a | 2011-04-19 17:56:33 | [diff] [blame] | 307 | // Handles asynchronous fetching of memory details. |
| 308 | // Will run the provided task after finished. |
| 309 | class MetricsMemoryDetails : public MemoryDetails { |
| 310 | public: |
| 311 | explicit MetricsMemoryDetails(Task* completion) : completion_(completion) {} |
| 312 | |
| 313 | virtual void OnDetailsAvailable() { |
| 314 | MessageLoop::current()->PostTask(FROM_HERE, completion_); |
| 315 | } |
| 316 | |
| 317 | private: |
| 318 | ~MetricsMemoryDetails() {} |
| 319 | |
| 320 | Task* completion_; |
| 321 | DISALLOW_COPY_AND_ASSIGN(MetricsMemoryDetails); |
| 322 | }; |
| 323 | |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 324 | class MetricsService::InitTaskComplete : public Task { |
jam@chromium.org | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 325 | public: |
brettw@chromium.org | 191eb3f7 | 2010-12-21 06:27:50 | [diff] [blame] | 326 | explicit InitTaskComplete( |
| 327 | const std::string& hardware_class, |
cpu@chromium.org | 91d9f3d | 2011-08-14 05:24:44 | [diff] [blame] | 328 | const std::vector<webkit::WebPluginInfo>& plugins) |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 329 | : hardware_class_(hardware_class), plugins_(plugins) {} |
| 330 | |
jamesr@chromium.org | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 331 | virtual void Run() { |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 332 | g_browser_process->metrics_service()->OnInitTaskComplete( |
| 333 | hardware_class_, plugins_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | } |
jam@chromium.org | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 335 | |
jamesr@chromium.org | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 336 | private: |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 337 | std::string hardware_class_; |
cpu@chromium.org | 91d9f3d | 2011-08-14 05:24:44 | [diff] [blame] | 338 | std::vector<webkit::WebPluginInfo> plugins_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 339 | }; |
| 340 | |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 341 | class MetricsService::InitTask : public Task { |
jamesr@chromium.org | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 342 | public: |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 343 | explicit InitTask(MessageLoop* callback_loop) |
jamesr@chromium.org | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 344 | : callback_loop_(callback_loop) {} |
| 345 | |
| 346 | virtual void Run() { |
cpu@chromium.org | 91d9f3d | 2011-08-14 05:24:44 | [diff] [blame] | 347 | std::vector<webkit::WebPluginInfo> plugins; |
bauerb@chromium.org | 6859807 | 2011-07-29 08:21:28 | [diff] [blame] | 348 | webkit::npapi::PluginList::Singleton()->GetPlugins(&plugins); |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 349 | std::string hardware_class; // Empty string by default. |
| 350 | #if defined(OS_CHROMEOS) |
satorux@chromium.org | d43970a7 | 2011-07-10 06:24:52 | [diff] [blame] | 351 | chromeos::system::StatisticsProvider::GetInstance()->GetMachineStatistic( |
satorux@chromium.org | 9b34516 | 2011-04-15 05:21:30 | [diff] [blame] | 352 | "hardware_class", &hardware_class); |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 353 | #endif // OS_CHROMEOS |
| 354 | callback_loop_->PostTask(FROM_HERE, new InitTaskComplete( |
| 355 | hardware_class, plugins)); |
jamesr@chromium.org | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | private: |
| 359 | MessageLoop* callback_loop_; |
| 360 | }; |
evan@chromium.org | 90d4137 | 2009-11-30 21:52:32 | [diff] [blame] | 361 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 362 | // static |
| 363 | void MetricsService::RegisterPrefs(PrefService* local_state) { |
| 364 | DCHECK(IsSingleThreaded()); |
estade@chromium.org | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 365 | local_state->RegisterStringPref(prefs::kMetricsClientID, ""); |
robertshield@google.com | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 366 | local_state->RegisterInt64Pref(prefs::kMetricsClientIDTimestamp, 0); |
| 367 | local_state->RegisterInt64Pref(prefs::kStabilityLaunchTimeSec, 0); |
| 368 | local_state->RegisterInt64Pref(prefs::kStabilityLastTimestampSec, 0); |
estade@chromium.org | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 369 | local_state->RegisterStringPref(prefs::kStabilityStatsVersion, ""); |
jar@chromium.org | 225c5084 | 2010-01-19 21:19:13 | [diff] [blame] | 370 | local_state->RegisterInt64Pref(prefs::kStabilityStatsBuildTime, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 371 | local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true); |
| 372 | local_state->RegisterBooleanPref(prefs::kStabilitySessionEndCompleted, true); |
| 373 | local_state->RegisterIntegerPref(prefs::kMetricsSessionID, -1); |
| 374 | local_state->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0); |
| 375 | local_state->RegisterIntegerPref(prefs::kStabilityCrashCount, 0); |
| 376 | local_state->RegisterIntegerPref(prefs::kStabilityIncompleteSessionEndCount, |
| 377 | 0); |
| 378 | local_state->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 379 | local_state->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0); |
asargent@chromium.org | 1f08562 | 2009-12-04 05:33:45 | [diff] [blame] | 380 | local_state->RegisterIntegerPref(prefs::kStabilityExtensionRendererCrashCount, |
| 381 | 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 382 | local_state->RegisterIntegerPref(prefs::kStabilityRendererHangCount, 0); |
asargent@chromium.org | 1f08562 | 2009-12-04 05:33:45 | [diff] [blame] | 383 | local_state->RegisterIntegerPref(prefs::kStabilityChildProcessCrashCount, 0); |
cpu@google.com | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 384 | local_state->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationFail, |
| 385 | 0); |
| 386 | local_state->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationSuccess, |
| 387 | 0); |
| 388 | local_state->RegisterIntegerPref(prefs::kStabilityDebuggerPresent, 0); |
| 389 | local_state->RegisterIntegerPref(prefs::kStabilityDebuggerNotPresent, 0); |
petkov@chromium.org | c1834a9 | 2011-01-21 18:21:03 | [diff] [blame] | 390 | #if defined(OS_CHROMEOS) |
| 391 | local_state->RegisterIntegerPref(prefs::kStabilityOtherUserCrashCount, 0); |
| 392 | local_state->RegisterIntegerPref(prefs::kStabilityKernelCrashCount, 0); |
| 393 | local_state->RegisterIntegerPref(prefs::kStabilitySystemUncleanShutdownCount, |
| 394 | 0); |
| 395 | #endif // OS_CHROMEOS |
cpu@google.com | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 396 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 397 | local_state->RegisterDictionaryPref(prefs::kProfileMetrics); |
| 398 | local_state->RegisterIntegerPref(prefs::kNumBookmarksOnBookmarkBar, 0); |
| 399 | local_state->RegisterIntegerPref(prefs::kNumFoldersOnBookmarkBar, 0); |
| 400 | local_state->RegisterIntegerPref(prefs::kNumBookmarksInOtherBookmarkFolder, |
| 401 | 0); |
| 402 | local_state->RegisterIntegerPref(prefs::kNumFoldersInOtherBookmarkFolder, 0); |
| 403 | local_state->RegisterIntegerPref(prefs::kNumKeywords, 0); |
| 404 | local_state->RegisterListPref(prefs::kMetricsInitialLogs); |
| 405 | local_state->RegisterListPref(prefs::kMetricsOngoingLogs); |
robertshield@google.com | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 406 | |
| 407 | local_state->RegisterInt64Pref(prefs::kUninstallMetricsPageLoadCount, 0); |
| 408 | local_state->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); |
robertshield@google.com | 6b5f21d | 2009-04-13 17:01:35 | [diff] [blame] | 409 | local_state->RegisterInt64Pref(prefs::kUninstallMetricsInstallDate, 0); |
robertshield@google.com | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 410 | local_state->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); |
| 411 | local_state->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0); |
| 412 | local_state->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | } |
| 414 | |
jar@chromium.org | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 415 | // static |
| 416 | void MetricsService::DiscardOldStabilityStats(PrefService* local_state) { |
| 417 | local_state->SetBoolean(prefs::kStabilityExitedCleanly, true); |
jar@chromium.org | c9abf24 | 2009-07-18 06:00:38 | [diff] [blame] | 418 | local_state->SetBoolean(prefs::kStabilitySessionEndCompleted, true); |
jar@chromium.org | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 419 | |
| 420 | local_state->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0); |
| 421 | local_state->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0); |
| 422 | local_state->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0); |
| 423 | local_state->SetInteger(prefs::kStabilityDebuggerPresent, 0); |
| 424 | local_state->SetInteger(prefs::kStabilityDebuggerNotPresent, 0); |
| 425 | |
| 426 | local_state->SetInteger(prefs::kStabilityLaunchCount, 0); |
| 427 | local_state->SetInteger(prefs::kStabilityCrashCount, 0); |
| 428 | |
| 429 | local_state->SetInteger(prefs::kStabilityPageLoadCount, 0); |
| 430 | local_state->SetInteger(prefs::kStabilityRendererCrashCount, 0); |
| 431 | local_state->SetInteger(prefs::kStabilityRendererHangCount, 0); |
| 432 | |
jar@chromium.org | 9165f74 | 2010-03-10 22:55:01 | [diff] [blame] | 433 | local_state->SetInt64(prefs::kStabilityLaunchTimeSec, 0); |
| 434 | local_state->SetInt64(prefs::kStabilityLastTimestampSec, 0); |
jar@chromium.org | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 435 | |
| 436 | local_state->ClearPref(prefs::kStabilityPluginStats); |
jar@chromium.org | ae155cb9 | 2009-06-19 06:10:37 | [diff] [blame] | 437 | |
battre@chromium.org | f8628c2 | 2011-04-05 12:10:18 | [diff] [blame] | 438 | local_state->ClearPref(prefs::kMetricsInitialLogs); |
| 439 | local_state->ClearPref(prefs::kMetricsOngoingLogs); |
jar@chromium.org | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 440 | } |
| 441 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 442 | MetricsService::MetricsService() |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 443 | : recording_active_(false), |
| 444 | reporting_active_(false), |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 445 | state_(INITIALIZED), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 446 | current_fetch_(NULL), |
rtenneti@chromium.org | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 447 | io_thread_(NULL), |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 448 | idle_since_last_transmission_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 449 | next_window_id_(0), |
maruel@chromium.org | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 450 | ALLOW_THIS_IN_INITIALIZER_LIST(log_sender_factory_(this)), |
| 451 | ALLOW_THIS_IN_INITIALIZER_LIST(state_saver_factory_(this)), |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 452 | waiting_for_asynchronus_reporting_step_(false) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 453 | DCHECK(IsSingleThreaded()); |
| 454 | InitializeMetricsState(); |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 455 | |
| 456 | base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, |
| 457 | base::Unretained(this)); |
| 458 | scheduler_.reset(new MetricsReportingScheduler(callback)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | MetricsService::~MetricsService() { |
| 462 | SetRecording(false); |
| 463 | } |
| 464 | |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 465 | void MetricsService::Start() { |
stuartmorgan@chromium.org | b1c8dc0 | 2011-04-13 18:32:04 | [diff] [blame] | 466 | HandleIdleSinceLastTransmission(false); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 467 | SetRecording(true); |
| 468 | SetReporting(true); |
| 469 | } |
| 470 | |
| 471 | void MetricsService::StartRecordingOnly() { |
| 472 | SetRecording(true); |
| 473 | SetReporting(false); |
| 474 | } |
| 475 | |
| 476 | void MetricsService::Stop() { |
stuartmorgan@chromium.org | b1c8dc0 | 2011-04-13 18:32:04 | [diff] [blame] | 477 | HandleIdleSinceLastTransmission(false); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 478 | SetReporting(false); |
| 479 | SetRecording(false); |
| 480 | } |
| 481 | |
joi@chromium.org | edafd4c | 2011-05-10 17:18:53 | [diff] [blame] | 482 | std::string MetricsService::GetClientId() { |
| 483 | return client_id_; |
| 484 | } |
| 485 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 486 | void MetricsService::SetRecording(bool enabled) { |
| 487 | DCHECK(IsSingleThreaded()); |
| 488 | |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 489 | if (enabled == recording_active_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 490 | return; |
| 491 | |
| 492 | if (enabled) { |
jar@chromium.org | b0c819f | 2009-03-08 04:52:15 | [diff] [blame] | 493 | if (client_id_.empty()) { |
| 494 | PrefService* pref = g_browser_process->local_state(); |
| 495 | DCHECK(pref); |
estade@chromium.org | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 496 | client_id_ = pref->GetString(prefs::kMetricsClientID); |
jar@chromium.org | b0c819f | 2009-03-08 04:52:15 | [diff] [blame] | 497 | if (client_id_.empty()) { |
| 498 | client_id_ = GenerateClientID(); |
estade@chromium.org | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 499 | pref->SetString(prefs::kMetricsClientID, client_id_); |
jar@chromium.org | b0c819f | 2009-03-08 04:52:15 | [diff] [blame] | 500 | |
| 501 | // Might as well make a note of how long this ID has existed |
| 502 | pref->SetString(prefs::kMetricsClientIDTimestamp, |
brettw@chromium.org | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 503 | base::Int64ToString(Time::Now().ToTimeT())); |
jar@chromium.org | b0c819f | 2009-03-08 04:52:15 | [diff] [blame] | 504 | } |
| 505 | } |
kuchhal@chromium.org | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 506 | child_process_logging::SetClientId(client_id_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 507 | StartRecording(); |
pkasting@chromium.org | 005ef3e | 2009-05-22 20:55:46 | [diff] [blame] | 508 | |
stuartmorgan@chromium.org | 3ffd3ae | 2011-03-17 22:17:52 | [diff] [blame] | 509 | SetUpNotifications(®istrar_, this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 510 | } else { |
pkasting@chromium.org | 005ef3e | 2009-05-22 20:55:46 | [diff] [blame] | 511 | registrar_.RemoveAll(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 512 | PushPendingLogsToUnsentLists(); |
| 513 | DCHECK(!pending_log()); |
| 514 | if (state_ > INITIAL_LOG_READY && unsent_logs()) |
| 515 | state_ = SEND_OLD_INITIAL_LOGS; |
| 516 | } |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 517 | recording_active_ = enabled; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 518 | } |
| 519 | |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 520 | bool MetricsService::recording_active() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 521 | DCHECK(IsSingleThreaded()); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 522 | return recording_active_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 523 | } |
| 524 | |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 525 | void MetricsService::SetReporting(bool enable) { |
| 526 | if (reporting_active_ != enable) { |
| 527 | reporting_active_ = enable; |
| 528 | if (reporting_active_) |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 529 | StartSchedulerIfNecessary(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 530 | } |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | bool MetricsService::reporting_active() const { |
| 534 | DCHECK(IsSingleThreaded()); |
| 535 | return reporting_active_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 536 | } |
| 537 | |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 538 | // static |
stuartmorgan@chromium.org | 3ffd3ae | 2011-03-17 22:17:52 | [diff] [blame] | 539 | void MetricsService::SetUpNotifications(NotificationRegistrar* registrar, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 540 | NotificationObserver* observer) { |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 541 | registrar->Add(observer, chrome::NOTIFICATION_BROWSER_OPENED, |
erg@google.com | 04024e2c | 2011-08-12 19:32:10 | [diff] [blame] | 542 | NotificationService::AllBrowserContextsAndSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 543 | registrar->Add(observer, chrome::NOTIFICATION_BROWSER_CLOSED, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 544 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 545 | registrar->Add(observer, content::NOTIFICATION_USER_ACTION, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 546 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 547 | registrar->Add(observer, content::NOTIFICATION_TAB_PARENTED, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 548 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 549 | registrar->Add(observer, content::NOTIFICATION_TAB_CLOSING, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 550 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 551 | registrar->Add(observer, content::NOTIFICATION_LOAD_START, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 552 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 553 | registrar->Add(observer, content::NOTIFICATION_LOAD_STOP, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 554 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 555 | registrar->Add(observer, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 556 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 557 | registrar->Add(observer, content::NOTIFICATION_RENDERER_PROCESS_HANG, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 558 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 559 | registrar->Add(observer, content::NOTIFICATION_CHILD_PROCESS_HOST_CONNECTED, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 560 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 561 | registrar->Add(observer, content::NOTIFICATION_CHILD_INSTANCE_CREATED, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 562 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 563 | registrar->Add(observer, content::NOTIFICATION_CHILD_PROCESS_CRASHED, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 564 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 565 | registrar->Add(observer, chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 566 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 567 | registrar->Add(observer, chrome::NOTIFICATION_OMNIBOX_OPENED_URL, |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 568 | NotificationService::AllSources()); |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 569 | registrar->Add(observer, chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED, |
erg@chromium.org | 2f35a31 | 2011-08-09 20:21:29 | [diff] [blame] | 570 | NotificationService::AllBrowserContextsAndSources()); |
rtenneti@chromium.org | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 571 | } |
| 572 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 573 | void MetricsService::Observe(int type, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 574 | const NotificationSource& source, |
| 575 | const NotificationDetails& details) { |
| 576 | DCHECK(current_log_); |
| 577 | DCHECK(IsSingleThreaded()); |
| 578 | |
| 579 | if (!CanLogNotification(type, source, details)) |
| 580 | return; |
| 581 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 582 | switch (type) { |
| 583 | case content::NOTIFICATION_USER_ACTION: |
evan@chromium.org | afe3a167 | 2009-11-17 19:04:12 | [diff] [blame] | 584 | current_log_->RecordUserAction(*Details<const char*>(details).ptr()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 585 | break; |
| 586 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 587 | case chrome::NOTIFICATION_BROWSER_OPENED: |
| 588 | case chrome::NOTIFICATION_BROWSER_CLOSED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 589 | LogWindowChange(type, source, details); |
| 590 | break; |
| 591 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 592 | case content::NOTIFICATION_TAB_PARENTED: |
| 593 | case content::NOTIFICATION_TAB_CLOSING: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 594 | LogWindowChange(type, source, details); |
| 595 | break; |
| 596 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 597 | case content::NOTIFICATION_LOAD_STOP: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 598 | LogLoadComplete(type, source, details); |
| 599 | break; |
| 600 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 601 | case content::NOTIFICATION_LOAD_START: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 602 | LogLoadStarted(); |
| 603 | break; |
| 604 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 605 | case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
kkania@chromium.org | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 606 | RenderProcessHost::RendererClosedDetails* process_details = |
| 607 | Details<RenderProcessHost::RendererClosedDetails>(details).ptr(); |
gspencer@chromium.org | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 608 | if (process_details->status == |
| 609 | base::TERMINATION_STATUS_PROCESS_CRASHED || |
| 610 | process_details->status == |
| 611 | base::TERMINATION_STATUS_ABNORMAL_TERMINATION) { |
kkania@chromium.org | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 612 | if (process_details->was_extension_renderer) { |
| 613 | LogExtensionRendererCrash(); |
| 614 | } else { |
| 615 | LogRendererCrash(); |
| 616 | } |
| 617 | } |
asargent@chromium.org | 1f08562 | 2009-12-04 05:33:45 | [diff] [blame] | 618 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 619 | break; |
| 620 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 621 | case content::NOTIFICATION_RENDERER_PROCESS_HANG: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 622 | LogRendererHang(); |
| 623 | break; |
| 624 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 625 | case content::NOTIFICATION_CHILD_PROCESS_HOST_CONNECTED: |
| 626 | case content::NOTIFICATION_CHILD_PROCESS_CRASHED: |
| 627 | case content::NOTIFICATION_CHILD_INSTANCE_CREATED: |
jam@chromium.org | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 628 | LogChildProcessChange(type, source, details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 629 | break; |
| 630 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 631 | case chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED: |
erg@google.com | 8e5c89a | 2011-06-07 18:13:33 | [diff] [blame] | 632 | LogKeywords(Source<TemplateURLService>(source).ptr()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 633 | break; |
| 634 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 635 | case chrome::NOTIFICATION_OMNIBOX_OPENED_URL: { |
ananta@chromium.org | 1226abb | 2010-06-10 18:01:28 | [diff] [blame] | 636 | MetricsLog* current_log = current_log_->AsMetricsLog(); |
| 637 | DCHECK(current_log); |
| 638 | current_log->RecordOmniboxOpenedURL( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 639 | *Details<AutocompleteLog>(details).ptr()); |
| 640 | break; |
ananta@chromium.org | 1226abb | 2010-06-10 18:01:28 | [diff] [blame] | 641 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 642 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 643 | case chrome::NOTIFICATION_BOOKMARK_MODEL_LOADED: { |
tim@chromium.org | b61236c6 | 2009-04-09 22:43:55 | [diff] [blame] | 644 | Profile* p = Source<Profile>(source).ptr(); |
| 645 | if (p) |
| 646 | LogBookmarks(p->GetBookmarkModel()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 647 | break; |
tim@chromium.org | b61236c6 | 2009-04-09 22:43:55 | [diff] [blame] | 648 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 649 | default: |
jar@chromium.org | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 650 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 651 | break; |
| 652 | } |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 653 | |
| 654 | HandleIdleSinceLastTransmission(false); |
| 655 | |
| 656 | if (current_log_) |
pkasting@chromium.org | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 657 | DVLOG(1) << "METRICS: NUMBER OF EVENTS = " << current_log_->num_events(); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | void MetricsService::HandleIdleSinceLastTransmission(bool in_idle) { |
| 661 | // If there wasn't a lot of action, maybe the computer was asleep, in which |
| 662 | // case, the log transmissions should have stopped. Here we start them up |
| 663 | // again. |
pkasting@chromium.org | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 664 | if (!in_idle && idle_since_last_transmission_) |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 665 | StartSchedulerIfNecessary(); |
pkasting@chromium.org | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 666 | idle_since_last_transmission_ = in_idle; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 667 | } |
| 668 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 669 | void MetricsService::RecordStartOfSessionEnd() { |
stuartmorgan@chromium.org | 466f3c1 | 2011-03-23 21:20:38 | [diff] [blame] | 670 | LogCleanShutdown(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 671 | RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, false); |
| 672 | } |
| 673 | |
| 674 | void MetricsService::RecordCompletedSessionEnd() { |
stuartmorgan@chromium.org | 466f3c1 | 2011-03-23 21:20:38 | [diff] [blame] | 675 | LogCleanShutdown(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 676 | RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, true); |
| 677 | } |
| 678 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 679 | void MetricsService::RecordBreakpadRegistration(bool success) { |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 680 | if (!success) |
cpu@google.com | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 681 | IncrementPrefValue(prefs::kStabilityBreakpadRegistrationFail); |
| 682 | else |
| 683 | IncrementPrefValue(prefs::kStabilityBreakpadRegistrationSuccess); |
| 684 | } |
| 685 | |
| 686 | void MetricsService::RecordBreakpadHasDebugger(bool has_debugger) { |
| 687 | if (!has_debugger) |
| 688 | IncrementPrefValue(prefs::kStabilityDebuggerNotPresent); |
| 689 | else |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 690 | IncrementPrefValue(prefs::kStabilityDebuggerPresent); |
cpu@google.com | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 691 | } |
| 692 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 693 | //------------------------------------------------------------------------------ |
| 694 | // private methods |
| 695 | //------------------------------------------------------------------------------ |
| 696 | |
| 697 | |
| 698 | //------------------------------------------------------------------------------ |
| 699 | // Initialization methods |
| 700 | |
| 701 | void MetricsService::InitializeMetricsState() { |
kuchhal@chromium.org | 79bf0b7 | 2009-04-27 21:30:55 | [diff] [blame] | 702 | #if defined(OS_POSIX) |
| 703 | server_url_ = L"https://clients4.google.com/firefox/metrics/collect"; |
rtenneti@chromium.org | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 704 | // TODO(rtenneti): Return the network stats server name. |
| 705 | network_stats_server_ = ""; |
kuchhal@chromium.org | 79bf0b7 | 2009-04-27 21:30:55 | [diff] [blame] | 706 | #else |
| 707 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 708 | server_url_ = dist->GetStatsServerURL(); |
rtenneti@chromium.org | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 709 | network_stats_server_ = dist->GetNetworkStatsServer(); |
kuchhal@chromium.org | 79bf0b7 | 2009-04-27 21:30:55 | [diff] [blame] | 710 | #endif |
| 711 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 712 | PrefService* pref = g_browser_process->local_state(); |
| 713 | DCHECK(pref); |
| 714 | |
jar@chromium.org | 225c5084 | 2010-01-19 21:19:13 | [diff] [blame] | 715 | if ((pref->GetInt64(prefs::kStabilityStatsBuildTime) |
| 716 | != MetricsLog::GetBuildTime()) || |
estade@chromium.org | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 717 | (pref->GetString(prefs::kStabilityStatsVersion) |
jar@chromium.org | 225c5084 | 2010-01-19 21:19:13 | [diff] [blame] | 718 | != MetricsLog::GetVersionString())) { |
jar@chromium.org | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 719 | // This is a new version, so we don't want to confuse the stats about the |
| 720 | // old version with info that we upload. |
| 721 | DiscardOldStabilityStats(pref); |
| 722 | pref->SetString(prefs::kStabilityStatsVersion, |
estade@chromium.org | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 723 | MetricsLog::GetVersionString()); |
jar@chromium.org | 225c5084 | 2010-01-19 21:19:13 | [diff] [blame] | 724 | pref->SetInt64(prefs::kStabilityStatsBuildTime, |
| 725 | MetricsLog::GetBuildTime()); |
jar@chromium.org | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 726 | } |
| 727 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 728 | // Update session ID |
| 729 | session_id_ = pref->GetInteger(prefs::kMetricsSessionID); |
| 730 | ++session_id_; |
| 731 | pref->SetInteger(prefs::kMetricsSessionID, session_id_); |
| 732 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 733 | // Stability bookkeeping |
cpu@google.com | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 734 | IncrementPrefValue(prefs::kStabilityLaunchCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 735 | |
cpu@google.com | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 736 | if (!pref->GetBoolean(prefs::kStabilityExitedCleanly)) { |
| 737 | IncrementPrefValue(prefs::kStabilityCrashCount); |
jar@chromium.org | c0c55e9 | 2011-09-10 18:47:30 | [diff] [blame^] | 738 | // Reset flag, and wait until we call LogNeedForCleanShutdown() before |
| 739 | // monitoring. |
| 740 | pref->SetBoolean(prefs::kStabilityExitedCleanly, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 741 | } |
cpu@google.com | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 742 | |
cpu@google.com | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 743 | if (!pref->GetBoolean(prefs::kStabilitySessionEndCompleted)) { |
| 744 | IncrementPrefValue(prefs::kStabilityIncompleteSessionEndCount); |
jar@chromium.org | c9abf24 | 2009-07-18 06:00:38 | [diff] [blame] | 745 | // This is marked false when we get a WM_ENDSESSION. |
| 746 | pref->SetBoolean(prefs::kStabilitySessionEndCompleted, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 747 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 748 | |
jar@chromium.org | 9165f74 | 2010-03-10 22:55:01 | [diff] [blame] | 749 | // Initialize uptime counters. |
| 750 | int64 startup_uptime = MetricsLog::GetIncrementalUptime(pref); |
mad@google.com | ae393ec70 | 2010-06-27 16:23:14 | [diff] [blame] | 751 | DCHECK_EQ(0, startup_uptime); |
jar@chromium.org | 9165f74 | 2010-03-10 22:55:01 | [diff] [blame] | 752 | // For backwards compatibility, leave this intact in case Omaha is checking |
| 753 | // them. prefs::kStabilityLastTimestampSec may also be useless now. |
| 754 | // TODO(jar): Delete these if they have no uses. |
robertshield@google.com | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 755 | pref->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); |
| 756 | |
| 757 | // Bookkeeping for the uninstall metrics. |
| 758 | IncrementLongPrefsValue(prefs::kUninstallLaunchCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 759 | |
| 760 | // Save profile metrics. |
| 761 | PrefService* prefs = g_browser_process->local_state(); |
| 762 | if (prefs) { |
| 763 | // Remove the current dictionary and store it for use when sending data to |
| 764 | // server. By removing the value we prune potentially dead profiles |
| 765 | // (and keys). All valid values are added back once services startup. |
| 766 | const DictionaryValue* profile_dictionary = |
| 767 | prefs->GetDictionary(prefs::kProfileMetrics); |
| 768 | if (profile_dictionary) { |
| 769 | // Do a deep copy of profile_dictionary since ClearPref will delete it. |
| 770 | profile_dictionary_.reset(static_cast<DictionaryValue*>( |
| 771 | profile_dictionary->DeepCopy())); |
| 772 | prefs->ClearPref(prefs::kProfileMetrics); |
| 773 | } |
| 774 | } |
| 775 | |
jar@chromium.org | 9274524 | 2009-06-12 16:52:21 | [diff] [blame] | 776 | // Get stats on use of command line. |
| 777 | const CommandLine* command_line(CommandLine::ForCurrentProcess()); |
| 778 | size_t common_commands = 0; |
| 779 | if (command_line->HasSwitch(switches::kUserDataDir)) { |
| 780 | ++common_commands; |
| 781 | UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineDatDirCount", 1); |
| 782 | } |
| 783 | |
| 784 | if (command_line->HasSwitch(switches::kApp)) { |
| 785 | ++common_commands; |
| 786 | UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineAppModeCount", 1); |
| 787 | } |
| 788 | |
msw@chromium.org | 62b4e52 | 2011-07-13 21:46:32 | [diff] [blame] | 789 | size_t switch_count = command_line->GetSwitches().size(); |
| 790 | UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineFlagCount", switch_count); |
jar@chromium.org | 9274524 | 2009-06-12 16:52:21 | [diff] [blame] | 791 | UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineUncommonFlagCount", |
msw@chromium.org | 62b4e52 | 2011-07-13 21:46:32 | [diff] [blame] | 792 | switch_count - common_commands); |
jar@chromium.org | 9274524 | 2009-06-12 16:52:21 | [diff] [blame] | 793 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 794 | // Kick off the process of saving the state (so the uptime numbers keep |
| 795 | // getting updated) every n minutes. |
| 796 | ScheduleNextStateSave(); |
| 797 | } |
| 798 | |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 799 | void MetricsService::OnInitTaskComplete( |
| 800 | const std::string& hardware_class, |
cpu@chromium.org | 91d9f3d | 2011-08-14 05:24:44 | [diff] [blame] | 801 | const std::vector<webkit::WebPluginInfo>& plugins) { |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 802 | DCHECK(state_ == INIT_TASK_SCHEDULED); |
| 803 | hardware_class_ = hardware_class; |
jam@chromium.org | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 804 | plugins_ = plugins; |
rtenneti@chromium.org | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 805 | io_thread_ = g_browser_process->io_thread(); |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 806 | if (state_ == INIT_TASK_SCHEDULED) |
| 807 | state_ = INIT_TASK_DONE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | std::string MetricsService::GenerateClientID() { |
dhollowa@chromium.org | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 811 | return guid::GenerateGUID(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 812 | } |
| 813 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 814 | //------------------------------------------------------------------------------ |
| 815 | // State save methods |
| 816 | |
| 817 | void MetricsService::ScheduleNextStateSave() { |
| 818 | state_saver_factory_.RevokeAll(); |
| 819 | |
| 820 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 821 | state_saver_factory_.NewRunnableMethod(&MetricsService::SaveLocalState), |
| 822 | kSaveStateInterval * 1000); |
| 823 | } |
| 824 | |
| 825 | void MetricsService::SaveLocalState() { |
| 826 | PrefService* pref = g_browser_process->local_state(); |
| 827 | if (!pref) { |
jar@chromium.org | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 828 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 829 | return; |
| 830 | } |
| 831 | |
| 832 | RecordCurrentState(pref); |
phajdan.jr@chromium.org | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 833 | pref->ScheduleSavePersistentPrefs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 834 | |
jar@chromium.org | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 835 | // TODO(jar): Does this run down the batteries???? |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 836 | ScheduleNextStateSave(); |
| 837 | } |
| 838 | |
| 839 | |
| 840 | //------------------------------------------------------------------------------ |
| 841 | // Recording control methods |
| 842 | |
| 843 | void MetricsService::StartRecording() { |
| 844 | if (current_log_) |
| 845 | return; |
| 846 | |
| 847 | current_log_ = new MetricsLog(client_id_, session_id_); |
| 848 | if (state_ == INITIALIZED) { |
| 849 | // We only need to schedule that run once. |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 850 | state_ = INIT_TASK_SCHEDULED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 851 | |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 852 | // Schedules a task on the file thread for execution of slower |
| 853 | // initialization steps (such as plugin list generation) necessary |
| 854 | // for sending the initial log. This avoids blocking the main UI |
| 855 | // thread. |
jamesr@chromium.org | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 856 | g_browser_process->file_thread()->message_loop()->PostDelayedTask(FROM_HERE, |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 857 | new InitTask(MessageLoop::current()), |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 858 | kInitializationDelaySeconds * 1000); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
stuartmorgan@chromium.org | 024b5cd | 2011-05-27 03:29:38 | [diff] [blame] | 862 | void MetricsService::StopRecording() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 863 | if (!current_log_) |
| 864 | return; |
| 865 | |
bryner@chromium.org | 49cd7e88 | 2011-02-01 03:54:36 | [diff] [blame] | 866 | current_log_->set_hardware_class(hardware_class_); // Adds to ongoing logs. |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 867 | |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 868 | // TODO(jar): Integrate bounds on log recording more consistently, so that we |
| 869 | // can stop recording logs that are too big much sooner. |
stuartmorgan@chromium.org | 54702c9 | 2011-04-15 15:06:43 | [diff] [blame] | 870 | if (current_log_->num_events() > kEventLimit) { |
dsh@google.com | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 871 | UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 872 | current_log_->num_events()); |
| 873 | current_log_->CloseLog(); |
| 874 | delete current_log_; |
jar@google.com | 29463878 | 2008-09-24 00:22:41 | [diff] [blame] | 875 | current_log_ = NULL; |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 876 | StartRecording(); // Start trivial log to hold our histograms. |
| 877 | } |
| 878 | |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 879 | // Put incremental data (histogram deltas, and realtime stats deltas) at the |
jar@google.com | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 880 | // end of all log transmissions (initial log handles this separately). |
stuartmorgan@chromium.org | 024b5cd | 2011-05-27 03:29:38 | [diff] [blame] | 881 | // RecordIncrementalStabilityElements only exists on the derived |
| 882 | // MetricsLog class. |
| 883 | MetricsLog* current_log = current_log_->AsMetricsLog(); |
| 884 | DCHECK(current_log); |
| 885 | current_log->RecordIncrementalStabilityElements(); |
| 886 | RecordCurrentHistograms(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 887 | |
| 888 | current_log_->CloseLog(); |
stuartmorgan@chromium.org | 024b5cd | 2011-05-27 03:29:38 | [diff] [blame] | 889 | pending_log_ = current_log_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 890 | current_log_ = NULL; |
| 891 | } |
| 892 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 893 | void MetricsService::PushPendingLogsToUnsentLists() { |
| 894 | if (state_ < INITIAL_LOG_READY) |
avi@google.com | 28ab7f9 | 2009-01-06 21:39:04 | [diff] [blame] | 895 | return; // We didn't and still don't have time to get plugin list etc. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 896 | |
| 897 | if (pending_log()) { |
| 898 | PreparePendingLogText(); |
| 899 | if (state_ == INITIAL_LOG_READY) { |
| 900 | // We may race here, and send second copy of initial log later. |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 901 | unsent_initial_logs_.push_back(compressed_log_); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 902 | state_ = SEND_OLD_INITIAL_LOGS; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 903 | } else { |
jar@chromium.org | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 904 | // TODO(jar): Verify correctness in other states, including sending unsent |
jar@chromium.org | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 905 | // initial logs. |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 906 | PushPendingLogTextToUnsentOngoingLogs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 907 | } |
| 908 | DiscardPendingLog(); |
| 909 | } |
| 910 | DCHECK(!pending_log()); |
stuartmorgan@chromium.org | 024b5cd | 2011-05-27 03:29:38 | [diff] [blame] | 911 | StopRecording(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 912 | PreparePendingLogText(); |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 913 | PushPendingLogTextToUnsentOngoingLogs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 914 | DiscardPendingLog(); |
| 915 | StoreUnsentLogs(); |
| 916 | } |
| 917 | |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 918 | void MetricsService::PushPendingLogTextToUnsentOngoingLogs() { |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 919 | if (compressed_log_.length() > |
paul@chromium.org | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 920 | static_cast<size_t>(kUploadLogAvoidRetransmitSize)) { |
dsh@google.com | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 921 | UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 922 | static_cast<int>(compressed_log_.length())); |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 923 | return; |
| 924 | } |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 925 | unsent_ongoing_logs_.push_back(compressed_log_); |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 926 | } |
| 927 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 928 | //------------------------------------------------------------------------------ |
| 929 | // Transmission of logs methods |
| 930 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 931 | void MetricsService::StartSchedulerIfNecessary() { |
| 932 | if (reporting_active() && recording_active()) |
| 933 | scheduler_->Start(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 934 | } |
| 935 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 936 | void MetricsService::StartScheduledUpload() { |
| 937 | // If reporting has been turned off, the scheduler doesn't need to run. |
| 938 | if (!reporting_active() || !recording_active()) { |
| 939 | scheduler_->Stop(); |
| 940 | scheduler_->UploadCancelled(); |
| 941 | return; |
| 942 | } |
| 943 | |
| 944 | DCHECK(!waiting_for_asynchronus_reporting_step_); |
| 945 | waiting_for_asynchronus_reporting_step_ = true; |
| 946 | |
sail@chromium.org | 84c988a | 2011-04-19 17:56:33 | [diff] [blame] | 947 | Task* task = log_sender_factory_. |
| 948 | NewRunnableMethod(&MetricsService::OnMemoryDetailCollectionDone); |
| 949 | |
| 950 | scoped_refptr<MetricsMemoryDetails> details(new MetricsMemoryDetails(task)); |
| 951 | details->StartFetch(); |
| 952 | |
| 953 | // Collect WebCore cache information to put into a histogram. |
| 954 | for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 955 | !i.IsAtEnd(); i.Advance()) |
ananta@chromium.org | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 956 | i.GetCurrentValue()->Send(new ChromeViewMsg_GetCacheResourceStats()); |
sail@chromium.org | 84c988a | 2011-04-19 17:56:33 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | void MetricsService::OnMemoryDetailCollectionDone() { |
jar@chromium.org | c9a3ef8 | 2009-05-28 22:02:46 | [diff] [blame] | 960 | DCHECK(IsSingleThreaded()); |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 961 | // This function should only be called as the callback from an ansynchronous |
| 962 | // step. |
| 963 | DCHECK(waiting_for_asynchronus_reporting_step_); |
jar@chromium.org | c9a3ef8 | 2009-05-28 22:02:46 | [diff] [blame] | 964 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 965 | // Right before the UMA transmission gets started, there's one more thing we'd |
| 966 | // like to record: the histogram of memory usage, so we spawn a task to |
| 967 | // collect the memory details and when that task is finished, it will call |
| 968 | // OnMemoryDetailCollectionDone, which will call HistogramSynchronization to |
| 969 | // collect histograms from all renderers and then we will call |
| 970 | // OnHistogramSynchronizationDone to continue processing. |
jar@chromium.org | c9a3ef8 | 2009-05-28 22:02:46 | [diff] [blame] | 971 | |
| 972 | // Create a callback_task for OnHistogramSynchronizationDone. |
| 973 | Task* callback_task = log_sender_factory_.NewRunnableMethod( |
| 974 | &MetricsService::OnHistogramSynchronizationDone); |
| 975 | |
| 976 | // Set up the callback to task to call after we receive histograms from all |
| 977 | // renderer processes. Wait time specifies how long to wait before absolutely |
| 978 | // calling us back on the task. |
| 979 | HistogramSynchronizer::FetchRendererHistogramsAsynchronously( |
| 980 | MessageLoop::current(), callback_task, |
| 981 | kMaxHistogramGatheringWaitDuration); |
| 982 | } |
| 983 | |
| 984 | void MetricsService::OnHistogramSynchronizationDone() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 985 | DCHECK(IsSingleThreaded()); |
| 986 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 987 | // If somehow there is a fetch in progress, we return and hope things work |
| 988 | // out. The scheduler isn't informed since if this happens, the scheduler |
| 989 | // will get a response from the upload. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 990 | DCHECK(!current_fetch_.get()); |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 991 | if (current_fetch_.get()) |
| 992 | return; |
| 993 | |
| 994 | // This function should only be called as the callback from an ansynchronous |
| 995 | // step. |
| 996 | DCHECK(waiting_for_asynchronus_reporting_step_); |
| 997 | waiting_for_asynchronus_reporting_step_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 998 | |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 999 | // If we're getting no notifications, then the log won't have much in it, and |
| 1000 | // it's possible the computer is about to go to sleep, so don't upload and |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1001 | // stop the scheduler. |
| 1002 | // Similarly, if logs should no longer be uploaded, stop here. |
| 1003 | if (idle_since_last_transmission_ || |
| 1004 | !recording_active() || !reporting_active()) { |
| 1005 | scheduler_->Stop(); |
| 1006 | scheduler_->UploadCancelled(); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1007 | return; |
| 1008 | } |
| 1009 | |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1010 | MakePendingLog(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1011 | |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1012 | // MakePendingLog should have put something in the pending log, if it didn't, |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1013 | // we skip this upload and hope things work out next time. |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1014 | if (!pending_log()) { |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1015 | scheduler_->UploadCancelled(); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1016 | return; |
| 1017 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1018 | |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1019 | PrepareFetchWithPendingLog(); |
| 1020 | |
| 1021 | if (!current_fetch_.get()) { |
| 1022 | // Compression failed, and log discarded :-/. |
| 1023 | DiscardPendingLog(); |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1024 | scheduler_->UploadCancelled(); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1025 | // TODO(jar): If compression failed, we should have created a tiny log and |
| 1026 | // compressed that, so that we can signal that we're losing logs. |
| 1027 | return; |
| 1028 | } |
| 1029 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1030 | DCHECK(!waiting_for_asynchronus_reporting_step_); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1031 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1032 | waiting_for_asynchronus_reporting_step_ = true; |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1033 | current_fetch_->Start(); |
| 1034 | |
| 1035 | HandleIdleSinceLastTransmission(true); |
| 1036 | } |
| 1037 | |
| 1038 | |
| 1039 | void MetricsService::MakePendingLog() { |
| 1040 | if (pending_log()) |
| 1041 | return; |
| 1042 | |
| 1043 | switch (state_) { |
| 1044 | case INITIALIZED: |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 1045 | case INIT_TASK_SCHEDULED: // We should be further along by now. |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1046 | DCHECK(false); |
| 1047 | return; |
| 1048 | |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 1049 | case INIT_TASK_DONE: |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1050 | // We need to wait for the initial log to be ready before sending |
| 1051 | // anything, because the server will tell us whether it wants to hear |
| 1052 | // from us. |
| 1053 | PrepareInitialLog(); |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 1054 | DCHECK(state_ == INIT_TASK_DONE); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1055 | RecallUnsentLogs(); |
| 1056 | state_ = INITIAL_LOG_READY; |
| 1057 | break; |
| 1058 | |
| 1059 | case SEND_OLD_INITIAL_LOGS: |
pkasting@chromium.org | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1060 | if (!unsent_initial_logs_.empty()) { |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1061 | compressed_log_ = unsent_initial_logs_.back(); |
stuartmorgan@chromium.org | d53e223 | 2011-06-30 15:54:57 | [diff] [blame] | 1062 | unsent_initial_logs_.pop_back(); |
pkasting@chromium.org | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1063 | break; |
| 1064 | } |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1065 | state_ = SENDING_OLD_LOGS; |
| 1066 | // Fall through. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1067 | |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1068 | case SENDING_OLD_LOGS: |
| 1069 | if (!unsent_ongoing_logs_.empty()) { |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1070 | compressed_log_ = unsent_ongoing_logs_.back(); |
stuartmorgan@chromium.org | d53e223 | 2011-06-30 15:54:57 | [diff] [blame] | 1071 | unsent_ongoing_logs_.pop_back(); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1072 | break; |
| 1073 | } |
| 1074 | state_ = SENDING_CURRENT_LOGS; |
| 1075 | // Fall through. |
| 1076 | |
| 1077 | case SENDING_CURRENT_LOGS: |
stuartmorgan@chromium.org | 024b5cd | 2011-05-27 03:29:38 | [diff] [blame] | 1078 | StopRecording(); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1079 | StartRecording(); |
| 1080 | break; |
| 1081 | |
| 1082 | default: |
jar@chromium.org | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1083 | NOTREACHED(); |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1084 | return; |
| 1085 | } |
| 1086 | |
| 1087 | DCHECK(pending_log()); |
| 1088 | } |
| 1089 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1090 | void MetricsService::PrepareInitialLog() { |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 1091 | DCHECK(state_ == INIT_TASK_DONE); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1092 | |
| 1093 | MetricsLog* log = new MetricsLog(client_id_, session_id_); |
zelidrag@chromium.org | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 1094 | log->set_hardware_class(hardware_class_); // Adds to initial log. |
jam@chromium.org | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 1095 | log->RecordEnvironment(plugins_, profile_dictionary_.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1096 | |
| 1097 | // Histograms only get written to current_log_, so setup for the write. |
ananta@chromium.org | 1226abb | 2010-06-10 18:01:28 | [diff] [blame] | 1098 | MetricsLogBase* save_log = current_log_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1099 | current_log_ = log; |
| 1100 | RecordCurrentHistograms(); // Into current_log_... which is really log. |
| 1101 | current_log_ = save_log; |
| 1102 | |
| 1103 | log->CloseLog(); |
| 1104 | DCHECK(!pending_log()); |
| 1105 | pending_log_ = log; |
| 1106 | } |
| 1107 | |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1108 | // static |
| 1109 | MetricsService::LogRecallStatus MetricsService::RecallUnsentLogsHelper( |
| 1110 | const ListValue& list, |
| 1111 | std::vector<std::string>* local_list) { |
| 1112 | DCHECK(local_list->empty()); |
| 1113 | if (list.GetSize() == 0) |
| 1114 | return MakeRecallStatusHistogram(LIST_EMPTY); |
| 1115 | if (list.GetSize() < 3) |
| 1116 | return MakeRecallStatusHistogram(LIST_SIZE_TOO_SMALL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1117 | |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1118 | // The size is stored at the beginning of the list. |
| 1119 | int size; |
| 1120 | bool valid = (*list.begin())->GetAsInteger(&size); |
| 1121 | if (!valid) |
| 1122 | return MakeRecallStatusHistogram(LIST_SIZE_MISSING); |
| 1123 | |
| 1124 | // Account for checksum and size included in the list. |
| 1125 | if (static_cast<unsigned int>(size) != |
| 1126 | list.GetSize() - kChecksumEntryCount) |
| 1127 | return MakeRecallStatusHistogram(LIST_SIZE_CORRUPTION); |
| 1128 | |
tfarina@chromium.org | 0ad1d2d6 | 2011-07-18 16:54:58 | [diff] [blame] | 1129 | base::MD5Context ctx; |
| 1130 | base::MD5Init(&ctx); |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1131 | std::string encoded_log; |
| 1132 | std::string decoded_log; |
| 1133 | for (ListValue::const_iterator it = list.begin() + 1; |
| 1134 | it != list.end() - 1; ++it) { // Last element is the checksum. |
| 1135 | valid = (*it)->GetAsString(&encoded_log); |
| 1136 | if (!valid) { |
| 1137 | local_list->clear(); |
| 1138 | return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION); |
| 1139 | } |
| 1140 | |
dominich@chromium.org | a3bcff6 | 2011-07-26 23:39:50 | [diff] [blame] | 1141 | base::MD5Update(&ctx, encoded_log); |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1142 | |
| 1143 | if (!base::Base64Decode(encoded_log, &decoded_log)) { |
| 1144 | local_list->clear(); |
| 1145 | return MakeRecallStatusHistogram(DECODE_FAIL); |
| 1146 | } |
| 1147 | local_list->push_back(decoded_log); |
| 1148 | } |
| 1149 | |
| 1150 | // Verify checksum. |
tfarina@chromium.org | 0ad1d2d6 | 2011-07-18 16:54:58 | [diff] [blame] | 1151 | base::MD5Digest digest; |
| 1152 | base::MD5Final(&digest, &ctx); |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1153 | std::string recovered_md5; |
| 1154 | // We store the hash at the end of the list. |
| 1155 | valid = (*(list.end() - 1))->GetAsString(&recovered_md5); |
| 1156 | if (!valid) { |
| 1157 | local_list->clear(); |
| 1158 | return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION); |
| 1159 | } |
tfarina@chromium.org | 0ad1d2d6 | 2011-07-18 16:54:58 | [diff] [blame] | 1160 | if (recovered_md5 != base::MD5DigestToBase16(digest)) { |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1161 | local_list->clear(); |
| 1162 | return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION); |
| 1163 | } |
| 1164 | return MakeRecallStatusHistogram(RECALL_SUCCESS); |
| 1165 | } |
| 1166 | void MetricsService::RecallUnsentLogs() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1167 | PrefService* local_state = g_browser_process->local_state(); |
| 1168 | DCHECK(local_state); |
| 1169 | |
battre@chromium.org | f8628c2 | 2011-04-05 12:10:18 | [diff] [blame] | 1170 | const ListValue* unsent_initial_logs = local_state->GetList( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1171 | prefs::kMetricsInitialLogs); |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1172 | RecallUnsentLogsHelper(*unsent_initial_logs, &unsent_initial_logs_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1173 | |
battre@chromium.org | f8628c2 | 2011-04-05 12:10:18 | [diff] [blame] | 1174 | const ListValue* unsent_ongoing_logs = local_state->GetList( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1175 | prefs::kMetricsOngoingLogs); |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1176 | RecallUnsentLogsHelper(*unsent_ongoing_logs, &unsent_ongoing_logs_); |
| 1177 | } |
| 1178 | |
| 1179 | // static |
| 1180 | void MetricsService::StoreUnsentLogsHelper( |
| 1181 | const std::vector<std::string>& local_list, |
| 1182 | const size_t kMaxLocalListSize, |
| 1183 | ListValue* list) { |
| 1184 | list->Clear(); |
| 1185 | size_t start = 0; |
| 1186 | if (local_list.size() > kMaxLocalListSize) |
| 1187 | start = local_list.size() - kMaxLocalListSize; |
| 1188 | DCHECK(start <= local_list.size()); |
| 1189 | if (local_list.size() == start) |
| 1190 | return; |
| 1191 | |
| 1192 | // Store size at the beginning of the list. |
| 1193 | list->Append(Value::CreateIntegerValue(local_list.size() - start)); |
| 1194 | |
tfarina@chromium.org | 0ad1d2d6 | 2011-07-18 16:54:58 | [diff] [blame] | 1195 | base::MD5Context ctx; |
| 1196 | base::MD5Init(&ctx); |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1197 | std::string encoded_log; |
| 1198 | for (std::vector<std::string>::const_iterator it = local_list.begin() + start; |
| 1199 | it != local_list.end(); ++it) { |
| 1200 | // We encode the compressed log as Value::CreateStringValue() expects to |
| 1201 | // take a valid UTF8 string. |
| 1202 | if (!base::Base64Encode(*it, &encoded_log)) { |
| 1203 | MakeStoreStatusHistogram(ENCODE_FAIL); |
| 1204 | list->Clear(); |
| 1205 | return; |
| 1206 | } |
dominich@chromium.org | a3bcff6 | 2011-07-26 23:39:50 | [diff] [blame] | 1207 | base::MD5Update(&ctx, encoded_log); |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1208 | list->Append(Value::CreateStringValue(encoded_log)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1209 | } |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1210 | |
| 1211 | // Append hash to the end of the list. |
tfarina@chromium.org | 0ad1d2d6 | 2011-07-18 16:54:58 | [diff] [blame] | 1212 | base::MD5Digest digest; |
| 1213 | base::MD5Final(&digest, &ctx); |
| 1214 | list->Append(Value::CreateStringValue(base::MD5DigestToBase16(digest))); |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1215 | DCHECK(list->GetSize() >= 3); // Minimum of 3 elements (size, data, hash). |
ziadh@chromium.org | 4e95d20 | 2010-07-24 01:47:56 | [diff] [blame] | 1216 | MakeStoreStatusHistogram(STORE_SUCCESS); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | void MetricsService::StoreUnsentLogs() { |
| 1220 | if (state_ < INITIAL_LOG_READY) |
| 1221 | return; // We never Recalled the prior unsent logs. |
| 1222 | |
| 1223 | PrefService* local_state = g_browser_process->local_state(); |
| 1224 | DCHECK(local_state); |
| 1225 | |
battre@chromium.org | f8628c2 | 2011-04-05 12:10:18 | [diff] [blame] | 1226 | { |
| 1227 | ListPrefUpdate update(local_state, prefs::kMetricsInitialLogs); |
| 1228 | ListValue* unsent_initial_logs = update.Get(); |
| 1229 | StoreUnsentLogsHelper(unsent_initial_logs_, kMaxInitialLogsPersisted, |
| 1230 | unsent_initial_logs); |
| 1231 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1232 | |
battre@chromium.org | f8628c2 | 2011-04-05 12:10:18 | [diff] [blame] | 1233 | { |
| 1234 | ListPrefUpdate update(local_state, prefs::kMetricsOngoingLogs); |
| 1235 | ListValue* unsent_ongoing_logs = update.Get(); |
| 1236 | StoreUnsentLogsHelper(unsent_ongoing_logs_, kMaxOngoingLogsPersisted, |
| 1237 | unsent_ongoing_logs); |
| 1238 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | void MetricsService::PreparePendingLogText() { |
| 1242 | DCHECK(pending_log()); |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1243 | if (!compressed_log_.empty()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1244 | return; |
mark@chromium.org | 9ffcccf4 | 2009-09-15 22:19:18 | [diff] [blame] | 1245 | int text_size = pending_log_->GetEncodedLogSize(); |
| 1246 | |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1247 | std::string pending_log_text; |
| 1248 | // Leave room for the NULL terminator. |
| 1249 | pending_log_->GetEncodedLog(WriteInto(&pending_log_text, text_size + 1), |
mark@chromium.org | 9ffcccf4 | 2009-09-15 22:19:18 | [diff] [blame] | 1250 | text_size); |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1251 | |
| 1252 | if (Bzip2Compress(pending_log_text, &compressed_log_)) { |
| 1253 | // Allow security conscious users to see all metrics logs that we send. |
pkasting@chromium.org | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1254 | VLOG(1) << "COMPRESSED FOLLOWING METRICS LOG: " << pending_log_text; |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1255 | } else { |
| 1256 | LOG(DFATAL) << "Failed to compress log for transmission."; |
| 1257 | // We can't discard the logs as other caller functions expect that |
| 1258 | // |compressed_log_| not be empty. We can detect this failure at the server |
| 1259 | // after we transmit. |
| 1260 | compressed_log_ = "Unable to compress!"; |
| 1261 | MakeStoreStatusHistogram(COMPRESS_FAIL); |
| 1262 | return; |
| 1263 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1264 | } |
| 1265 | |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1266 | void MetricsService::PrepareFetchWithPendingLog() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1267 | DCHECK(pending_log()); |
| 1268 | DCHECK(!current_fetch_.get()); |
| 1269 | PreparePendingLogText(); |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1270 | DCHECK(!compressed_log_.empty()); |
pkasting@chromium.org | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1271 | |
kuchhal@chromium.org | 79bf0b7 | 2009-04-27 21:30:55 | [diff] [blame] | 1272 | current_fetch_.reset(new URLFetcher(GURL(WideToUTF16(server_url_)), |
| 1273 | URLFetcher::POST, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1274 | this)); |
willchan@chromium.org | 9abfa19 | 2011-07-20 15:48:53 | [diff] [blame] | 1275 | current_fetch_->set_request_context( |
nkostylev@chromium.org | 8ef3d805 | 2011-07-22 09:03:00 | [diff] [blame] | 1276 | g_browser_process->system_request_context()); |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1277 | current_fetch_->set_upload_data(kMetricsType, compressed_log_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1278 | } |
| 1279 | |
tfarina@chromium.org | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1280 | static const char* StatusToString(const net::URLRequestStatus& status) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1281 | switch (status.status()) { |
tfarina@chromium.org | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1282 | case net::URLRequestStatus::SUCCESS: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1283 | return "SUCCESS"; |
| 1284 | |
tfarina@chromium.org | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1285 | case net::URLRequestStatus::IO_PENDING: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1286 | return "IO_PENDING"; |
| 1287 | |
tfarina@chromium.org | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1288 | case net::URLRequestStatus::HANDLED_EXTERNALLY: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1289 | return "HANDLED_EXTERNALLY"; |
| 1290 | |
tfarina@chromium.org | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1291 | case net::URLRequestStatus::CANCELED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1292 | return "CANCELED"; |
| 1293 | |
tfarina@chromium.org | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1294 | case net::URLRequestStatus::FAILED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1295 | return "FAILED"; |
| 1296 | |
| 1297 | default: |
jar@chromium.org | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1298 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1299 | return "Unknown"; |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | void MetricsService::OnURLFetchComplete(const URLFetcher* source, |
| 1304 | const GURL& url, |
tfarina@chromium.org | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1305 | const net::URLRequestStatus& status, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1306 | int response_code, |
tfarina@chromium.org | cb04f5e | 2011-05-06 01:10:00 | [diff] [blame] | 1307 | const net::ResponseCookies& cookies, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1308 | const std::string& data) { |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1309 | DCHECK(waiting_for_asynchronus_reporting_step_); |
| 1310 | waiting_for_asynchronus_reporting_step_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1311 | DCHECK(current_fetch_.get()); |
| 1312 | current_fetch_.reset(NULL); // We're not allowed to re-use it. |
| 1313 | |
| 1314 | // Confirm send so that we can move on. |
pkasting@chromium.org | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1315 | VLOG(1) << "METRICS RESPONSE CODE: " << response_code |
| 1316 | << " status=" << StatusToString(status); |
petersont@google.com | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1317 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1318 | bool upload_succeeded = response_code == 200; |
| 1319 | |
jar@chromium.org | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1320 | // Provide boolean for error recovery (allow us to ignore response_code). |
paul@chromium.org | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 1321 | bool discard_log = false; |
jar@chromium.org | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1322 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1323 | if (!upload_succeeded && |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1324 | (compressed_log_.length() > |
| 1325 | static_cast<size_t>(kUploadLogAvoidRetransmitSize))) { |
dsh@google.com | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 1326 | UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded", |
ziadh@chromium.org | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1327 | static_cast<int>(compressed_log_.length())); |
jar@chromium.org | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1328 | discard_log = true; |
| 1329 | } else if (response_code == 400) { |
| 1330 | // Bad syntax. Retransmission won't work. |
dsh@google.com | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 1331 | UMA_HISTOGRAM_COUNTS("UMA.Unacceptable_Log_Discarded", state_); |
jar@chromium.org | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1332 | discard_log = true; |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1333 | } |
| 1334 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1335 | if (!upload_succeeded && !discard_log) { |
pkasting@chromium.org | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1336 | VLOG(1) << "METRICS: transmission attempt returned a failure code: " |
| 1337 | << response_code << ". Verify network connectivity"; |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1338 | LogBadResponseCode(); |
jar@chromium.org | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1339 | } else { // Successful receipt (or we are discarding log). |
pkasting@chromium.org | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1340 | VLOG(1) << "METRICS RESPONSE DATA: " << data; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1341 | switch (state_) { |
| 1342 | case INITIAL_LOG_READY: |
| 1343 | state_ = SEND_OLD_INITIAL_LOGS; |
| 1344 | break; |
| 1345 | |
| 1346 | case SEND_OLD_INITIAL_LOGS: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1347 | case SENDING_OLD_LOGS: |
stuartmorgan@chromium.org | d53e223 | 2011-06-30 15:54:57 | [diff] [blame] | 1348 | // Store the updated list to disk now that the removed log is uploaded. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1349 | StoreUnsentLogs(); |
| 1350 | break; |
| 1351 | |
| 1352 | case SENDING_CURRENT_LOGS: |
| 1353 | break; |
| 1354 | |
| 1355 | default: |
jar@chromium.org | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1356 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1357 | break; |
| 1358 | } |
petersont@google.com | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1359 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1360 | DiscardPendingLog(); |
jar@google.com | 29be9255 | 2008-08-07 22:49:27 | [diff] [blame] | 1361 | // Since we sent a log, make sure our in-memory state is recorded to disk. |
| 1362 | PrefService* local_state = g_browser_process->local_state(); |
| 1363 | DCHECK(local_state); |
| 1364 | if (local_state) |
phajdan.jr@chromium.org | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 1365 | local_state->ScheduleSavePersistentPrefs(); |
petersont@google.com | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1366 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1367 | if (unsent_logs()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1368 | DCHECK(state_ < SENDING_CURRENT_LOGS); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1369 | } |
petersont@google.com | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1370 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1371 | // Error 400 indicates a problem with the log, not with the server, so |
| 1372 | // don't consider that a sign that the server is in trouble. |
| 1373 | bool server_is_healthy = upload_succeeded || response_code == 400; |
| 1374 | |
| 1375 | scheduler_->UploadFinished(server_is_healthy, unsent_logs()); |
rtenneti@chromium.org | d67d105 | 2011-06-09 05:11:41 | [diff] [blame] | 1376 | |
| 1377 | // Collect network stats if UMA upload succeeded. |
| 1378 | if (server_is_healthy && io_thread_) |
| 1379 | chrome_browser_net::CollectNetworkStats(network_stats_server_, io_thread_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1380 | } |
| 1381 | |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1382 | void MetricsService::LogBadResponseCode() { |
pkasting@chromium.org | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1383 | VLOG(1) << "Verify your metrics logs are formatted correctly. Verify server " |
| 1384 | "is active at " << server_url_; |
petersont@google.com | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1385 | if (!pending_log()) { |
pkasting@chromium.org | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1386 | VLOG(1) << "METRICS: Recorder shutdown during log transmission."; |
petersont@google.com | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1387 | } else { |
stuartmorgan@chromium.org | 7f7f196 | 2011-04-20 15:58:16 | [diff] [blame] | 1388 | VLOG(1) << "METRICS: transmission retry being scheduled for " |
pkasting@chromium.org | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1389 | << compressed_log_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1390 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1391 | } |
| 1392 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 1393 | void MetricsService::LogWindowChange(int type, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1394 | const NotificationSource& source, |
| 1395 | const NotificationDetails& details) { |
brettw@google.com | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1396 | int controller_id = -1; |
| 1397 | uintptr_t window_or_tab = source.map_key(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1398 | MetricsLog::WindowEventType window_type; |
| 1399 | |
| 1400 | // Note: since we stop all logging when a single OTR session is active, it is |
| 1401 | // possible that we start getting notifications about a window that we don't |
| 1402 | // know about. |
brettw@google.com | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1403 | if (window_map_.find(window_or_tab) == window_map_.end()) { |
| 1404 | controller_id = next_window_id_++; |
| 1405 | window_map_[window_or_tab] = controller_id; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1406 | } else { |
brettw@google.com | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1407 | controller_id = window_map_[window_or_tab]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1408 | } |
jar@chromium.org | 9274524 | 2009-06-12 16:52:21 | [diff] [blame] | 1409 | DCHECK_NE(controller_id, -1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1410 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 1411 | switch (type) { |
| 1412 | case content::NOTIFICATION_TAB_PARENTED: |
| 1413 | case chrome::NOTIFICATION_BROWSER_OPENED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1414 | window_type = MetricsLog::WINDOW_CREATE; |
| 1415 | break; |
| 1416 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 1417 | case content::NOTIFICATION_TAB_CLOSING: |
| 1418 | case chrome::NOTIFICATION_BROWSER_CLOSED: |
brettw@google.com | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1419 | window_map_.erase(window_map_.find(window_or_tab)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1420 | window_type = MetricsLog::WINDOW_DESTROY; |
| 1421 | break; |
| 1422 | |
| 1423 | default: |
jar@chromium.org | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1424 | NOTREACHED(); |
paul@chromium.org | 68d74f0 | 2009-02-13 01:36:50 | [diff] [blame] | 1425 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1426 | } |
| 1427 | |
brettw@google.com | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1428 | // TODO(brettw) we should have some kind of ID for the parent. |
| 1429 | current_log_->RecordWindowEvent(window_type, controller_id, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1430 | } |
| 1431 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 1432 | void MetricsService::LogLoadComplete(int type, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1433 | const NotificationSource& source, |
| 1434 | const NotificationDetails& details) { |
| 1435 | if (details == NotificationService::NoDetails()) |
| 1436 | return; |
| 1437 | |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1438 | // TODO(jar): There is a bug causing this to be called too many times, and |
| 1439 | // the log overflows. For now, we won't record these events. |
dsh@google.com | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 1440 | UMA_HISTOGRAM_COUNTS("UMA.LogLoadComplete called", 1); |
jar@google.com | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1441 | return; |
| 1442 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1443 | const Details<LoadNotificationDetails> load_details(details); |
brettw@google.com | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1444 | int controller_id = window_map_[details.map_key()]; |
| 1445 | current_log_->RecordLoadEvent(controller_id, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1446 | load_details->url(), |
| 1447 | load_details->origin(), |
| 1448 | load_details->session_index(), |
| 1449 | load_details->load_time()); |
| 1450 | } |
| 1451 | |
viettrungluu@chromium.org | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1452 | void MetricsService::IncrementPrefValue(const char* path) { |
cpu@google.com | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1453 | PrefService* pref = g_browser_process->local_state(); |
| 1454 | DCHECK(pref); |
| 1455 | int value = pref->GetInteger(path); |
| 1456 | pref->SetInteger(path, value + 1); |
| 1457 | } |
| 1458 | |
viettrungluu@chromium.org | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1459 | void MetricsService::IncrementLongPrefsValue(const char* path) { |
robertshield@google.com | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1460 | PrefService* pref = g_browser_process->local_state(); |
| 1461 | DCHECK(pref); |
| 1462 | int64 value = pref->GetInt64(path); |
jar@chromium.org | b42c5e4 | 2010-06-03 20:43:25 | [diff] [blame] | 1463 | pref->SetInt64(path, value + 1); |
robertshield@google.com | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1464 | } |
| 1465 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1466 | void MetricsService::LogLoadStarted() { |
jar@chromium.org | dd8d12a | 2011-09-02 02:10:15 | [diff] [blame] | 1467 | HISTOGRAM_ENUMERATION("Chrome.UmaPageloadCounter", 1, 2); |
cpu@google.com | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1468 | IncrementPrefValue(prefs::kStabilityPageLoadCount); |
robertshield@google.com | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1469 | IncrementLongPrefsValue(prefs::kUninstallMetricsPageLoadCount); |
jar@google.com | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 1470 | // We need to save the prefs, as page load count is a critical stat, and it |
| 1471 | // might be lost due to a crash :-(. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1472 | } |
| 1473 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1474 | void MetricsService::LogRendererCrash() { |
cpu@google.com | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1475 | IncrementPrefValue(prefs::kStabilityRendererCrashCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1476 | } |
| 1477 | |
asargent@chromium.org | 1f08562 | 2009-12-04 05:33:45 | [diff] [blame] | 1478 | void MetricsService::LogExtensionRendererCrash() { |
| 1479 | IncrementPrefValue(prefs::kStabilityExtensionRendererCrashCount); |
| 1480 | } |
| 1481 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1482 | void MetricsService::LogRendererHang() { |
cpu@google.com | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1483 | IncrementPrefValue(prefs::kStabilityRendererHangCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1484 | } |
| 1485 | |
jar@chromium.org | c0c55e9 | 2011-09-10 18:47:30 | [diff] [blame^] | 1486 | void MetricsService::LogNeedForCleanShutdown() { |
| 1487 | PrefService* pref = g_browser_process->local_state(); |
| 1488 | pref->SetBoolean(prefs::kStabilityExitedCleanly, false); |
| 1489 | // Redundant setting to be sure we call for a clean shutdown. |
| 1490 | clean_shutdown_status_ = NEED_TO_SHUTDOWN; |
| 1491 | } |
| 1492 | |
| 1493 | bool MetricsService::UmaMetricsProperlyShutdown() { |
| 1494 | CHECK(clean_shutdown_status_ == CLEANLY_SHUTDOWN || |
| 1495 | clean_shutdown_status_ == NEED_TO_SHUTDOWN); |
| 1496 | return clean_shutdown_status_ == CLEANLY_SHUTDOWN; |
| 1497 | } |
| 1498 | |
stuartmorgan@chromium.org | 466f3c1 | 2011-03-23 21:20:38 | [diff] [blame] | 1499 | void MetricsService::LogCleanShutdown() { |
jar@chromium.org | acd55b3 | 2011-09-05 17:35:31 | [diff] [blame] | 1500 | // Redundant hack to write pref ASAP. |
| 1501 | PrefService* pref = g_browser_process->local_state(); |
| 1502 | pref->SetBoolean(prefs::kStabilityExitedCleanly, true); |
| 1503 | pref->SavePersistentPrefs(); |
jar@chromium.org | c0c55e9 | 2011-09-10 18:47:30 | [diff] [blame^] | 1504 | // Redundant setting to assure that we always reset this value at shutdown |
| 1505 | // (and that we don't use some alternate path, and not call LogCleanShutdown). |
| 1506 | clean_shutdown_status_ = CLEANLY_SHUTDOWN; |
jar@chromium.org | acd55b3 | 2011-09-05 17:35:31 | [diff] [blame] | 1507 | |
stuartmorgan@chromium.org | 466f3c1 | 2011-03-23 21:20:38 | [diff] [blame] | 1508 | RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); |
| 1509 | } |
| 1510 | |
petkov@chromium.org | c1834a9 | 2011-01-21 18:21:03 | [diff] [blame] | 1511 | #if defined(OS_CHROMEOS) |
| 1512 | void MetricsService::LogChromeOSCrash(const std::string &crash_type) { |
| 1513 | if (crash_type == "user") |
| 1514 | IncrementPrefValue(prefs::kStabilityOtherUserCrashCount); |
| 1515 | else if (crash_type == "kernel") |
| 1516 | IncrementPrefValue(prefs::kStabilityKernelCrashCount); |
| 1517 | else if (crash_type == "uncleanshutdown") |
| 1518 | IncrementPrefValue(prefs::kStabilitySystemUncleanShutdownCount); |
| 1519 | else |
| 1520 | NOTREACHED() << "Unexpected Chrome OS crash type " << crash_type; |
| 1521 | // Wake up metrics logs sending if necessary now that new |
| 1522 | // log data is available. |
| 1523 | HandleIdleSinceLastTransmission(false); |
| 1524 | } |
| 1525 | #endif // OS_CHROMEOS |
| 1526 | |
jam@chromium.org | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1527 | void MetricsService::LogChildProcessChange( |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 1528 | int type, |
jam@chromium.org | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1529 | const NotificationSource& source, |
| 1530 | const NotificationDetails& details) { |
gregoryd@google.com | 0d84c5d | 2009-10-09 01:10:42 | [diff] [blame] | 1531 | Details<ChildProcessInfo> child_details(details); |
evan@chromium.org | 68b9e72b | 2011-08-05 23:08:22 | [diff] [blame] | 1532 | const string16& child_name = child_details->name(); |
gregoryd@google.com | 0d84c5d | 2009-10-09 01:10:42 | [diff] [blame] | 1533 | |
jam@chromium.org | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1534 | if (child_process_stats_buffer_.find(child_name) == |
| 1535 | child_process_stats_buffer_.end()) { |
gregoryd@google.com | 0d84c5d | 2009-10-09 01:10:42 | [diff] [blame] | 1536 | child_process_stats_buffer_[child_name] = |
| 1537 | ChildProcessStats(child_details->type()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1538 | } |
| 1539 | |
jam@chromium.org | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1540 | ChildProcessStats& stats = child_process_stats_buffer_[child_name]; |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 1541 | switch (type) { |
| 1542 | case content::NOTIFICATION_CHILD_PROCESS_HOST_CONNECTED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1543 | stats.process_launches++; |
| 1544 | break; |
| 1545 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 1546 | case content::NOTIFICATION_CHILD_INSTANCE_CREATED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1547 | stats.instances++; |
| 1548 | break; |
| 1549 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 1550 | case content::NOTIFICATION_CHILD_PROCESS_CRASHED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1551 | stats.process_crashes++; |
asargent@chromium.org | 1f08562 | 2009-12-04 05:33:45 | [diff] [blame] | 1552 | // Exclude plugin crashes from the count below because we report them via |
| 1553 | // a separate UMA metric. |
| 1554 | if (child_details->type() != ChildProcessInfo::PLUGIN_PROCESS) { |
| 1555 | IncrementPrefValue(prefs::kStabilityChildProcessCrashCount); |
| 1556 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1557 | break; |
| 1558 | |
| 1559 | default: |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 1560 | NOTREACHED() << "Unexpected notification type " << type; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1561 | return; |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | // Recursively counts the number of bookmarks and folders in node. |
munjal@chromium.org | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 1566 | static void CountBookmarks(const BookmarkNode* node, |
| 1567 | int* bookmarks, |
| 1568 | int* folders) { |
tfarina@chromium.org | 0890e60e | 2011-06-27 14:55:21 | [diff] [blame] | 1569 | if (node->is_url()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1570 | (*bookmarks)++; |
| 1571 | else |
| 1572 | (*folders)++; |
tfarina@chromium.org | 9c1a75a | 2011-03-10 02:38:12 | [diff] [blame] | 1573 | for (int i = 0; i < node->child_count(); ++i) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1574 | CountBookmarks(node->GetChild(i), bookmarks, folders); |
| 1575 | } |
| 1576 | |
munjal@chromium.org | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 1577 | void MetricsService::LogBookmarks(const BookmarkNode* node, |
viettrungluu@chromium.org | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1578 | const char* num_bookmarks_key, |
| 1579 | const char* num_folders_key) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1580 | DCHECK(node); |
| 1581 | int num_bookmarks = 0; |
| 1582 | int num_folders = 0; |
| 1583 | CountBookmarks(node, &num_bookmarks, &num_folders); |
| 1584 | num_folders--; // Don't include the root folder in the count. |
| 1585 | |
| 1586 | PrefService* pref = g_browser_process->local_state(); |
| 1587 | DCHECK(pref); |
| 1588 | pref->SetInteger(num_bookmarks_key, num_bookmarks); |
| 1589 | pref->SetInteger(num_folders_key, num_folders); |
| 1590 | } |
| 1591 | |
sky@google.com | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 1592 | void MetricsService::LogBookmarks(BookmarkModel* model) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1593 | DCHECK(model); |
tfarina@chromium.org | 72bdcfe | 2011-07-22 17:21:58 | [diff] [blame] | 1594 | LogBookmarks(model->bookmark_bar_node(), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1595 | prefs::kNumBookmarksOnBookmarkBar, |
| 1596 | prefs::kNumFoldersOnBookmarkBar); |
| 1597 | LogBookmarks(model->other_node(), |
| 1598 | prefs::kNumBookmarksInOtherBookmarkFolder, |
| 1599 | prefs::kNumFoldersInOtherBookmarkFolder); |
| 1600 | ScheduleNextStateSave(); |
| 1601 | } |
| 1602 | |
erg@google.com | 8e5c89a | 2011-06-07 18:13:33 | [diff] [blame] | 1603 | void MetricsService::LogKeywords(const TemplateURLService* url_model) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1604 | DCHECK(url_model); |
| 1605 | |
| 1606 | PrefService* pref = g_browser_process->local_state(); |
| 1607 | DCHECK(pref); |
| 1608 | pref->SetInteger(prefs::kNumKeywords, |
| 1609 | static_cast<int>(url_model->GetTemplateURLs().size())); |
| 1610 | ScheduleNextStateSave(); |
| 1611 | } |
| 1612 | |
| 1613 | void MetricsService::RecordPluginChanges(PrefService* pref) { |
battre@chromium.org | f8628c2 | 2011-04-05 12:10:18 | [diff] [blame] | 1614 | ListPrefUpdate update(pref, prefs::kStabilityPluginStats); |
| 1615 | ListValue* plugins = update.Get(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1616 | DCHECK(plugins); |
| 1617 | |
| 1618 | for (ListValue::iterator value_iter = plugins->begin(); |
| 1619 | value_iter != plugins->end(); ++value_iter) { |
| 1620 | if (!(*value_iter)->IsType(Value::TYPE_DICTIONARY)) { |
jar@chromium.org | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1621 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1622 | continue; |
| 1623 | } |
| 1624 | |
| 1625 | DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*value_iter); |
viettrungluu@chromium.org | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1626 | std::string plugin_name; |
nsylvain@chromium.org | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1627 | plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name); |
nsylvain@chromium.org | 6470ee8f | 2009-03-03 20:46:40 | [diff] [blame] | 1628 | if (plugin_name.empty()) { |
jar@chromium.org | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1629 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1630 | continue; |
| 1631 | } |
| 1632 | |
viettrungluu@chromium.org | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1633 | // TODO(viettrungluu): remove conversions |
evan@chromium.org | 68b9e72b | 2011-08-05 23:08:22 | [diff] [blame] | 1634 | string16 name16 = UTF8ToUTF16(plugin_name); |
| 1635 | if (child_process_stats_buffer_.find(name16) == |
| 1636 | child_process_stats_buffer_.end()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1637 | continue; |
evan@chromium.org | 68b9e72b | 2011-08-05 23:08:22 | [diff] [blame] | 1638 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1639 | |
evan@chromium.org | 68b9e72b | 2011-08-05 23:08:22 | [diff] [blame] | 1640 | ChildProcessStats stats = child_process_stats_buffer_[name16]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1641 | if (stats.process_launches) { |
| 1642 | int launches = 0; |
nsylvain@chromium.org | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1643 | plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1644 | launches += stats.process_launches; |
nsylvain@chromium.org | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1645 | plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, launches); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1646 | } |
| 1647 | if (stats.process_crashes) { |
| 1648 | int crashes = 0; |
nsylvain@chromium.org | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1649 | plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1650 | crashes += stats.process_crashes; |
nsylvain@chromium.org | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1651 | plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, crashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1652 | } |
| 1653 | if (stats.instances) { |
| 1654 | int instances = 0; |
nsylvain@chromium.org | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1655 | plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1656 | instances += stats.instances; |
nsylvain@chromium.org | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1657 | plugin_dict->SetInteger(prefs::kStabilityPluginInstances, instances); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1658 | } |
| 1659 | |
evan@chromium.org | 68b9e72b | 2011-08-05 23:08:22 | [diff] [blame] | 1660 | child_process_stats_buffer_.erase(name16); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | // Now go through and add dictionaries for plugins that didn't already have |
| 1664 | // reports in Local State. |
evan@chromium.org | 68b9e72b | 2011-08-05 23:08:22 | [diff] [blame] | 1665 | for (std::map<string16, ChildProcessStats>::iterator cache_iter = |
jam@chromium.org | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1666 | child_process_stats_buffer_.begin(); |
| 1667 | cache_iter != child_process_stats_buffer_.end(); ++cache_iter) { |
jam@chromium.org | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1668 | ChildProcessStats stats = cache_iter->second; |
gregoryd@google.com | 0d84c5d | 2009-10-09 01:10:42 | [diff] [blame] | 1669 | |
| 1670 | // Insert only plugins information into the plugins list. |
| 1671 | if (ChildProcessInfo::PLUGIN_PROCESS != stats.process_type) |
| 1672 | continue; |
| 1673 | |
viettrungluu@chromium.org | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1674 | // TODO(viettrungluu): remove conversion |
evan@chromium.org | 68b9e72b | 2011-08-05 23:08:22 | [diff] [blame] | 1675 | std::string plugin_name = UTF16ToUTF8(cache_iter->first); |
gregoryd@google.com | 0d84c5d | 2009-10-09 01:10:42 | [diff] [blame] | 1676 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1677 | DictionaryValue* plugin_dict = new DictionaryValue; |
| 1678 | |
nsylvain@chromium.org | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1679 | plugin_dict->SetString(prefs::kStabilityPluginName, plugin_name); |
| 1680 | plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1681 | stats.process_launches); |
nsylvain@chromium.org | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1682 | plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1683 | stats.process_crashes); |
nsylvain@chromium.org | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1684 | plugin_dict->SetInteger(prefs::kStabilityPluginInstances, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1685 | stats.instances); |
| 1686 | plugins->Append(plugin_dict); |
| 1687 | } |
jam@chromium.org | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1688 | child_process_stats_buffer_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1689 | } |
| 1690 | |
ananta@chromium.org | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 1691 | bool MetricsService::CanLogNotification(int type, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1692 | const NotificationSource& source, |
| 1693 | const NotificationDetails& details) { |
akalin@chromium.org | 2c910b7 | 2011-03-08 21:16:32 | [diff] [blame] | 1694 | // We simply don't log anything to UMA if there is a single incognito |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1695 | // session visible. The problem is that we always notify using the orginal |
| 1696 | // profile in order to simplify notification processing. |
| 1697 | return !BrowserList::IsOffTheRecordSessionActive(); |
| 1698 | } |
| 1699 | |
viettrungluu@chromium.org | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1700 | void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1701 | DCHECK(IsSingleThreaded()); |
| 1702 | |
| 1703 | PrefService* pref = g_browser_process->local_state(); |
| 1704 | DCHECK(pref); |
| 1705 | |
| 1706 | pref->SetBoolean(path, value); |
| 1707 | RecordCurrentState(pref); |
| 1708 | } |
| 1709 | |
| 1710 | void MetricsService::RecordCurrentState(PrefService* pref) { |
robertshield@google.com | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1711 | pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1712 | |
| 1713 | RecordPluginChanges(pref); |
| 1714 | } |
| 1715 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1716 | static bool IsSingleThreaded() { |
brettw@chromium.org | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 1717 | static base::PlatformThreadId thread_id = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1718 | if (!thread_id) |
brettw@chromium.org | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 1719 | thread_id = base::PlatformThread::CurrentId(); |
| 1720 | return base::PlatformThread::CurrentId() == thread_id; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1721 | } |
rvargas@google.com | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 1722 | |
| 1723 | #if defined(OS_CHROMEOS) |
sky@chromium.org | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 1724 | void MetricsService::StartExternalMetrics() { |
rvargas@google.com | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 1725 | external_metrics_ = new chromeos::ExternalMetrics; |
sky@chromium.org | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 1726 | external_metrics_->Start(); |
rvargas@google.com | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 1727 | } |
| 1728 | #endif |
sreeram@chromium.org | 3819f2ee | 2011-08-21 09:44:38 | [diff] [blame] | 1729 | |
| 1730 | // static |
| 1731 | bool MetricsServiceHelper::IsMetricsReportingEnabled() { |
| 1732 | bool result = false; |
| 1733 | const PrefService* local_state = g_browser_process->local_state(); |
| 1734 | if (local_state) { |
| 1735 | const PrefService::Preference* uma_pref = |
| 1736 | local_state->FindPreference(prefs::kMetricsReportingEnabled); |
| 1737 | if (uma_pref) { |
| 1738 | bool success = uma_pref->GetValue()->GetAsBoolean(&result); |
| 1739 | DCHECK(success); |
| 1740 | } |
| 1741 | } |
| 1742 | return result; |
| 1743 | } |