summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/drive_prefetcher.h
blob: e2eca078a38da7310b51d3be69b336ec196a50f3 (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
// 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_PREFETCHER_H_
#define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_PREFETCHER_H_

#include <set>
#include <string>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"
#include "chrome/browser/chromeos/drive/drive_file_system_interface.h"
#include "chrome/browser/chromeos/drive/drive_file_system_observer.h"

namespace base {
class FilePath;
}

namespace google_apis {
class EventLogger;
}

namespace drive {

// The parameters for DrivePrefetcher construction.
struct DrivePrefetcherOptions {
  DrivePrefetcherOptions();  // Sets the default values.

  int initial_prefetch_count;
  int64 prefetch_file_size_limit;
};

// DrivePrefetcher is used to observe and scan the Drive file system for
// maintaining the prioritized list of files to prefetch into the cache.
//
// All the methods (including ctor and dtor) must be called from UI thread.
class DrivePrefetcher : public DriveFileSystemObserver {
 public:
  DrivePrefetcher(DriveFileSystemInterface* file_system,
                  google_apis::EventLogger* event_logger,
                  const DrivePrefetcherOptions& options);
  virtual ~DrivePrefetcher();

  // DriveFileSystemObserver overrides.
  virtual void OnInitialLoadFinished() OVERRIDE;
  virtual void OnDirectoryChanged(
      const base::FilePath& directory_path) OVERRIDE;

 private:
  // Scans the file system and calls DoPrefetch().
  void StartPrefetcherCycle();

  // Fetches the file with the highest prefetch priority. If prefetching is
  // currently suspended, do nothing.
  void DoPrefetch();

  // Called when DoPrefetch is done.
  void OnPrefetchFinished(const std::string& resource_id,
                          DriveFileError error,
                          const base::FilePath& file_path,
                          const std::string& mime_type,
                          DriveFileType file_type);

  // Creates the |queue_| from the list of files with |latest_| timestamps.
  void ReconstructQueue();

  // Helper methods to traverse over the file system.
  void VisitFile(const DriveEntryProto& entry);
  void VisitDirectory(const base::FilePath& directory_path);
  void OnReadDirectory(const base::FilePath& directory_path,
                       DriveFileError error,
                       bool hide_hosted_documents,
                       scoped_ptr<DriveEntryProtoVector> entries);
  void OnReadDirectoryFinished();

  // Keeps the kNumberOfLatestFilesToKeepInCache latest files in the filesystem.
  typedef bool (*PrefetchPriorityComparator)(const DriveEntryProto&,
                                             const DriveEntryProto&);
  typedef std::set<DriveEntryProto, PrefetchPriorityComparator> LatestFileSet;
  LatestFileSet latest_files_;

  // Number of in-flight |VisitDirectory| calls that has not finished yet.
  int number_of_inflight_traversals_;

  // Number of files to put into prefetch queue
  int initial_prefetch_count_;

  // The maximum file size for prefetched files. Files larger than the limit is
  // ignored from the prefetcher.
  int64 prefetch_file_size_limit_;

  // File system is owned by DriveSystemService.
  DriveFileSystemInterface* file_system_;

  // Event logger is owned by DriveSystemService.
  google_apis::EventLogger* event_logger_;

  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<DrivePrefetcher> weak_ptr_factory_;
  DISALLOW_COPY_AND_ASSIGN(DrivePrefetcher);
};

}  // namespace drive

#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_PREFETCHER_H_