1 // Copyright 2015 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.
5 #include "components/drive/file_system/set_property_operation.h"
8 #include "base/files/file_path.h"
9 #include "base/sequenced_task_runner.h"
10 #include "components/drive/drive.pb.h"
11 #include "components/drive/file_errors.h"
12 #include "components/drive/file_system/operation_delegate.h"
13 #include "components/drive/job_scheduler.h"
14 #include "components/drive/resource_metadata.h"
17 namespace file_system
{
21 // Adds the property to resource entry. Overwrites existing property if exists.
22 // If no change has been made (same key, visibility and value is already added)
23 // then FILE_ERROR_EXISTS is returned.
24 FileError
UpdateLocalState(internal::ResourceMetadata
* metadata
,
25 const base::FilePath
& file_path
,
26 google_apis::drive::Property::Visibility visibility
,
27 const std::string
& key
,
28 const std::string
& value
,
29 ResourceEntry
* entry
) {
30 using google_apis::drive::Property
;
31 FileError error
= metadata
->GetResourceEntryByPath(file_path
, entry
);
32 if (error
!= FILE_ERROR_OK
)
35 Property_Visibility proto_visibility
= Property_Visibility_PRIVATE
;
37 case Property::VISIBILITY_PRIVATE
:
38 proto_visibility
= Property_Visibility_PRIVATE
;
40 case Property::VISIBILITY_PUBLIC
:
41 proto_visibility
= Property_Visibility_PUBLIC
;
45 ::drive::Property
* property_to_update
= nullptr;
46 for (auto& property
: *entry
->mutable_new_properties()) {
47 if (property
.visibility() == proto_visibility
&& property
.key() == key
) {
48 // Exactly the same property exists, so don't update the local state.
49 if (property
.value() == value
)
50 return FILE_ERROR_EXISTS
;
51 property_to_update
= &property
;
56 // If no property to update has been found, then add a new one.
57 if (!property_to_update
)
58 property_to_update
= entry
->mutable_new_properties()->Add();
60 property_to_update
->set_visibility(proto_visibility
);
61 property_to_update
->set_key(key
);
62 property_to_update
->set_value(value
);
63 entry
->set_metadata_edit_state(ResourceEntry::DIRTY
);
64 entry
->set_modification_date(base::Time::Now().ToInternalValue());
66 return metadata
->RefreshEntry(*entry
);
71 SetPropertyOperation::SetPropertyOperation(
72 base::SequencedTaskRunner
* blocking_task_runner
,
73 OperationDelegate
* delegate
,
74 internal::ResourceMetadata
* metadata
)
75 : blocking_task_runner_(blocking_task_runner
),
78 weak_ptr_factory_(this) {
81 SetPropertyOperation::~SetPropertyOperation() {
84 void SetPropertyOperation::SetProperty(
85 const base::FilePath
& file_path
,
86 google_apis::drive::Property::Visibility visibility
,
87 const std::string
& key
,
88 const std::string
& value
,
89 const FileOperationCallback
& callback
) {
90 DCHECK(thread_checker_
.CalledOnValidThread());
91 DCHECK(!callback
.is_null());
93 ResourceEntry
* entry
= new ResourceEntry
;
94 base::PostTaskAndReplyWithResult(
95 blocking_task_runner_
.get(), FROM_HERE
,
96 base::Bind(&UpdateLocalState
, metadata_
, file_path
, visibility
, key
,
98 base::Bind(&SetPropertyOperation::SetPropertyAfterUpdateLocalState
,
99 weak_ptr_factory_
.GetWeakPtr(), callback
, base::Owned(entry
)));
102 void SetPropertyOperation::SetPropertyAfterUpdateLocalState(
103 const FileOperationCallback
& callback
,
104 const ResourceEntry
* entry
,
106 DCHECK(thread_checker_
.CalledOnValidThread());
107 DCHECK(!callback
.is_null());
109 if (result
== FILE_ERROR_OK
) {
110 // Do not notify about the file change, as properties are write only and
111 // cannot be read, so there is no visible change.
112 delegate_
->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED
),
116 // Even if exists, return success, as the set property operation always
117 // overwrites existing values.
118 callback
.Run(result
== FILE_ERROR_EXISTS
? FILE_ERROR_OK
: result
);
121 } // namespace file_system