1 // Copyright 2015 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 "components/omnibox/browser/omnibox_prefs.h"
6 
7 #include "base/check.h"
8 #include "base/metrics/sparse_histogram.h"
9 #include "base/optional.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "components/prefs/pref_registry_simple.h"
14 #include "components/prefs/pref_service.h"
15 #include "components/prefs/scoped_user_pref_update.h"
16 
17 namespace omnibox {
18 
19 const char kToggleSuggestionGroupIdOffHistogram[] =
20     "Omnibox.ToggleSuggestionGroupId.Off";
21 const char kToggleSuggestionGroupIdOnHistogram[] =
22     "Omnibox.ToggleSuggestionGroupId.On";
23 
24 // A client-side toggle for document (Drive) suggestions.
25 // Also gated by a feature and server-side Admin Panel controls.
26 const char kDocumentSuggestEnabled[] = "documentsuggest.enabled";
27 
28 // Enum specifying the active behavior for the intranet redirect detector.
29 // The browser pref kDNSInterceptionChecksEnabled also impacts the redirector.
30 // Values are defined in omnibox::IntranetRedirectorBehavior.
31 const char kIntranetRedirectBehavior[] = "browser.intranet_redirect_behavior";
32 
33 // A dictionary of visibility preferences for suggestion groups. The key is the
34 // suggestion group ID serialized as a string, and the value is
35 // SuggestionGroupVisibility serialized as an integer.
36 const char kSuggestionGroupVisibility[] = "omnibox.suggestionGroupVisibility";
37 
38 // Boolean that specifies whether to always show full URLs in the omnibox.
39 const char kPreventUrlElisionsInOmnibox[] = "omnibox.prevent_url_elisions";
40 
41 // A cache of zero suggest results using JSON serialized into a string.
42 const char kZeroSuggestCachedResults[] = "zerosuggest.cachedresults";
43 
RegisterProfilePrefs(PrefRegistrySimple * registry)44 void RegisterProfilePrefs(PrefRegistrySimple* registry) {
45   registry->RegisterDictionaryPref(kSuggestionGroupVisibility);
46 }
47 
GetUserPreferenceForSuggestionGroupVisibility(PrefService * prefs,int suggestion_group_id)48 SuggestionGroupVisibility GetUserPreferenceForSuggestionGroupVisibility(
49     PrefService* prefs,
50     int suggestion_group_id) {
51   DCHECK(prefs);
52 
53   const base::DictionaryValue* dictionary =
54       prefs->GetDictionary(kSuggestionGroupVisibility);
55   DCHECK(dictionary);
56 
57   base::Optional<int> value =
58       dictionary->FindIntKey(base::NumberToString(suggestion_group_id));
59 
60   if (value == SuggestionGroupVisibility::HIDDEN ||
61       value == SuggestionGroupVisibility::SHOWN) {
62     return static_cast<SuggestionGroupVisibility>(*value);
63   }
64 
65   return SuggestionGroupVisibility::DEFAULT;
66 }
67 
SetSuggestionGroupVisibility(PrefService * prefs,int suggestion_group_id,SuggestionGroupVisibility new_value)68 void SetSuggestionGroupVisibility(PrefService* prefs,
69                                   int suggestion_group_id,
70                                   SuggestionGroupVisibility new_value) {
71   DCHECK(prefs);
72 
73   DictionaryPrefUpdate update(prefs, kSuggestionGroupVisibility);
74   update->SetIntKey(base::NumberToString(suggestion_group_id), new_value);
75 
76   base::SparseHistogram::FactoryGet(
77       new_value == SuggestionGroupVisibility::SHOWN
78           ? kToggleSuggestionGroupIdOnHistogram
79           : kToggleSuggestionGroupIdOffHistogram,
80       base::HistogramBase::kUmaTargetedHistogramFlag)
81       ->Add(suggestion_group_id);
82 }
83 
84 }  // namespace omnibox
85