summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/drive_prefetcher.cc
blob: 4e3623b2dc1ca405148ce550dabcfe047ff43797 (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
215
216
217
218
219
220
221
222
// 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.

#include "chrome/browser/chromeos/drive/drive_prefetcher.h"

#include "base/bind.h"
#include "base/command_line.h"
#include "base/location.h"
#include "base/message_loop_proxy.h"
#include "chrome/browser/chromeos/drive/drive_file_system_interface.h"
#include "chrome/browser/chromeos/drive/drive_file_system_util.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace drive {

namespace {

const int kInitialPrefetchCount = 100;
const int64 kPrefetchFileSizeLimit = 10 << 20;  // 10MB

// Returns true if prefetching is disabled by a command line option.
bool IsPrefetchDisabled() {
  return CommandLine::ForCurrentProcess()->HasSwitch(
      switches::kDisableDrivePrefetch);
}

// Returns true if |left| has lower priority than |right|.
bool ComparePrefetchPriority(const DriveEntryProto& left,
                             const DriveEntryProto& right) {
  // First, compare last access time. The older entry has less priority.
  if (left.file_info().last_accessed() != right.file_info().last_accessed())
    return left.file_info().last_accessed() < right.file_info().last_accessed();

  // When the entries have the same last access time (which happens quite often
  // because Drive server doesn't set the field until an entry is viewed via
  // drive.google.com), we use last modified time as the tie breaker.
  if (left.file_info().last_modified() != right.file_info().last_modified())
    return left.file_info().last_modified() < right.file_info().last_modified();

  // Two entries have the same priority. To make this function a valid
  // comparator for std::set, we need to differentiate them anyhow.
  return left.resource_id() < right.resource_id();
}

}

DrivePrefetcherOptions::DrivePrefetcherOptions()
    : initial_prefetch_count(kInitialPrefetchCount),
      prefetch_file_size_limit(kPrefetchFileSizeLimit) {
}

DrivePrefetcher::DrivePrefetcher(DriveFileSystemInterface* file_system,
                                 const DrivePrefetcherOptions& options)
    : latest_files_(&ComparePrefetchPriority),
      number_of_inflight_prefetches_(0),
      number_of_inflight_traversals_(0),
      should_suspend_prefetch_(true),
      initial_prefetch_count_(options.initial_prefetch_count),
      prefetch_file_size_limit_(options.prefetch_file_size_limit),
      file_system_(file_system),
      weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  file_system_->AddObserver(this);
}

DrivePrefetcher::~DrivePrefetcher() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  file_system_->RemoveObserver(this);
}

void DrivePrefetcher::OnInitialLoadFinished(DriveFileError error) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  if (error == DRIVE_FILE_OK)
    DoFullScan();
}

void DrivePrefetcher::OnDirectoryChanged(const FilePath& directory_path) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  // TODO(kinaba): crbug.com/156270.
  // Update the list of latest files and the prefetch queue if needed.
}

void DrivePrefetcher::OnSyncTaskStarted() {
  should_suspend_prefetch_ = true;
}

void DrivePrefetcher::OnSyncClientStopped() {
  should_suspend_prefetch_ = true;
}

void DrivePrefetcher::OnSyncClientIdle() {
  should_suspend_prefetch_ = IsPrefetchDisabled();
  DoPrefetch();
}

void DrivePrefetcher::DoFullScan() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  if (IsPrefetchDisabled())
    return;

  FilePath root(util::ExtractDrivePath(util::GetDriveMountPointPath()));
  VisitDirectory(root);
}

void DrivePrefetcher::DoPrefetch() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  if (should_suspend_prefetch_ ||
      queue_.empty() ||
      number_of_inflight_prefetches_ > 0)
    return;

  std::string resource_id = queue_.front();
  queue_.pop_front();

  ++number_of_inflight_prefetches_;
  file_system_->GetFileByResourceId(
      resource_id,
      base::Bind(&DrivePrefetcher::OnPrefetchFinished,
                 weak_ptr_factory_.GetWeakPtr()),
      google_apis::GetContentCallback());
}

void DrivePrefetcher::OnPrefetchFinished(DriveFileError error,
                                         const FilePath& file_path,
                                         const std::string& mime_type,
                                         DriveFileType file_type) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  if (error != DRIVE_FILE_OK) {
    LOG(WARNING) << "Prefetch failed: " << error;
  }

  --number_of_inflight_prefetches_;
  DoPrefetch();  // Start next prefetch.
}

void DrivePrefetcher::ReconstructQueue() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  // Put the files with latest timestamp into the queue.
  queue_.clear();
  for (LatestFileSet::reverse_iterator it = latest_files_.rbegin();
      it != latest_files_.rend(); ++it) {
    queue_.push_back(it->resource_id());
  }
}

void DrivePrefetcher::VisitFile(const DriveEntryProto& entry) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  // Excessively large files will not be fetched.
  if (entry.file_info().size() > prefetch_file_size_limit_)
    return;

  // Remember the file in the set ordered by the |last_accessed| field.
  latest_files_.insert(entry);
  // If the set become too big, forget the oldest entry.
  if (latest_files_.size() > static_cast<size_t>(initial_prefetch_count_))
    latest_files_.erase(latest_files_.begin());
}

void DrivePrefetcher::VisitDirectory(const FilePath& directory_path) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  ++number_of_inflight_traversals_;
  file_system_->ReadDirectoryByPath(
      directory_path,
      base::Bind(&DrivePrefetcher::OnReadDirectory,
                 weak_ptr_factory_.GetWeakPtr(),
                 directory_path));
}

void DrivePrefetcher::OnReadDirectory(
    const FilePath& directory_path,
    DriveFileError error,
    bool hide_hosted_documents,
    scoped_ptr<DriveEntryProtoVector> entries) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  if (error != DRIVE_FILE_OK) {
    LOG(WARNING) << "Directory cannot be scanned by prefetcher: "
                 << directory_path.value();
    OnReadDirectoryFinished();
    return;
  }

  // TODO(kinaba): if entries->size() is so big and it does not contain any
  // directories, the loop below may run on UI thread long time. Consider
  // splitting it into a smaller asynchronous tasks.
  for (size_t i = 0; i < entries->size(); ++i) {
    const DriveEntryProto& entry = (*entries)[i];

    if (entry.file_info().is_directory()) {
      VisitDirectory(directory_path.Append(entry.base_name()));
    } else if (entry.has_file_specific_info() &&
               !entry.file_specific_info().is_hosted_document()) {
      VisitFile(entry);
    }
  }

  OnReadDirectoryFinished();
}

void DrivePrefetcher::OnReadDirectoryFinished() {
  DCHECK(number_of_inflight_traversals_ > 0);

  --number_of_inflight_traversals_;
  if (number_of_inflight_traversals_ == 0) {
    ReconstructQueue();
    DoPrefetch();  // Start prefetching.
  }
}

}  // namespace drive