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
|
// Copyright 2015 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 "components/drive/file_system/set_property_operation.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/sequenced_task_runner.h"
#include "components/drive/drive.pb.h"
#include "components/drive/file_errors.h"
#include "components/drive/file_system/operation_delegate.h"
#include "components/drive/job_scheduler.h"
#include "components/drive/resource_metadata.h"
namespace drive {
namespace file_system {
namespace {
// Adds the property to resource entry. Overwrites existing property if exists.
// If no change has been made (same key, visibility and value is already added)
// then FILE_ERROR_EXISTS is returned.
FileError UpdateLocalState(internal::ResourceMetadata* metadata,
const base::FilePath& file_path,
google_apis::drive::Property::Visibility visibility,
const std::string& key,
const std::string& value,
ResourceEntry* entry) {
using google_apis::drive::Property;
FileError error = metadata->GetResourceEntryByPath(file_path, entry);
if (error != FILE_ERROR_OK)
return error;
Property_Visibility proto_visibility = Property_Visibility_PRIVATE;
switch (visibility) {
case Property::VISIBILITY_PRIVATE:
proto_visibility = Property_Visibility_PRIVATE;
break;
case Property::VISIBILITY_PUBLIC:
proto_visibility = Property_Visibility_PUBLIC;
break;
}
::drive::Property* property_to_update = nullptr;
for (auto& property : *entry->mutable_new_properties()) {
if (property.visibility() == proto_visibility && property.key() == key) {
// Exactly the same property exists, so don't update the local state.
if (property.value() == value)
return FILE_ERROR_EXISTS;
property_to_update = &property;
break;
}
}
// If no property to update has been found, then add a new one.
if (!property_to_update)
property_to_update = entry->mutable_new_properties()->Add();
property_to_update->set_visibility(proto_visibility);
property_to_update->set_key(key);
property_to_update->set_value(value);
entry->set_metadata_edit_state(ResourceEntry::DIRTY);
entry->set_modification_date(base::Time::Now().ToInternalValue());
return metadata->RefreshEntry(*entry);
}
} // namespace
SetPropertyOperation::SetPropertyOperation(
base::SequencedTaskRunner* blocking_task_runner,
OperationDelegate* delegate,
internal::ResourceMetadata* metadata)
: blocking_task_runner_(blocking_task_runner),
delegate_(delegate),
metadata_(metadata),
weak_ptr_factory_(this) {
}
SetPropertyOperation::~SetPropertyOperation() {
}
void SetPropertyOperation::SetProperty(
const base::FilePath& file_path,
google_apis::drive::Property::Visibility visibility,
const std::string& key,
const std::string& value,
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, visibility, key,
value, entry),
base::Bind(&SetPropertyOperation::SetPropertyAfterUpdateLocalState,
weak_ptr_factory_.GetWeakPtr(), callback, base::Owned(entry)));
}
void SetPropertyOperation::SetPropertyAfterUpdateLocalState(
const FileOperationCallback& callback,
const ResourceEntry* entry,
FileError result) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!callback.is_null());
if (result == FILE_ERROR_OK) {
// Do not notify about the file change, as properties are write only and
// cannot be read, so there is no visible change.
delegate_->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED),
entry->local_id());
}
// Even if exists, return success, as the set property operation always
// overwrites existing values.
callback.Run(result == FILE_ERROR_EXISTS ? FILE_ERROR_OK : result);
}
} // namespace file_system
} // namespace drive
|