summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/drive_cache_metadata.h
blob: ec6471b66275f009667f82c26176ac19989d7006 (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
// 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_DRIVE_CACHE_METADATA_H_
#define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_CACHE_METADATA_H_

#include <map>
#include <vector>
#include <string>

#include "base/callback_forward.h"
#include "base/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"

namespace base {

class SequencedTaskRunner;

}  // namespace base

namespace drive {

class DriveCacheEntry;

// Callback for Iterate().
typedef base::Callback<void(const std::string& resource_id,
                            const DriveCacheEntry& cache_entry)>
    CacheIterateCallback;

// DriveCacheMetadata is interface to maintain metadata of DriveCache's cached
// files. This class only manages metadata. File operations are done by
// DriveCache.
// All member access including ctor and dtor must be made on the blocking pool.
class DriveCacheMetadata {
 public:
  // A map table of cache file's resource id to its CacheEntry* entry.
  typedef std::map<std::string, DriveCacheEntry> CacheMap;

  // Database path.
  static const base::FilePath::CharType* kDriveCacheMetadataDBPath;

  virtual ~DriveCacheMetadata();

  // Creates DriveCacheMetadata instance.
  static scoped_ptr<DriveCacheMetadata> CreateDriveCacheMetadata(
      base::SequencedTaskRunner* blocking_task_runner);

  // Creates DriveCacheMetadata instance. This uses FakeDriveCacheMetadata,
  // which is an in-memory implementation and faster than DriveCacheMetadataDB.
  static scoped_ptr<DriveCacheMetadata> CreateDriveCacheMetadataForTesting(
      base::SequencedTaskRunner* blocking_task_runner);

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

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

  // 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, which may
  // happen when looking for pinned entries where symlinks' filenames have no
  // extension and hence no md5.
  virtual bool GetCacheEntry(const std::string& resource_id,
                             const std::string& md5,
                             DriveCacheEntry* entry) = 0;

  // Removes temporary files (files in CACHE_TYPE_TMP) from the cache map.
  virtual void RemoveTemporaryFiles() = 0;

  // Iterates over all the cache entries synchronously. |callback| is called
  // on each cache entry.
  virtual void Iterate(const CacheIterateCallback& callback) = 0;

 protected:
  explicit DriveCacheMetadata(base::SequencedTaskRunner* blocking_task_runner);

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

 private:
  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;

  DISALLOW_COPY_AND_ASSIGN(DriveCacheMetadata);
};

}  // namespace drive

#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_CACHE_METADATA_H_