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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
// 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 <utility>
#include "base/metrics/histogram.h"
#include "chrome/browser/chromeos/drive/drive_feed_processor.h"
#include "chrome/browser/chromeos/drive/drive_files.h"
#include "chrome/browser/chromeos/drive/drive_resource_metadata.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace drive {
FeedToFileResourceMapUmaStats::FeedToFileResourceMapUmaStats()
: num_regular_files(0),
num_hosted_documents(0) {
}
FeedToFileResourceMapUmaStats::~FeedToFileResourceMapUmaStats() {
}
DriveFeedProcessor::DriveFeedProcessor(
DriveResourceMetadata* resource_metadata)
: resource_metadata_(resource_metadata) {
}
DriveFeedProcessor::~DriveFeedProcessor() {
}
DriveFileError DriveFeedProcessor::ApplyFeeds(
const ScopedVector<gdata::DocumentFeed>& feed_list,
int64 start_changestamp,
int64 root_feed_changestamp,
std::set<FilePath>* changed_dirs) {
bool is_delta_feed = start_changestamp != 0;
resource_metadata_->set_origin(FROM_SERVER);
int64 delta_feed_changestamp = 0;
FeedToFileResourceMapUmaStats uma_stats;
FileResourceIdMap file_map;
DriveFileError error = FeedToFileResourceMap(feed_list,
&file_map,
&delta_feed_changestamp,
&uma_stats);
if (error != DRIVE_FILE_OK)
return error;
ApplyFeedFromFileUrlMap(
is_delta_feed,
is_delta_feed ? delta_feed_changestamp : root_feed_changestamp,
&file_map,
changed_dirs);
// Shouldn't record histograms when processing delta feeds.
if (!is_delta_feed)
UpdateFileCountUmaHistograms(uma_stats);
return DRIVE_FILE_OK;
}
void DriveFeedProcessor::UpdateFileCountUmaHistograms(
const FeedToFileResourceMapUmaStats& uma_stats) const {
const int num_total_files =
uma_stats.num_hosted_documents + uma_stats.num_regular_files;
UMA_HISTOGRAM_COUNTS("Drive.NumberOfRegularFiles",
uma_stats.num_regular_files);
UMA_HISTOGRAM_COUNTS("Drive.NumberOfHostedDocuments",
uma_stats.num_hosted_documents);
UMA_HISTOGRAM_COUNTS("Drive.NumberOfTotalFiles", num_total_files);
}
void DriveFeedProcessor::ApplyFeedFromFileUrlMap(
bool is_delta_feed,
int64 feed_changestamp,
FileResourceIdMap* file_map,
std::set<FilePath>* changed_dirs) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(changed_dirs);
if (!is_delta_feed) { // Full update.
resource_metadata_->root()->RemoveChildren();
changed_dirs->insert(resource_metadata_->root()->GetFilePath());
}
resource_metadata_->set_largest_changestamp(feed_changestamp);
scoped_ptr<DriveResourceMetadata> orphaned_resources(
new DriveResourceMetadata);
// Go through all entries generated by the feed and apply them to the local
// snapshot of the file system.
for (FileResourceIdMap::iterator it = file_map->begin();
it != file_map->end();) {
// Ensure that the entry is deleted, unless the ownership is explicitly
// transferred by entry.release().
scoped_ptr<DriveEntry> entry(it->second);
DCHECK_EQ(it->first, entry->resource_id());
// Erase the entry so the deleted entry won't be referenced.
file_map->erase(it++);
DriveEntry* old_entry =
resource_metadata_->GetEntryByResourceId(entry->resource_id());
DriveDirectory* dest_dir = NULL;
if (entry->is_deleted()) { // Deleted file/directory.
DVLOG(1) << "Removing file " << entry->base_name();
if (!old_entry)
continue;
dest_dir = old_entry->parent();
if (!dest_dir) {
NOTREACHED();
continue;
}
RemoveEntryFromDirectoryAndCollectChangedDirectories(
dest_dir, old_entry, changed_dirs);
} else if (old_entry) { // Change or move of existing entry.
// Please note that entry rename is just a special case of change here
// since name is just one of the properties that can change.
DVLOG(1) << "Changed file " << entry->base_name();
dest_dir = old_entry->parent();
if (!dest_dir) {
NOTREACHED();
continue;
}
// Move children files over if we are dealing with directories.
if (old_entry->AsDriveDirectory() && entry->AsDriveDirectory()) {
entry->AsDriveDirectory()->TakeOverEntries(
old_entry->AsDriveDirectory());
}
// Remove the old instance of this entry.
RemoveEntryFromDirectoryAndCollectChangedDirectories(
dest_dir, old_entry, changed_dirs);
// Did we actually move the new file to another directory?
if (dest_dir->resource_id() != entry->parent_resource_id()) {
changed_dirs->insert(dest_dir->GetFilePath());
dest_dir = FindDirectoryForNewEntry(entry.get(),
*file_map,
orphaned_resources.get());
}
DCHECK(dest_dir);
AddEntryToDirectoryAndCollectChangedDirectories(
entry.release(),
dest_dir,
orphaned_resources.get(),
changed_dirs);
} else { // Adding a new file.
dest_dir = FindDirectoryForNewEntry(entry.get(),
*file_map,
orphaned_resources.get());
DCHECK(dest_dir);
AddEntryToDirectoryAndCollectChangedDirectories(
entry.release(),
dest_dir,
orphaned_resources.get(),
changed_dirs);
}
// Record changed directory if this was a delta feed and the parent
// directory is already properly rooted within its parent.
if (dest_dir && (dest_dir->parent() ||
dest_dir == resource_metadata_->root()) &&
dest_dir != orphaned_resources->root() && is_delta_feed) {
changed_dirs->insert(dest_dir->GetFilePath());
}
}
// All entry must be erased from the map.
DCHECK(file_map->empty());
}
// static
void DriveFeedProcessor::AddEntryToDirectoryAndCollectChangedDirectories(
DriveEntry* entry,
DriveDirectory* directory,
DriveResourceMetadata* orphaned_resources,
std::set<FilePath>* changed_dirs) {
directory->AddEntry(entry);
if (entry->AsDriveDirectory() && directory != orphaned_resources->root())
changed_dirs->insert(entry->GetFilePath());
}
// static
void DriveFeedProcessor::RemoveEntryFromDirectoryAndCollectChangedDirectories(
DriveDirectory* directory,
DriveEntry* entry,
std::set<FilePath>* changed_dirs) {
// Get the list of all sub-directory paths, so we can notify their listeners
// that they are smoked.
DriveDirectory* dir = entry->AsDriveDirectory();
if (dir)
dir->GetChildDirectoryPaths(changed_dirs);
directory->RemoveEntry(entry);
}
DriveDirectory* DriveFeedProcessor::FindDirectoryForNewEntry(
DriveEntry* new_entry,
const FileResourceIdMap& file_map,
DriveResourceMetadata* orphaned_resources) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DriveDirectory* dir = NULL;
// Added file.
const std::string& parent_id = new_entry->parent_resource_id();
if (parent_id.empty()) {
dir = resource_metadata_->root();
DVLOG(1) << "Root parent for " << new_entry->base_name();
} else {
DriveEntry* entry = resource_metadata_->GetEntryByResourceId(parent_id);
dir = entry ? entry->AsDriveDirectory() : NULL;
if (!dir) {
// The parent directory was also added with this set of feeds.
FileResourceIdMap::const_iterator find_iter =
file_map.find(parent_id);
dir = (find_iter != file_map.end() &&
find_iter->second) ?
find_iter->second->AsDriveDirectory() : NULL;
if (dir) {
DVLOG(1) << "Found parent for " << new_entry->base_name()
<< " in file_map " << parent_id;
} else {
DVLOG(1) << "Adding orphan " << new_entry->GetFilePath().value();
dir = orphaned_resources->root();
}
}
}
return dir;
}
DriveFileError DriveFeedProcessor::FeedToFileResourceMap(
const ScopedVector<gdata::DocumentFeed>& feed_list,
FileResourceIdMap* file_map,
int64* feed_changestamp,
FeedToFileResourceMapUmaStats* uma_stats) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(uma_stats);
DriveFileError error = DRIVE_FILE_OK;
uma_stats->num_regular_files = 0;
uma_stats->num_hosted_documents = 0;
for (size_t i = 0; i < feed_list.size(); ++i) {
const gdata::DocumentFeed* feed = feed_list[i];
// Get upload url from the root feed. Links for all other collections will
// be handled in GDatadirectory::FromDocumentEntry();
if (i == 0) {
const gdata::Link* root_feed_upload_link =
feed->GetLinkByType(gdata::Link::LINK_RESUMABLE_CREATE_MEDIA);
if (root_feed_upload_link)
resource_metadata_->root()->set_upload_url(
root_feed_upload_link->href());
*feed_changestamp = feed->largest_changestamp();
DCHECK_GE(*feed_changestamp, 0);
}
for (ScopedVector<gdata::DocumentEntry>::const_iterator iter =
feed->entries().begin();
iter != feed->entries().end(); ++iter) {
gdata::DocumentEntry* doc = *iter;
scoped_ptr<DriveEntry> entry =
resource_metadata_->FromDocumentEntry(*doc);
// Some document entries don't map into files (i.e. sites).
if (!entry.get())
continue;
// Count the number of files.
DriveFile* as_file = entry->AsDriveFile();
if (as_file) {
if (as_file->is_hosted_document())
++uma_stats->num_hosted_documents;
else
++uma_stats->num_regular_files;
}
FileResourceIdMap::iterator map_entry =
file_map->find(entry->resource_id());
// An entry with the same self link may already exist, so we need to
// release the existing DriveEntry instance before overwriting the
// entry with another DriveEntry instance.
if (map_entry != file_map->end()) {
LOG(WARNING) << "Found duplicate file "
<< map_entry->second->base_name();
delete map_entry->second;
file_map->erase(map_entry);
}
// Must use this temporary because entry.release() may be evaluated
// first in make_pair.
const std::string& resource_id = entry->resource_id();
file_map->insert(std::make_pair(resource_id, entry.release()));
}
}
if (error != DRIVE_FILE_OK) {
// If the code above fails to parse a feed, any DriveEntry instance
// added to |file_by_url| is not managed by a DriveDirectory instance,
// so we need to explicitly release them here.
STLDeleteValues(file_map);
}
return error;
}
} // namespace drive
|