- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / prefs / pref_metrics_service.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/prefs/pref_metrics_service.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/metrics/histogram.h"
11 #include "base/prefs/pref_registry_simple.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/prefs/scoped_user_pref_update.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/browser_shutdown.h"
17 // Accessing the Device ID API here is a layering violation.
18 // TODO(bbudge) Move the API so it's usable here.
19 // http://crbug.com/276485
20 #include "chrome/browser/extensions/api/music_manager_private/device_id.h"
21 #include "chrome/browser/prefs/pref_service_syncable.h"
22 #include "chrome/browser/prefs/session_startup_pref.h"
23 #include "chrome/browser/prefs/synced_pref_change_registrar.h"
24 #include "chrome/browser/profiles/incognito_helpers.h"
25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
27 #include "chrome/browser/ui/tabs/pinned_tab_codec.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/pref_names.h"
30 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
31 #include "crypto/hmac.h"
32 #include "grit/browser_resources.h"
33 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
34 #include "ui/base/resource/resource_bundle.h"
35
36 namespace {
37
38 const int kSessionStartupPrefValueMax = SessionStartupPref::kPrefValueMax;
39
40 #if defined(OS_ANDROID)
41 // An unregistered preference to fill in indices in kTrackedPrefs below for
42 // preferences that aren't defined on every platform. This is fine as the code
43 // below (e.g. CheckTrackedPreferences()) skips unregistered preferences and
44 // should thus never report any data about that index on the platforms where
45 // that preference is unimplemented.
46 const char kUnregisteredPreference[] = "_";
47 #endif
48
49 // These preferences must be kept in sync with the TrackedPreference enum in
50 // tools/metrics/histograms/histograms.xml. To add a new preference, append it
51 // to the array and add a corresponding value to the histogram enum.
52 const char* kTrackedPrefs[] = {
53   prefs::kShowHomeButton,
54   prefs::kHomePageIsNewTabPage,
55   prefs::kHomePage,
56   prefs::kRestoreOnStartup,
57   prefs::kURLsToRestoreOnStartup,
58   prefs::kExtensionsPref,
59   prefs::kGoogleServicesLastUsername,
60   prefs::kSearchProviderOverrides,
61   prefs::kDefaultSearchProviderSearchURL,
62   prefs::kDefaultSearchProviderKeyword,
63   prefs::kDefaultSearchProviderName,
64 #if !defined(OS_ANDROID)
65   prefs::kPinnedTabs,
66 #else
67   kUnregisteredPreference,
68 #endif
69   prefs::kExtensionKnownDisabled,
70 };
71
72 const size_t kSHA256DigestSize = 32;
73
74 }  // namespace
75
76 PrefMetricsService::PrefMetricsService(Profile* profile)
77     : profile_(profile),
78       prefs_(profile_->GetPrefs()),
79       local_state_(g_browser_process->local_state()),
80       profile_name_(profile_->GetPath().AsUTF8Unsafe()),
81       tracked_pref_paths_(kTrackedPrefs),
82       tracked_pref_path_count_(arraysize(kTrackedPrefs)),
83       checked_tracked_prefs_(false),
84       weak_factory_(this) {
85   pref_hash_seed_ = ResourceBundle::GetSharedInstance().GetRawDataResource(
86       IDR_PREF_HASH_SEED_BIN).as_string();
87
88   RecordLaunchPrefs();
89
90   PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
91   synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs));
92
93   RegisterSyncedPrefObservers();
94
95   // The following code might cause callbacks into this instance before we exit
96   // the constructor. This instance should be initialized at this point.
97 #if defined(OS_WIN) || defined(OS_MACOSX)
98   // We need the machine id to compute pref value hashes. Fetch that, and then
99   // call CheckTrackedPreferences in the callback.
100   extensions::api::DeviceId::GetDeviceId(
101       "PrefMetricsService",  // non-empty string to obfuscate the device id.
102       Bind(&PrefMetricsService::GetDeviceIdCallback,
103            weak_factory_.GetWeakPtr()));
104 #endif  // defined(OS_WIN) || defined(OS_MACOSX)
105 }
106
107 // For unit testing only.
108 PrefMetricsService::PrefMetricsService(Profile* profile,
109                                        PrefService* local_state,
110                                        const std::string& device_id,
111                                        const char** tracked_pref_paths,
112                                        int tracked_pref_path_count)
113     : profile_(profile),
114       prefs_(profile->GetPrefs()),
115       local_state_(local_state),
116       profile_name_(profile_->GetPath().AsUTF8Unsafe()),
117       pref_hash_seed_(kSHA256DigestSize, 0),
118       device_id_(device_id),
119       tracked_pref_paths_(tracked_pref_paths),
120       tracked_pref_path_count_(tracked_pref_path_count),
121       checked_tracked_prefs_(false),
122       weak_factory_(this) {
123   CheckTrackedPreferences();
124 }
125
126 PrefMetricsService::~PrefMetricsService() {
127 }
128
129 void PrefMetricsService::RecordLaunchPrefs() {
130   bool show_home_button = prefs_->GetBoolean(prefs::kShowHomeButton);
131   bool home_page_is_ntp = prefs_->GetBoolean(prefs::kHomePageIsNewTabPage);
132   UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button);
133   if (show_home_button) {
134     UMA_HISTOGRAM_BOOLEAN("Settings.GivenShowHomeButton_HomePageIsNewTabPage",
135                           home_page_is_ntp);
136   }
137
138   // For non-NTP homepages, see if the URL comes from the same TLD+1 as a known
139   // search engine.  Note that this is only an approximation of search engine
140   // use, due to both false negatives (pages that come from unknown TLD+1 X but
141   // consist of a search box that sends to known TLD+1 Y) and false positives
142   // (pages that share a TLD+1 with a known engine but aren't actually search
143   // pages, e.g. plus.google.com).
144   if (!home_page_is_ntp) {
145     GURL homepage_url(prefs_->GetString(prefs::kHomePage));
146     if (homepage_url.is_valid()) {
147       UMA_HISTOGRAM_ENUMERATION(
148           "Settings.HomePageEngineType",
149           TemplateURLPrepopulateData::GetEngineType(homepage_url),
150           SEARCH_ENGINE_MAX);
151     }
152   }
153
154   int restore_on_startup = prefs_->GetInteger(prefs::kRestoreOnStartup);
155   UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings",
156                             restore_on_startup, kSessionStartupPrefValueMax);
157   if (restore_on_startup == SessionStartupPref::kPrefValueURLs) {
158     const ListValue* url_list = prefs_->GetList(prefs::kURLsToRestoreOnStartup);
159     UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.StartupPageLoadURLs",
160                                 url_list->GetSize(), 1, 50, 20);
161     // Similarly, check startup pages for known search engine TLD+1s.
162     std::string url_text;
163     for (size_t i = 0; i < url_list->GetSize(); ++i) {
164       if (url_list->GetString(i, &url_text)) {
165         GURL start_url(url_text);
166         if (start_url.is_valid()) {
167           UMA_HISTOGRAM_ENUMERATION(
168               "Settings.StartupPageEngineTypes",
169               TemplateURLPrepopulateData::GetEngineType(start_url),
170               SEARCH_ENGINE_MAX);
171         }
172       }
173     }
174   }
175
176 #if !defined(OS_ANDROID)
177   StartupTabs startup_tabs = PinnedTabCodec::ReadPinnedTabs(profile_);
178   UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.PinnedTabs",
179                               startup_tabs.size(), 1, 50, 20);
180   for (size_t i = 0; i < startup_tabs.size(); ++i) {
181     GURL start_url(startup_tabs.at(i).url);
182     if (start_url.is_valid()) {
183       UMA_HISTOGRAM_ENUMERATION(
184           "Settings.PinnedTabEngineTypes",
185           TemplateURLPrepopulateData::GetEngineType(start_url),
186           SEARCH_ENGINE_MAX);
187     }
188   }
189 #endif
190 }
191
192 // static
193 void PrefMetricsService::RegisterPrefs(PrefRegistrySimple* registry) {
194   // Register the top level dictionary to map profile names to dictionaries of
195   // tracked preferences.
196   registry->RegisterDictionaryPref(prefs::kProfilePreferenceHashes);
197 }
198
199 void PrefMetricsService::RegisterSyncedPrefObservers() {
200   LogHistogramValueCallback booleanHandler = base::Bind(
201       &PrefMetricsService::LogBooleanPrefChange, base::Unretained(this));
202
203   AddPrefObserver(prefs::kShowHomeButton, "ShowHomeButton", booleanHandler);
204   AddPrefObserver(prefs::kHomePageIsNewTabPage, "HomePageIsNewTabPage",
205                   booleanHandler);
206
207   AddPrefObserver(prefs::kRestoreOnStartup, "StartupPageLoadSettings",
208                   base::Bind(&PrefMetricsService::LogIntegerPrefChange,
209                              base::Unretained(this),
210                              kSessionStartupPrefValueMax));
211 }
212
213 void PrefMetricsService::AddPrefObserver(
214     const std::string& path,
215     const std::string& histogram_name_prefix,
216     const LogHistogramValueCallback& callback) {
217   synced_pref_change_registrar_->Add(path.c_str(),
218       base::Bind(&PrefMetricsService::OnPrefChanged,
219                  base::Unretained(this),
220                  histogram_name_prefix, callback));
221 }
222
223 void PrefMetricsService::OnPrefChanged(
224     const std::string& histogram_name_prefix,
225     const LogHistogramValueCallback& callback,
226     const std::string& path,
227     bool from_sync) {
228   PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
229   const PrefService::Preference* pref = prefs->FindPreference(path.c_str());
230   DCHECK(pref);
231   std::string source_name(
232       from_sync ? ".PulledFromSync" : ".PushedToSync");
233   std::string histogram_name("Settings." + histogram_name_prefix + source_name);
234   callback.Run(histogram_name, pref->GetValue());
235 };
236
237 void PrefMetricsService::LogBooleanPrefChange(const std::string& histogram_name,
238                                               const Value* value) {
239   bool boolean_value = false;
240   if (!value->GetAsBoolean(&boolean_value))
241     return;
242   base::HistogramBase* histogram = base::BooleanHistogram::FactoryGet(
243       histogram_name, base::HistogramBase::kUmaTargetedHistogramFlag);
244   histogram->Add(boolean_value);
245 }
246
247 void PrefMetricsService::LogIntegerPrefChange(int boundary_value,
248                                               const std::string& histogram_name,
249                                               const Value* value) {
250   int integer_value = 0;
251   if (!value->GetAsInteger(&integer_value))
252     return;
253   base::HistogramBase* histogram = base::LinearHistogram::FactoryGet(
254       histogram_name,
255       1,
256       boundary_value,
257       boundary_value + 1,
258       base::HistogramBase::kUmaTargetedHistogramFlag);
259   histogram->Add(integer_value);
260 }
261
262 void PrefMetricsService::LogListPrefChange(
263     const LogHistogramValueCallback& item_callback,
264     const std::string& histogram_name,
265     const Value* value) {
266   const ListValue* items = NULL;
267   if (!value->GetAsList(&items))
268     return;
269   for (size_t i = 0; i < items->GetSize(); ++i) {
270     const Value *item_value = NULL;
271     if (items->Get(i, &item_value))
272       item_callback.Run(histogram_name, item_value);
273   }
274 }
275
276 void PrefMetricsService::GetDeviceIdCallback(const std::string& device_id) {
277   device_id_ = device_id;
278   // On Aura, this seems to be called twice.
279   if (!checked_tracked_prefs_)
280     CheckTrackedPreferences();
281 }
282
283 // To detect changes to Preferences that happen outside of Chrome, we hash
284 // selected pref values and save them in local state. CheckTrackedPreferences
285 // compares the saved values to the values observed in the profile's prefs. A
286 // dictionary of dictionaries in local state holds the hashed values, grouped by
287 // profile. To make the system more resistant to spoofing, pref values are
288 // hashed with the pref path and the device id.
289 void PrefMetricsService::CheckTrackedPreferences() {
290   DCHECK(!checked_tracked_prefs_);
291
292   const base::DictionaryValue* pref_hash_dicts =
293       local_state_->GetDictionary(prefs::kProfilePreferenceHashes);
294   // Get the hashed prefs dictionary if it exists. If it doesn't, it will be
295   // created if we set preference values below.
296   const base::DictionaryValue* hashed_prefs = NULL;
297   pref_hash_dicts->GetDictionaryWithoutPathExpansion(profile_name_,
298                                                      &hashed_prefs);
299   for (int i = 0; i < tracked_pref_path_count_; ++i) {
300     // Skip prefs that haven't been registered.
301     if (!prefs_->FindPreference(tracked_pref_paths_[i]))
302       continue;
303
304     const base::Value* value = prefs_->GetUserPrefValue(tracked_pref_paths_[i]);
305     std::string last_hash;
306     // First try to get the stored expected hash...
307     if (hashed_prefs &&
308         hashed_prefs->GetString(tracked_pref_paths_[i], &last_hash)) {
309       // ... if we have one get the hash of the current value...
310       const std::string value_hash =
311           GetHashedPrefValue(tracked_pref_paths_[i], value,
312                              HASHED_PREF_STYLE_NEW);
313       // ... and check that it matches...
314       if (value_hash == last_hash) {
315         UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceUnchanged",
316             i, tracked_pref_path_count_);
317       } else {
318         // ... if it doesn't: was the value simply cleared?
319         if (!value) {
320           UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceCleared",
321               i, tracked_pref_path_count_);
322         } else {
323           // ... or does it match the old style hash?
324           std::string old_style_hash =
325               GetHashedPrefValue(tracked_pref_paths_[i], value,
326                                  HASHED_PREF_STYLE_DEPRECATED);
327           if (old_style_hash == last_hash) {
328             UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceMigrated",
329                 i, tracked_pref_path_count_);
330           } else {
331             // ... or was it simply changed to something else?
332             UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceChanged",
333                 i, tracked_pref_path_count_);
334           }
335         }
336
337         // ... either way update the expected hash to match the new value.
338         UpdateTrackedPreference(tracked_pref_paths_[i]);
339       }
340     } else {
341       // Record that we haven't tracked this preference yet, or the hash in
342       // local state was removed.
343       UMA_HISTOGRAM_ENUMERATION("Settings.TrackedPreferenceInitialized",
344           i, tracked_pref_path_count_);
345       UpdateTrackedPreference(tracked_pref_paths_[i]);
346     }
347   }
348
349   checked_tracked_prefs_ = true;
350
351   // Now that we've checked the incoming preferences, register for change
352   // notifications, unless this is test code.
353   // TODO(bbudge) Fix failing browser_tests and so we can remove this test.
354   // Several tests fail when they shutdown before they can write local state.
355   if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType) &&
356       !CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) {
357     InitializePrefObservers();
358   }
359 }
360
361 void PrefMetricsService::UpdateTrackedPreference(const char* path) {
362   const base::Value* value = prefs_->GetUserPrefValue(path);
363   DictionaryPrefUpdate update(local_state_, prefs::kProfilePreferenceHashes);
364   DictionaryValue* child_dictionary = NULL;
365
366   // Get the dictionary corresponding to the profile name,
367   // which may have a '.'
368   if (!update->GetDictionaryWithoutPathExpansion(profile_name_,
369                                                  &child_dictionary)) {
370     child_dictionary = new DictionaryValue;
371     update->SetWithoutPathExpansion(profile_name_, child_dictionary);
372   }
373
374   child_dictionary->SetString(path,
375                               GetHashedPrefValue(path, value,
376                                                  HASHED_PREF_STYLE_NEW));
377 }
378
379 std::string PrefMetricsService::GetHashedPrefValue(
380     const char* path,
381     const base::Value* value,
382     HashedPrefStyle desired_style) {
383   // Dictionary values may contain empty lists and sub-dictionaries. Make a
384   // deep copy with those removed to make the hash more stable.
385   const DictionaryValue* dict_value;
386   scoped_ptr<DictionaryValue> canonical_dict_value;
387   if (value &&
388       value->GetAsDictionary(&dict_value)) {
389     canonical_dict_value.reset(dict_value->DeepCopyWithoutEmptyChildren());
390     value = canonical_dict_value.get();
391   }
392
393   std::string value_as_string;
394   // If |value| is NULL, we will still build a unique hash based on |device_id_|
395   // and |path| below.
396   if (value) {
397     JSONStringValueSerializer serializer(&value_as_string);
398     serializer.Serialize(*value);
399   }
400
401   std::string string_to_hash;
402   // TODO(gab): Remove this as the old style is phased out.
403   if (desired_style == HASHED_PREF_STYLE_NEW) {
404     string_to_hash.append(device_id_);
405     string_to_hash.append(path);
406   }
407   string_to_hash.append(value_as_string);
408
409   crypto::HMAC hmac(crypto::HMAC::SHA256);
410   unsigned char digest[kSHA256DigestSize];
411   if (!hmac.Init(pref_hash_seed_) ||
412       !hmac.Sign(string_to_hash, digest, kSHA256DigestSize)) {
413     NOTREACHED();
414     return std::string();
415   }
416
417   return base::HexEncode(digest, kSHA256DigestSize);
418 }
419
420 void PrefMetricsService::InitializePrefObservers() {
421   pref_registrar_.Init(prefs_);
422   for (int i = 0; i < tracked_pref_path_count_; ++i) {
423     // Skip prefs that haven't been registered.
424     if (!prefs_->FindPreference(tracked_pref_paths_[i]))
425       continue;
426
427     pref_registrar_.Add(
428         tracked_pref_paths_[i],
429         base::Bind(&PrefMetricsService::UpdateTrackedPreference,
430                    weak_factory_.GetWeakPtr(),
431                    tracked_pref_paths_[i]));
432   }
433 }
434
435 // static
436 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() {
437   return Singleton<PrefMetricsService::Factory>::get();
438 }
439
440 // static
441 PrefMetricsService* PrefMetricsService::Factory::GetForProfile(
442     Profile* profile) {
443   return static_cast<PrefMetricsService*>(
444       GetInstance()->GetServiceForBrowserContext(profile, true));
445 }
446
447 PrefMetricsService::Factory::Factory()
448     : BrowserContextKeyedServiceFactory(
449         "PrefMetricsService",
450         BrowserContextDependencyManager::GetInstance()) {
451 }
452
453 PrefMetricsService::Factory::~Factory() {
454 }
455
456 BrowserContextKeyedService*
457 PrefMetricsService::Factory::BuildServiceInstanceFor(
458     content::BrowserContext* profile) const {
459   return new PrefMetricsService(static_cast<Profile*>(profile));
460 }
461
462 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const {
463   return true;
464 }
465
466 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const {
467   return false;
468 }
469
470 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse(
471     content::BrowserContext* context) const {
472   return chrome::GetBrowserContextRedirectedInIncognito(context);
473 }