summaryrefslogtreecommitdiff
path: root/chromium/components/download/database/in_progress/in_progress_cache_impl.cc
blob: a02ec9390fdeda8024e9092209e6fad26781d911 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// 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.

#include "components/download/database/in_progress/in_progress_cache_impl.h"

#include "base/bind.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/files/important_file_writer.h"
#include "base/task_runner_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/download/database/download_db_conversions.h"

namespace download {

const base::FilePath::CharType kDownloadMetadataStoreFilename[] =
    FILE_PATH_LITERAL("in_progress_download_metadata_store");

namespace {

// Helper functions for |entries_| related operations.
int GetIndexFromEntries(const download_pb::DownloadEntries& entries,
                        const std::string& guid) {
  int size_of_entries = entries.entries_size();
  for (int i = 0; i < size_of_entries; i++) {
    if (entries.entries(i).guid() == guid)
      return i;
  }
  return -1;
}

void AddOrReplaceEntryInEntries(download_pb::DownloadEntries& entries,
                                const DownloadEntry& entry) {
  download_pb::DownloadEntry metadata_entry =
      DownloadDBConversions::DownloadEntryToProto(entry);
  int entry_index = GetIndexFromEntries(entries, metadata_entry.guid());
  download_pb::DownloadEntry* entry_ptr =
      (entry_index < 0) ? entries.add_entries()
                        : entries.mutable_entries(entry_index);
  *entry_ptr = metadata_entry;
}

base::Optional<DownloadEntry> GetEntryFromEntries(
    const download_pb::DownloadEntries& entries,
    const std::string& guid) {
  int entry_index = GetIndexFromEntries(entries, guid);
  if (entry_index < 0)
    return base::nullopt;

  return DownloadDBConversions::DownloadEntryFromProto(
      entries.entries(entry_index));
}

void RemoveEntryFromEntries(download_pb::DownloadEntries& entries,
                            const std::string& guid) {
  int entry_index = GetIndexFromEntries(entries, guid);
  if (entry_index >= 0)
    entries.mutable_entries()->DeleteSubrange(entry_index, 1);
}

// Helper functions for file read/write operations.
std::vector<char> ReadEntriesFromFile(base::FilePath file_path) {
  if (file_path.empty())
    return std::vector<char>();

  // Check validity of file.
  base::File entries_file(file_path,
                          base::File::FLAG_OPEN | base::File::FLAG_READ);
  if (!entries_file.IsValid()) {
    // The file just doesn't exist yet (potentially because no entries have been
    // written yet) so we can't read from it.
    return std::vector<char>();
  }

  // Get file info.
  base::File::Info info;
  if (!entries_file.GetInfo(&info)) {
    LOG(ERROR) << "Could not read download entries from file "
               << "because get info failed.";
    return std::vector<char>();
  }

  if (info.is_directory) {
    LOG(ERROR) << "Could not read download entries from file "
               << "because file is a directory.";
    return std::vector<char>();
  }

  // Read and parse file.
  if (info.size < 0) {
    LOG(ERROR) << "Could not read download entries from file "
               << "because the file size was unexpected.";
    return std::vector<char>();
  }

  auto file_data = std::vector<char>(info.size);
  if (entries_file.Read(0, file_data.data(), info.size) < 0) {
    LOG(ERROR) << "Could not read download entries from file "
               << "because there was a read failure.";
    return std::vector<char>();
  }

  return file_data;
}

std::string EntriesToString(const download_pb::DownloadEntries& entries) {
  std::string entries_string;
  if (!entries.SerializeToString(&entries_string)) {
    // TODO(crbug.com/778425): Have more robust error-handling for serialization
    // error.
    LOG(ERROR) << "Could not write download entries to file "
               << "because of a serialization issue.";
    return std::string();
  }
  return entries_string;
}

void WriteEntriesToFile(const std::string& entries, base::FilePath file_path) {
  if (file_path.empty())
    return;

  if (!base::ImportantFileWriter::WriteFileAtomically(file_path, entries)) {
    LOG(ERROR) << "Could not write download entries to file: "
               << file_path.value();
  }
}
}  // namespace

InProgressCacheImpl::InProgressCacheImpl(
    const base::FilePath& cache_file_path,
    const scoped_refptr<base::SequencedTaskRunner>& task_runner)
    : file_path_(cache_file_path),
      initialization_status_(CACHE_UNINITIALIZED),
      task_runner_(task_runner),
      weak_ptr_factory_(this) {}

InProgressCacheImpl::~InProgressCacheImpl() = default;

void InProgressCacheImpl::Initialize(base::OnceClosure callback) {
  // If it's already initialized, just run the callback.
  if (initialization_status_ == CACHE_INITIALIZED) {
    base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
                                                  std::move(callback));
    return;
  }

  // If uninitialized, initialize |entries_| by reading from file.
  if (initialization_status_ == CACHE_UNINITIALIZED) {
    base::PostTaskAndReplyWithResult(
        task_runner_.get(), FROM_HERE,
        base::BindOnce(&ReadEntriesFromFile, file_path_),
        base::BindOnce(&InProgressCacheImpl::OnInitialized,
                       weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
  }
}

void InProgressCacheImpl::OnInitialized(base::OnceClosure callback,
                                        const std::vector<char>& entries) {
  if (!entries.empty()) {
    if (!entries_.ParseFromArray(entries.data(), entries.size())) {
      // TODO(crbug.com/778425): Get UMA for errors.
      LOG(ERROR) << "Could not read download entries from file "
                 << "because there was a parse failure.";
      return;
    }
  }

  initialization_status_ = CACHE_INITIALIZED;

  std::move(callback).Run();
}

void InProgressCacheImpl::AddOrReplaceEntry(const DownloadEntry& entry) {
  if (initialization_status_ != CACHE_INITIALIZED) {
    LOG(ERROR) << "Cache is not initialized, cannot AddOrReplaceEntry.";
    return;
  }

  // Update |entries_|.
  AddOrReplaceEntryInEntries(entries_, entry);

  // Serialize |entries_| and write to file.
  std::string entries_string = EntriesToString(entries_);
  task_runner_->PostTask(FROM_HERE, base::BindOnce(&WriteEntriesToFile,
                                                   entries_string, file_path_));
}

base::Optional<DownloadEntry> InProgressCacheImpl::RetrieveEntry(
    const std::string& guid) {
  if (initialization_status_ != CACHE_INITIALIZED) {
    LOG(ERROR) << "Cache is not initialized, cannot RetrieveEntry.";
    return base::nullopt;
  }

  return GetEntryFromEntries(entries_, guid);
}

void InProgressCacheImpl::RemoveEntry(const std::string& guid) {
  if (initialization_status_ != CACHE_INITIALIZED) {
    LOG(ERROR) << "Cache is not initialized, cannot RemoveEntry.";
    return;
  }

  // Update |entries_|.
  RemoveEntryFromEntries(entries_, guid);

  // Serialize |entries_| and write to file.
  std::string entries_string = EntriesToString(entries_);
  task_runner_->PostTask(FROM_HERE, base::BindOnce(&WriteEntriesToFile,
                                                   entries_string, file_path_));
}

}  // namespace download