blob: 86e9d281daaedbbfb94eece701cba7a9fca28bba [file] [log] [blame]
initial.commit09911bf2008-07-26 23:55:291// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31
32//------------------------------------------------------------------------------
33// Description of the life cycle of a instance of MetricsService.
34//
35// OVERVIEW
36//
37// A MetricsService instance is typically created at application startup. It
38// is the central controller for the acquisition of log data, and the automatic
39// transmission of that log data to an external server. Its major job is to
40// manage logs, grouping them for transmission, and transmitting them. As part
41// of its grouping, MS finalizes logs by including some just-in-time gathered
42// memory statistics, snapshotting the current stats of numerous histograms,
43// closing the logs, translating to XML text, and compressing the results for
44// transmission. Transmission includes submitting a compressed log as data in a
45// URL-get, and retransmitting (or retaining at process termination) if the
46// attempted transmission failed. Retention across process terminations is done
47// using the the PrefServices facilities. The format for the retained
48// logs (ones that never got transmitted) is always the uncompressed textual
49// representation.
50//
51// Logs fall into one of two categories: "Initial logs," and "ongoing logs."
52// There is at most one initial log sent for each complete run of the chrome
53// product (from startup, to browser shutdown). An initial log is generally
54// transmitted some short time (1 minute?) after startup, and includes stats
55// such as recent crash info, the number and types of plugins, etc. The
56// external server's response to the initial log conceptually tells
57// this MS if it should continue transmitting logs (during this session). The
58// server response can actually be much more detailed, and always includes (at
59// a minimum) how often additional ongoing logs should be sent.
60//
61// After the above initial log, a series of ongoing logs will be transmitted.
62// The first ongoing log actually begins to accumulate information stating when
63// the MS was first constructed. Note that even though the initial log is
64// commonly sent a full minute after startup, the initial log does not include
65// much in the way of user stats. The most common interlog period (delay)
66// is 5 minutes. That time period starts when the first user action causes a
67// logging event. This means that if there is no user action, there may be long
68// periods without any (ongoing) log transmissions. Ongoing log typically
69// contain very detailed records of user activities (ex: opened tab, closed
70// tab, fetched URL, maximized window, etc.) In addition, just before an
71// ongoing log is closed out, a call is made to gather memory statistics. Those
72// memory statistics are deposited into a histogram, and the log finalization
73// code is then called. In the finalization, a call to a Histogram server
74// acquires a list of all local histograms that have been flagged for upload
75// to the UMA server.
76//
77// When the browser shuts down, there will typically be a fragment of an ongoing
78// log that has not yet been transmitted. At shutdown time, that fragment
79// is closed (including snapshotting histograms), and converted to text. Note
80// that memory stats are not gathered during shutdown, as gathering *might* be
81// too time consuming. The textual representation of the fragment of the
82// ongoing log is then stored persistently as a string in the PrefServices, for
83// potential transmission during a future run of the product.
84//
85// There are two slightly abnormal shutdown conditions. There is a
86// "disconnected scenario," and a "really fast startup and shutdown" scenario.
87// In the "never connected" situation, the user has (during the running of the
88// process) never established an internet connection. As a result, attempts to
89// transmit the initial log have failed, and a lot(?) of data has accumulated in
90// the ongoing log (which didn't yet get closed, because there was never even a
91// contemplation of sending it). There is also a kindred "lost connection"
92// situation, where a loss of connection prevented an ongoing log from being
93// transmitted, and a (still open) log was stuck accumulating a lot(?) of data,
94// while the earlier log retried its transmission. In both of these
95// disconnected situations, two logs need to be, and are, persistently stored
96// for future transmission.
97//
98// The other unusual shutdown condition, termed "really fast startup and
99// shutdown," involves the deliberate user termination of the process before
100// the initial log is even formed or transmitted. In that situation, no logging
101// is done, but the historical crash statistics remain (unlogged) for inclusion
102// in a future run's initial log. (i.e., we don't lose crash stats).
103//
104// With the above overview, we can now describe the state machine's various
105// stats, based on the State enum specified in the state_ member. Those states
106// are:
107//
108// INITIALIZED, // Constructor was called.
109// PLUGIN_LIST_REQUESTED, // Waiting for DLL list to be loaded.
110// PLUGIN_LIST_ARRIVED, // Waiting for timer to send initial log.
111// INITIAL_LOG_READY, // Initial log generated, and waiting for reply.
112// SEND_OLD_INITIAL_LOGS, // Sending unsent logs from previous session.
113// SENDING_OLD_LOGS, // Sending unsent logs from previous session.
114// SENDING_CURRENT_LOGS, // Sending standard current logs as they accrue.
115//
116// In more detail, we have:
117//
118// INITIALIZED, // Constructor was called.
119// The MS has been constructed, but has taken no actions to compose the
120// initial log.
121//
122// PLUGIN_LIST_REQUESTED, // Waiting for DLL list to be loaded.
123// Typically about 30 seconds after startup, a task is sent to a second thread
124// to get the list of plugins. That task will (when complete) make an async
125// callback (via a Task) to indicate the completion.
126//
127// PLUGIN_LIST_ARRIVED, // Waiting for timer to send initial log.
128// The callback has arrived, and it is now possible for an initial log to be
129// created. This callback typically arrives back less than one second after
130// the task is dispatched.
131//
132// INITIAL_LOG_READY, // Initial log generated, and waiting for reply.
133// This state is entered only after an initial log has been composed, and
134// prepared for transmission. It is also the case that any previously unsent
135// logs have been loaded into instance variables for possible transmission.
136//
137// SEND_OLD_INITIAL_LOGS, // Sending unsent logs from previous session.
138// This state indicates that the initial log for this session has been
139// successfully sent and it is now time to send any "initial logs" that were
140// saved from previous sessions. Most commonly, there are none, but all old
141// logs that were "initial logs" must be sent before this state is exited.
142//
143// SENDING_OLD_LOGS, // Sending unsent logs from previous session.
144// This state indicates that there are no more unsent initial logs, and now any
145// ongoing logs from previous sessions should be transmitted. All such logs
146// will be transmitted before exiting this state, and proceeding with ongoing
147// logs from the current session (see next state).
148//
149// SENDING_CURRENT_LOGS, // Sending standard current logs as they accrue.
150// Current logs are being accumulated. Typically every 5 minutes a log is
151// closed and finalized for transmission, at the same time as a new log is
152// started.
153//
154// The progression through the above states is simple, and sequential, in the
155// most common use cases. States proceed from INITIAL to SENDING_CURRENT_LOGS,
156// and remain in the latter until shutdown.
157//
158// The one unusual case is when the user asks that we stop logging. When that
159// happens, any pending (transmission in progress) log is pushed into the list
160// of old unsent logs (the appropriate list, depending on whether it is an
161// initial log, or an ongoing log). An addition, any log that is currently
162// accumulating is also finalized, and pushed into the unsent log list. With
163// those pushed performed, we regress back to the SEND_OLD_INITIAL_LOGS state in
164// case the user enables log recording again during this session. This way
165// anything we have "pushed back" will be sent automatically if/when we progress
166// back to SENDING_CURRENT_LOG state.
167//
168// Also note that whenever the member variables containing unsent logs are
169// modified (i.e., when we send an old log), we mirror the list of logs into
170// the PrefServices. This ensures that IF we crash, we won't start up and
171// retransmit our old logs again.
172//
173// Due to race conditions, it is always possible that a log file could be sent
174// twice. For example, if a log file is sent, but not yet acknowledged by
175// the external server, and the user shuts down, then a copy of the log may be
176// saved for re-transmission. These duplicates could be filtered out server
177// side, but are not expected to be a significantly statistical problem.
178//
179//
180//------------------------------------------------------------------------------
181
182#include <windows.h>
183
184#include "chrome/browser/metrics_service.h"
185
186#include "base/histogram.h"
187#include "base/path_service.h"
188#include "base/string_util.h"
189#include "base/task.h"
initial.commit09911bf2008-07-26 23:55:29190#include "chrome/browser/bookmark_bar_model.h"
191#include "chrome/browser/browser.h"
192#include "chrome/browser/browser_list.h"
193#include "chrome/browser/browser_process.h"
194#include "chrome/browser/load_notification_details.h"
195#include "chrome/browser/memory_details.h"
196#include "chrome/browser/plugin_process_info.h"
197#include "chrome/browser/plugin_service.h"
198#include "chrome/browser/profile.h"
199#include "chrome/browser/render_process_host.h"
200#include "chrome/browser/template_url.h"
201#include "chrome/browser/template_url_model.h"
202#include "chrome/common/chrome_paths.h"
[email protected]252873ef2008-08-04 21:59:45203#include "chrome/common/libxml_utils.h"
initial.commit09911bf2008-07-26 23:55:29204#include "chrome/common/pref_names.h"
205#include "chrome/common/pref_service.h"
[email protected]6e93e522008-08-14 19:28:17206#include "chrome/installer/util/google_update_settings.h"
initial.commit09911bf2008-07-26 23:55:29207#include "googleurl/src/gurl.h"
208#include "net/base/load_flags.h"
209#include "third_party/bzip2/bzlib.h"
210
211// Check to see that we're being called on only one thread.
212static bool IsSingleThreaded();
213
214static const char kMetricsURL[] =
215 "https://toolbarqueries.google.com/firefox/metrics/collect";
216
217static const char kMetricsType[] = "application/vnd.mozilla.metrics.bz2";
218
219// The delay, in seconds, after startup before sending the first log message.
[email protected]252873ef2008-08-04 21:59:45220static const int kInitialInterlogDuration = 60; // one minute
221
222// The default maximum number of events in a log uploaded to the UMA server.
223// TODO(petersont): Honor the limit when the log is actually sent.
[email protected]68475e602008-08-22 03:21:15224static const int kInitialEventLimit = 600;
225
226// If an upload fails, and the transmission was over this byte count, then we
227// will discard the log, and not try to retransmit it. We also don't persist
228// the log to the prefs for transmission during the next chrome session if this
229// limit is exceeded.
230static const int kUploadLogAvoidRetransmitSize = 50000;
initial.commit09911bf2008-07-26 23:55:29231
232// When we have logs from previous Chrome sessions to send, how long should we
233// delay (in seconds) between each log transmission.
234static const int kUnsentLogDelay = 15; // 15 seconds
235
236// Minimum time a log typically exists before sending, in seconds.
237// This number is supplied by the server, but until we parse it out of a server
238// response, we use this duration to specify how long we should wait before
239// sending the next log. If the channel is busy, such as when there is a
240// failure during an attempt to transmit a previous log, then a log may wait
241// (and continue to accrue now log entries) for a much greater period of time.
242static const int kMinSecondsPerLog = 5 * 60; // five minutes
243
244// We accept suggestions from the log server for how long to wait between
245// submitting logs. We validate that this "suggestion" is at least the
246// following:
247static const int kMinSuggestedSecondsPerLog = 60;
248
249// When we don't succeed at transmitting a log to a server, we progressively
250// wait longer and longer before sending the next log. This backoff process
251// help reduce load on the server, and makes the amount of backoff vary between
252// clients so that a collision (server overload?) on retransmit is less likely.
253// The following is the constant we use to expand that inter-log duration.
254static const double kBackoff = 1.1;
255// We limit the maximum backoff to be no greater than some multiple of the
256// default kMinSecondsPerLog. The following is that maximum ratio.
257static const int kMaxBackoff = 10;
258
259// Interval, in seconds, between state saves.
260static const int kSaveStateInterval = 5 * 60; // five minutes
261
262// The number of "initial" logs we're willing to save, and hope to send during
263// a future Chrome session. Initial logs contain crash stats, and are pretty
264// small.
265static const size_t kMaxInitialLogsPersisted = 20;
266
267// The number of ongoing logs we're willing to save persistently, and hope to
268// send during a this or future sessions. Note that each log will be pretty
269// large, as presumably the related "initial" log wasn't sent (probably nothing
270// was, as the user was probably off-line). As a result, the log probably kept
271// accumulating while the "initial" log was stalled (pending_), and couldn't be
272// sent. As a result, we don't want to save too many of these mega-logs.
273// A "standard shutdown" will create a small log, including just the data that
274// was not yet been transmitted, and that is normal (to have exactly one
275// ongoing_log_ at startup).
276static const size_t kMaxOngoingLogsPersisted = 4;
277
278
279// Handles asynchronous fetching of memory details.
280// Will run the provided task after finished.
281class MetricsMemoryDetails : public MemoryDetails {
282 public:
283 explicit MetricsMemoryDetails(Task* completion) : completion_(completion) {}
284
285 virtual void OnDetailsAvailable() {
286 MessageLoop::current()->PostTask(FROM_HERE, completion_);
287 }
288
289 private:
290 Task* completion_;
291 DISALLOW_EVIL_CONSTRUCTORS(MetricsMemoryDetails);
292};
293
294class MetricsService::GetPluginListTaskComplete : public Task {
295 virtual void Run() {
296 g_browser_process->metrics_service()->OnGetPluginListTaskComplete();
297 }
298};
299
300class MetricsService::GetPluginListTask : public Task {
301 public:
302 explicit GetPluginListTask(MessageLoop* callback_loop)
303 : callback_loop_(callback_loop) {}
304
305 virtual void Run() {
306 std::vector<WebPluginInfo> plugins;
307 PluginService::GetInstance()->GetPlugins(false, &plugins);
308
309 callback_loop_->PostTask(FROM_HERE, new GetPluginListTaskComplete());
310 }
311
312 private:
313 MessageLoop* callback_loop_;
314};
315
316// static
317void MetricsService::RegisterPrefs(PrefService* local_state) {
318 DCHECK(IsSingleThreaded());
319 local_state->RegisterStringPref(prefs::kMetricsClientID, L"");
320 local_state->RegisterStringPref(prefs::kMetricsClientIDTimestamp, L"0");
321 local_state->RegisterStringPref(prefs::kStabilityLaunchTimeSec, L"0");
322 local_state->RegisterStringPref(prefs::kStabilityLastTimestampSec, L"0");
323 local_state->RegisterStringPref(prefs::kStabilityUptimeSec, L"0");
324 local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true);
325 local_state->RegisterBooleanPref(prefs::kStabilitySessionEndCompleted, true);
326 local_state->RegisterIntegerPref(prefs::kMetricsSessionID, -1);
327 local_state->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0);
328 local_state->RegisterIntegerPref(prefs::kStabilityCrashCount, 0);
329 local_state->RegisterIntegerPref(prefs::kStabilityIncompleteSessionEndCount,
330 0);
331 local_state->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0);
332 local_state->RegisterIntegerPref(prefs::kSecurityRendererOnSboxDesktop, 0);
333 local_state->RegisterIntegerPref(prefs::kSecurityRendererOnDefaultDesktop, 0);
334 local_state->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0);
335 local_state->RegisterIntegerPref(prefs::kStabilityRendererHangCount, 0);
[email protected]e73c01972008-08-13 00:18:24336 local_state->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationFail,
337 0);
338 local_state->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationSuccess,
339 0);
340 local_state->RegisterIntegerPref(prefs::kStabilityDebuggerPresent, 0);
341 local_state->RegisterIntegerPref(prefs::kStabilityDebuggerNotPresent, 0);
342
initial.commit09911bf2008-07-26 23:55:29343 local_state->RegisterDictionaryPref(prefs::kProfileMetrics);
344 local_state->RegisterIntegerPref(prefs::kNumBookmarksOnBookmarkBar, 0);
345 local_state->RegisterIntegerPref(prefs::kNumFoldersOnBookmarkBar, 0);
346 local_state->RegisterIntegerPref(prefs::kNumBookmarksInOtherBookmarkFolder,
347 0);
348 local_state->RegisterIntegerPref(prefs::kNumFoldersInOtherBookmarkFolder, 0);
349 local_state->RegisterIntegerPref(prefs::kNumKeywords, 0);
350 local_state->RegisterListPref(prefs::kMetricsInitialLogs);
351 local_state->RegisterListPref(prefs::kMetricsOngoingLogs);
352}
353
354MetricsService::MetricsService()
355 : recording_(false),
356 reporting_(true),
357 pending_log_(NULL),
358 pending_log_text_(""),
359 current_fetch_(NULL),
360 current_log_(NULL),
361 state_(INITIALIZED),
362 next_window_id_(0),
363 log_sender_factory_(this),
364 state_saver_factory_(this),
365 logged_samples_(),
[email protected]252873ef2008-08-04 21:59:45366 interlog_duration_(TimeDelta::FromSeconds(kInitialInterlogDuration)),
367 event_limit_(kInitialEventLimit),
initial.commit09911bf2008-07-26 23:55:29368 timer_pending_(false) {
369 DCHECK(IsSingleThreaded());
370 InitializeMetricsState();
371}
372
373MetricsService::~MetricsService() {
374 SetRecording(false);
375}
376
377void MetricsService::SetRecording(bool enabled) {
378 DCHECK(IsSingleThreaded());
379
380 if (enabled == recording_)
381 return;
382
383 if (enabled) {
384 StartRecording();
385 ListenerRegistration(true);
386 } else {
387 // Turn off all observers.
388 ListenerRegistration(false);
389 PushPendingLogsToUnsentLists();
390 DCHECK(!pending_log());
391 if (state_ > INITIAL_LOG_READY && unsent_logs())
392 state_ = SEND_OLD_INITIAL_LOGS;
393 }
394 recording_ = enabled;
395}
396
397bool MetricsService::IsRecording() const {
398 DCHECK(IsSingleThreaded());
399 return recording_;
400}
401
402bool MetricsService::EnableReporting(bool enable) {
403 bool done = GoogleUpdateSettings::SetCollectStatsConsent(enable);
404 if (!done) {
405 bool update_pref = GoogleUpdateSettings::GetCollectStatsConsent();
406 if (enable != update_pref) {
407 DLOG(INFO) << "METRICS: Unable to set crash report status to " << enable;
408 return false;
409 }
410 }
411 if (reporting_ != enable) {
412 reporting_ = enable;
413 if (reporting_)
414 StartLogTransmissionTimer();
415 }
416 return true;
417}
418
419void MetricsService::Observe(NotificationType type,
420 const NotificationSource& source,
421 const NotificationDetails& details) {
422 DCHECK(current_log_);
423 DCHECK(IsSingleThreaded());
424
425 if (!CanLogNotification(type, source, details))
426 return;
427
428 switch (type) {
429 case NOTIFY_USER_ACTION:
430 current_log_->RecordUserAction(*Details<const wchar_t*>(details).ptr());
431 break;
432
433 case NOTIFY_BROWSER_OPENED:
434 case NOTIFY_BROWSER_CLOSED:
435 LogWindowChange(type, source, details);
436 break;
437
[email protected]534e54b2008-08-13 15:40:09438 case NOTIFY_TAB_PARENTED:
initial.commit09911bf2008-07-26 23:55:29439 case NOTIFY_TAB_CLOSING:
440 LogWindowChange(type, source, details);
441 break;
442
443 case NOTIFY_LOAD_STOP:
444 LogLoadComplete(type, source, details);
445 break;
446
447 case NOTIFY_LOAD_START:
448 LogLoadStarted();
449 break;
450
451 case NOTIFY_RENDERER_PROCESS_TERMINATED:
452 if (!*Details<bool>(details).ptr())
453 LogRendererCrash();
454 break;
455
456 case NOTIFY_RENDERER_PROCESS_HANG:
457 LogRendererHang();
458 break;
459
460 case NOTIFY_RENDERER_PROCESS_IN_SBOX:
461 LogRendererInSandbox(*Details<bool>(details).ptr());
462 break;
463
464 case NOTIFY_PLUGIN_PROCESS_HOST_CONNECTED:
465 case NOTIFY_PLUGIN_PROCESS_CRASHED:
466 case NOTIFY_PLUGIN_INSTANCE_CREATED:
467 LogPluginChange(type, source, details);
468 break;
469
470 case TEMPLATE_URL_MODEL_LOADED:
471 LogKeywords(Source<TemplateURLModel>(source).ptr());
472 break;
473
474 case NOTIFY_OMNIBOX_OPENED_URL:
475 current_log_->RecordOmniboxOpenedURL(
476 *Details<AutocompleteLog>(details).ptr());
477 break;
478
479 case NOTIFY_BOOKMARK_MODEL_LOADED:
480 LogBookmarks(Source<Profile>(source)->GetBookmarkBarModel());
481 break;
482
483 default:
484 NOTREACHED();
485 break;
486 }
487 StartLogTransmissionTimer();
488}
489
490void MetricsService::RecordCleanShutdown() {
491 RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true);
492}
493
494void MetricsService::RecordStartOfSessionEnd() {
495 RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, false);
496}
497
498void MetricsService::RecordCompletedSessionEnd() {
499 RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, true);
500}
501
[email protected]e73c01972008-08-13 00:18:24502void MetricsService:: RecordBreakpadRegistration(bool success) {
[email protected]68475e602008-08-22 03:21:15503 if (!success)
[email protected]e73c01972008-08-13 00:18:24504 IncrementPrefValue(prefs::kStabilityBreakpadRegistrationFail);
505 else
506 IncrementPrefValue(prefs::kStabilityBreakpadRegistrationSuccess);
507}
508
509void MetricsService::RecordBreakpadHasDebugger(bool has_debugger) {
510 if (!has_debugger)
511 IncrementPrefValue(prefs::kStabilityDebuggerNotPresent);
512 else
[email protected]68475e602008-08-22 03:21:15513 IncrementPrefValue(prefs::kStabilityDebuggerPresent);
[email protected]e73c01972008-08-13 00:18:24514}
515
initial.commit09911bf2008-07-26 23:55:29516//------------------------------------------------------------------------------
517// private methods
518//------------------------------------------------------------------------------
519
520
521//------------------------------------------------------------------------------
522// Initialization methods
523
524void MetricsService::InitializeMetricsState() {
525 PrefService* pref = g_browser_process->local_state();
526 DCHECK(pref);
527
528 client_id_ = WideToUTF8(pref->GetString(prefs::kMetricsClientID));
529 if (client_id_.empty()) {
530 client_id_ = GenerateClientID();
531 pref->SetString(prefs::kMetricsClientID, UTF8ToWide(client_id_));
532
533 // Might as well make a note of how long this ID has existed
534 pref->SetString(prefs::kMetricsClientIDTimestamp,
535 Int64ToWString(Time::Now().ToTimeT()));
536 }
537
538 // Update session ID
539 session_id_ = pref->GetInteger(prefs::kMetricsSessionID);
540 ++session_id_;
541 pref->SetInteger(prefs::kMetricsSessionID, session_id_);
542
543 bool done = EnableReporting(GoogleUpdateSettings::GetCollectStatsConsent());
544 DCHECK(done);
545
546 // Stability bookkeeping
[email protected]e73c01972008-08-13 00:18:24547 IncrementPrefValue(prefs::kStabilityLaunchCount);
initial.commit09911bf2008-07-26 23:55:29548
[email protected]e73c01972008-08-13 00:18:24549 if (!pref->GetBoolean(prefs::kStabilityExitedCleanly)) {
550 IncrementPrefValue(prefs::kStabilityCrashCount);
initial.commit09911bf2008-07-26 23:55:29551 }
[email protected]e73c01972008-08-13 00:18:24552
553 // This will be set to 'true' if we exit cleanly.
initial.commit09911bf2008-07-26 23:55:29554 pref->SetBoolean(prefs::kStabilityExitedCleanly, false);
555
[email protected]e73c01972008-08-13 00:18:24556 if (!pref->GetBoolean(prefs::kStabilitySessionEndCompleted)) {
557 IncrementPrefValue(prefs::kStabilityIncompleteSessionEndCount);
initial.commit09911bf2008-07-26 23:55:29558 }
559 // This is marked false when we get a WM_ENDSESSION.
560 pref->SetBoolean(prefs::kStabilitySessionEndCompleted, true);
561
562 int64 last_start_time =
563 StringToInt64(pref->GetString(prefs::kStabilityLaunchTimeSec));
564 int64 last_end_time =
565 StringToInt64(pref->GetString(prefs::kStabilityLastTimestampSec));
566 int64 uptime =
567 StringToInt64(pref->GetString(prefs::kStabilityUptimeSec));
568
569 if (last_start_time && last_end_time) {
570 // TODO(JAR): Exclude sleep time. ... which must be gathered in UI loop.
571 uptime += last_end_time - last_start_time;
572 pref->SetString(prefs::kStabilityUptimeSec, Int64ToWString(uptime));
573 }
574 pref->SetString(prefs::kStabilityLaunchTimeSec,
575 Int64ToWString(Time::Now().ToTimeT()));
576
577 // Save profile metrics.
578 PrefService* prefs = g_browser_process->local_state();
579 if (prefs) {
580 // Remove the current dictionary and store it for use when sending data to
581 // server. By removing the value we prune potentially dead profiles
582 // (and keys). All valid values are added back once services startup.
583 const DictionaryValue* profile_dictionary =
584 prefs->GetDictionary(prefs::kProfileMetrics);
585 if (profile_dictionary) {
586 // Do a deep copy of profile_dictionary since ClearPref will delete it.
587 profile_dictionary_.reset(static_cast<DictionaryValue*>(
588 profile_dictionary->DeepCopy()));
589 prefs->ClearPref(prefs::kProfileMetrics);
590 }
591 }
592
593 // Kick off the process of saving the state (so the uptime numbers keep
594 // getting updated) every n minutes.
595 ScheduleNextStateSave();
596}
597
598void MetricsService::OnGetPluginListTaskComplete() {
599 DCHECK(state_ == PLUGIN_LIST_REQUESTED);
600 if (state_ == PLUGIN_LIST_REQUESTED)
601 state_ = PLUGIN_LIST_ARRIVED;
602}
603
604std::string MetricsService::GenerateClientID() {
605 const int kGUIDSize = 39;
606
607 GUID guid;
608 HRESULT guid_result = CoCreateGuid(&guid);
609 DCHECK(SUCCEEDED(guid_result));
610
611 std::wstring guid_string;
612 int result = StringFromGUID2(guid,
613 WriteInto(&guid_string, kGUIDSize), kGUIDSize);
614 DCHECK(result == kGUIDSize);
615
616 return WideToUTF8(guid_string.substr(1, guid_string.length() - 2));
617}
618
619
620//------------------------------------------------------------------------------
621// State save methods
622
623void MetricsService::ScheduleNextStateSave() {
624 state_saver_factory_.RevokeAll();
625
626 MessageLoop::current()->PostDelayedTask(FROM_HERE,
627 state_saver_factory_.NewRunnableMethod(&MetricsService::SaveLocalState),
628 kSaveStateInterval * 1000);
629}
630
631void MetricsService::SaveLocalState() {
632 PrefService* pref = g_browser_process->local_state();
633 if (!pref) {
634 NOTREACHED();
635 return;
636 }
637
638 RecordCurrentState(pref);
639 pref->ScheduleSavePersistentPrefs(g_browser_process->file_thread());
640
641 ScheduleNextStateSave();
642}
643
644
645//------------------------------------------------------------------------------
646// Recording control methods
647
648void MetricsService::StartRecording() {
649 if (current_log_)
650 return;
651
652 current_log_ = new MetricsLog(client_id_, session_id_);
653 if (state_ == INITIALIZED) {
654 // We only need to schedule that run once.
655 state_ = PLUGIN_LIST_REQUESTED;
656
657 // Make sure the plugin list is loaded before the inital log is sent, so
658 // that the main thread isn't blocked generating the list.
659 g_browser_process->file_thread()->message_loop()->PostDelayedTask(FROM_HERE,
660 new GetPluginListTask(MessageLoop::current()),
[email protected]252873ef2008-08-04 21:59:45661 kInitialInterlogDuration * 1000 / 2);
initial.commit09911bf2008-07-26 23:55:29662 }
663}
664
665void MetricsService::StopRecording(MetricsLog** log) {
666 if (!current_log_)
667 return;
668
[email protected]68475e602008-08-22 03:21:15669 // TODO(jar): Integrate bounds on log recording more consistently, so that we
670 // can stop recording logs that are too big much sooner.
671 if (current_log_->num_events() > kInitialEventLimit) {
672 UMA_HISTOGRAM_COUNTS(L"UMA.Discarded Log Events",
673 current_log_->num_events());
674 current_log_->CloseLog();
675 delete current_log_;
676 StartRecording(); // Start trivial log to hold our histograms.
677 }
678
initial.commit09911bf2008-07-26 23:55:29679 // Put incremental histogram data at the end of every log transmission.
680 // Don't bother if we're going to discard current_log_.
681 if (log)
682 RecordCurrentHistograms();
683
684 current_log_->CloseLog();
685 if (log) {
686 *log = current_log_;
687 } else {
688 delete current_log_;
689 }
690 current_log_ = NULL;
691}
692
693void MetricsService::ListenerRegistration(bool start_listening) {
694 AddOrRemoveObserver(this, NOTIFY_BROWSER_OPENED, start_listening);
695 AddOrRemoveObserver(this, NOTIFY_BROWSER_CLOSED, start_listening);
696 AddOrRemoveObserver(this, NOTIFY_USER_ACTION, start_listening);
[email protected]534e54b2008-08-13 15:40:09697 AddOrRemoveObserver(this, NOTIFY_TAB_PARENTED, start_listening);
initial.commit09911bf2008-07-26 23:55:29698 AddOrRemoveObserver(this, NOTIFY_TAB_CLOSING, start_listening);
699 AddOrRemoveObserver(this, NOTIFY_LOAD_START, start_listening);
700 AddOrRemoveObserver(this, NOTIFY_LOAD_STOP, start_listening);
701 AddOrRemoveObserver(this, NOTIFY_RENDERER_PROCESS_IN_SBOX, start_listening);
702 AddOrRemoveObserver(this, NOTIFY_RENDERER_PROCESS_TERMINATED,
703 start_listening);
704 AddOrRemoveObserver(this, NOTIFY_RENDERER_PROCESS_HANG, start_listening);
705 AddOrRemoveObserver(this, NOTIFY_PLUGIN_PROCESS_HOST_CONNECTED,
706 start_listening);
707 AddOrRemoveObserver(this, NOTIFY_PLUGIN_INSTANCE_CREATED, start_listening);
708 AddOrRemoveObserver(this, NOTIFY_PLUGIN_PROCESS_CRASHED, start_listening);
709 AddOrRemoveObserver(this, TEMPLATE_URL_MODEL_LOADED, start_listening);
710 AddOrRemoveObserver(this, NOTIFY_OMNIBOX_OPENED_URL, start_listening);
711 AddOrRemoveObserver(this, NOTIFY_BOOKMARK_MODEL_LOADED, start_listening);
712}
713
714// static
715void MetricsService::AddOrRemoveObserver(NotificationObserver* observer,
716 NotificationType type,
717 bool is_add) {
718 NotificationService* service = NotificationService::current();
719
720 if (is_add) {
721 service->AddObserver(observer, type, NotificationService::AllSources());
722 } else {
723 service->RemoveObserver(observer, type, NotificationService::AllSources());
724 }
725}
726
727void MetricsService::PushPendingLogsToUnsentLists() {
728 if (state_ < INITIAL_LOG_READY)
729 return; // We didn't and still don't have time to get DLL list etc.
730
731 if (pending_log()) {
732 PreparePendingLogText();
733 if (state_ == INITIAL_LOG_READY) {
734 // We may race here, and send second copy of initial log later.
735 unsent_initial_logs_.push_back(pending_log_text_);
736 state_ = SENDING_CURRENT_LOGS;
737 } else {
[email protected]68475e602008-08-22 03:21:15738 PushPendingLogTextToUnsentOngoingLogs();
initial.commit09911bf2008-07-26 23:55:29739 }
740 DiscardPendingLog();
741 }
742 DCHECK(!pending_log());
743 StopRecording(&pending_log_);
744 PreparePendingLogText();
[email protected]68475e602008-08-22 03:21:15745 PushPendingLogTextToUnsentOngoingLogs();
initial.commit09911bf2008-07-26 23:55:29746 DiscardPendingLog();
747 StoreUnsentLogs();
748}
749
[email protected]68475e602008-08-22 03:21:15750void MetricsService::PushPendingLogTextToUnsentOngoingLogs() {
751 if (pending_log_text_.length() > kUploadLogAvoidRetransmitSize) {
752 UMA_HISTOGRAM_COUNTS(L"UMA.Large Accumulated Log Not Persisted",
753 static_cast<int>(pending_log_text_.length()));
754 return;
755 }
756 unsent_ongoing_logs_.push_back(pending_log_text_);
757}
758
initial.commit09911bf2008-07-26 23:55:29759//------------------------------------------------------------------------------
760// Transmission of logs methods
761
762void MetricsService::StartLogTransmissionTimer() {
763 if (!current_log_)
764 return; // Recorder is shutdown.
765 if (timer_pending_ || !reporting_)
766 return;
767 // If there is no work to do, don't set a timer yet.
768 if (!current_log_->num_events() && !pending_log() && !unsent_logs())
769 return;
770 timer_pending_ = true;
771 MessageLoop::current()->PostDelayedTask(FROM_HERE,
772 log_sender_factory_.
773 NewRunnableMethod(&MetricsService::CollectMemoryDetails),
774 static_cast<int>(interlog_duration_.InMilliseconds()));
775}
776
777void MetricsService::TryToStartTransmission() {
778 DCHECK(IsSingleThreaded());
779
780 DCHECK(timer_pending_); // ONLY call via timer.
781
782 DCHECK(!current_fetch_.get());
783 if (current_fetch_.get())
784 return; // Redundant defensive coding.
785
786 timer_pending_ = false;
787
788 if (!current_log_)
789 return; // Logging was disabled.
790 if (!reporting_ )
791 return; // Don't do work if we're not going to send anything now.
792
793 if (!pending_log())
794 switch (state_) {
795 case INITIALIZED: // We must be further along by now.
796 DCHECK(false);
797 return;
798
799 case PLUGIN_LIST_REQUESTED:
800 StartLogTransmissionTimer();
801 return;
802
803 case PLUGIN_LIST_ARRIVED:
804 // We need to wait for the initial log to be ready before sending
805 // anything, because the server will tell us whether it wants to hear
806 // from us.
807 PrepareInitialLog();
808 DCHECK(state_ == PLUGIN_LIST_ARRIVED);
809 RecallUnsentLogs();
810 state_ = INITIAL_LOG_READY;
811 break;
812
813 case SEND_OLD_INITIAL_LOGS:
814 if (!unsent_initial_logs_.empty()) {
815 pending_log_text_ = unsent_initial_logs_.back();
816 break;
817 }
818 state_ = SENDING_OLD_LOGS;
819 // Fall through.
820
821 case SENDING_OLD_LOGS:
822 if (!unsent_ongoing_logs_.empty()) {
823 pending_log_text_ = unsent_ongoing_logs_.back();
824 break;
825 }
826 state_ = SENDING_CURRENT_LOGS;
827 // Fall through.
828
829 case SENDING_CURRENT_LOGS:
830 if (!current_log_->num_events())
831 return; // Nothing to send.
832 StopRecording(&pending_log_);
833 StartRecording();
834 break;
835
836 default:
837 DCHECK(false);
838 return;
839 }
840 DCHECK(pending_log());
841
842 PreparePendingLogForTransmission();
843 if (!current_fetch_.get())
844 return; // Compression failed, and log discarded :-/.
845
846 DCHECK(!timer_pending_);
847 timer_pending_ = true; // The URL fetch is a pseudo timer.
848 current_fetch_->Start();
849}
850
851void MetricsService::CollectMemoryDetails() {
852 Task* task = log_sender_factory_.
853 NewRunnableMethod(&MetricsService::TryToStartTransmission);
854 MetricsMemoryDetails* details = new MetricsMemoryDetails(task);
855 details->StartFetch();
856
857 // Collect WebCore cache information to put into a histogram.
858 for (RenderProcessHost::iterator it = RenderProcessHost::begin();
859 it != RenderProcessHost::end(); ++it) {
860 it->second->Send(new ViewMsg_GetCacheResourceStats());
861 }
862}
863
864void MetricsService::PrepareInitialLog() {
865 DCHECK(state_ == PLUGIN_LIST_ARRIVED);
866 std::vector<WebPluginInfo> plugins;
867 PluginService::GetInstance()->GetPlugins(false, &plugins);
868
869 MetricsLog* log = new MetricsLog(client_id_, session_id_);
870 log->RecordEnvironment(plugins, profile_dictionary_.get());
871
872 // Histograms only get written to current_log_, so setup for the write.
873 MetricsLog* save_log = current_log_;
874 current_log_ = log;
875 RecordCurrentHistograms(); // Into current_log_... which is really log.
876 current_log_ = save_log;
877
878 log->CloseLog();
879 DCHECK(!pending_log());
880 pending_log_ = log;
881}
882
883void MetricsService::RecallUnsentLogs() {
884 DCHECK(unsent_initial_logs_.empty());
885 DCHECK(unsent_ongoing_logs_.empty());
886
887 PrefService* local_state = g_browser_process->local_state();
888 DCHECK(local_state);
889
890 ListValue* unsent_initial_logs = local_state->GetMutableList(
891 prefs::kMetricsInitialLogs);
892 for (ListValue::iterator it = unsent_initial_logs->begin();
893 it != unsent_initial_logs->end(); ++it) {
894 std::wstring wide_log;
895 (*it)->GetAsString(&wide_log);
896 unsent_initial_logs_.push_back(WideToUTF8(wide_log));
897 }
898
899 ListValue* unsent_ongoing_logs = local_state->GetMutableList(
900 prefs::kMetricsOngoingLogs);
901 for (ListValue::iterator it = unsent_ongoing_logs->begin();
902 it != unsent_ongoing_logs->end(); ++it) {
903 std::wstring wide_log;
904 (*it)->GetAsString(&wide_log);
905 unsent_ongoing_logs_.push_back(WideToUTF8(wide_log));
906 }
907}
908
909void MetricsService::StoreUnsentLogs() {
910 if (state_ < INITIAL_LOG_READY)
911 return; // We never Recalled the prior unsent logs.
912
913 PrefService* local_state = g_browser_process->local_state();
914 DCHECK(local_state);
915
916 ListValue* unsent_initial_logs = local_state->GetMutableList(
917 prefs::kMetricsInitialLogs);
918 unsent_initial_logs->Clear();
919 size_t start = 0;
920 if (unsent_initial_logs_.size() > kMaxInitialLogsPersisted)
921 start = unsent_initial_logs_.size() - kMaxInitialLogsPersisted;
922 for (size_t i = start; i < unsent_initial_logs_.size(); ++i)
923 unsent_initial_logs->Append(
924 Value::CreateStringValue(UTF8ToWide(unsent_initial_logs_[i])));
925
926 ListValue* unsent_ongoing_logs = local_state->GetMutableList(
927 prefs::kMetricsOngoingLogs);
928 unsent_ongoing_logs->Clear();
929 start = 0;
930 if (unsent_ongoing_logs_.size() > kMaxOngoingLogsPersisted)
931 start = unsent_ongoing_logs_.size() - kMaxOngoingLogsPersisted;
932 for (size_t i = start; i < unsent_ongoing_logs_.size(); ++i)
933 unsent_ongoing_logs->Append(
934 Value::CreateStringValue(UTF8ToWide(unsent_ongoing_logs_[i])));
935}
936
937void MetricsService::PreparePendingLogText() {
938 DCHECK(pending_log());
939 if (!pending_log_text_.empty())
940 return;
941 int original_size = pending_log_->GetEncodedLogSize();
942 pending_log_->GetEncodedLog(WriteInto(&pending_log_text_, original_size),
943 original_size);
944}
945
946void MetricsService::PreparePendingLogForTransmission() {
947 DCHECK(pending_log());
948 DCHECK(!current_fetch_.get());
949 PreparePendingLogText();
950 DCHECK(!pending_log_text_.empty());
951
952 // Allow security conscious users to see all metrics logs that we send.
953 LOG(INFO) << "METRICS LOG: " << pending_log_text_;
954
955 std::string compressed_log;
956 bool result = Bzip2Compress(pending_log_text_, &compressed_log);
957
958 if (!result) {
959 NOTREACHED() << "Failed to compress log for transmission.";
960 DiscardPendingLog();
961 StartLogTransmissionTimer(); // Maybe we'll do better on next log :-/.
962 return;
963 }
964 current_fetch_.reset(new URLFetcher(GURL(kMetricsURL), URLFetcher::POST,
965 this));
966 current_fetch_->set_request_context(Profile::GetDefaultRequestContext());
967 current_fetch_->set_upload_data(kMetricsType, compressed_log);
968 // This flag works around the cert mismatch on toolbarqueries.google.com.
969 current_fetch_->set_load_flags(net::LOAD_IGNORE_CERT_COMMON_NAME_INVALID);
970}
971
972void MetricsService::DiscardPendingLog() {
973 if (pending_log_) { // Shutdown might have deleted it!
974 delete pending_log_;
975 pending_log_ = NULL;
976 }
977 pending_log_text_.clear();
978}
979
980// This implementation is based on the Firefox MetricsService implementation.
981bool MetricsService::Bzip2Compress(const std::string& input,
982 std::string* output) {
983 bz_stream stream = {0};
984 // As long as our input is smaller than the bzip2 block size, we should get
985 // the best compression. For example, if your input was 250k, using a block
986 // size of 300k or 500k should result in the same compression ratio. Since
987 // our data should be under 100k, using the minimum block size of 100k should
988 // allocate less temporary memory, but result in the same compression ratio.
989 int result = BZ2_bzCompressInit(&stream,
990 1, // 100k (min) block size
991 0, // quiet
992 0); // default "work factor"
993 if (result != BZ_OK) { // out of memory?
994 return false;
995 }
996
997 output->clear();
998
999 stream.next_in = const_cast<char*>(input.data());
1000 stream.avail_in = static_cast<int>(input.size());
1001 // NOTE: we don't need a BZ_RUN phase since our input buffer contains
1002 // the entire input
1003 do {
1004 output->resize(output->size() + 1024);
1005 stream.next_out = &((*output)[stream.total_out_lo32]);
1006 stream.avail_out = static_cast<int>(output->size()) - stream.total_out_lo32;
1007 result = BZ2_bzCompress(&stream, BZ_FINISH);
1008 } while (result == BZ_FINISH_OK);
1009 if (result != BZ_STREAM_END) // unknown failure?
1010 return false;
1011 result = BZ2_bzCompressEnd(&stream);
1012 DCHECK(result == BZ_OK);
1013
1014 output->resize(stream.total_out_lo32);
1015
1016 return true;
1017}
1018
1019static const char* StatusToString(const URLRequestStatus& status) {
1020 switch (status.status()) {
1021 case URLRequestStatus::SUCCESS:
1022 return "SUCCESS";
1023
1024 case URLRequestStatus::IO_PENDING:
1025 return "IO_PENDING";
1026
1027 case URLRequestStatus::HANDLED_EXTERNALLY:
1028 return "HANDLED_EXTERNALLY";
1029
1030 case URLRequestStatus::CANCELED:
1031 return "CANCELED";
1032
1033 case URLRequestStatus::FAILED:
1034 return "FAILED";
1035
1036 default:
1037 NOTREACHED();
1038 return "Unknown";
1039 }
1040}
1041
1042void MetricsService::OnURLFetchComplete(const URLFetcher* source,
1043 const GURL& url,
1044 const URLRequestStatus& status,
1045 int response_code,
1046 const ResponseCookies& cookies,
1047 const std::string& data) {
1048 DCHECK(timer_pending_);
1049 timer_pending_ = false;
1050 DCHECK(current_fetch_.get());
1051 current_fetch_.reset(NULL); // We're not allowed to re-use it.
1052
1053 // Confirm send so that we can move on.
1054 DLOG(INFO) << "METRICS RESPONSE CODE: " << response_code
1055 << " status=" << StatusToString(status);
[email protected]252873ef2008-08-04 21:59:451056
[email protected]68475e602008-08-22 03:21:151057 // TODO(petersont): Refactor or remove the following so that we don't have to
1058 // fake a valid response code.
1059 if (response_code != 200 &&
1060 pending_log_text_.length() > kUploadLogAvoidRetransmitSize) {
1061 UMA_HISTOGRAM_COUNTS(L"UMA.Large Rejected Log was Discarded",
1062 static_cast<int>(pending_log_text_.length()));
1063 response_code = 200; // Simulate transmission so we will discard log.
1064 }
1065
[email protected]252873ef2008-08-04 21:59:451066 if (response_code != 200) {
1067 HandleBadResponseCode();
1068 } else { // Success.
initial.commit09911bf2008-07-26 23:55:291069 switch (state_) {
1070 case INITIAL_LOG_READY:
1071 state_ = SEND_OLD_INITIAL_LOGS;
1072 break;
1073
1074 case SEND_OLD_INITIAL_LOGS:
1075 DCHECK(!unsent_initial_logs_.empty());
1076 unsent_initial_logs_.pop_back();
1077 StoreUnsentLogs();
1078 break;
1079
1080 case SENDING_OLD_LOGS:
1081 DCHECK(!unsent_ongoing_logs_.empty());
1082 unsent_ongoing_logs_.pop_back();
1083 StoreUnsentLogs();
1084 break;
1085
1086 case SENDING_CURRENT_LOGS:
1087 break;
1088
1089 default:
1090 DCHECK(false);
1091 break;
1092 }
initial.commit09911bf2008-07-26 23:55:291093 DLOG(INFO) << "METRICS RESPONSE DATA: " << data;
1094 DiscardPendingLog();
[email protected]29be92552008-08-07 22:49:271095 // Since we sent a log, make sure our in-memory state is recorded to disk.
1096 PrefService* local_state = g_browser_process->local_state();
1097 DCHECK(local_state);
1098 if (local_state)
1099 local_state->ScheduleSavePersistentPrefs(
1100 g_browser_process->file_thread());
[email protected]252873ef2008-08-04 21:59:451101
1102 GetSettingsFromResponseData(data);
[email protected]252873ef2008-08-04 21:59:451103 // Override server specified interlog delay if there are unsent logs to
[email protected]29be92552008-08-07 22:49:271104 // transmit.
initial.commit09911bf2008-07-26 23:55:291105 if (unsent_logs()) {
1106 DCHECK(state_ < SENDING_CURRENT_LOGS);
1107 interlog_duration_ = TimeDelta::FromSeconds(kUnsentLogDelay);
initial.commit09911bf2008-07-26 23:55:291108 }
1109 }
[email protected]252873ef2008-08-04 21:59:451110
initial.commit09911bf2008-07-26 23:55:291111 StartLogTransmissionTimer();
1112}
1113
[email protected]252873ef2008-08-04 21:59:451114void MetricsService::HandleBadResponseCode() {
1115 DLOG(INFO) << "METRICS: transmission attempt returned a failure code. "
1116 "Verify network connectivity";
1117#ifndef NDEBUG
1118 DLOG(INFO) << "Verify your metrics logs are formatted correctly."
1119 " Verify server is active at " << kMetricsURL;
1120#endif
1121 if (!pending_log()) {
1122 DLOG(INFO) << "METRICS: Recorder shutdown during log transmission.";
1123 } else {
1124 // Send progressively less frequently.
1125 DCHECK(kBackoff > 1.0);
1126 interlog_duration_ = TimeDelta::FromMicroseconds(
1127 static_cast<int64>(kBackoff * interlog_duration_.InMicroseconds()));
1128
1129 if (kMaxBackoff * TimeDelta::FromSeconds(kMinSecondsPerLog) <
1130 interlog_duration_)
1131 interlog_duration_ = kMaxBackoff *
1132 TimeDelta::FromSeconds(kMinSecondsPerLog);
1133
1134 DLOG(INFO) << "METRICS: transmission retry being scheduled in " <<
1135 interlog_duration_.InSeconds() << " seconds for " <<
1136 pending_log_text_;
initial.commit09911bf2008-07-26 23:55:291137 }
initial.commit09911bf2008-07-26 23:55:291138}
1139
[email protected]252873ef2008-08-04 21:59:451140void MetricsService::GetSettingsFromResponseData(const std::string& data) {
1141 // We assume that the file is structured as a block opened by <response>
1142 // and that inside response, there is a block opened by tag <config>
1143 // other tags are ignored for now except the content of <config>.
1144 DLOG(INFO) << data;
1145 int data_size = static_cast<int>(data.size());
1146 if (data_size < 0) {
[email protected]29be92552008-08-07 22:49:271147 DLOG(INFO) << "METRICS: server response data bad size " <<
[email protected]252873ef2008-08-04 21:59:451148 " aborting extraction of settings";
1149 return;
1150 }
1151 xmlDocPtr doc = xmlReadMemory(data.c_str(), data_size,
1152 "", NULL, 0);
1153 DCHECK(doc);
1154 // if the document is malformed, we just use the settings that were there
1155 if (!doc)
1156 return;
1157
1158 xmlNodePtr top_node = xmlDocGetRootElement(doc), config_node = NULL;
1159 // Here, we find the config node by name.
1160 for (xmlNodePtr p = top_node->children; p; p = p->next) {
1161 if (xmlStrEqual(p->name, BAD_CAST "config")) {
1162 config_node = p;
1163 break;
1164 }
1165 }
1166 // If the server data is formatted wrong and there is no
1167 // config node where we expect, we just drop out.
1168 if (config_node != NULL)
1169 GetSettingsFromConfigNode(config_node);
1170 xmlFreeDoc(doc);
1171}
1172
1173void MetricsService::GetSettingsFromConfigNode(xmlNodePtr config_node) {
1174 for (xmlNodePtr current_node = config_node->children;
1175 current_node;
1176 current_node = current_node->next) {
[email protected]252873ef2008-08-04 21:59:451177 // If the node is collectors list, we iterate through the children
1178 // to get the types of collectors.
1179 if (xmlStrEqual(current_node->name, BAD_CAST "collectors")) {
1180 collectors_.clear();
1181 // Iterate through children and get the property "type".
1182 for (xmlNodePtr sub_node = current_node->children;
1183 sub_node;
1184 sub_node = sub_node->next) {
1185 if (xmlStrEqual(sub_node->name, BAD_CAST "collector")) {
1186 xmlChar* type_value = xmlGetProp(sub_node, BAD_CAST "type");
1187 collectors_.insert(reinterpret_cast<char*>(type_value));
1188 }
1189 }
1190 continue;
1191 }
1192 // Search for other tags, limit and upload. Again if the server data
1193 // does not contain those tags, the settings remain unchanged.
1194 if (xmlStrEqual(current_node->name, BAD_CAST "limit")) {
1195 xmlChar* event_limit_value = xmlGetProp(current_node, BAD_CAST "events");
1196 event_limit_ = atoi(reinterpret_cast<char*>(event_limit_value));
1197 continue;
1198 }
1199 if (xmlStrEqual(current_node->name, BAD_CAST "upload")) {
1200 xmlChar* upload_interval_val = xmlGetProp(current_node,
1201 BAD_CAST "interval");
[email protected]29be92552008-08-07 22:49:271202 int upload_interval_sec =
[email protected]252873ef2008-08-04 21:59:451203 atoi(reinterpret_cast<char*>(upload_interval_val));
1204 interlog_duration_ = TimeDelta::FromSeconds(upload_interval_sec);
1205 continue;
1206 }
1207 }
1208}
initial.commit09911bf2008-07-26 23:55:291209
1210void MetricsService::LogWindowChange(NotificationType type,
1211 const NotificationSource& source,
1212 const NotificationDetails& details) {
[email protected]534e54b2008-08-13 15:40:091213 int controller_id = -1;
1214 uintptr_t window_or_tab = source.map_key();
initial.commit09911bf2008-07-26 23:55:291215 MetricsLog::WindowEventType window_type;
1216
1217 // Note: since we stop all logging when a single OTR session is active, it is
1218 // possible that we start getting notifications about a window that we don't
1219 // know about.
[email protected]534e54b2008-08-13 15:40:091220 if (window_map_.find(window_or_tab) == window_map_.end()) {
1221 controller_id = next_window_id_++;
1222 window_map_[window_or_tab] = controller_id;
initial.commit09911bf2008-07-26 23:55:291223 } else {
[email protected]534e54b2008-08-13 15:40:091224 controller_id = window_map_[window_or_tab];
initial.commit09911bf2008-07-26 23:55:291225 }
[email protected]534e54b2008-08-13 15:40:091226 DCHECK(controller_id != -1);
initial.commit09911bf2008-07-26 23:55:291227
1228 switch (type) {
[email protected]534e54b2008-08-13 15:40:091229 case NOTIFY_TAB_PARENTED:
initial.commit09911bf2008-07-26 23:55:291230 case NOTIFY_BROWSER_OPENED:
1231 window_type = MetricsLog::WINDOW_CREATE;
1232 break;
1233
1234 case NOTIFY_TAB_CLOSING:
1235 case NOTIFY_BROWSER_CLOSED:
[email protected]534e54b2008-08-13 15:40:091236 window_map_.erase(window_map_.find(window_or_tab));
initial.commit09911bf2008-07-26 23:55:291237 window_type = MetricsLog::WINDOW_DESTROY;
1238 break;
1239
1240 default:
1241 NOTREACHED();
1242 break;
1243 }
1244
[email protected]534e54b2008-08-13 15:40:091245 // TODO(brettw) we should have some kind of ID for the parent.
1246 current_log_->RecordWindowEvent(window_type, controller_id, 0);
initial.commit09911bf2008-07-26 23:55:291247}
1248
1249void MetricsService::LogLoadComplete(NotificationType type,
1250 const NotificationSource& source,
1251 const NotificationDetails& details) {
1252 if (details == NotificationService::NoDetails())
1253 return;
1254
[email protected]68475e602008-08-22 03:21:151255 // TODO(jar): There is a bug causing this to be called too many times, and
1256 // the log overflows. For now, we won't record these events.
1257 UMA_HISTOGRAM_COUNTS(L"UMA.LogLoadComplete called", 1);
1258 return;
1259
initial.commit09911bf2008-07-26 23:55:291260 const Details<LoadNotificationDetails> load_details(details);
[email protected]534e54b2008-08-13 15:40:091261 int controller_id = window_map_[details.map_key()];
1262 current_log_->RecordLoadEvent(controller_id,
initial.commit09911bf2008-07-26 23:55:291263 load_details->url(),
1264 load_details->origin(),
1265 load_details->session_index(),
1266 load_details->load_time());
1267}
1268
[email protected]e73c01972008-08-13 00:18:241269void MetricsService::IncrementPrefValue(const wchar_t* path) {
1270 PrefService* pref = g_browser_process->local_state();
1271 DCHECK(pref);
1272 int value = pref->GetInteger(path);
1273 pref->SetInteger(path, value + 1);
1274}
1275
initial.commit09911bf2008-07-26 23:55:291276void MetricsService::LogLoadStarted() {
[email protected]e73c01972008-08-13 00:18:241277 IncrementPrefValue(prefs::kStabilityPageLoadCount);
initial.commit09911bf2008-07-26 23:55:291278 // We need to save the prefs, as page load count is a critical stat, and
1279 // it might be lost due to a crash :-(.
1280}
1281
1282void MetricsService::LogRendererInSandbox(bool on_sandbox_desktop) {
1283 PrefService* prefs = g_browser_process->local_state();
1284 DCHECK(prefs);
[email protected]e73c01972008-08-13 00:18:241285 if (on_sandbox_desktop)
1286 IncrementPrefValue(prefs::kSecurityRendererOnSboxDesktop);
1287 else
1288 IncrementPrefValue(prefs::kSecurityRendererOnDefaultDesktop);
initial.commit09911bf2008-07-26 23:55:291289}
1290
1291void MetricsService::LogRendererCrash() {
[email protected]e73c01972008-08-13 00:18:241292 IncrementPrefValue(prefs::kStabilityRendererCrashCount);
initial.commit09911bf2008-07-26 23:55:291293}
1294
1295void MetricsService::LogRendererHang() {
[email protected]e73c01972008-08-13 00:18:241296 IncrementPrefValue(prefs::kStabilityRendererHangCount);
initial.commit09911bf2008-07-26 23:55:291297}
1298
1299void MetricsService::LogPluginChange(NotificationType type,
1300 const NotificationSource& source,
1301 const NotificationDetails& details) {
1302 std::wstring plugin = Details<PluginProcessInfo>(details)->dll_path();
1303
1304 if (plugin_stats_buffer_.find(plugin) == plugin_stats_buffer_.end()) {
1305 plugin_stats_buffer_[plugin] = PluginStats();
1306 }
1307
1308 PluginStats& stats = plugin_stats_buffer_[plugin];
1309 switch (type) {
1310 case NOTIFY_PLUGIN_PROCESS_HOST_CONNECTED:
1311 stats.process_launches++;
1312 break;
1313
1314 case NOTIFY_PLUGIN_INSTANCE_CREATED:
1315 stats.instances++;
1316 break;
1317
1318 case NOTIFY_PLUGIN_PROCESS_CRASHED:
1319 stats.process_crashes++;
1320 break;
1321
1322 default:
1323 NOTREACHED() << "Unexpected notification type " << type;
1324 return;
1325 }
1326}
1327
1328// Recursively counts the number of bookmarks and folders in node.
1329static void CountBookmarks(BookmarkBarNode* node,
1330 int* bookmarks,
1331 int* folders) {
1332 if (node->GetType() == history::StarredEntry::URL)
1333 (*bookmarks)++;
1334 else
1335 (*folders)++;
1336 for (int i = 0; i < node->GetChildCount(); ++i)
1337 CountBookmarks(node->GetChild(i), bookmarks, folders);
1338}
1339
1340void MetricsService::LogBookmarks(BookmarkBarNode* node,
1341 const wchar_t* num_bookmarks_key,
1342 const wchar_t* num_folders_key) {
1343 DCHECK(node);
1344 int num_bookmarks = 0;
1345 int num_folders = 0;
1346 CountBookmarks(node, &num_bookmarks, &num_folders);
1347 num_folders--; // Don't include the root folder in the count.
1348
1349 PrefService* pref = g_browser_process->local_state();
1350 DCHECK(pref);
1351 pref->SetInteger(num_bookmarks_key, num_bookmarks);
1352 pref->SetInteger(num_folders_key, num_folders);
1353}
1354
1355void MetricsService::LogBookmarks(BookmarkBarModel* model) {
1356 DCHECK(model);
1357 LogBookmarks(model->GetBookmarkBarNode(),
1358 prefs::kNumBookmarksOnBookmarkBar,
1359 prefs::kNumFoldersOnBookmarkBar);
1360 LogBookmarks(model->other_node(),
1361 prefs::kNumBookmarksInOtherBookmarkFolder,
1362 prefs::kNumFoldersInOtherBookmarkFolder);
1363 ScheduleNextStateSave();
1364}
1365
1366void MetricsService::LogKeywords(const TemplateURLModel* url_model) {
1367 DCHECK(url_model);
1368
1369 PrefService* pref = g_browser_process->local_state();
1370 DCHECK(pref);
1371 pref->SetInteger(prefs::kNumKeywords,
1372 static_cast<int>(url_model->GetTemplateURLs().size()));
1373 ScheduleNextStateSave();
1374}
1375
1376void MetricsService::RecordPluginChanges(PrefService* pref) {
1377 ListValue* plugins = pref->GetMutableList(prefs::kStabilityPluginStats);
1378 DCHECK(plugins);
1379
1380 for (ListValue::iterator value_iter = plugins->begin();
1381 value_iter != plugins->end(); ++value_iter) {
1382 if (!(*value_iter)->IsType(Value::TYPE_DICTIONARY)) {
1383 NOTREACHED();
1384 continue;
1385 }
1386
1387 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*value_iter);
1388 std::wstring plugin_path;
1389 plugin_dict->GetString(prefs::kStabilityPluginPath, &plugin_path);
1390 if (plugin_path.empty()) {
1391 NOTREACHED();
1392 continue;
1393 }
1394
1395 if (plugin_stats_buffer_.find(plugin_path) == plugin_stats_buffer_.end())
1396 continue;
1397
1398 PluginStats stats = plugin_stats_buffer_[plugin_path];
1399 if (stats.process_launches) {
1400 int launches = 0;
1401 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
1402 launches += stats.process_launches;
1403 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, launches);
1404 }
1405 if (stats.process_crashes) {
1406 int crashes = 0;
1407 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
1408 crashes += stats.process_crashes;
1409 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, crashes);
1410 }
1411 if (stats.instances) {
1412 int instances = 0;
1413 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
1414 instances += stats.instances;
1415 plugin_dict->SetInteger(prefs::kStabilityPluginInstances, instances);
1416 }
1417
1418 plugin_stats_buffer_.erase(plugin_path);
1419 }
1420
1421 // Now go through and add dictionaries for plugins that didn't already have
1422 // reports in Local State.
1423 for (std::map<std::wstring, PluginStats>::iterator cache_iter =
1424 plugin_stats_buffer_.begin();
1425 cache_iter != plugin_stats_buffer_.end(); ++cache_iter) {
1426 std::wstring plugin_path = cache_iter->first;
1427 PluginStats stats = cache_iter->second;
1428 DictionaryValue* plugin_dict = new DictionaryValue;
1429
1430 plugin_dict->SetString(prefs::kStabilityPluginPath, plugin_path);
1431 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches,
1432 stats.process_launches);
1433 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes,
1434 stats.process_crashes);
1435 plugin_dict->SetInteger(prefs::kStabilityPluginInstances,
1436 stats.instances);
1437 plugins->Append(plugin_dict);
1438 }
1439 plugin_stats_buffer_.clear();
1440}
1441
1442bool MetricsService::CanLogNotification(NotificationType type,
1443 const NotificationSource& source,
1444 const NotificationDetails& details) {
1445 // We simply don't log anything to UMA if there is a single off the record
1446 // session visible. The problem is that we always notify using the orginal
1447 // profile in order to simplify notification processing.
1448 return !BrowserList::IsOffTheRecordSessionActive();
1449}
1450
1451void MetricsService::RecordBooleanPrefValue(const wchar_t* path, bool value) {
1452 DCHECK(IsSingleThreaded());
1453
1454 PrefService* pref = g_browser_process->local_state();
1455 DCHECK(pref);
1456
1457 pref->SetBoolean(path, value);
1458 RecordCurrentState(pref);
1459}
1460
1461void MetricsService::RecordCurrentState(PrefService* pref) {
1462 pref->SetString(prefs::kStabilityLastTimestampSec,
1463 Int64ToWString(Time::Now().ToTimeT()));
1464
1465 RecordPluginChanges(pref);
1466}
1467
1468void MetricsService::RecordCurrentHistograms() {
1469 DCHECK(current_log_);
1470
1471 StatisticsRecorder::Histograms histograms;
1472 StatisticsRecorder::GetHistograms(&histograms);
1473 for (StatisticsRecorder::Histograms::iterator it = histograms.begin();
1474 histograms.end() != it;
1475 it++) {
1476 if ((*it)->flags() & kUmaTargetedHistogramFlag)
1477 RecordHistogram(**it);
1478 }
1479}
1480
1481void MetricsService::RecordHistogram(const Histogram& histogram) {
1482 // Get up-to-date snapshot of sample stats.
1483 Histogram::SampleSet snapshot;
1484 histogram.SnapshotSample(&snapshot);
1485
1486 const std::string& histogram_name = histogram.histogram_name();
1487
1488 // Find the already sent stats, or create an empty set.
1489 LoggedSampleMap::iterator it = logged_samples_.find(histogram_name);
1490 Histogram::SampleSet* already_logged;
1491 if (logged_samples_.end() == it) {
1492 // Add new entry
1493 already_logged = &logged_samples_[histogram.histogram_name()];
1494 already_logged->Resize(histogram); // Complete initialization.
1495 } else {
1496 already_logged = &(it->second);
1497 // Deduct any stats we've already logged from our snapshot.
1498 snapshot.Subtract(*already_logged);
1499 }
1500
1501 // snapshot now contains only a delta to what we've already_logged.
1502
1503 if (snapshot.TotalCount() > 0) {
1504 current_log_->RecordHistogramDelta(histogram, snapshot);
1505 // Add new data into our running total.
1506 already_logged->Add(snapshot);
1507 }
1508}
1509
1510void MetricsService::AddProfileMetric(Profile* profile,
1511 const std::wstring& key,
1512 int value) {
1513 // Restriction of types is needed for writing values. See
1514 // MetricsLog::WriteProfileMetrics.
1515 DCHECK(profile && !key.empty());
1516 PrefService* prefs = g_browser_process->local_state();
1517 DCHECK(prefs);
1518
1519 // Key is stored in prefs, which interpret '.'s as paths. As such, key
1520 // shouldn't have any '.'s in it.
1521 DCHECK(key.find(L'.') == std::wstring::npos);
1522 // The id is most likely an email address. We shouldn't send it to the server.
1523 const std::wstring id_hash =
1524 UTF8ToWide(MetricsLog::CreateBase64Hash(WideToUTF8(profile->GetID())));
1525 DCHECK(id_hash.find('.') == std::string::npos);
1526
1527 DictionaryValue* prof_prefs = prefs->GetMutableDictionary(
1528 prefs::kProfileMetrics);
1529 DCHECK(prof_prefs);
1530 const std::wstring pref_key = std::wstring(prefs::kProfilePrefix) + id_hash +
1531 L"." + key;
1532 prof_prefs->SetInteger(pref_key.c_str(), value);
1533}
1534
1535static bool IsSingleThreaded() {
1536 static int thread_id = 0;
1537 if (!thread_id)
1538 thread_id = GetCurrentThreadId();
1539 return GetCurrentThreadId() == thread_id;
1540}