summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/file_system/touch_operation.cc
blob: c48e4334404b9067fb3f0adbf2447037df8e00e5 (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 2013 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/touch_operation.h"

#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/sequenced_task_runner.h"
#include "base/time/time.h"
#include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
#include "chrome/browser/chromeos/drive/resource_metadata.h"
#include "components/drive/file_change.h"
#include "components/drive/file_errors.h"
#include "components/drive/job_scheduler.h"

namespace drive {
namespace file_system {

namespace {

// Updates the timestamps of the entry specified by |file_path|.
FileError UpdateLocalState(internal::ResourceMetadata* metadata,
                           const base::FilePath& file_path,
                           const base::Time& last_access_time,
                           const base::Time& last_modified_time,
                           ResourceEntry* entry) {
  FileError error = metadata->GetResourceEntryByPath(file_path, entry);
  if (error != FILE_ERROR_OK)
    return error;

  PlatformFileInfoProto* file_info = entry->mutable_file_info();
  if (!last_access_time.is_null())
    file_info->set_last_accessed(last_access_time.ToInternalValue());
  if (!last_modified_time.is_null())
    file_info->set_last_modified(last_modified_time.ToInternalValue());
  entry->set_metadata_edit_state(ResourceEntry::DIRTY);
  entry->set_modification_date(base::Time::Now().ToInternalValue());
  return metadata->RefreshEntry(*entry);
}

}  // namespace

TouchOperation::TouchOperation(base::SequencedTaskRunner* blocking_task_runner,
                               OperationDelegate* delegate,
                               internal::ResourceMetadata* metadata)
    : blocking_task_runner_(blocking_task_runner),
      delegate_(delegate),
      metadata_(metadata),
      weak_ptr_factory_(this) {
}

TouchOperation::~TouchOperation() {
}

void TouchOperation::TouchFile(const base::FilePath& file_path,
                               const base::Time& last_access_time,
                               const base::Time& last_modified_time,
                               const FileOperationCallback& callback) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(!callback.is_null());

  ResourceEntry* entry = new ResourceEntry;
  base::PostTaskAndReplyWithResult(
      blocking_task_runner_.get(), FROM_HERE,
      base::Bind(&UpdateLocalState, metadata_, file_path, last_access_time,
                 last_modified_time, entry),
      base::Bind(&TouchOperation::TouchFileAfterUpdateLocalState,
                 weak_ptr_factory_.GetWeakPtr(), file_path, callback,
                 base::Owned(entry)));
}

void TouchOperation::TouchFileAfterUpdateLocalState(
    const base::FilePath& file_path,
    const FileOperationCallback& callback,
    const ResourceEntry* entry,
    FileError error) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(!callback.is_null());

  FileChange changed_files;
  changed_files.Update(file_path, entry->file_info().is_directory()
                                      ? FileChange::FILE_TYPE_DIRECTORY
                                      : FileChange::FILE_TYPE_FILE,
                       FileChange::CHANGE_TYPE_ADD_OR_UPDATE);

  if (error == FILE_ERROR_OK) {
    delegate_->OnFileChangedByOperation(changed_files);
    delegate_->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED),
                                         entry->local_id());
  }
  callback.Run(error);
}

}  // namespace file_system
}  // namespace drive