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