summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/file_cache_metadata.h
blob: c2c4f1e25ae48f023b221ed2460a51fdd015ade6 (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
// Copyright (c) 2012 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 CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_METADATA_H_
#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_METADATA_H_

#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"

namespace base {
class SequencedTaskRunner;
}  // namespace base

namespace leveldb {
class DB;
class Iterator;
}  // namespace leveldb

namespace drive {
namespace internal {

// FileCacheMetadata maintains metadata of FileCache's cached files.
// This class only manages metadata. File operations are done by FileCache.
// All member access including ctor and dtor must be made on the blocking pool.
class FileCacheMetadata {
 public:
  // Database path.
  static const base::FilePath::CharType* kCacheMetadataDBPath;

  // Object to iterate over entries stored in the cache metadata.
  class Iterator {
   public:
    explicit Iterator(scoped_ptr<leveldb::Iterator> it);
    ~Iterator();

    // Returns true if this iterator cannot advance any more and does not point
    // to a valid entry. GetKey(), GetValue() and Advance() should not be called
    // in such cases.
    bool IsAtEnd() const;

    // Returns the key of the entry currently pointed by this object.
    std::string GetKey() const;

    // Returns the value of the entry currently pointed by this object.
    const FileCacheEntry& GetValue() const;

    // Advances to the next entry.
    void Advance();

    // Returns true if this object has encountered any error.
    bool HasError() const;

   private:
    // Used to implement Advance().
    void AdvanceInternal();

    scoped_ptr<leveldb::Iterator> it_;
    FileCacheEntry entry_;

    DISALLOW_COPY_AND_ASSIGN(Iterator);
  };

  // Tests are allowed to pass NULL as |blocking_task_runner|.
  explicit FileCacheMetadata(base::SequencedTaskRunner* blocking_task_runner);

  ~FileCacheMetadata();

  // Initialize the cache metadata store. Returns true on success.
  bool Initialize(const std::vector<base::FilePath>& cache_paths);
  // Adds a new cache entry corresponding to |resource_id| if it doesn't
  // exist, otherwise update the existing entry.
  void AddOrUpdateCacheEntry(const std::string& resource_id,
                             const FileCacheEntry& cache_entry);

  // Removes entry corresponding to |resource_id| from cache map.
  void RemoveCacheEntry(const std::string& resource_id);

  // Gets the cache entry for file corresponding to |resource_id| and |md5|
  // and returns true if entry exists in cache map.  Otherwise, returns false.
  // |md5| can be empty if only matching |resource_id| is desired.
  bool GetCacheEntry(const std::string& resource_id,
                     const std::string& md5,
                     FileCacheEntry* entry);

  // Returns an object to iterate over entries.
  scoped_ptr<Iterator> GetIterator();

 private:
  // Checks whether the current thread is on the right sequenced worker pool
  // with the right sequence ID. If not, DCHECK will fail.
  void AssertOnSequencedWorkerPool();

  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
  scoped_ptr<leveldb::DB> level_db_;

  DISALLOW_COPY_AND_ASSIGN(FileCacheMetadata);
};

}  // namespace internal
}  // namespace drive

#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_CACHE_METADATA_H_