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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
// 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/file_system/copy_operation.h"
#include <string>
#include "base/task_runner_util.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"
#include "chrome/browser/chromeos/drive/file_cache.h"
#include "chrome/browser/chromeos/drive/file_change.h"
#include "chrome/browser/chromeos/drive/file_system/create_file_operation.h"
#include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
#include "chrome/browser/chromeos/drive/file_system_core_util.h"
#include "chrome/browser/chromeos/drive/job_scheduler.h"
#include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
#include "chrome/browser/chromeos/drive/resource_metadata.h"
#include "components/drive/drive_api_util.h"
#include "google_apis/drive/drive_api_parser.h"
namespace drive {
namespace file_system {
struct CopyOperation::CopyParams {
base::FilePath src_file_path;
base::FilePath dest_file_path;
bool preserve_last_modified;
FileOperationCallback callback;
ResourceEntry src_entry;
ResourceEntry parent_entry;
};
// Enum for categorizing where a gdoc represented by a JSON file exists.
enum JsonGdocLocationType {
NOT_IN_METADATA,
IS_ORPHAN,
HAS_PARENT,
};
struct CopyOperation::TransferJsonGdocParams {
TransferJsonGdocParams(const FileOperationCallback& callback,
const std::string& resource_id,
const ResourceEntry& parent_entry,
const std::string& new_title)
: callback(callback),
resource_id(resource_id),
parent_resource_id(parent_entry.resource_id()),
parent_local_id(parent_entry.local_id()),
new_title(new_title),
location_type(NOT_IN_METADATA) {
}
// Parameters supplied or calculated from operation arguments.
const FileOperationCallback callback;
const std::string resource_id;
const std::string parent_resource_id;
const std::string parent_local_id;
const std::string new_title;
// Values computed during operation.
JsonGdocLocationType location_type; // types where the gdoc file is located.
std::string local_id; // the local_id of the file (if exists in metadata.)
base::FilePath changed_path;
};
namespace {
FileError TryToCopyLocally(internal::ResourceMetadata* metadata,
internal::FileCache* cache,
CopyOperation::CopyParams* params,
std::vector<std::string>* updated_local_ids,
bool* directory_changed,
bool* should_copy_on_server) {
FileError error = metadata->GetResourceEntryByPath(params->src_file_path,
¶ms->src_entry);
if (error != FILE_ERROR_OK)
return error;
error = metadata->GetResourceEntryByPath(params->dest_file_path.DirName(),
¶ms->parent_entry);
if (error != FILE_ERROR_OK)
return error;
if (!params->parent_entry.file_info().is_directory())
return FILE_ERROR_NOT_A_DIRECTORY;
// Drive File System doesn't support recursive copy.
if (params->src_entry.file_info().is_directory())
return FILE_ERROR_NOT_A_FILE;
// Check destination.
ResourceEntry dest_entry;
error = metadata->GetResourceEntryByPath(params->dest_file_path, &dest_entry);
switch (error) {
case FILE_ERROR_OK:
// File API spec says it is an error to try to "copy a file to a path
// occupied by a directory".
if (dest_entry.file_info().is_directory())
return FILE_ERROR_INVALID_OPERATION;
// Move the existing entry to the trash.
dest_entry.set_parent_local_id(util::kDriveTrashDirLocalId);
error = metadata->RefreshEntry(dest_entry);
if (error != FILE_ERROR_OK)
return error;
updated_local_ids->push_back(dest_entry.local_id());
*directory_changed = true;
break;
case FILE_ERROR_NOT_FOUND:
break;
default:
return error;
}
// If the cache file is not present and the entry exists on the server,
// server side copy should be used.
if (!params->src_entry.file_specific_info().cache_state().is_present() &&
!params->src_entry.resource_id().empty()) {
*should_copy_on_server = true;
return FILE_ERROR_OK;
}
// Copy locally.
ResourceEntry entry;
const int64 now = base::Time::Now().ToInternalValue();
entry.set_title(params->dest_file_path.BaseName().AsUTF8Unsafe());
entry.set_parent_local_id(params->parent_entry.local_id());
entry.mutable_file_specific_info()->set_content_mime_type(
params->src_entry.file_specific_info().content_mime_type());
entry.set_metadata_edit_state(ResourceEntry::DIRTY);
entry.set_modification_date(base::Time::Now().ToInternalValue());
entry.mutable_file_info()->set_last_modified(
params->preserve_last_modified ?
params->src_entry.file_info().last_modified() : now);
entry.mutable_file_info()->set_last_accessed(now);
std::string local_id;
error = metadata->AddEntry(entry, &local_id);
if (error != FILE_ERROR_OK)
return error;
updated_local_ids->push_back(local_id);
*directory_changed = true;
if (!params->src_entry.file_specific_info().cache_state().is_present()) {
DCHECK(params->src_entry.resource_id().empty());
// Locally created empty file may have no cache file.
return FILE_ERROR_OK;
}
base::FilePath cache_file_path;
error = cache->GetFile(params->src_entry.local_id(), &cache_file_path);
if (error != FILE_ERROR_OK)
return error;
return cache->Store(local_id, std::string(), cache_file_path,
internal::FileCache::FILE_OPERATION_COPY);
}
// Stores the entry returned from the server and returns its path.
FileError UpdateLocalStateForServerSideOperation(
internal::ResourceMetadata* metadata,
scoped_ptr<google_apis::FileResource> file_resource,
ResourceEntry* entry,
base::FilePath* file_path) {
DCHECK(file_resource);
std::string parent_resource_id;
if (!ConvertFileResourceToResourceEntry(
*file_resource, entry, &parent_resource_id) ||
parent_resource_id.empty())
return FILE_ERROR_NOT_A_FILE;
std::string parent_local_id;
FileError error = metadata->GetIdByResourceId(parent_resource_id,
&parent_local_id);
if (error != FILE_ERROR_OK)
return error;
entry->set_parent_local_id(parent_local_id);
std::string local_id;
error = metadata->AddEntry(*entry, &local_id);
// Depending on timing, the metadata may have inserted via change list
// already. So, FILE_ERROR_EXISTS is not an error.
if (error == FILE_ERROR_EXISTS)
error = metadata->GetIdByResourceId(entry->resource_id(), &local_id);
if (error != FILE_ERROR_OK)
return error;
return metadata->GetFilePath(local_id, file_path);
}
// Stores the file at |local_file_path| to the cache as a content of entry at
// |remote_dest_path|, and marks it dirty.
FileError UpdateLocalStateForScheduleTransfer(
internal::ResourceMetadata* metadata,
internal::FileCache* cache,
const base::FilePath& local_src_path,
const base::FilePath& remote_dest_path,
ResourceEntry* entry,
std::string* local_id) {
FileError error = metadata->GetIdByPath(remote_dest_path, local_id);
if (error != FILE_ERROR_OK)
return error;
error = metadata->GetResourceEntryById(*local_id, entry);
if (error != FILE_ERROR_OK)
return error;
return cache->Store(*local_id, std::string(), local_src_path,
internal::FileCache::FILE_OPERATION_COPY);
}
// Gets the file size of the |local_path|, and the ResourceEntry for the parent
// of |remote_path| to prepare the necessary information for transfer.
FileError PrepareTransferFileFromLocalToRemote(
internal::ResourceMetadata* metadata,
const base::FilePath& local_src_path,
const base::FilePath& remote_dest_path,
std::string* gdoc_resource_id,
ResourceEntry* parent_entry) {
FileError error = metadata->GetResourceEntryByPath(
remote_dest_path.DirName(), parent_entry);
if (error != FILE_ERROR_OK)
return error;
// The destination's parent must be a directory.
if (!parent_entry->file_info().is_directory())
return FILE_ERROR_NOT_A_DIRECTORY;
// Try to parse GDoc File and extract the resource id, if necessary.
// Failing isn't problem. It'd be handled as a regular file, then.
if (util::HasHostedDocumentExtension(local_src_path))
*gdoc_resource_id = util::ReadResourceIdFromGDocFile(local_src_path);
return FILE_ERROR_OK;
}
// Performs local work before server-side work for transferring JSON-represented
// gdoc files.
FileError LocalWorkForTransferJsonGdocFile(
internal::ResourceMetadata* metadata,
CopyOperation::TransferJsonGdocParams* params) {
std::string local_id;
FileError error = metadata->GetIdByResourceId(params->resource_id, &local_id);
if (error != FILE_ERROR_OK) {
params->location_type = NOT_IN_METADATA;
return error == FILE_ERROR_NOT_FOUND ? FILE_ERROR_OK : error;
}
ResourceEntry entry;
error = metadata->GetResourceEntryById(local_id, &entry);
if (error != FILE_ERROR_OK)
return error;
params->local_id = entry.local_id();
if (entry.parent_local_id() == util::kDriveOtherDirLocalId) {
params->location_type = IS_ORPHAN;
entry.set_title(params->new_title);
entry.set_parent_local_id(params->parent_local_id);
entry.set_metadata_edit_state(ResourceEntry::DIRTY);
entry.set_modification_date(base::Time::Now().ToInternalValue());
error = metadata->RefreshEntry(entry);
if (error != FILE_ERROR_OK)
return error;
return metadata->GetFilePath(local_id, ¶ms->changed_path);
}
params->location_type = HAS_PARENT;
return FILE_ERROR_OK;
}
} // namespace
CopyOperation::CopyOperation(base::SequencedTaskRunner* blocking_task_runner,
OperationDelegate* delegate,
JobScheduler* scheduler,
internal::ResourceMetadata* metadata,
internal::FileCache* cache)
: blocking_task_runner_(blocking_task_runner),
delegate_(delegate),
scheduler_(scheduler),
metadata_(metadata),
cache_(cache),
create_file_operation_(new CreateFileOperation(blocking_task_runner,
delegate,
metadata)),
weak_ptr_factory_(this) {
}
CopyOperation::~CopyOperation() {
DCHECK(thread_checker_.CalledOnValidThread());
}
void CopyOperation::Copy(const base::FilePath& src_file_path,
const base::FilePath& dest_file_path,
bool preserve_last_modified,
const FileOperationCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!callback.is_null());
CopyParams* params = new CopyParams;
params->src_file_path = src_file_path;
params->dest_file_path = dest_file_path;
params->preserve_last_modified = preserve_last_modified;
params->callback = callback;
std::vector<std::string>* updated_local_ids = new std::vector<std::string>;
bool* directory_changed = new bool(false);
bool* should_copy_on_server = new bool(false);
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
base::Bind(&TryToCopyLocally, metadata_, cache_, params,
updated_local_ids, directory_changed, should_copy_on_server),
base::Bind(&CopyOperation::CopyAfterTryToCopyLocally,
weak_ptr_factory_.GetWeakPtr(), base::Owned(params),
base::Owned(updated_local_ids), base::Owned(directory_changed),
base::Owned(should_copy_on_server)));
}
void CopyOperation::CopyAfterTryToCopyLocally(
const CopyParams* params,
const std::vector<std::string>* updated_local_ids,
const bool* directory_changed,
const bool* should_copy_on_server,
FileError error) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!params->callback.is_null());
for (const auto& id : *updated_local_ids) {
// Syncing for copy should be done in background, so pass the BACKGROUND
// context. See: crbug.com/420278.
delegate_->OnEntryUpdatedByOperation(ClientContext(BACKGROUND), id);
}
if (*directory_changed) {
FileChange changed_file;
DCHECK(!params->src_entry.file_info().is_directory());
changed_file.Update(params->dest_file_path, FileChange::FILE_TYPE_FILE,
FileChange::CHANGE_TYPE_ADD_OR_UPDATE);
delegate_->OnFileChangedByOperation(changed_file);
}
if (error != FILE_ERROR_OK || !*should_copy_on_server) {
params->callback.Run(error);
return;
}
if (params->parent_entry.resource_id().empty()) {
// Parent entry may be being synced.
const bool waiting = delegate_->WaitForSyncComplete(
params->parent_entry.local_id(),
base::Bind(&CopyOperation::CopyAfterParentSync,
weak_ptr_factory_.GetWeakPtr(), *params));
if (!waiting)
params->callback.Run(FILE_ERROR_NOT_FOUND);
} else {
CopyAfterGetParentResourceId(*params, ¶ms->parent_entry, FILE_ERROR_OK);
}
}
void CopyOperation::CopyAfterParentSync(const CopyParams& params,
FileError error) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!params.callback.is_null());
if (error != FILE_ERROR_OK) {
params.callback.Run(error);
return;
}
ResourceEntry* parent = new ResourceEntry;
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
base::Bind(&internal::ResourceMetadata::GetResourceEntryById,
base::Unretained(metadata_),
params.parent_entry.local_id(),
parent),
base::Bind(&CopyOperation::CopyAfterGetParentResourceId,
weak_ptr_factory_.GetWeakPtr(),
params,
base::Owned(parent)));
}
void CopyOperation::CopyAfterGetParentResourceId(const CopyParams& params,
const ResourceEntry* parent,
FileError error) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!params.callback.is_null());
if (error != FILE_ERROR_OK) {
params.callback.Run(error);
return;
}
base::FilePath new_title = params.dest_file_path.BaseName();
if (params.src_entry.file_specific_info().is_hosted_document()) {
// Drop the document extension, which should not be in the title.
// TODO(yoshiki): Remove this code with crbug.com/223304.
new_title = new_title.RemoveExtension();
}
base::Time last_modified =
params.preserve_last_modified ?
base::Time::FromInternalValue(
params.src_entry.file_info().last_modified()) : base::Time();
CopyResourceOnServer(
params.src_entry.resource_id(), parent->resource_id(),
new_title.AsUTF8Unsafe(), last_modified, params.callback);
}
void CopyOperation::TransferFileFromLocalToRemote(
const base::FilePath& local_src_path,
const base::FilePath& remote_dest_path,
const FileOperationCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!callback.is_null());
std::string* gdoc_resource_id = new std::string;
ResourceEntry* parent_entry = new ResourceEntry;
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
base::Bind(
&PrepareTransferFileFromLocalToRemote,
metadata_, local_src_path, remote_dest_path,
gdoc_resource_id, parent_entry),
base::Bind(
&CopyOperation::TransferFileFromLocalToRemoteAfterPrepare,
weak_ptr_factory_.GetWeakPtr(),
local_src_path, remote_dest_path, callback,
base::Owned(gdoc_resource_id), base::Owned(parent_entry)));
}
void CopyOperation::TransferFileFromLocalToRemoteAfterPrepare(
const base::FilePath& local_src_path,
const base::FilePath& remote_dest_path,
const FileOperationCallback& callback,
std::string* gdoc_resource_id,
ResourceEntry* parent_entry,
FileError error) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!callback.is_null());
if (error != FILE_ERROR_OK) {
callback.Run(error);
return;
}
// For regular files, schedule the transfer.
if (gdoc_resource_id->empty()) {
ScheduleTransferRegularFile(local_src_path, remote_dest_path, callback);
return;
}
// GDoc file may contain a resource ID in the old format.
const std::string canonicalized_resource_id =
util::CanonicalizeResourceId(*gdoc_resource_id);
// Drop the document extension, which should not be in the title.
// TODO(yoshiki): Remove this code with crbug.com/223304.
const std::string new_title =
remote_dest_path.BaseName().RemoveExtension().AsUTF8Unsafe();
// This is uploading a JSON file representing a hosted document.
TransferJsonGdocParams* params = new TransferJsonGdocParams(
callback, canonicalized_resource_id, *parent_entry, new_title);
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
base::Bind(&LocalWorkForTransferJsonGdocFile, metadata_, params),
base::Bind(&CopyOperation::TransferJsonGdocFileAfterLocalWork,
weak_ptr_factory_.GetWeakPtr(), base::Owned(params)));
}
void CopyOperation::TransferJsonGdocFileAfterLocalWork(
TransferJsonGdocParams* params,
FileError error) {
DCHECK(thread_checker_.CalledOnValidThread());
if (error != FILE_ERROR_OK) {
params->callback.Run(error);
return;
}
switch (params->location_type) {
// When |resource_id| is found in the local metadata and it has a specific
// parent folder, we assume the user's intention is to copy the document and
// thus perform the server-side copy operation.
case HAS_PARENT:
CopyResourceOnServer(params->resource_id,
params->parent_resource_id,
params->new_title,
base::Time(),
params->callback);
break;
// When |resource_id| has no parent, we just set the new destination folder
// as the parent, for sharing the document between the original source.
// This reparenting is already done in LocalWorkForTransferJsonGdocFile().
case IS_ORPHAN: {
DCHECK(!params->changed_path.empty());
// Syncing for copy should be done in background, so pass the BACKGROUND
// context. See: crbug.com/420278.
delegate_->OnEntryUpdatedByOperation(ClientContext(BACKGROUND),
params->local_id);
FileChange changed_file;
changed_file.Update(
params->changed_path,
FileChange::FILE_TYPE_FILE, // This must be a hosted document.
FileChange::CHANGE_TYPE_ADD_OR_UPDATE);
delegate_->OnFileChangedByOperation(changed_file);
params->callback.Run(error);
break;
}
// When the |resource_id| is not in the local metadata, assume it to be a
// document just now shared on the server but not synced locally.
// Same as the IS_ORPHAN case, we want to deal the case by setting parent,
// but this time we need to resort to server side operation.
case NOT_IN_METADATA:
scheduler_->UpdateResource(
params->resource_id, params->parent_resource_id, params->new_title,
base::Time(), base::Time(), google_apis::drive::Properties(),
ClientContext(USER_INITIATED),
base::Bind(&CopyOperation::UpdateAfterServerSideOperation,
weak_ptr_factory_.GetWeakPtr(), params->callback));
break;
}
}
void CopyOperation::CopyResourceOnServer(
const std::string& resource_id,
const std::string& parent_resource_id,
const std::string& new_title,
const base::Time& last_modified,
const FileOperationCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!callback.is_null());
scheduler_->CopyResource(
resource_id, parent_resource_id, new_title, last_modified,
base::Bind(&CopyOperation::UpdateAfterServerSideOperation,
weak_ptr_factory_.GetWeakPtr(),
callback));
}
void CopyOperation::UpdateAfterServerSideOperation(
const FileOperationCallback& callback,
google_apis::DriveApiErrorCode status,
scoped_ptr<google_apis::FileResource> entry) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!callback.is_null());
FileError error = GDataToFileError(status);
if (error != FILE_ERROR_OK) {
callback.Run(error);
return;
}
ResourceEntry* resource_entry = new ResourceEntry;
// The copy on the server side is completed successfully. Update the local
// metadata.
base::FilePath* file_path = new base::FilePath;
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
base::Bind(&UpdateLocalStateForServerSideOperation,
metadata_,
base::Passed(&entry),
resource_entry,
file_path),
base::Bind(&CopyOperation::UpdateAfterLocalStateUpdate,
weak_ptr_factory_.GetWeakPtr(),
callback,
base::Owned(file_path),
base::Owned(resource_entry)));
}
void CopyOperation::UpdateAfterLocalStateUpdate(
const FileOperationCallback& callback,
base::FilePath* file_path,
const ResourceEntry* entry,
FileError error) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!callback.is_null());
if (error == FILE_ERROR_OK) {
FileChange changed_file;
changed_file.Update(*file_path, *entry,
FileChange::CHANGE_TYPE_ADD_OR_UPDATE);
delegate_->OnFileChangedByOperation(changed_file);
}
callback.Run(error);
}
void CopyOperation::ScheduleTransferRegularFile(
const base::FilePath& local_src_path,
const base::FilePath& remote_dest_path,
const FileOperationCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!callback.is_null());
create_file_operation_->CreateFile(
remote_dest_path,
false, // Not exclusive (OK even if a file already exists).
std::string(), // no specific mime type; CreateFile should guess it.
base::Bind(&CopyOperation::ScheduleTransferRegularFileAfterCreate,
weak_ptr_factory_.GetWeakPtr(),
local_src_path, remote_dest_path, callback));
}
void CopyOperation::ScheduleTransferRegularFileAfterCreate(
const base::FilePath& local_src_path,
const base::FilePath& remote_dest_path,
const FileOperationCallback& callback,
FileError error) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!callback.is_null());
if (error != FILE_ERROR_OK) {
callback.Run(error);
return;
}
std::string* local_id = new std::string;
ResourceEntry* entry = new ResourceEntry;
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
base::Bind(&UpdateLocalStateForScheduleTransfer,
metadata_,
cache_,
local_src_path,
remote_dest_path,
entry,
local_id),
base::Bind(
&CopyOperation::ScheduleTransferRegularFileAfterUpdateLocalState,
weak_ptr_factory_.GetWeakPtr(),
callback,
remote_dest_path,
base::Owned(entry),
base::Owned(local_id)));
}
void CopyOperation::ScheduleTransferRegularFileAfterUpdateLocalState(
const FileOperationCallback& callback,
const base::FilePath& remote_dest_path,
const ResourceEntry* entry,
std::string* local_id,
FileError error) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!callback.is_null());
if (error == FILE_ERROR_OK) {
FileChange changed_file;
changed_file.Update(remote_dest_path, *entry,
FileChange::CHANGE_TYPE_ADD_OR_UPDATE);
delegate_->OnFileChangedByOperation(changed_file);
// Syncing for copy should be done in background, so pass the BACKGROUND
// context. See: crbug.com/420278.
delegate_->OnEntryUpdatedByOperation(ClientContext(BACKGROUND), *local_id);
}
callback.Run(error);
}
} // namespace file_system
} // namespace drive
|