Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / file_system / touch_operation.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/drive/file_system/touch_operation.h"
6
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/sequenced_task_runner.h"
10 #include "base/time/time.h"
11 #include "chrome/browser/chromeos/drive/file_change.h"
12 #include "chrome/browser/chromeos/drive/file_errors.h"
13 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
14 #include "chrome/browser/chromeos/drive/resource_metadata.h"
15 #include "content/public/browser/browser_thread.h"
16
17 using content::BrowserThread;
18
19 namespace drive {
20 namespace file_system {
21
22 namespace {
23
24 // Updates the timestamps of the entry specified by |file_path|.
25 FileError UpdateLocalState(internal::ResourceMetadata* metadata,
26                            const base::FilePath& file_path,
27                            const base::Time& last_access_time,
28                            const base::Time& last_modified_time,
29                            ResourceEntry* entry,
30                            std::string* local_id) {
31   FileError error = metadata->GetResourceEntryByPath(file_path, entry);
32   if (error != FILE_ERROR_OK)
33     return error;
34   *local_id = entry->local_id();
35
36   PlatformFileInfoProto* file_info = entry->mutable_file_info();
37   if (!last_access_time.is_null())
38     file_info->set_last_accessed(last_access_time.ToInternalValue());
39   if (!last_modified_time.is_null())
40     file_info->set_last_modified(last_modified_time.ToInternalValue());
41   entry->set_metadata_edit_state(ResourceEntry::DIRTY);
42   entry->set_modification_date(base::Time::Now().ToInternalValue());
43   return metadata->RefreshEntry(*entry);
44 }
45
46 }  // namespace
47
48 TouchOperation::TouchOperation(base::SequencedTaskRunner* blocking_task_runner,
49                                OperationDelegate* delegate,
50                                internal::ResourceMetadata* metadata)
51     : blocking_task_runner_(blocking_task_runner),
52       delegate_(delegate),
53       metadata_(metadata),
54       weak_ptr_factory_(this) {
55 }
56
57 TouchOperation::~TouchOperation() {
58 }
59
60 void TouchOperation::TouchFile(const base::FilePath& file_path,
61                                const base::Time& last_access_time,
62                                const base::Time& last_modified_time,
63                                const FileOperationCallback& callback) {
64   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
65   DCHECK(!callback.is_null());
66
67   std::string* local_id = new std::string;
68   ResourceEntry* entry = new ResourceEntry;
69   base::PostTaskAndReplyWithResult(
70       blocking_task_runner_.get(),
71       FROM_HERE,
72       base::Bind(&UpdateLocalState,
73                  metadata_,
74                  file_path,
75                  last_access_time,
76                  last_modified_time,
77                  entry,
78                  local_id),
79       base::Bind(&TouchOperation::TouchFileAfterUpdateLocalState,
80                  weak_ptr_factory_.GetWeakPtr(),
81                  file_path,
82                  callback,
83                  base::Owned(entry),
84                  base::Owned(local_id)));
85 }
86
87 void TouchOperation::TouchFileAfterUpdateLocalState(
88     const base::FilePath& file_path,
89     const FileOperationCallback& callback,
90     const ResourceEntry* entry,
91     const std::string* local_id,
92     FileError error) {
93   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
94   DCHECK(!callback.is_null());
95
96   FileChange changed_files;
97   changed_files.Update(
98       file_path,
99       entry->file_info().is_directory() ?
100           FileChange::FILE_TYPE_DIRECTORY : FileChange::FILE_TYPE_FILE,
101       FileChange::ADD_OR_UPDATE);
102
103   if (error == FILE_ERROR_OK) {
104     delegate_->OnFileChangedByOperation(changed_files);
105     delegate_->OnEntryUpdatedByOperation(*local_id);
106   }
107   callback.Run(error);
108 }
109
110 }  // namespace file_system
111 }  // namespace drive