[email protected] | 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 |
[email protected] | 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 |
[email protected] | 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 | // |
[email protected] | 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 |
[email protected] | 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) |
[email protected] | 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 |
[email protected] | 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 |
[email protected] | 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. |
[email protected] | 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 | // |
[email protected] | 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 |
[email protected] | 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 | // |
[email protected] | 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 |
[email protected] | 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. |
[email protected] | 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 |
[email protected] | 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 |
[email protected] | 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 | |
[email protected] | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 157 | #include "chrome/browser/metrics/metrics_service.h" |
| 158 | |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 159 | #include "base/base64.h" |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 160 | #include "base/command_line.h" |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 161 | #include "base/md5.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 162 | #include "base/metrics/histogram.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 163 | #include "base/string_number_conversions.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 164 | #include "base/threading/platform_thread.h" |
[email protected] | b3841c50 | 2011-03-09 01:21:31 | [diff] [blame] | 165 | #include "base/threading/thread.h" |
[email protected] | 440b37b2 | 2010-08-30 05:31:40 | [diff] [blame] | 166 | #include "base/utf_string_conversions.h" |
[email protected] | 67908205 | 2010-07-21 21:30:13 | [diff] [blame] | 167 | #include "base/values.h" |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 168 | #include "chrome/browser/bookmarks/bookmark_model.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | #include "chrome/browser/browser_list.h" |
| 170 | #include "chrome/browser/browser_process.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 171 | #include "chrome/browser/load_notification_details.h" |
[email protected] | 7c927b6 | 2010-02-24 09:54:13 | [diff] [blame] | 172 | #include "chrome/browser/metrics/histogram_synchronizer.h" |
[email protected] | 67908205 | 2010-07-21 21:30:13 | [diff] [blame] | 173 | #include "chrome/browser/metrics/metrics_log.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 174 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 175 | #include "chrome/browser/profiles/profile.h" |
[email protected] | d54e03a5 | 2009-01-16 00:31:04 | [diff] [blame] | 176 | #include "chrome/browser/search_engines/template_url_model.h" |
[email protected] | 67908205 | 2010-07-21 21:30:13 | [diff] [blame] | 177 | #include "chrome/common/child_process_info.h" |
[email protected] | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 178 | #include "chrome/common/child_process_logging.h" |
[email protected] | 9274524 | 2009-06-12 16:52:21 | [diff] [blame] | 179 | #include "chrome/common/chrome_switches.h" |
[email protected] | 3eb0d8f7 | 2010-12-15 23:38:25 | [diff] [blame] | 180 | #include "chrome/common/guid.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | #include "chrome/common/pref_names.h" |
[email protected] | e09ba55 | 2009-02-05 03:26:29 | [diff] [blame] | 182 | #include "chrome/common/render_messages.h" |
[email protected] | 5de63471 | 2011-03-02 00:20:19 | [diff] [blame] | 183 | #include "content/browser/renderer_host/render_process_host.h" |
[email protected] | b3841c50 | 2011-03-09 01:21:31 | [diff] [blame] | 184 | #include "content/common/notification_service.h" |
| 185 | #include "libxml/xmlwriter.h" |
[email protected] | 191eb3f7 | 2010-12-21 06:27:50 | [diff] [blame] | 186 | #include "webkit/plugins/npapi/plugin_list.h" |
| 187 | #include "webkit/plugins/npapi/webplugininfo.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 188 | |
[email protected] | e06131d | 2010-02-10 18:40:33 | [diff] [blame] | 189 | // TODO(port): port browser_distribution.h. |
| 190 | #if !defined(OS_POSIX) |
[email protected] | 79bf0b7 | 2009-04-27 21:30:55 | [diff] [blame] | 191 | #include "chrome/installer/util/browser_distribution.h" |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 192 | #endif |
| 193 | |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 194 | #if defined(OS_CHROMEOS) |
[email protected] | db342d5 | 2010-08-09 21:19:37 | [diff] [blame] | 195 | #include "chrome/browser/chromeos/cros/cros_library.h" |
| 196 | #include "chrome/browser/chromeos/cros/system_library.h" |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 197 | #include "chrome/browser/chromeos/external_metrics.h" |
| 198 | #endif |
| 199 | |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 200 | namespace { |
| 201 | MetricsService::LogRecallStatus MakeRecallStatusHistogram( |
| 202 | MetricsService::LogRecallStatus status) { |
| 203 | UMA_HISTOGRAM_ENUMERATION("PrefService.PersistentLogRecall", status, |
| 204 | MetricsService::END_RECALL_STATUS); |
| 205 | return status; |
| 206 | } |
| 207 | |
| 208 | // TODO(ziadh): Remove this when done with experiment. |
| 209 | void MakeStoreStatusHistogram(MetricsService::LogStoreStatus status) { |
[email protected] | 4e95d20 | 2010-07-24 01:47:56 | [diff] [blame] | 210 | UMA_HISTOGRAM_ENUMERATION("PrefService.PersistentLogStore2", status, |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 211 | MetricsService::END_STORE_STATUS); |
| 212 | } |
| 213 | } // namespace |
| 214 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 215 | using base::Time; |
| 216 | using base::TimeDelta; |
| 217 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | // Check to see that we're being called on only one thread. |
| 219 | static bool IsSingleThreaded(); |
| 220 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 221 | static const char kMetricsType[] = "application/vnd.mozilla.metrics.bz2"; |
| 222 | |
| 223 | // The delay, in seconds, after startup before sending the first log message. |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 224 | static const int kInitialInterlogDuration = 60; // one minute |
| 225 | |
[email protected] | c9a3ef8 | 2009-05-28 22:02:46 | [diff] [blame] | 226 | // This specifies the amount of time to wait for all renderers to send their |
| 227 | // data. |
| 228 | static const int kMaxHistogramGatheringWaitDuration = 60000; // 60 seconds. |
| 229 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 230 | // The default maximum number of events in a log uploaded to the UMA server. |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 231 | static const int kInitialEventLimit = 2400; |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 232 | |
| 233 | // If an upload fails, and the transmission was over this byte count, then we |
| 234 | // will discard the log, and not try to retransmit it. We also don't persist |
| 235 | // the log to the prefs for transmission during the next chrome session if this |
| 236 | // limit is exceeded. |
| 237 | static const int kUploadLogAvoidRetransmitSize = 50000; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | |
| 239 | // When we have logs from previous Chrome sessions to send, how long should we |
| 240 | // delay (in seconds) between each log transmission. |
| 241 | static const int kUnsentLogDelay = 15; // 15 seconds |
| 242 | |
| 243 | // Minimum time a log typically exists before sending, in seconds. |
| 244 | // This number is supplied by the server, but until we parse it out of a server |
| 245 | // response, we use this duration to specify how long we should wait before |
| 246 | // sending the next log. If the channel is busy, such as when there is a |
| 247 | // failure during an attempt to transmit a previous log, then a log may wait |
[email protected] | 2fe42fe | 2010-05-07 19:22:39 | [diff] [blame] | 248 | // (and continue to accrue new log entries) for a much greater period of time. |
| 249 | static const int kMinSecondsPerLog = 30 * 60; // Thirty minutes. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 251 | // When we don't succeed at transmitting a log to a server, we progressively |
| 252 | // wait longer and longer before sending the next log. This backoff process |
| 253 | // help reduce load on the server, and makes the amount of backoff vary between |
| 254 | // clients so that a collision (server overload?) on retransmit is less likely. |
| 255 | // The following is the constant we use to expand that inter-log duration. |
| 256 | static const double kBackoff = 1.1; |
| 257 | // We limit the maximum backoff to be no greater than some multiple of the |
| 258 | // default kMinSecondsPerLog. The following is that maximum ratio. |
| 259 | static const int kMaxBackoff = 10; |
| 260 | |
| 261 | // Interval, in seconds, between state saves. |
| 262 | static const int kSaveStateInterval = 5 * 60; // five minutes |
| 263 | |
| 264 | // The number of "initial" logs we're willing to save, and hope to send during |
| 265 | // a future Chrome session. Initial logs contain crash stats, and are pretty |
| 266 | // small. |
| 267 | static const size_t kMaxInitialLogsPersisted = 20; |
| 268 | |
| 269 | // The number of ongoing logs we're willing to save persistently, and hope to |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 270 | // 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] | 271 | // large, as presumably the related "initial" log wasn't sent (probably nothing |
| 272 | // was, as the user was probably off-line). As a result, the log probably kept |
| 273 | // accumulating while the "initial" log was stalled (pending_), and couldn't be |
| 274 | // sent. As a result, we don't want to save too many of these mega-logs. |
| 275 | // A "standard shutdown" will create a small log, including just the data that |
| 276 | // was not yet been transmitted, and that is normal (to have exactly one |
| 277 | // ongoing_log_ at startup). |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 278 | static const size_t kMaxOngoingLogsPersisted = 8; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 280 | // We append (2) more elements to persisted lists: the size of the list and a |
| 281 | // checksum of the elements. |
| 282 | static const size_t kChecksumEntryCount = 2; |
| 283 | |
[email protected] | 67908205 | 2010-07-21 21:30:13 | [diff] [blame] | 284 | // This is used to quickly log stats from child process related notifications in |
| 285 | // MetricsService::child_stats_buffer_. The buffer's contents are transferred |
| 286 | // out when Local State is periodically saved. The information is then |
| 287 | // reported to the UMA server on next launch. |
| 288 | struct MetricsService::ChildProcessStats { |
| 289 | public: |
| 290 | explicit ChildProcessStats(ChildProcessInfo::ProcessType type) |
| 291 | : process_launches(0), |
| 292 | process_crashes(0), |
| 293 | instances(0), |
| 294 | process_type(type) {} |
| 295 | |
| 296 | // This constructor is only used by the map to return some default value for |
| 297 | // an index for which no value has been assigned. |
| 298 | ChildProcessStats() |
| 299 | : process_launches(0), |
| 300 | process_crashes(0), |
| 301 | instances(0), |
| 302 | process_type(ChildProcessInfo::UNKNOWN_PROCESS) {} |
| 303 | |
| 304 | // The number of times that the given child process has been launched |
| 305 | int process_launches; |
| 306 | |
| 307 | // The number of times that the given child process has crashed |
| 308 | int process_crashes; |
| 309 | |
| 310 | // The number of instances of this child process that have been created. |
| 311 | // An instance is a DOM object rendered by this child process during a page |
| 312 | // load. |
| 313 | int instances; |
| 314 | |
| 315 | ChildProcessInfo::ProcessType process_type; |
| 316 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 317 | |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 318 | class MetricsService::InitTaskComplete : public Task { |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 319 | public: |
[email protected] | 191eb3f7 | 2010-12-21 06:27:50 | [diff] [blame] | 320 | explicit InitTaskComplete( |
| 321 | const std::string& hardware_class, |
| 322 | const std::vector<webkit::npapi::WebPluginInfo>& plugins) |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 323 | : hardware_class_(hardware_class), plugins_(plugins) {} |
| 324 | |
[email protected] | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 325 | virtual void Run() { |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 326 | g_browser_process->metrics_service()->OnInitTaskComplete( |
| 327 | hardware_class_, plugins_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 328 | } |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 329 | |
[email protected] | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 330 | private: |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 331 | std::string hardware_class_; |
[email protected] | 191eb3f7 | 2010-12-21 06:27:50 | [diff] [blame] | 332 | std::vector<webkit::npapi::WebPluginInfo> plugins_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 333 | }; |
| 334 | |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 335 | class MetricsService::InitTask : public Task { |
[email protected] | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 336 | public: |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 337 | explicit InitTask(MessageLoop* callback_loop) |
[email protected] | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 338 | : callback_loop_(callback_loop) {} |
| 339 | |
| 340 | virtual void Run() { |
[email protected] | 191eb3f7 | 2010-12-21 06:27:50 | [diff] [blame] | 341 | std::vector<webkit::npapi::WebPluginInfo> plugins; |
| 342 | webkit::npapi::PluginList::Singleton()->GetPlugins(false, &plugins); |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 343 | std::string hardware_class; // Empty string by default. |
| 344 | #if defined(OS_CHROMEOS) |
[email protected] | db342d5 | 2010-08-09 21:19:37 | [diff] [blame] | 345 | chromeos::SystemLibrary* system_library = |
[email protected] | 191eb3f7 | 2010-12-21 06:27:50 | [diff] [blame] | 346 | chromeos::CrosLibrary::Get()->GetSystemLibrary(); |
[email protected] | db342d5 | 2010-08-09 21:19:37 | [diff] [blame] | 347 | system_library->GetMachineStatistic("hardware_class", &hardware_class); |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 348 | #endif // OS_CHROMEOS |
| 349 | callback_loop_->PostTask(FROM_HERE, new InitTaskComplete( |
| 350 | hardware_class, plugins)); |
[email protected] | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | private: |
| 354 | MessageLoop* callback_loop_; |
| 355 | }; |
[email protected] | 90d4137 | 2009-11-30 21:52:32 | [diff] [blame] | 356 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 357 | // static |
| 358 | void MetricsService::RegisterPrefs(PrefService* local_state) { |
| 359 | DCHECK(IsSingleThreaded()); |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 360 | local_state->RegisterStringPref(prefs::kMetricsClientID, ""); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 361 | local_state->RegisterInt64Pref(prefs::kMetricsClientIDTimestamp, 0); |
| 362 | local_state->RegisterInt64Pref(prefs::kStabilityLaunchTimeSec, 0); |
| 363 | local_state->RegisterInt64Pref(prefs::kStabilityLastTimestampSec, 0); |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 364 | local_state->RegisterStringPref(prefs::kStabilityStatsVersion, ""); |
[email protected] | 225c5084 | 2010-01-19 21:19:13 | [diff] [blame] | 365 | local_state->RegisterInt64Pref(prefs::kStabilityStatsBuildTime, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 366 | local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true); |
| 367 | local_state->RegisterBooleanPref(prefs::kStabilitySessionEndCompleted, true); |
| 368 | local_state->RegisterIntegerPref(prefs::kMetricsSessionID, -1); |
| 369 | local_state->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0); |
| 370 | local_state->RegisterIntegerPref(prefs::kStabilityCrashCount, 0); |
| 371 | local_state->RegisterIntegerPref(prefs::kStabilityIncompleteSessionEndCount, |
| 372 | 0); |
| 373 | local_state->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 374 | local_state->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0); |
[email protected] | 1f08562 | 2009-12-04 05:33:45 | [diff] [blame] | 375 | local_state->RegisterIntegerPref(prefs::kStabilityExtensionRendererCrashCount, |
| 376 | 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 377 | local_state->RegisterIntegerPref(prefs::kStabilityRendererHangCount, 0); |
[email protected] | 1f08562 | 2009-12-04 05:33:45 | [diff] [blame] | 378 | local_state->RegisterIntegerPref(prefs::kStabilityChildProcessCrashCount, 0); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 379 | local_state->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationFail, |
| 380 | 0); |
| 381 | local_state->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationSuccess, |
| 382 | 0); |
| 383 | local_state->RegisterIntegerPref(prefs::kStabilityDebuggerPresent, 0); |
| 384 | local_state->RegisterIntegerPref(prefs::kStabilityDebuggerNotPresent, 0); |
[email protected] | c1834a9 | 2011-01-21 18:21:03 | [diff] [blame] | 385 | #if defined(OS_CHROMEOS) |
| 386 | local_state->RegisterIntegerPref(prefs::kStabilityOtherUserCrashCount, 0); |
| 387 | local_state->RegisterIntegerPref(prefs::kStabilityKernelCrashCount, 0); |
| 388 | local_state->RegisterIntegerPref(prefs::kStabilitySystemUncleanShutdownCount, |
| 389 | 0); |
| 390 | #endif // OS_CHROMEOS |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 391 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | local_state->RegisterDictionaryPref(prefs::kProfileMetrics); |
| 393 | local_state->RegisterIntegerPref(prefs::kNumBookmarksOnBookmarkBar, 0); |
| 394 | local_state->RegisterIntegerPref(prefs::kNumFoldersOnBookmarkBar, 0); |
| 395 | local_state->RegisterIntegerPref(prefs::kNumBookmarksInOtherBookmarkFolder, |
| 396 | 0); |
| 397 | local_state->RegisterIntegerPref(prefs::kNumFoldersInOtherBookmarkFolder, 0); |
| 398 | local_state->RegisterIntegerPref(prefs::kNumKeywords, 0); |
| 399 | local_state->RegisterListPref(prefs::kMetricsInitialLogs); |
| 400 | local_state->RegisterListPref(prefs::kMetricsOngoingLogs); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 401 | |
| 402 | local_state->RegisterInt64Pref(prefs::kUninstallMetricsPageLoadCount, 0); |
| 403 | local_state->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); |
[email protected] | 6b5f21d | 2009-04-13 17:01:35 | [diff] [blame] | 404 | local_state->RegisterInt64Pref(prefs::kUninstallMetricsInstallDate, 0); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 405 | local_state->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); |
| 406 | local_state->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0); |
| 407 | local_state->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 410 | // static |
| 411 | void MetricsService::DiscardOldStabilityStats(PrefService* local_state) { |
| 412 | local_state->SetBoolean(prefs::kStabilityExitedCleanly, true); |
[email protected] | c9abf24 | 2009-07-18 06:00:38 | [diff] [blame] | 413 | local_state->SetBoolean(prefs::kStabilitySessionEndCompleted, true); |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 414 | |
| 415 | local_state->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0); |
| 416 | local_state->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0); |
| 417 | local_state->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0); |
| 418 | local_state->SetInteger(prefs::kStabilityDebuggerPresent, 0); |
| 419 | local_state->SetInteger(prefs::kStabilityDebuggerNotPresent, 0); |
| 420 | |
| 421 | local_state->SetInteger(prefs::kStabilityLaunchCount, 0); |
| 422 | local_state->SetInteger(prefs::kStabilityCrashCount, 0); |
| 423 | |
| 424 | local_state->SetInteger(prefs::kStabilityPageLoadCount, 0); |
| 425 | local_state->SetInteger(prefs::kStabilityRendererCrashCount, 0); |
| 426 | local_state->SetInteger(prefs::kStabilityRendererHangCount, 0); |
| 427 | |
[email protected] | 9165f74 | 2010-03-10 22:55:01 | [diff] [blame] | 428 | local_state->SetInt64(prefs::kStabilityLaunchTimeSec, 0); |
| 429 | local_state->SetInt64(prefs::kStabilityLastTimestampSec, 0); |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 430 | |
| 431 | local_state->ClearPref(prefs::kStabilityPluginStats); |
[email protected] | ae155cb9 | 2009-06-19 06:10:37 | [diff] [blame] | 432 | |
| 433 | ListValue* unsent_initial_logs = local_state->GetMutableList( |
| 434 | prefs::kMetricsInitialLogs); |
| 435 | unsent_initial_logs->Clear(); |
| 436 | |
| 437 | ListValue* unsent_ongoing_logs = local_state->GetMutableList( |
| 438 | prefs::kMetricsOngoingLogs); |
| 439 | unsent_ongoing_logs->Clear(); |
[email protected] | 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() |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 443 | : recording_active_(false), |
| 444 | reporting_active_(false), |
| 445 | user_permits_upload_(false), |
| 446 | server_permits_upload_(true), |
| 447 | state_(INITIALIZED), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 448 | current_fetch_(NULL), |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 449 | idle_since_last_transmission_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | next_window_id_(0), |
[email protected] | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 451 | ALLOW_THIS_IN_INITIALIZER_LIST(log_sender_factory_(this)), |
| 452 | ALLOW_THIS_IN_INITIALIZER_LIST(state_saver_factory_(this)), |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 453 | interlog_duration_(TimeDelta::FromSeconds(kInitialInterlogDuration)), |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 454 | log_event_limit_(kInitialEventLimit), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 455 | timer_pending_(false) { |
| 456 | DCHECK(IsSingleThreaded()); |
| 457 | InitializeMetricsState(); |
| 458 | } |
| 459 | |
| 460 | MetricsService::~MetricsService() { |
| 461 | SetRecording(false); |
| 462 | } |
| 463 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 464 | void MetricsService::SetUserPermitsUpload(bool enabled) { |
| 465 | HandleIdleSinceLastTransmission(false); |
| 466 | user_permits_upload_ = enabled; |
| 467 | } |
| 468 | |
| 469 | void MetricsService::Start() { |
| 470 | SetRecording(true); |
| 471 | SetReporting(true); |
| 472 | } |
| 473 | |
| 474 | void MetricsService::StartRecordingOnly() { |
| 475 | SetRecording(true); |
| 476 | SetReporting(false); |
| 477 | } |
| 478 | |
| 479 | void MetricsService::Stop() { |
| 480 | SetReporting(false); |
| 481 | SetRecording(false); |
| 482 | } |
| 483 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 484 | void MetricsService::SetRecording(bool enabled) { |
| 485 | DCHECK(IsSingleThreaded()); |
| 486 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 487 | if (enabled == recording_active_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 488 | return; |
| 489 | |
| 490 | if (enabled) { |
[email protected] | b0c819f | 2009-03-08 04:52:15 | [diff] [blame] | 491 | if (client_id_.empty()) { |
| 492 | PrefService* pref = g_browser_process->local_state(); |
| 493 | DCHECK(pref); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 494 | client_id_ = pref->GetString(prefs::kMetricsClientID); |
[email protected] | b0c819f | 2009-03-08 04:52:15 | [diff] [blame] | 495 | if (client_id_.empty()) { |
| 496 | client_id_ = GenerateClientID(); |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 497 | pref->SetString(prefs::kMetricsClientID, client_id_); |
[email protected] | b0c819f | 2009-03-08 04:52:15 | [diff] [blame] | 498 | |
| 499 | // Might as well make a note of how long this ID has existed |
| 500 | pref->SetString(prefs::kMetricsClientIDTimestamp, |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 501 | base::Int64ToString(Time::Now().ToTimeT())); |
[email protected] | b0c819f | 2009-03-08 04:52:15 | [diff] [blame] | 502 | } |
| 503 | } |
[email protected] | 157d547 | 2009-11-05 22:31:03 | [diff] [blame] | 504 | child_process_logging::SetClientId(client_id_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 505 | StartRecording(); |
[email protected] | 005ef3e | 2009-05-22 20:55:46 | [diff] [blame] | 506 | |
[email protected] | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 507 | SetupNotifications(®istrar_, this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 508 | } else { |
[email protected] | 005ef3e | 2009-05-22 20:55:46 | [diff] [blame] | 509 | registrar_.RemoveAll(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 510 | PushPendingLogsToUnsentLists(); |
| 511 | DCHECK(!pending_log()); |
| 512 | if (state_ > INITIAL_LOG_READY && unsent_logs()) |
| 513 | state_ = SEND_OLD_INITIAL_LOGS; |
| 514 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 515 | recording_active_ = enabled; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 516 | } |
| 517 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 518 | bool MetricsService::recording_active() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 519 | DCHECK(IsSingleThreaded()); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 520 | return recording_active_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 521 | } |
| 522 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 523 | void MetricsService::SetReporting(bool enable) { |
| 524 | if (reporting_active_ != enable) { |
| 525 | reporting_active_ = enable; |
| 526 | if (reporting_active_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 527 | StartLogTransmissionTimer(); |
| 528 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | bool MetricsService::reporting_active() const { |
| 532 | DCHECK(IsSingleThreaded()); |
| 533 | return reporting_active_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 534 | } |
| 535 | |
[email protected] | 87ef9ea | 2011-02-26 03:15:15 | [diff] [blame] | 536 | // static |
| 537 | void MetricsService::SetupNotifications(NotificationRegistrar* registrar, |
| 538 | NotificationObserver* observer) { |
| 539 | registrar->Add(observer, NotificationType::BROWSER_OPENED, |
| 540 | NotificationService::AllSources()); |
| 541 | registrar->Add(observer, NotificationType::BROWSER_CLOSED, |
| 542 | NotificationService::AllSources()); |
| 543 | registrar->Add(observer, NotificationType::USER_ACTION, |
| 544 | NotificationService::AllSources()); |
| 545 | registrar->Add(observer, NotificationType::TAB_PARENTED, |
| 546 | NotificationService::AllSources()); |
| 547 | registrar->Add(observer, NotificationType::TAB_CLOSING, |
| 548 | NotificationService::AllSources()); |
| 549 | registrar->Add(observer, NotificationType::LOAD_START, |
| 550 | NotificationService::AllSources()); |
| 551 | registrar->Add(observer, NotificationType::LOAD_STOP, |
| 552 | NotificationService::AllSources()); |
| 553 | registrar->Add(observer, NotificationType::RENDERER_PROCESS_CLOSED, |
| 554 | NotificationService::AllSources()); |
| 555 | registrar->Add(observer, NotificationType::RENDERER_PROCESS_HANG, |
| 556 | NotificationService::AllSources()); |
| 557 | registrar->Add(observer, NotificationType::CHILD_PROCESS_HOST_CONNECTED, |
| 558 | NotificationService::AllSources()); |
| 559 | registrar->Add(observer, NotificationType::CHILD_INSTANCE_CREATED, |
| 560 | NotificationService::AllSources()); |
| 561 | registrar->Add(observer, NotificationType::CHILD_PROCESS_CRASHED, |
| 562 | NotificationService::AllSources()); |
| 563 | registrar->Add(observer, NotificationType::TEMPLATE_URL_MODEL_LOADED, |
| 564 | NotificationService::AllSources()); |
| 565 | registrar->Add(observer, NotificationType::OMNIBOX_OPENED_URL, |
| 566 | NotificationService::AllSources()); |
| 567 | registrar->Add(observer, NotificationType::BOOKMARK_MODEL_LOADED, |
| 568 | NotificationService::AllSources()); |
| 569 | } |
| 570 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 571 | void MetricsService::Observe(NotificationType type, |
| 572 | const NotificationSource& source, |
| 573 | const NotificationDetails& details) { |
| 574 | DCHECK(current_log_); |
| 575 | DCHECK(IsSingleThreaded()); |
| 576 | |
| 577 | if (!CanLogNotification(type, source, details)) |
| 578 | return; |
| 579 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 580 | switch (type.value) { |
| 581 | case NotificationType::USER_ACTION: |
[email protected] | afe3a167 | 2009-11-17 19:04:12 | [diff] [blame] | 582 | current_log_->RecordUserAction(*Details<const char*>(details).ptr()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 583 | break; |
| 584 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 585 | case NotificationType::BROWSER_OPENED: |
| 586 | case NotificationType::BROWSER_CLOSED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 587 | LogWindowChange(type, source, details); |
| 588 | break; |
| 589 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 590 | case NotificationType::TAB_PARENTED: |
| 591 | case NotificationType::TAB_CLOSING: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 592 | LogWindowChange(type, source, details); |
| 593 | break; |
| 594 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 595 | case NotificationType::LOAD_STOP: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 596 | LogLoadComplete(type, source, details); |
| 597 | break; |
| 598 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 599 | case NotificationType::LOAD_START: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 600 | LogLoadStarted(); |
| 601 | break; |
| 602 | |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 603 | case NotificationType::RENDERER_PROCESS_CLOSED: { |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 604 | RenderProcessHost::RendererClosedDetails* process_details = |
| 605 | Details<RenderProcessHost::RendererClosedDetails>(details).ptr(); |
[email protected] | 443b80e | 2010-12-14 00:42:23 | [diff] [blame] | 606 | if (process_details->status == |
| 607 | base::TERMINATION_STATUS_PROCESS_CRASHED || |
| 608 | process_details->status == |
| 609 | base::TERMINATION_STATUS_ABNORMAL_TERMINATION) { |
[email protected] | cd69619b | 2010-05-05 02:41:38 | [diff] [blame] | 610 | if (process_details->was_extension_renderer) { |
| 611 | LogExtensionRendererCrash(); |
| 612 | } else { |
| 613 | LogRendererCrash(); |
| 614 | } |
| 615 | } |
[email protected] | 1f08562 | 2009-12-04 05:33:45 | [diff] [blame] | 616 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 617 | break; |
| 618 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 619 | case NotificationType::RENDERER_PROCESS_HANG: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 620 | LogRendererHang(); |
| 621 | break; |
| 622 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 623 | case NotificationType::CHILD_PROCESS_HOST_CONNECTED: |
| 624 | case NotificationType::CHILD_PROCESS_CRASHED: |
| 625 | case NotificationType::CHILD_INSTANCE_CREATED: |
| 626 | LogChildProcessChange(type, source, details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 627 | break; |
| 628 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 629 | case NotificationType::TEMPLATE_URL_MODEL_LOADED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 630 | LogKeywords(Source<TemplateURLModel>(source).ptr()); |
| 631 | break; |
| 632 | |
[email protected] | 1226abb | 2010-06-10 18:01:28 | [diff] [blame] | 633 | case NotificationType::OMNIBOX_OPENED_URL: { |
| 634 | MetricsLog* current_log = current_log_->AsMetricsLog(); |
| 635 | DCHECK(current_log); |
| 636 | current_log->RecordOmniboxOpenedURL( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 637 | *Details<AutocompleteLog>(details).ptr()); |
| 638 | break; |
[email protected] | 1226abb | 2010-06-10 18:01:28 | [diff] [blame] | 639 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 640 | |
[email protected] | b61236c6 | 2009-04-09 22:43:55 | [diff] [blame] | 641 | case NotificationType::BOOKMARK_MODEL_LOADED: { |
| 642 | Profile* p = Source<Profile>(source).ptr(); |
| 643 | if (p) |
| 644 | LogBookmarks(p->GetBookmarkModel()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 645 | break; |
[email protected] | b61236c6 | 2009-04-09 22:43:55 | [diff] [blame] | 646 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 647 | default: |
[email protected] | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 648 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 649 | break; |
| 650 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 651 | |
| 652 | HandleIdleSinceLastTransmission(false); |
| 653 | |
| 654 | if (current_log_) |
[email protected] | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 655 | DVLOG(1) << "METRICS: NUMBER OF EVENTS = " << current_log_->num_events(); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | void MetricsService::HandleIdleSinceLastTransmission(bool in_idle) { |
| 659 | // If there wasn't a lot of action, maybe the computer was asleep, in which |
| 660 | // case, the log transmissions should have stopped. Here we start them up |
| 661 | // again. |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 662 | if (!in_idle && idle_since_last_transmission_) |
| 663 | StartLogTransmissionTimer(); |
| 664 | idle_since_last_transmission_ = in_idle; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | void MetricsService::RecordCleanShutdown() { |
| 668 | RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); |
| 669 | } |
| 670 | |
| 671 | void MetricsService::RecordStartOfSessionEnd() { |
| 672 | RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, false); |
| 673 | } |
| 674 | |
| 675 | void MetricsService::RecordCompletedSessionEnd() { |
| 676 | RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, true); |
| 677 | } |
| 678 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 679 | void MetricsService:: RecordBreakpadRegistration(bool success) { |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 680 | if (!success) |
[email protected] | 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 |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 690 | IncrementPrefValue(prefs::kStabilityDebuggerPresent); |
[email protected] | 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() { |
[email protected] | 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"; |
| 704 | #else |
| 705 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 706 | server_url_ = dist->GetStatsServerURL(); |
| 707 | #endif |
| 708 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 709 | PrefService* pref = g_browser_process->local_state(); |
| 710 | DCHECK(pref); |
| 711 | |
[email protected] | 225c5084 | 2010-01-19 21:19:13 | [diff] [blame] | 712 | if ((pref->GetInt64(prefs::kStabilityStatsBuildTime) |
| 713 | != MetricsLog::GetBuildTime()) || |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 714 | (pref->GetString(prefs::kStabilityStatsVersion) |
[email protected] | 225c5084 | 2010-01-19 21:19:13 | [diff] [blame] | 715 | != MetricsLog::GetVersionString())) { |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 716 | // This is a new version, so we don't want to confuse the stats about the |
| 717 | // old version with info that we upload. |
| 718 | DiscardOldStabilityStats(pref); |
| 719 | pref->SetString(prefs::kStabilityStatsVersion, |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 720 | MetricsLog::GetVersionString()); |
[email protected] | 225c5084 | 2010-01-19 21:19:13 | [diff] [blame] | 721 | pref->SetInt64(prefs::kStabilityStatsBuildTime, |
| 722 | MetricsLog::GetBuildTime()); |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 723 | } |
| 724 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 725 | // Update session ID |
| 726 | session_id_ = pref->GetInteger(prefs::kMetricsSessionID); |
| 727 | ++session_id_; |
| 728 | pref->SetInteger(prefs::kMetricsSessionID, session_id_); |
| 729 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 730 | // Stability bookkeeping |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 731 | IncrementPrefValue(prefs::kStabilityLaunchCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 732 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 733 | if (!pref->GetBoolean(prefs::kStabilityExitedCleanly)) { |
| 734 | IncrementPrefValue(prefs::kStabilityCrashCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 735 | } |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 736 | |
| 737 | // This will be set to 'true' if we exit cleanly. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 738 | pref->SetBoolean(prefs::kStabilityExitedCleanly, false); |
| 739 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 740 | if (!pref->GetBoolean(prefs::kStabilitySessionEndCompleted)) { |
| 741 | IncrementPrefValue(prefs::kStabilityIncompleteSessionEndCount); |
[email protected] | c9abf24 | 2009-07-18 06:00:38 | [diff] [blame] | 742 | // This is marked false when we get a WM_ENDSESSION. |
| 743 | pref->SetBoolean(prefs::kStabilitySessionEndCompleted, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 744 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 745 | |
[email protected] | 9165f74 | 2010-03-10 22:55:01 | [diff] [blame] | 746 | // Initialize uptime counters. |
| 747 | int64 startup_uptime = MetricsLog::GetIncrementalUptime(pref); |
[email protected] | ae393ec70 | 2010-06-27 16:23:14 | [diff] [blame] | 748 | DCHECK_EQ(0, startup_uptime); |
[email protected] | 9165f74 | 2010-03-10 22:55:01 | [diff] [blame] | 749 | // For backwards compatibility, leave this intact in case Omaha is checking |
| 750 | // them. prefs::kStabilityLastTimestampSec may also be useless now. |
| 751 | // TODO(jar): Delete these if they have no uses. |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 752 | pref->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); |
| 753 | |
| 754 | // Bookkeeping for the uninstall metrics. |
| 755 | IncrementLongPrefsValue(prefs::kUninstallLaunchCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 756 | |
| 757 | // Save profile metrics. |
| 758 | PrefService* prefs = g_browser_process->local_state(); |
| 759 | if (prefs) { |
| 760 | // Remove the current dictionary and store it for use when sending data to |
| 761 | // server. By removing the value we prune potentially dead profiles |
| 762 | // (and keys). All valid values are added back once services startup. |
| 763 | const DictionaryValue* profile_dictionary = |
| 764 | prefs->GetDictionary(prefs::kProfileMetrics); |
| 765 | if (profile_dictionary) { |
| 766 | // Do a deep copy of profile_dictionary since ClearPref will delete it. |
| 767 | profile_dictionary_.reset(static_cast<DictionaryValue*>( |
| 768 | profile_dictionary->DeepCopy())); |
| 769 | prefs->ClearPref(prefs::kProfileMetrics); |
| 770 | } |
| 771 | } |
| 772 | |
[email protected] | 9274524 | 2009-06-12 16:52:21 | [diff] [blame] | 773 | // Get stats on use of command line. |
| 774 | const CommandLine* command_line(CommandLine::ForCurrentProcess()); |
| 775 | size_t common_commands = 0; |
| 776 | if (command_line->HasSwitch(switches::kUserDataDir)) { |
| 777 | ++common_commands; |
| 778 | UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineDatDirCount", 1); |
| 779 | } |
| 780 | |
| 781 | if (command_line->HasSwitch(switches::kApp)) { |
| 782 | ++common_commands; |
| 783 | UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineAppModeCount", 1); |
| 784 | } |
| 785 | |
| 786 | UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineFlagCount", |
| 787 | command_line->GetSwitchCount()); |
| 788 | UMA_HISTOGRAM_COUNTS_100("Chrome.CommandLineUncommonFlagCount", |
| 789 | command_line->GetSwitchCount() - common_commands); |
| 790 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 791 | // Kick off the process of saving the state (so the uptime numbers keep |
| 792 | // getting updated) every n minutes. |
| 793 | ScheduleNextStateSave(); |
| 794 | } |
| 795 | |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 796 | void MetricsService::OnInitTaskComplete( |
| 797 | const std::string& hardware_class, |
[email protected] | 191eb3f7 | 2010-12-21 06:27:50 | [diff] [blame] | 798 | const std::vector<webkit::npapi::WebPluginInfo>& plugins) { |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 799 | DCHECK(state_ == INIT_TASK_SCHEDULED); |
| 800 | hardware_class_ = hardware_class; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 801 | plugins_ = plugins; |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 802 | if (state_ == INIT_TASK_SCHEDULED) |
| 803 | state_ = INIT_TASK_DONE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | std::string MetricsService::GenerateClientID() { |
[email protected] | 3469e7e | 2010-10-14 20:34:59 | [diff] [blame] | 807 | return guid::GenerateGUID(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 808 | } |
| 809 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 810 | //------------------------------------------------------------------------------ |
| 811 | // State save methods |
| 812 | |
| 813 | void MetricsService::ScheduleNextStateSave() { |
| 814 | state_saver_factory_.RevokeAll(); |
| 815 | |
| 816 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 817 | state_saver_factory_.NewRunnableMethod(&MetricsService::SaveLocalState), |
| 818 | kSaveStateInterval * 1000); |
| 819 | } |
| 820 | |
| 821 | void MetricsService::SaveLocalState() { |
| 822 | PrefService* pref = g_browser_process->local_state(); |
| 823 | if (!pref) { |
[email protected] | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 824 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 825 | return; |
| 826 | } |
| 827 | |
| 828 | RecordCurrentState(pref); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 829 | pref->ScheduleSavePersistentPrefs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 830 | |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 831 | // TODO(jar): Does this run down the batteries???? |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 832 | ScheduleNextStateSave(); |
| 833 | } |
| 834 | |
| 835 | |
| 836 | //------------------------------------------------------------------------------ |
| 837 | // Recording control methods |
| 838 | |
| 839 | void MetricsService::StartRecording() { |
| 840 | if (current_log_) |
| 841 | return; |
| 842 | |
| 843 | current_log_ = new MetricsLog(client_id_, session_id_); |
| 844 | if (state_ == INITIALIZED) { |
| 845 | // We only need to schedule that run once. |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 846 | state_ = INIT_TASK_SCHEDULED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 847 | |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 848 | // Schedules a task on the file thread for execution of slower |
| 849 | // initialization steps (such as plugin list generation) necessary |
| 850 | // for sending the initial log. This avoids blocking the main UI |
| 851 | // thread. |
[email protected] | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 852 | g_browser_process->file_thread()->message_loop()->PostDelayedTask(FROM_HERE, |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 853 | new InitTask(MessageLoop::current()), |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 854 | kInitialInterlogDuration * 1000 / 2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | |
[email protected] | 1226abb | 2010-06-10 18:01:28 | [diff] [blame] | 858 | void MetricsService::StopRecording(MetricsLogBase** log) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 859 | if (!current_log_) |
| 860 | return; |
| 861 | |
[email protected] | 49cd7e88 | 2011-02-01 03:54:36 | [diff] [blame] | 862 | current_log_->set_hardware_class(hardware_class_); // Adds to ongoing logs. |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 863 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 864 | // TODO(jar): Integrate bounds on log recording more consistently, so that we |
| 865 | // can stop recording logs that are too big much sooner. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 866 | if (current_log_->num_events() > log_event_limit_) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 867 | UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 868 | current_log_->num_events()); |
| 869 | current_log_->CloseLog(); |
| 870 | delete current_log_; |
[email protected] | 29463878 | 2008-09-24 00:22:41 | [diff] [blame] | 871 | current_log_ = NULL; |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 872 | StartRecording(); // Start trivial log to hold our histograms. |
| 873 | } |
| 874 | |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 875 | // Put incremental data (histogram deltas, and realtime stats deltas) at the |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 876 | // end of all log transmissions (initial log handles this separately). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 877 | // Don't bother if we're going to discard current_log_. |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 878 | if (log) { |
[email protected] | 49cd7e88 | 2011-02-01 03:54:36 | [diff] [blame] | 879 | // RecordIncrementalStabilityElements only exists on the derived |
| 880 | // MetricsLog class. |
| 881 | MetricsLog* current_log = current_log_->AsMetricsLog(); |
| 882 | DCHECK(current_log); |
[email protected] | 1226abb | 2010-06-10 18:01:28 | [diff] [blame] | 883 | current_log->RecordIncrementalStabilityElements(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 884 | RecordCurrentHistograms(); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 885 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 886 | |
| 887 | current_log_->CloseLog(); |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 888 | if (log) |
[email protected] | 49cd7e88 | 2011-02-01 03:54:36 | [diff] [blame] | 889 | *log = current_log_; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 890 | else |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 891 | delete current_log_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 892 | current_log_ = NULL; |
| 893 | } |
| 894 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 895 | void MetricsService::PushPendingLogsToUnsentLists() { |
| 896 | if (state_ < INITIAL_LOG_READY) |
[email protected] | 28ab7f9 | 2009-01-06 21:39:04 | [diff] [blame] | 897 | 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] | 898 | |
| 899 | if (pending_log()) { |
| 900 | PreparePendingLogText(); |
| 901 | if (state_ == INITIAL_LOG_READY) { |
| 902 | // We may race here, and send second copy of initial log later. |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 903 | unsent_initial_logs_.push_back(compressed_log_); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 904 | state_ = SEND_OLD_INITIAL_LOGS; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 905 | } else { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 906 | // TODO(jar): Verify correctness in other states, including sending unsent |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 907 | // initial logs. |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 908 | PushPendingLogTextToUnsentOngoingLogs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 909 | } |
| 910 | DiscardPendingLog(); |
| 911 | } |
| 912 | DCHECK(!pending_log()); |
| 913 | StopRecording(&pending_log_); |
| 914 | PreparePendingLogText(); |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 915 | PushPendingLogTextToUnsentOngoingLogs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 916 | DiscardPendingLog(); |
| 917 | StoreUnsentLogs(); |
| 918 | } |
| 919 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 920 | void MetricsService::PushPendingLogTextToUnsentOngoingLogs() { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 921 | // If UMA response told us not to upload, there's no need to save the pending |
| 922 | // log. It wasn't supposed to be uploaded anyway. |
| 923 | if (!server_permits_upload_) |
| 924 | return; |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 925 | if (compressed_log_.length() > |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 926 | static_cast<size_t>(kUploadLogAvoidRetransmitSize)) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 927 | UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 928 | static_cast<int>(compressed_log_.length())); |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 929 | return; |
| 930 | } |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 931 | unsent_ongoing_logs_.push_back(compressed_log_); |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 932 | } |
| 933 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 934 | //------------------------------------------------------------------------------ |
| 935 | // Transmission of logs methods |
| 936 | |
| 937 | void MetricsService::StartLogTransmissionTimer() { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 938 | // If we're not reporting, there's no point in starting a log transmission |
| 939 | // timer. |
| 940 | if (!reporting_active()) |
| 941 | return; |
| 942 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 943 | if (!current_log_) |
| 944 | return; // Recorder is shutdown. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 945 | |
| 946 | // If there is already a timer running, we leave it running. |
| 947 | // If timer_pending is true because the fetch is waiting for a response, |
| 948 | // we return for now and let the response handler start the timer. |
| 949 | if (timer_pending_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 950 | return; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 951 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 952 | // Before starting the timer, set timer_pending_ to true. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 953 | timer_pending_ = true; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 954 | |
| 955 | // Right before the UMA transmission gets started, there's one more thing we'd |
| 956 | // like to record: the histogram of memory usage, so we spawn a task to |
[email protected] | 134d74a0 | 2011-03-09 01:38:32 | [diff] [blame^] | 957 | // collect histograms from all the renderers and when that task is finished, |
| 958 | // it will call OnHistogramSynchronizationDone to continue processing. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 959 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 960 | log_sender_factory_. |
[email protected] | c9a3ef8 | 2009-05-28 22:02:46 | [diff] [blame] | 961 | NewRunnableMethod(&MetricsService::LogTransmissionTimerDone), |
[email protected] | 743ace4 | 2009-06-17 17:23:51 | [diff] [blame] | 962 | interlog_duration_.InMilliseconds()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 963 | } |
| 964 | |
[email protected] | c9a3ef8 | 2009-05-28 22:02:46 | [diff] [blame] | 965 | void MetricsService::LogTransmissionTimerDone() { |
[email protected] | c9a3ef8 | 2009-05-28 22:02:46 | [diff] [blame] | 966 | DCHECK(IsSingleThreaded()); |
| 967 | |
| 968 | // HistogramSynchronizer will Collect histograms from all renderers and it |
| 969 | // will call OnHistogramSynchronizationDone (if wait time elapses before it |
| 970 | // heard from all renderers, then also it will call |
| 971 | // OnHistogramSynchronizationDone). |
| 972 | |
| 973 | // Create a callback_task for OnHistogramSynchronizationDone. |
| 974 | Task* callback_task = log_sender_factory_.NewRunnableMethod( |
| 975 | &MetricsService::OnHistogramSynchronizationDone); |
| 976 | |
| 977 | // Set up the callback to task to call after we receive histograms from all |
| 978 | // renderer processes. Wait time specifies how long to wait before absolutely |
| 979 | // calling us back on the task. |
| 980 | HistogramSynchronizer::FetchRendererHistogramsAsynchronously( |
| 981 | MessageLoop::current(), callback_task, |
| 982 | kMaxHistogramGatheringWaitDuration); |
[email protected] | 134d74a0 | 2011-03-09 01:38:32 | [diff] [blame^] | 983 | |
| 984 | // Collect WebCore cache information to put into a histogram. |
| 985 | for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); |
| 986 | !i.IsAtEnd(); i.Advance()) |
| 987 | i.GetCurrentValue()->Send(new ViewMsg_GetCacheResourceStats()); |
[email protected] | c9a3ef8 | 2009-05-28 22:02:46 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | void MetricsService::OnHistogramSynchronizationDone() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 991 | DCHECK(IsSingleThreaded()); |
| 992 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 993 | // This function should only be called via timer, so timer_pending_ |
| 994 | // should be true. |
| 995 | DCHECK(timer_pending_); |
| 996 | timer_pending_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 997 | |
| 998 | DCHECK(!current_fetch_.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 999 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1000 | // If we're getting no notifications, then the log won't have much in it, and |
| 1001 | // it's possible the computer is about to go to sleep, so don't upload and |
| 1002 | // don't restart the transmission timer. |
| 1003 | if (idle_since_last_transmission_) |
| 1004 | return; |
| 1005 | |
| 1006 | // If somehow there is a fetch in progress, we return setting timer_pending_ |
| 1007 | // to true and hope things work out. |
| 1008 | if (current_fetch_.get()) { |
| 1009 | timer_pending_ = true; |
| 1010 | return; |
| 1011 | } |
| 1012 | |
| 1013 | // If uploads are forbidden by UMA response, there's no point in keeping |
| 1014 | // the current_log_, and the more often we delete it, the less likely it is |
| 1015 | // to expand forever. |
| 1016 | if (!server_permits_upload_ && current_log_) { |
| 1017 | StopRecording(NULL); |
| 1018 | StartRecording(); |
| 1019 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1020 | |
| 1021 | if (!current_log_) |
| 1022 | return; // Logging was disabled. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1023 | if (!reporting_active()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1024 | return; // Don't do work if we're not going to send anything now. |
| 1025 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1026 | MakePendingLog(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1027 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1028 | // MakePendingLog should have put something in the pending log, if it didn't, |
| 1029 | // we start the timer again, return and hope things work out. |
| 1030 | if (!pending_log()) { |
| 1031 | StartLogTransmissionTimer(); |
| 1032 | return; |
| 1033 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1034 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1035 | // If we're not supposed to upload any UMA data because the response or the |
| 1036 | // user said so, cancel the upload at this point, but start the timer. |
| 1037 | if (!TransmissionPermitted()) { |
| 1038 | DiscardPendingLog(); |
| 1039 | StartLogTransmissionTimer(); |
| 1040 | return; |
| 1041 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1042 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1043 | PrepareFetchWithPendingLog(); |
| 1044 | |
| 1045 | if (!current_fetch_.get()) { |
| 1046 | // Compression failed, and log discarded :-/. |
| 1047 | DiscardPendingLog(); |
| 1048 | StartLogTransmissionTimer(); // Maybe we'll do better next time |
| 1049 | // TODO(jar): If compression failed, we should have created a tiny log and |
| 1050 | // compressed that, so that we can signal that we're losing logs. |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | DCHECK(!timer_pending_); |
| 1055 | |
| 1056 | // The URL fetch is a like timer in that after a while we get called back |
| 1057 | // so we set timer_pending_ true just as we start the url fetch. |
| 1058 | timer_pending_ = true; |
| 1059 | current_fetch_->Start(); |
| 1060 | |
| 1061 | HandleIdleSinceLastTransmission(true); |
| 1062 | } |
| 1063 | |
| 1064 | |
| 1065 | void MetricsService::MakePendingLog() { |
| 1066 | if (pending_log()) |
| 1067 | return; |
| 1068 | |
| 1069 | switch (state_) { |
| 1070 | case INITIALIZED: |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 1071 | case INIT_TASK_SCHEDULED: // We should be further along by now. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1072 | DCHECK(false); |
| 1073 | return; |
| 1074 | |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 1075 | case INIT_TASK_DONE: |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1076 | // We need to wait for the initial log to be ready before sending |
| 1077 | // anything, because the server will tell us whether it wants to hear |
| 1078 | // from us. |
| 1079 | PrepareInitialLog(); |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 1080 | DCHECK(state_ == INIT_TASK_DONE); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1081 | RecallUnsentLogs(); |
| 1082 | state_ = INITIAL_LOG_READY; |
| 1083 | break; |
| 1084 | |
| 1085 | case SEND_OLD_INITIAL_LOGS: |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1086 | if (!unsent_initial_logs_.empty()) { |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1087 | compressed_log_ = unsent_initial_logs_.back(); |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1088 | break; |
| 1089 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1090 | state_ = SENDING_OLD_LOGS; |
| 1091 | // Fall through. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1092 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1093 | case SENDING_OLD_LOGS: |
| 1094 | if (!unsent_ongoing_logs_.empty()) { |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1095 | compressed_log_ = unsent_ongoing_logs_.back(); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1096 | break; |
| 1097 | } |
| 1098 | state_ = SENDING_CURRENT_LOGS; |
| 1099 | // Fall through. |
| 1100 | |
| 1101 | case SENDING_CURRENT_LOGS: |
| 1102 | StopRecording(&pending_log_); |
| 1103 | StartRecording(); |
| 1104 | break; |
| 1105 | |
| 1106 | default: |
[email protected] | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1107 | NOTREACHED(); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1108 | return; |
| 1109 | } |
| 1110 | |
| 1111 | DCHECK(pending_log()); |
| 1112 | } |
| 1113 | |
| 1114 | bool MetricsService::TransmissionPermitted() const { |
| 1115 | // If the user forbids uploading that's they're business, and we don't upload |
| 1116 | // anything. If the server forbids uploading, that's our business, so we take |
| 1117 | // that to mean it forbids current logs, but we still send up the inital logs |
| 1118 | // and any old logs. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1119 | if (!user_permits_upload_) |
| 1120 | return false; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1121 | if (server_permits_upload_) |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1122 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1123 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1124 | switch (state_) { |
| 1125 | case INITIAL_LOG_READY: |
| 1126 | case SEND_OLD_INITIAL_LOGS: |
| 1127 | case SENDING_OLD_LOGS: |
| 1128 | return true; |
| 1129 | |
| 1130 | case SENDING_CURRENT_LOGS: |
| 1131 | default: |
| 1132 | return false; |
[email protected] | 8c8824b | 2008-09-20 01:55:50 | [diff] [blame] | 1133 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1134 | } |
| 1135 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1136 | void MetricsService::PrepareInitialLog() { |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 1137 | DCHECK(state_ == INIT_TASK_DONE); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1138 | |
| 1139 | MetricsLog* log = new MetricsLog(client_id_, session_id_); |
[email protected] | 85ed9d4 | 2010-06-08 22:37:44 | [diff] [blame] | 1140 | log->set_hardware_class(hardware_class_); // Adds to initial log. |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 1141 | log->RecordEnvironment(plugins_, profile_dictionary_.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1142 | |
| 1143 | // Histograms only get written to current_log_, so setup for the write. |
[email protected] | 1226abb | 2010-06-10 18:01:28 | [diff] [blame] | 1144 | MetricsLogBase* save_log = current_log_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1145 | current_log_ = log; |
| 1146 | RecordCurrentHistograms(); // Into current_log_... which is really log. |
| 1147 | current_log_ = save_log; |
| 1148 | |
| 1149 | log->CloseLog(); |
| 1150 | DCHECK(!pending_log()); |
| 1151 | pending_log_ = log; |
| 1152 | } |
| 1153 | |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1154 | // static |
| 1155 | MetricsService::LogRecallStatus MetricsService::RecallUnsentLogsHelper( |
| 1156 | const ListValue& list, |
| 1157 | std::vector<std::string>* local_list) { |
| 1158 | DCHECK(local_list->empty()); |
| 1159 | if (list.GetSize() == 0) |
| 1160 | return MakeRecallStatusHistogram(LIST_EMPTY); |
| 1161 | if (list.GetSize() < 3) |
| 1162 | return MakeRecallStatusHistogram(LIST_SIZE_TOO_SMALL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1163 | |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1164 | // The size is stored at the beginning of the list. |
| 1165 | int size; |
| 1166 | bool valid = (*list.begin())->GetAsInteger(&size); |
| 1167 | if (!valid) |
| 1168 | return MakeRecallStatusHistogram(LIST_SIZE_MISSING); |
| 1169 | |
| 1170 | // Account for checksum and size included in the list. |
| 1171 | if (static_cast<unsigned int>(size) != |
| 1172 | list.GetSize() - kChecksumEntryCount) |
| 1173 | return MakeRecallStatusHistogram(LIST_SIZE_CORRUPTION); |
| 1174 | |
| 1175 | MD5Context ctx; |
| 1176 | MD5Init(&ctx); |
| 1177 | std::string encoded_log; |
| 1178 | std::string decoded_log; |
| 1179 | for (ListValue::const_iterator it = list.begin() + 1; |
| 1180 | it != list.end() - 1; ++it) { // Last element is the checksum. |
| 1181 | valid = (*it)->GetAsString(&encoded_log); |
| 1182 | if (!valid) { |
| 1183 | local_list->clear(); |
| 1184 | return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION); |
| 1185 | } |
| 1186 | |
| 1187 | MD5Update(&ctx, encoded_log.data(), encoded_log.length()); |
| 1188 | |
| 1189 | if (!base::Base64Decode(encoded_log, &decoded_log)) { |
| 1190 | local_list->clear(); |
| 1191 | return MakeRecallStatusHistogram(DECODE_FAIL); |
| 1192 | } |
| 1193 | local_list->push_back(decoded_log); |
| 1194 | } |
| 1195 | |
| 1196 | // Verify checksum. |
| 1197 | MD5Digest digest; |
| 1198 | MD5Final(&digest, &ctx); |
| 1199 | std::string recovered_md5; |
| 1200 | // We store the hash at the end of the list. |
| 1201 | valid = (*(list.end() - 1))->GetAsString(&recovered_md5); |
| 1202 | if (!valid) { |
| 1203 | local_list->clear(); |
| 1204 | return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION); |
| 1205 | } |
| 1206 | if (recovered_md5 != MD5DigestToBase16(digest)) { |
| 1207 | local_list->clear(); |
| 1208 | return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION); |
| 1209 | } |
| 1210 | return MakeRecallStatusHistogram(RECALL_SUCCESS); |
| 1211 | } |
| 1212 | void MetricsService::RecallUnsentLogs() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1213 | PrefService* local_state = g_browser_process->local_state(); |
| 1214 | DCHECK(local_state); |
| 1215 | |
| 1216 | ListValue* unsent_initial_logs = local_state->GetMutableList( |
| 1217 | prefs::kMetricsInitialLogs); |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1218 | RecallUnsentLogsHelper(*unsent_initial_logs, &unsent_initial_logs_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1219 | |
| 1220 | ListValue* unsent_ongoing_logs = local_state->GetMutableList( |
| 1221 | prefs::kMetricsOngoingLogs); |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1222 | RecallUnsentLogsHelper(*unsent_ongoing_logs, &unsent_ongoing_logs_); |
| 1223 | } |
| 1224 | |
| 1225 | // static |
| 1226 | void MetricsService::StoreUnsentLogsHelper( |
| 1227 | const std::vector<std::string>& local_list, |
| 1228 | const size_t kMaxLocalListSize, |
| 1229 | ListValue* list) { |
| 1230 | list->Clear(); |
| 1231 | size_t start = 0; |
| 1232 | if (local_list.size() > kMaxLocalListSize) |
| 1233 | start = local_list.size() - kMaxLocalListSize; |
| 1234 | DCHECK(start <= local_list.size()); |
| 1235 | if (local_list.size() == start) |
| 1236 | return; |
| 1237 | |
| 1238 | // Store size at the beginning of the list. |
| 1239 | list->Append(Value::CreateIntegerValue(local_list.size() - start)); |
| 1240 | |
| 1241 | MD5Context ctx; |
| 1242 | MD5Init(&ctx); |
| 1243 | std::string encoded_log; |
| 1244 | for (std::vector<std::string>::const_iterator it = local_list.begin() + start; |
| 1245 | it != local_list.end(); ++it) { |
| 1246 | // We encode the compressed log as Value::CreateStringValue() expects to |
| 1247 | // take a valid UTF8 string. |
| 1248 | if (!base::Base64Encode(*it, &encoded_log)) { |
| 1249 | MakeStoreStatusHistogram(ENCODE_FAIL); |
| 1250 | list->Clear(); |
| 1251 | return; |
| 1252 | } |
| 1253 | MD5Update(&ctx, encoded_log.data(), encoded_log.length()); |
| 1254 | list->Append(Value::CreateStringValue(encoded_log)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1255 | } |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1256 | |
| 1257 | // Append hash to the end of the list. |
| 1258 | MD5Digest digest; |
| 1259 | MD5Final(&digest, &ctx); |
| 1260 | list->Append(Value::CreateStringValue(MD5DigestToBase16(digest))); |
| 1261 | DCHECK(list->GetSize() >= 3); // Minimum of 3 elements (size, data, hash). |
[email protected] | 4e95d20 | 2010-07-24 01:47:56 | [diff] [blame] | 1262 | MakeStoreStatusHistogram(STORE_SUCCESS); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | void MetricsService::StoreUnsentLogs() { |
| 1266 | if (state_ < INITIAL_LOG_READY) |
| 1267 | return; // We never Recalled the prior unsent logs. |
| 1268 | |
| 1269 | PrefService* local_state = g_browser_process->local_state(); |
| 1270 | DCHECK(local_state); |
| 1271 | |
| 1272 | ListValue* unsent_initial_logs = local_state->GetMutableList( |
| 1273 | prefs::kMetricsInitialLogs); |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1274 | StoreUnsentLogsHelper(unsent_initial_logs_, kMaxInitialLogsPersisted, |
| 1275 | unsent_initial_logs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1276 | |
| 1277 | ListValue* unsent_ongoing_logs = local_state->GetMutableList( |
| 1278 | prefs::kMetricsOngoingLogs); |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1279 | StoreUnsentLogsHelper(unsent_ongoing_logs_, kMaxOngoingLogsPersisted, |
| 1280 | unsent_ongoing_logs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | void MetricsService::PreparePendingLogText() { |
| 1284 | DCHECK(pending_log()); |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1285 | if (!compressed_log_.empty()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1286 | return; |
[email protected] | 9ffcccf4 | 2009-09-15 22:19:18 | [diff] [blame] | 1287 | int text_size = pending_log_->GetEncodedLogSize(); |
| 1288 | |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1289 | std::string pending_log_text; |
| 1290 | // Leave room for the NULL terminator. |
| 1291 | pending_log_->GetEncodedLog(WriteInto(&pending_log_text, text_size + 1), |
[email protected] | 9ffcccf4 | 2009-09-15 22:19:18 | [diff] [blame] | 1292 | text_size); |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1293 | |
| 1294 | if (Bzip2Compress(pending_log_text, &compressed_log_)) { |
| 1295 | // Allow security conscious users to see all metrics logs that we send. |
[email protected] | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1296 | VLOG(1) << "COMPRESSED FOLLOWING METRICS LOG: " << pending_log_text; |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1297 | } else { |
| 1298 | LOG(DFATAL) << "Failed to compress log for transmission."; |
| 1299 | // We can't discard the logs as other caller functions expect that |
| 1300 | // |compressed_log_| not be empty. We can detect this failure at the server |
| 1301 | // after we transmit. |
| 1302 | compressed_log_ = "Unable to compress!"; |
| 1303 | MakeStoreStatusHistogram(COMPRESS_FAIL); |
| 1304 | return; |
| 1305 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1306 | } |
| 1307 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1308 | void MetricsService::PrepareFetchWithPendingLog() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1309 | DCHECK(pending_log()); |
| 1310 | DCHECK(!current_fetch_.get()); |
| 1311 | PreparePendingLogText(); |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1312 | DCHECK(!compressed_log_.empty()); |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1313 | |
[email protected] | 79bf0b7 | 2009-04-27 21:30:55 | [diff] [blame] | 1314 | current_fetch_.reset(new URLFetcher(GURL(WideToUTF16(server_url_)), |
| 1315 | URLFetcher::POST, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1316 | this)); |
| 1317 | current_fetch_->set_request_context(Profile::GetDefaultRequestContext()); |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1318 | current_fetch_->set_upload_data(kMetricsType, compressed_log_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1319 | } |
| 1320 | |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1321 | static const char* StatusToString(const net::URLRequestStatus& status) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1322 | switch (status.status()) { |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1323 | case net::URLRequestStatus::SUCCESS: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1324 | return "SUCCESS"; |
| 1325 | |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1326 | case net::URLRequestStatus::IO_PENDING: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1327 | return "IO_PENDING"; |
| 1328 | |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1329 | case net::URLRequestStatus::HANDLED_EXTERNALLY: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1330 | return "HANDLED_EXTERNALLY"; |
| 1331 | |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1332 | case net::URLRequestStatus::CANCELED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1333 | return "CANCELED"; |
| 1334 | |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1335 | case net::URLRequestStatus::FAILED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1336 | return "FAILED"; |
| 1337 | |
| 1338 | default: |
[email protected] | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1339 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1340 | return "Unknown"; |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | void MetricsService::OnURLFetchComplete(const URLFetcher* source, |
| 1345 | const GURL& url, |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 1346 | const net::URLRequestStatus& status, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1347 | int response_code, |
| 1348 | const ResponseCookies& cookies, |
| 1349 | const std::string& data) { |
| 1350 | DCHECK(timer_pending_); |
| 1351 | timer_pending_ = false; |
| 1352 | DCHECK(current_fetch_.get()); |
| 1353 | current_fetch_.reset(NULL); // We're not allowed to re-use it. |
| 1354 | |
| 1355 | // Confirm send so that we can move on. |
[email protected] | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1356 | VLOG(1) << "METRICS RESPONSE CODE: " << response_code |
| 1357 | << " status=" << StatusToString(status); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1358 | |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1359 | // Provide boolean for error recovery (allow us to ignore response_code). |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 1360 | bool discard_log = false; |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1361 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1362 | if (response_code != 200 && |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1363 | (compressed_log_.length() > |
| 1364 | static_cast<size_t>(kUploadLogAvoidRetransmitSize))) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 1365 | UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded", |
[email protected] | 46f89e14 | 2010-07-19 08:00:42 | [diff] [blame] | 1366 | static_cast<int>(compressed_log_.length())); |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1367 | discard_log = true; |
| 1368 | } else if (response_code == 400) { |
| 1369 | // Bad syntax. Retransmission won't work. |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 1370 | UMA_HISTOGRAM_COUNTS("UMA.Unacceptable_Log_Discarded", state_); |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1371 | discard_log = true; |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1372 | } |
| 1373 | |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1374 | if (response_code != 200 && !discard_log) { |
[email protected] | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1375 | VLOG(1) << "METRICS: transmission attempt returned a failure code: " |
| 1376 | << response_code << ". Verify network connectivity"; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1377 | HandleBadResponseCode(); |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1378 | } else { // Successful receipt (or we are discarding log). |
[email protected] | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1379 | VLOG(1) << "METRICS RESPONSE DATA: " << data; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1380 | switch (state_) { |
| 1381 | case INITIAL_LOG_READY: |
| 1382 | state_ = SEND_OLD_INITIAL_LOGS; |
| 1383 | break; |
| 1384 | |
| 1385 | case SEND_OLD_INITIAL_LOGS: |
| 1386 | DCHECK(!unsent_initial_logs_.empty()); |
| 1387 | unsent_initial_logs_.pop_back(); |
| 1388 | StoreUnsentLogs(); |
| 1389 | break; |
| 1390 | |
| 1391 | case SENDING_OLD_LOGS: |
| 1392 | DCHECK(!unsent_ongoing_logs_.empty()); |
| 1393 | unsent_ongoing_logs_.pop_back(); |
| 1394 | StoreUnsentLogs(); |
| 1395 | break; |
| 1396 | |
| 1397 | case SENDING_CURRENT_LOGS: |
| 1398 | break; |
| 1399 | |
| 1400 | default: |
[email protected] | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1401 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1402 | break; |
| 1403 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1404 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1405 | DiscardPendingLog(); |
[email protected] | 29be9255 | 2008-08-07 22:49:27 | [diff] [blame] | 1406 | // Since we sent a log, make sure our in-memory state is recorded to disk. |
| 1407 | PrefService* local_state = g_browser_process->local_state(); |
| 1408 | DCHECK(local_state); |
| 1409 | if (local_state) |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 1410 | local_state->ScheduleSavePersistentPrefs(); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1411 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 1412 | // Provide a default (free of exponetial backoff, other varances) in case |
| 1413 | // the server does not specify a value. |
| 1414 | interlog_duration_ = TimeDelta::FromSeconds(kMinSecondsPerLog); |
| 1415 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1416 | GetSettingsFromResponseData(data); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1417 | // Override server specified interlog delay if there are unsent logs to |
[email protected] | 29be9255 | 2008-08-07 22:49:27 | [diff] [blame] | 1418 | // transmit. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1419 | if (unsent_logs()) { |
| 1420 | DCHECK(state_ < SENDING_CURRENT_LOGS); |
| 1421 | interlog_duration_ = TimeDelta::FromSeconds(kUnsentLogDelay); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1422 | } |
| 1423 | } |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1424 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1425 | StartLogTransmissionTimer(); |
| 1426 | } |
| 1427 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1428 | void MetricsService::HandleBadResponseCode() { |
[email protected] | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1429 | VLOG(1) << "Verify your metrics logs are formatted correctly. Verify server " |
| 1430 | "is active at " << server_url_; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1431 | if (!pending_log()) { |
[email protected] | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1432 | VLOG(1) << "METRICS: Recorder shutdown during log transmission."; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1433 | } else { |
| 1434 | // Send progressively less frequently. |
| 1435 | DCHECK(kBackoff > 1.0); |
| 1436 | interlog_duration_ = TimeDelta::FromMicroseconds( |
| 1437 | static_cast<int64>(kBackoff * interlog_duration_.InMicroseconds())); |
| 1438 | |
| 1439 | if (kMaxBackoff * TimeDelta::FromSeconds(kMinSecondsPerLog) < |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1440 | interlog_duration_) { |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1441 | interlog_duration_ = kMaxBackoff * |
| 1442 | TimeDelta::FromSeconds(kMinSecondsPerLog); |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1443 | } |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1444 | |
[email protected] | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1445 | VLOG(1) << "METRICS: transmission retry being scheduled in " |
| 1446 | << interlog_duration_.InSeconds() << " seconds for " |
| 1447 | << compressed_log_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1448 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1449 | } |
| 1450 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1451 | void MetricsService::GetSettingsFromResponseData(const std::string& data) { |
| 1452 | // We assume that the file is structured as a block opened by <response> |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1453 | // and that inside response, there is a block opened by tag <chrome_config> |
| 1454 | // other tags are ignored for now except the content of <chrome_config>. |
[email protected] | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1455 | VLOG(1) << "METRICS: getting settings from response data: " << data; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1456 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1457 | int data_size = static_cast<int>(data.size()); |
| 1458 | if (data_size < 0) { |
[email protected] | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1459 | VLOG(1) << "METRICS: server response data bad size: " << data_size |
| 1460 | << "; aborting extraction of settings"; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1461 | return; |
| 1462 | } |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1463 | xmlDocPtr doc = xmlReadMemory(data.c_str(), data_size, "", NULL, 0); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1464 | // If the document is malformed, we just use the settings that were there. |
| 1465 | if (!doc) { |
[email protected] | 66620503 | 2010-10-21 20:56:58 | [diff] [blame] | 1466 | VLOG(1) << "METRICS: reading xml from server response data failed"; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1467 | return; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1468 | } |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1469 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1470 | xmlNodePtr top_node = xmlDocGetRootElement(doc), chrome_config_node = NULL; |
| 1471 | // Here, we find the chrome_config node by name. |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1472 | for (xmlNodePtr p = top_node->children; p; p = p->next) { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1473 | if (xmlStrEqual(p->name, BAD_CAST "chrome_config")) { |
| 1474 | chrome_config_node = p; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1475 | break; |
| 1476 | } |
| 1477 | } |
| 1478 | // If the server data is formatted wrong and there is no |
| 1479 | // config node where we expect, we just drop out. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1480 | if (chrome_config_node != NULL) |
| 1481 | GetSettingsFromChromeConfigNode(chrome_config_node); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1482 | xmlFreeDoc(doc); |
| 1483 | } |
| 1484 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1485 | void MetricsService::GetSettingsFromChromeConfigNode( |
| 1486 | xmlNodePtr chrome_config_node) { |
| 1487 | // Iterate through all children of the config node. |
| 1488 | for (xmlNodePtr current_node = chrome_config_node->children; |
| 1489 | current_node; |
| 1490 | current_node = current_node->next) { |
| 1491 | // If we find the upload tag, we appeal to another function |
| 1492 | // GetSettingsFromUploadNode to read all the data in it. |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1493 | if (xmlStrEqual(current_node->name, BAD_CAST "upload")) { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1494 | GetSettingsFromUploadNode(current_node); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1495 | continue; |
| 1496 | } |
| 1497 | } |
| 1498 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1499 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1500 | void MetricsService::InheritedProperties::OverwriteWhereNeeded( |
| 1501 | xmlNodePtr node) { |
| 1502 | xmlChar* salt_value = xmlGetProp(node, BAD_CAST "salt"); |
| 1503 | if (salt_value) // If the property isn't there, xmlGetProp returns NULL. |
| 1504 | salt = atoi(reinterpret_cast<char*>(salt_value)); |
| 1505 | // If the property isn't there, we keep the value the property had before |
| 1506 | |
| 1507 | xmlChar* denominator_value = xmlGetProp(node, BAD_CAST "denominator"); |
| 1508 | if (denominator_value) |
| 1509 | denominator = atoi(reinterpret_cast<char*>(denominator_value)); |
| 1510 | } |
| 1511 | |
| 1512 | void MetricsService::GetSettingsFromUploadNode(xmlNodePtr upload_node) { |
| 1513 | InheritedProperties props; |
| 1514 | GetSettingsFromUploadNodeRecursive(upload_node, props, "", true); |
| 1515 | } |
| 1516 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1517 | void MetricsService::GetSettingsFromUploadNodeRecursive( |
| 1518 | xmlNodePtr node, |
| 1519 | InheritedProperties props, |
| 1520 | std::string path_prefix, |
| 1521 | bool uploadOn) { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1522 | props.OverwriteWhereNeeded(node); |
| 1523 | |
| 1524 | // The bool uploadOn is set to true if the data represented by current |
| 1525 | // node should be uploaded. This gets inherited in the tree; the children |
| 1526 | // of a node that has already been rejected for upload get rejected for |
| 1527 | // upload. |
| 1528 | uploadOn = uploadOn && NodeProbabilityTest(node, props); |
| 1529 | |
| 1530 | // The path is a / separated list of the node names ancestral to the current |
| 1531 | // one. So, if you want to check if the current node has a certain name, |
| 1532 | // compare to name. If you want to check if it is a certan tag at a certain |
| 1533 | // place in the tree, compare to the whole path. |
| 1534 | std::string name = std::string(reinterpret_cast<const char*>(node->name)); |
| 1535 | std::string path = path_prefix + "/" + name; |
| 1536 | |
| 1537 | if (path == "/upload") { |
| 1538 | xmlChar* upload_interval_val = xmlGetProp(node, BAD_CAST "interval"); |
| 1539 | if (upload_interval_val) { |
| 1540 | interlog_duration_ = TimeDelta::FromSeconds( |
| 1541 | atoi(reinterpret_cast<char*>(upload_interval_val))); |
| 1542 | } |
| 1543 | |
| 1544 | server_permits_upload_ = uploadOn; |
[email protected] | 24d07e3 | 2010-07-10 00:31:27 | [diff] [blame] | 1545 | } else if (path == "/upload/logs") { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1546 | xmlChar* log_event_limit_val = xmlGetProp(node, BAD_CAST "event_limit"); |
| 1547 | if (log_event_limit_val) |
| 1548 | log_event_limit_ = atoi(reinterpret_cast<char*>(log_event_limit_val)); |
| 1549 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1550 | |
| 1551 | // Recursive call. If the node is a leaf i.e. if it ends in a "/>", then it |
| 1552 | // doesn't have children, so node->children is NULL, and this loop doesn't |
| 1553 | // call (that's how the recursion ends). |
| 1554 | for (xmlNodePtr child_node = node->children; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1555 | child_node; |
| 1556 | child_node = child_node->next) { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1557 | GetSettingsFromUploadNodeRecursive(child_node, props, path, uploadOn); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | bool MetricsService::NodeProbabilityTest(xmlNodePtr node, |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1562 | InheritedProperties props) const { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1563 | // Default value of probability on any node is 1, but recall that |
| 1564 | // its parents can already have been rejected for upload. |
| 1565 | double probability = 1; |
| 1566 | |
| 1567 | // If a probability is specified in the node, we use it instead. |
| 1568 | xmlChar* probability_value = xmlGetProp(node, BAD_CAST "probability"); |
| 1569 | if (probability_value) |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 1570 | probability = atoi(reinterpret_cast<char*>(probability_value)); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1571 | |
| 1572 | return ProbabilityTest(probability, props.salt, props.denominator); |
| 1573 | } |
| 1574 | |
| 1575 | bool MetricsService::ProbabilityTest(double probability, |
| 1576 | int salt, |
| 1577 | int denominator) const { |
| 1578 | // Okay, first we figure out how many of the digits of the |
| 1579 | // client_id_ we need in order to make a nice pseudorandomish |
| 1580 | // number in the range [0,denominator). Too many digits is |
| 1581 | // fine. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1582 | |
| 1583 | // n is the length of the client_id_ string |
| 1584 | size_t n = client_id_.size(); |
| 1585 | |
| 1586 | // idnumber is a positive integer generated from the client_id_. |
| 1587 | // It plus salt is going to give us our pseudorandom number. |
| 1588 | int idnumber = 0; |
| 1589 | const char* client_id_c_str = client_id_.c_str(); |
| 1590 | |
| 1591 | // Here we hash the relevant digits of the client_id_ |
| 1592 | // string somehow to get a big integer idnumber (could be negative |
| 1593 | // from wraparound) |
| 1594 | int big = 1; |
[email protected] | 5ed7334 | 2009-03-18 17:39:43 | [diff] [blame] | 1595 | int last_pos = n - 1; |
| 1596 | for (size_t j = 0; j < n; ++j) { |
| 1597 | idnumber += static_cast<int>(client_id_c_str[last_pos - j]) * big; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1598 | big *= 10; |
| 1599 | } |
| 1600 | |
| 1601 | // Mod id number by denominator making sure to get a non-negative |
| 1602 | // answer. |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1603 | idnumber = ((idnumber % denominator) + denominator) % denominator; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1604 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1605 | // ((idnumber + salt) % denominator) / denominator is in the range [0,1] |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1606 | // if it's less than probability we call that an affirmative coin |
| 1607 | // toss. |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1608 | return static_cast<double>((idnumber + salt) % denominator) < |
| 1609 | probability * denominator; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1610 | } |
| 1611 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1612 | void MetricsService::LogWindowChange(NotificationType type, |
| 1613 | const NotificationSource& source, |
| 1614 | const NotificationDetails& details) { |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1615 | int controller_id = -1; |
| 1616 | uintptr_t window_or_tab = source.map_key(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1617 | MetricsLog::WindowEventType window_type; |
| 1618 | |
| 1619 | // Note: since we stop all logging when a single OTR session is active, it is |
| 1620 | // possible that we start getting notifications about a window that we don't |
| 1621 | // know about. |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1622 | if (window_map_.find(window_or_tab) == window_map_.end()) { |
| 1623 | controller_id = next_window_id_++; |
| 1624 | window_map_[window_or_tab] = controller_id; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1625 | } else { |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1626 | controller_id = window_map_[window_or_tab]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1627 | } |
[email protected] | 9274524 | 2009-06-12 16:52:21 | [diff] [blame] | 1628 | DCHECK_NE(controller_id, -1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1629 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1630 | switch (type.value) { |
| 1631 | case NotificationType::TAB_PARENTED: |
| 1632 | case NotificationType::BROWSER_OPENED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1633 | window_type = MetricsLog::WINDOW_CREATE; |
| 1634 | break; |
| 1635 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1636 | case NotificationType::TAB_CLOSING: |
| 1637 | case NotificationType::BROWSER_CLOSED: |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1638 | window_map_.erase(window_map_.find(window_or_tab)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1639 | window_type = MetricsLog::WINDOW_DESTROY; |
| 1640 | break; |
| 1641 | |
| 1642 | default: |
[email protected] | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1643 | NOTREACHED(); |
[email protected] | 68d74f0 | 2009-02-13 01:36:50 | [diff] [blame] | 1644 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1645 | } |
| 1646 | |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1647 | // TODO(brettw) we should have some kind of ID for the parent. |
| 1648 | current_log_->RecordWindowEvent(window_type, controller_id, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | void MetricsService::LogLoadComplete(NotificationType type, |
| 1652 | const NotificationSource& source, |
| 1653 | const NotificationDetails& details) { |
| 1654 | if (details == NotificationService::NoDetails()) |
| 1655 | return; |
| 1656 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1657 | // TODO(jar): There is a bug causing this to be called too many times, and |
| 1658 | // the log overflows. For now, we won't record these events. |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 1659 | UMA_HISTOGRAM_COUNTS("UMA.LogLoadComplete called", 1); |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1660 | return; |
| 1661 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1662 | const Details<LoadNotificationDetails> load_details(details); |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1663 | int controller_id = window_map_[details.map_key()]; |
| 1664 | current_log_->RecordLoadEvent(controller_id, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1665 | load_details->url(), |
| 1666 | load_details->origin(), |
| 1667 | load_details->session_index(), |
| 1668 | load_details->load_time()); |
| 1669 | } |
| 1670 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1671 | void MetricsService::IncrementPrefValue(const char* path) { |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1672 | PrefService* pref = g_browser_process->local_state(); |
| 1673 | DCHECK(pref); |
| 1674 | int value = pref->GetInteger(path); |
| 1675 | pref->SetInteger(path, value + 1); |
| 1676 | } |
| 1677 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1678 | void MetricsService::IncrementLongPrefsValue(const char* path) { |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1679 | PrefService* pref = g_browser_process->local_state(); |
| 1680 | DCHECK(pref); |
| 1681 | int64 value = pref->GetInt64(path); |
[email protected] | b42c5e4 | 2010-06-03 20:43:25 | [diff] [blame] | 1682 | pref->SetInt64(path, value + 1); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1683 | } |
| 1684 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1685 | void MetricsService::LogLoadStarted() { |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1686 | IncrementPrefValue(prefs::kStabilityPageLoadCount); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1687 | IncrementLongPrefsValue(prefs::kUninstallMetricsPageLoadCount); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 1688 | // We need to save the prefs, as page load count is a critical stat, and it |
| 1689 | // might be lost due to a crash :-(. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1690 | } |
| 1691 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1692 | void MetricsService::LogRendererCrash() { |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1693 | IncrementPrefValue(prefs::kStabilityRendererCrashCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1694 | } |
| 1695 | |
[email protected] | 1f08562 | 2009-12-04 05:33:45 | [diff] [blame] | 1696 | void MetricsService::LogExtensionRendererCrash() { |
| 1697 | IncrementPrefValue(prefs::kStabilityExtensionRendererCrashCount); |
| 1698 | } |
| 1699 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1700 | void MetricsService::LogRendererHang() { |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1701 | IncrementPrefValue(prefs::kStabilityRendererHangCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1702 | } |
| 1703 | |
[email protected] | c1834a9 | 2011-01-21 18:21:03 | [diff] [blame] | 1704 | #if defined(OS_CHROMEOS) |
| 1705 | void MetricsService::LogChromeOSCrash(const std::string &crash_type) { |
| 1706 | if (crash_type == "user") |
| 1707 | IncrementPrefValue(prefs::kStabilityOtherUserCrashCount); |
| 1708 | else if (crash_type == "kernel") |
| 1709 | IncrementPrefValue(prefs::kStabilityKernelCrashCount); |
| 1710 | else if (crash_type == "uncleanshutdown") |
| 1711 | IncrementPrefValue(prefs::kStabilitySystemUncleanShutdownCount); |
| 1712 | else |
| 1713 | NOTREACHED() << "Unexpected Chrome OS crash type " << crash_type; |
| 1714 | // Wake up metrics logs sending if necessary now that new |
| 1715 | // log data is available. |
| 1716 | HandleIdleSinceLastTransmission(false); |
| 1717 | } |
| 1718 | #endif // OS_CHROMEOS |
| 1719 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1720 | void MetricsService::LogChildProcessChange( |
| 1721 | NotificationType type, |
| 1722 | const NotificationSource& source, |
| 1723 | const NotificationDetails& details) { |
[email protected] | 0d84c5d | 2009-10-09 01:10:42 | [diff] [blame] | 1724 | Details<ChildProcessInfo> child_details(details); |
| 1725 | const std::wstring& child_name = child_details->name(); |
| 1726 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1727 | if (child_process_stats_buffer_.find(child_name) == |
| 1728 | child_process_stats_buffer_.end()) { |
[email protected] | 0d84c5d | 2009-10-09 01:10:42 | [diff] [blame] | 1729 | child_process_stats_buffer_[child_name] = |
| 1730 | ChildProcessStats(child_details->type()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1731 | } |
| 1732 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1733 | ChildProcessStats& stats = child_process_stats_buffer_[child_name]; |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1734 | switch (type.value) { |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1735 | case NotificationType::CHILD_PROCESS_HOST_CONNECTED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1736 | stats.process_launches++; |
| 1737 | break; |
| 1738 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1739 | case NotificationType::CHILD_INSTANCE_CREATED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1740 | stats.instances++; |
| 1741 | break; |
| 1742 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1743 | case NotificationType::CHILD_PROCESS_CRASHED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1744 | stats.process_crashes++; |
[email protected] | 1f08562 | 2009-12-04 05:33:45 | [diff] [blame] | 1745 | // Exclude plugin crashes from the count below because we report them via |
| 1746 | // a separate UMA metric. |
| 1747 | if (child_details->type() != ChildProcessInfo::PLUGIN_PROCESS) { |
| 1748 | IncrementPrefValue(prefs::kStabilityChildProcessCrashCount); |
| 1749 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1750 | break; |
| 1751 | |
| 1752 | default: |
[email protected] | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1753 | NOTREACHED() << "Unexpected notification type " << type.value; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1754 | return; |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | // Recursively counts the number of bookmarks and folders in node. |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 1759 | static void CountBookmarks(const BookmarkNode* node, |
| 1760 | int* bookmarks, |
| 1761 | int* folders) { |
[email protected] | 037db00 | 2009-10-19 20:06:08 | [diff] [blame] | 1762 | if (node->type() == BookmarkNode::URL) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1763 | (*bookmarks)++; |
| 1764 | else |
| 1765 | (*folders)++; |
| 1766 | for (int i = 0; i < node->GetChildCount(); ++i) |
| 1767 | CountBookmarks(node->GetChild(i), bookmarks, folders); |
| 1768 | } |
| 1769 | |
[email protected] | b3c33d46 | 2009-06-26 22:29:20 | [diff] [blame] | 1770 | void MetricsService::LogBookmarks(const BookmarkNode* node, |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1771 | const char* num_bookmarks_key, |
| 1772 | const char* num_folders_key) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1773 | DCHECK(node); |
| 1774 | int num_bookmarks = 0; |
| 1775 | int num_folders = 0; |
| 1776 | CountBookmarks(node, &num_bookmarks, &num_folders); |
| 1777 | num_folders--; // Don't include the root folder in the count. |
| 1778 | |
| 1779 | PrefService* pref = g_browser_process->local_state(); |
| 1780 | DCHECK(pref); |
| 1781 | pref->SetInteger(num_bookmarks_key, num_bookmarks); |
| 1782 | pref->SetInteger(num_folders_key, num_folders); |
| 1783 | } |
| 1784 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 1785 | void MetricsService::LogBookmarks(BookmarkModel* model) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1786 | DCHECK(model); |
| 1787 | LogBookmarks(model->GetBookmarkBarNode(), |
| 1788 | prefs::kNumBookmarksOnBookmarkBar, |
| 1789 | prefs::kNumFoldersOnBookmarkBar); |
| 1790 | LogBookmarks(model->other_node(), |
| 1791 | prefs::kNumBookmarksInOtherBookmarkFolder, |
| 1792 | prefs::kNumFoldersInOtherBookmarkFolder); |
| 1793 | ScheduleNextStateSave(); |
| 1794 | } |
| 1795 | |
| 1796 | void MetricsService::LogKeywords(const TemplateURLModel* url_model) { |
| 1797 | DCHECK(url_model); |
| 1798 | |
| 1799 | PrefService* pref = g_browser_process->local_state(); |
| 1800 | DCHECK(pref); |
| 1801 | pref->SetInteger(prefs::kNumKeywords, |
| 1802 | static_cast<int>(url_model->GetTemplateURLs().size())); |
| 1803 | ScheduleNextStateSave(); |
| 1804 | } |
| 1805 | |
| 1806 | void MetricsService::RecordPluginChanges(PrefService* pref) { |
| 1807 | ListValue* plugins = pref->GetMutableList(prefs::kStabilityPluginStats); |
| 1808 | DCHECK(plugins); |
| 1809 | |
| 1810 | for (ListValue::iterator value_iter = plugins->begin(); |
| 1811 | value_iter != plugins->end(); ++value_iter) { |
| 1812 | if (!(*value_iter)->IsType(Value::TYPE_DICTIONARY)) { |
[email protected] | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1813 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1814 | continue; |
| 1815 | } |
| 1816 | |
| 1817 | DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*value_iter); |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1818 | std::string plugin_name; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1819 | plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name); |
[email protected] | 6470ee8f | 2009-03-03 20:46:40 | [diff] [blame] | 1820 | if (plugin_name.empty()) { |
[email protected] | a063c10 | 2010-07-22 22:20:19 | [diff] [blame] | 1821 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1822 | continue; |
| 1823 | } |
| 1824 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1825 | // TODO(viettrungluu): remove conversions |
| 1826 | if (child_process_stats_buffer_.find(UTF8ToWide(plugin_name)) == |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1827 | child_process_stats_buffer_.end()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1828 | continue; |
| 1829 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1830 | ChildProcessStats stats = |
| 1831 | child_process_stats_buffer_[UTF8ToWide(plugin_name)]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1832 | if (stats.process_launches) { |
| 1833 | int launches = 0; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1834 | plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1835 | launches += stats.process_launches; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1836 | plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, launches); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1837 | } |
| 1838 | if (stats.process_crashes) { |
| 1839 | int crashes = 0; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1840 | plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1841 | crashes += stats.process_crashes; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1842 | plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, crashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1843 | } |
| 1844 | if (stats.instances) { |
| 1845 | int instances = 0; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1846 | plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1847 | instances += stats.instances; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1848 | plugin_dict->SetInteger(prefs::kStabilityPluginInstances, instances); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1849 | } |
| 1850 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1851 | child_process_stats_buffer_.erase(UTF8ToWide(plugin_name)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | // Now go through and add dictionaries for plugins that didn't already have |
| 1855 | // reports in Local State. |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1856 | for (std::map<std::wstring, ChildProcessStats>::iterator cache_iter = |
| 1857 | child_process_stats_buffer_.begin(); |
| 1858 | cache_iter != child_process_stats_buffer_.end(); ++cache_iter) { |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1859 | ChildProcessStats stats = cache_iter->second; |
[email protected] | 0d84c5d | 2009-10-09 01:10:42 | [diff] [blame] | 1860 | |
| 1861 | // Insert only plugins information into the plugins list. |
| 1862 | if (ChildProcessInfo::PLUGIN_PROCESS != stats.process_type) |
| 1863 | continue; |
| 1864 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1865 | // TODO(viettrungluu): remove conversion |
| 1866 | std::string plugin_name = WideToUTF8(cache_iter->first); |
[email protected] | 0d84c5d | 2009-10-09 01:10:42 | [diff] [blame] | 1867 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1868 | DictionaryValue* plugin_dict = new DictionaryValue; |
| 1869 | |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1870 | plugin_dict->SetString(prefs::kStabilityPluginName, plugin_name); |
| 1871 | plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1872 | stats.process_launches); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1873 | plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1874 | stats.process_crashes); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1875 | plugin_dict->SetInteger(prefs::kStabilityPluginInstances, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1876 | stats.instances); |
| 1877 | plugins->Append(plugin_dict); |
| 1878 | } |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1879 | child_process_stats_buffer_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | bool MetricsService::CanLogNotification(NotificationType type, |
| 1883 | const NotificationSource& source, |
| 1884 | const NotificationDetails& details) { |
[email protected] | 2c910b7 | 2011-03-08 21:16:32 | [diff] [blame] | 1885 | // 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] | 1886 | // session visible. The problem is that we always notify using the orginal |
| 1887 | // profile in order to simplify notification processing. |
| 1888 | return !BrowserList::IsOffTheRecordSessionActive(); |
| 1889 | } |
| 1890 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 1891 | void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1892 | DCHECK(IsSingleThreaded()); |
| 1893 | |
| 1894 | PrefService* pref = g_browser_process->local_state(); |
| 1895 | DCHECK(pref); |
| 1896 | |
| 1897 | pref->SetBoolean(path, value); |
| 1898 | RecordCurrentState(pref); |
| 1899 | } |
| 1900 | |
| 1901 | void MetricsService::RecordCurrentState(PrefService* pref) { |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1902 | pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1903 | |
| 1904 | RecordPluginChanges(pref); |
| 1905 | } |
| 1906 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1907 | static bool IsSingleThreaded() { |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 1908 | static base::PlatformThreadId thread_id = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1909 | if (!thread_id) |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 1910 | thread_id = base::PlatformThread::CurrentId(); |
| 1911 | return base::PlatformThread::CurrentId() == thread_id; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1912 | } |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 1913 | |
| 1914 | #if defined(OS_CHROMEOS) |
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 1915 | void MetricsService::StartExternalMetrics() { |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 1916 | external_metrics_ = new chromeos::ExternalMetrics; |
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 1917 | external_metrics_->Start(); |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 1918 | } |
| 1919 | #endif |