summaryrefslogtreecommitdiff
path: root/chromium/components/feature_engagement/internal/persistent_event_store.h
blob: 61941c57227e6a4bd51074e15be60b67821f3563 (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
// Copyright 2017 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_FEATURE_ENGAGEMENT_INTERNAL_PERSISTENT_EVENT_STORE_H_
#define COMPONENTS_FEATURE_ENGAGEMENT_INTERNAL_PERSISTENT_EVENT_STORE_H_

#include <memory>
#include <vector>

#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/feature_engagement/internal/event_store.h"
#include "components/feature_engagement/internal/proto/feature_event.pb.h"
#include "components/leveldb_proto/public/proto_database.h"

namespace feature_engagement {

// A PersistentEventStore provides a DB layer that persists the data to disk.
// The data is retrieved once during the load process and after that this store
// is write only.  Data will be persisted asynchronously so it is not guaranteed
// to always save every write during shutdown.
class PersistentEventStore : public EventStore {
 public:
  // Builds a PersistentEventStore backed by the ProtoDatabase |db|.
  PersistentEventStore(std::unique_ptr<leveldb_proto::ProtoDatabase<Event>> db);
  ~PersistentEventStore() override;

  // EventStore implementation.
  void Load(const OnLoadedCallback& callback) override;
  bool IsReady() const override;
  void WriteEvent(const Event& event) override;
  void DeleteEvent(const std::string& event_name) override;

 private:
  void OnInitComplete(const OnLoadedCallback& callback,
                      leveldb_proto::Enums::InitStatus status);
  void OnLoadComplete(const OnLoadedCallback& callback,
                      bool success,
                      std::unique_ptr<std::vector<Event>> entries);

  const base::FilePath storage_dir_;
  std::unique_ptr<leveldb_proto::ProtoDatabase<Event>> db_;

  // Whether or not the underlying ProtoDatabase is ready.  This will be false
  // until the OnLoadedCallback is broadcast.  It will also be false if loading
  // fails.
  bool ready_;

  base::WeakPtrFactory<PersistentEventStore> weak_ptr_factory_{this};

  DISALLOW_COPY_AND_ASSIGN(PersistentEventStore);
};

}  // namespace feature_engagement

#endif  // COMPONENTS_FEATURE_ENGAGEMENT_INTERNAL_PERSISTENT_EVENT_STORE_H_