summaryrefslogtreecommitdiffstats
path: root/components/suggestions/suggestions_store.h
blob: 3ad8b093b8201b8f67cc4d2287e1032a314290db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SUGGESTIONS_SUGGESTIONS_STORE_H_
#define COMPONENTS_SUGGESTIONS_SUGGESTIONS_STORE_H_

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/clock.h"
#include "components/suggestions/proto/suggestions.pb.h"

class PrefService;

namespace user_prefs {
class PrefRegistrySyncable;
}  // namespace user_prefs

namespace suggestions {

// A helper class for reading and writing the suggestions to the profile's
// preference file.
class SuggestionsStore {
 public:
  explicit SuggestionsStore(PrefService* profile_prefs);
  virtual ~SuggestionsStore();

  // Loads the suggestion data from the profile's preferences into
  // |suggestions|. If there is a problem with loading, the pref value is
  // cleared, false is returned and |suggestions| is cleared. If successful,
  // |suggestions| will contain the loaded data and true is returned.
  virtual bool LoadSuggestions(SuggestionsProfile* suggestions);

  // Stores the provided |suggestions| to the profile's preferences, using
  // a base64 encoding of its protobuf serialization.
  virtual bool StoreSuggestions(const SuggestionsProfile& suggestions);

  // Clears any suggestion data from the profile's preferences.
  virtual void ClearSuggestions();

  // Register SuggestionsStore related prefs in the Profile prefs.
  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

  void SetClockForTesting(scoped_ptr<base::Clock> clock);

 protected:
  // Test seam. For simplicity of mock creation.
  SuggestionsStore();

 private:
  // The pref service used to persist the suggestions data.
  PrefService* pref_service_;
  // Can be set for testing.
  scoped_ptr<base::Clock> clock_;

  DISALLOW_COPY_AND_ASSIGN(SuggestionsStore);

  // Filters expired suggestions.
  void FilterExpiredSuggestions(SuggestionsProfile* suggestions);
};

}  // namespace suggestions

#endif  // COMPONENTS_SUGGESTIONS_SUGGESTIONS_STORE_H_