summaryrefslogtreecommitdiff
path: root/chromium/components/download/internal/model.h
blob: ce5de88e4500878f1f7667c461b30cfb53991144 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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_DOWNLOAD_INTERNAL_MODEL_H_
#define COMPONENTS_DOWNLOAD_INTERNAL_MODEL_H_

#include <memory>
#include <string>
#include <vector>

#include "components/download/public/clients.h"

namespace download {

struct Entry;
class Store;

// The model that contains a runtime representation of Entry entries. Any
// updates to the model will be persisted to a backing |Store| as necessary.
class Model {
 public:
  // The Client which is responsible for handling all relevant messages from the
  // model.
  class Client {
   public:
    virtual ~Client() = default;

    // Called asynchronously in response to a Model::Initialize call.  If
    // |success| is |false|, initialization of the Model and/or the underlying
    // Store failed.  Initialization of the Model is complete after this
    // callback.  If |success| is true it can be accessed now.
    virtual void OnInitialized(bool success) = 0;

    // Called asynchronously in response to a Model::Destroy call.  If |success|
    // is |false|, destruction of the Model and/or the underlying Store failed.
    // Destruction of the Model is effectively complete after this callback.
    virtual void OnDestroyed(bool success) = 0;

    // Called when an Entry addition is complete.  |success| determines whether
    // or not the entry has been successfully persisted to the Store.
    virtual void OnItemAdded(bool success,
                             DownloadClient client,
                             const std::string& guid) = 0;

    // Called when an Entry update is complete.  |success| determines whether or
    // not the update has been successfully persisted to the Store.
    virtual void OnItemUpdated(bool success,
                               DownloadClient client,
                               const std::string& guid) = 0;

    // Called when an Entry removal is complete.  |success| determines whether
    // or not the entry has been successfully removed from the Store.
    virtual void OnItemRemoved(bool success,
                               DownloadClient client,
                               const std::string& guid) = 0;
  };

  using EntryList = std::vector<Entry*>;

  virtual ~Model() = default;

  // Initializes the Model.  Client::OnInitialized() will be called in response.
  // The Model can be used after that call.
  virtual void Initialize() = 0;

  // Destroys the Model.  Client::OnDestroyed() will be called in response.
  virtual void Destroy() = 0;

  // Adds |entry| to this Model and attempts to write |entry| to the Store.
  // Client::OnItemAdded() will be called in response asynchronously.
  virtual void Add(const Entry& entry) = 0;

  // Updates |entry| in this Model and attempts to write |entry| to the Store.
  // Client::OnItemUpdated() will be called in response asynchronously.
  virtual void Update(const Entry& entry) = 0;

  // Removes the Entry specified by |guid| from this Model and attempts to
  // remove that entry from the Store.  Client::OnItemRemoved() will be called
  // in response asynchronously.
  virtual void Remove(const std::string& guid) = 0;

  // Retrieves an Entry identified by |guid| or |nullptr| if no entry exists.
  // IMPORTANT NOTE: The result of this method should be used immediately and
  // NOT stored.  The underlying data may get updated or removed by any other
  // modifications to this model.
  virtual Entry* Get(const std::string& guid) = 0;

  // Returns a temporary list of Entry objects that this Model stores.
  // IMPORTANT NOTE: Like Get(), the result of this method should be used
  // immediately and NOT stored.  The underlying data may get updated or removed
  // by any other modifications to this model.
  virtual EntryList PeekEntries() = 0;
};

}  // namespace download

#endif  // COMPONENTS_DOWNLOAD_INTERNAL_MODEL_H_