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