Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / search_engines / default_search_manager.h
1 // Copyright 2014 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 #ifndef CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_
6 #define CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_
7
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/prefs/pref_change_registrar.h"
12
13 namespace base {
14 class DictionaryValue;
15 }
16
17 namespace user_prefs {
18 class PrefRegistrySyncable;
19 }
20
21 class PrefService;
22 class PrefValueMap;
23 struct TemplateURLData;
24
25 // DefaultSearchManager handles the loading and writing of the user's default
26 // search engine selection to and from prefs.
27 class DefaultSearchManager {
28  public:
29   static const char kDefaultSearchProviderDataPrefName[];
30
31   static const char kID[];
32   static const char kShortName[];
33   static const char kKeyword[];
34   static const char kPrepopulateID[];
35   static const char kSyncGUID[];
36
37   static const char kURL[];
38   static const char kSuggestionsURL[];
39   static const char kInstantURL[];
40   static const char kImageURL[];
41   static const char kNewTabURL[];
42   static const char kFaviconURL[];
43   static const char kOriginatingURL[];
44
45   static const char kSearchURLPostParams[];
46   static const char kSuggestionsURLPostParams[];
47   static const char kInstantURLPostParams[];
48   static const char kImageURLPostParams[];
49
50   static const char kSafeForAutoReplace[];
51   static const char kInputEncodings[];
52
53   static const char kDateCreated[];
54   static const char kLastModified[];
55
56   static const char kUsageCount[];
57   static const char kAlternateURLs[];
58   static const char kSearchTermsReplacementKey[];
59   static const char kCreatedByPolicy[];
60   static const char kDisabledByPolicy[];
61
62   enum Source {
63     FROM_FALLBACK = 0,
64     FROM_USER,
65     FROM_EXTENSION,
66     FROM_POLICY,
67   };
68
69   typedef base::Callback<void(const TemplateURLData*, Source)> ObserverCallback;
70
71   DefaultSearchManager(PrefService* pref_service,
72                        const ObserverCallback& change_observer);
73
74   ~DefaultSearchManager();
75
76   // Register prefs needed for tracking the default search provider.
77   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
78
79   // Save default search provider pref values into the map provided.
80   static void AddPrefValueToMap(base::DictionaryValue* value,
81                                 PrefValueMap* pref_value_map);
82
83   // Gets a pointer to the current Default Search Engine. If NULL, indicates
84   // that Default Search is explicitly disabled. |source|, if not NULL, will be
85   // filled in with the source of the result.
86   TemplateURLData* GetDefaultSearchEngine(Source* source) const;
87
88   // Gets the source of the current Default Search Engine value.
89   Source GetDefaultSearchEngineSource() const;
90
91   // Write default search provider data to |pref_service_|.
92   void SetUserSelectedDefaultSearchEngine(const TemplateURLData& data);
93
94   // Override the default search provider with an extension.
95   void SetExtensionControlledDefaultSearchEngine(const TemplateURLData& data);
96
97   // Clear the extension-provided default search engine. Does not explicitly
98   // disable Default Search. The new current default search engine will be
99   // defined by policy, extensions, or pre-populated data.
100   void ClearExtensionControlledDefaultSearchEngine();
101
102   // Clear the user's default search provider choice from |pref_service_|. Does
103   // not explicitly disable Default Search. The new default search
104   // engine will be defined by policy, extensions, or pre-populated data.
105   void ClearUserSelectedDefaultSearchEngine();
106
107  private:
108   // Handles changes to kDefaultSearchProviderData pref. This includes sync and
109   // policy changes. Calls LoadDefaultSearchEngineFromPrefs() and
110   // NotifyObserver() if the effective DSE might have changed.
111   void OnDefaultSearchPrefChanged();
112
113   // Handles changes to kSearchProviderOverrides pref. Calls
114   // LoadPrepopulatedDefaultSearch() and NotifyObserver() if the effective DSE
115   // might have changed.
116   void OnOverridesPrefChanged();
117
118   // Updates |prefs_default_search_| with values from its corresponding
119   // pre-populated search provider record, if any.
120   void MergePrefsDataWithPrepopulated();
121
122   // Reads default search provider data from |pref_service_|, updating
123   // |prefs_default_search_| and |default_search_controlled_by_policy_|.
124   // Invokes MergePrefsDataWithPrepopulated().
125   void LoadDefaultSearchEngineFromPrefs();
126
127   // Reads pre-populated search providers, which will be built-in or overridden
128   // by kSearchProviderOverrides. Updates |fallback_default_search_|. Invoke
129   // MergePrefsDataWithPrepopulated().
130   void LoadPrepopulatedDefaultSearch();
131
132   // Invokes |change_observer_| if it is not NULL.
133   void NotifyObserver();
134
135   PrefService* pref_service_;
136   const ObserverCallback change_observer_;
137   PrefChangeRegistrar pref_change_registrar_;
138
139   // Default search engine provided by pre-populated data or by the
140   // |kSearchProviderOverrides| pref. This will be used when no other default
141   // search engine has been selected.
142   scoped_ptr<TemplateURLData> fallback_default_search_;
143
144   // Default search engine provided by prefs (either user prefs or policy
145   // prefs). This will be null if no value was set in the pref store.
146   scoped_ptr<TemplateURLData> extension_default_search_;
147
148   // Default search engine provided by extension (usings Settings Override API).
149   // This will be null if there are no extensions installed which provide
150   // default search engines.
151   scoped_ptr<TemplateURLData> prefs_default_search_;
152
153   // True if the default search is currently enforced by policy.
154   bool default_search_controlled_by_policy_;
155
156   DISALLOW_COPY_AND_ASSIGN(DefaultSearchManager);
157 };
158
159 #endif  // CHROME_BROWSER_SEARCH_ENGINES_DEFAULT_SEARCH_MANAGER_H_