Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / file_system / set_property_operation.cc
blob111f9b79a188645d8e9da2af56733de3c9a7db72
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 "chrome/browser/chromeos/drive/file_system/set_property_operation.h"
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/sequenced_task_runner.h"
10 #include "chrome/browser/chromeos/drive/drive.pb.h"
11 #include "chrome/browser/chromeos/drive/file_errors.h"
12 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
13 #include "chrome/browser/chromeos/drive/job_scheduler.h"
14 #include "chrome/browser/chromeos/drive/resource_metadata.h"
15 #include "content/public/browser/browser_thread.h"
17 using content::BrowserThread;
19 namespace drive {
20 namespace file_system {
22 namespace {
24 // Adds the property to resource entry. Overwrites existing property if exists.
25 // If no change has been made (same key, visibility and value is already added)
26 // then FILE_ERROR_EXISTS is returned.
27 FileError UpdateLocalState(internal::ResourceMetadata* metadata,
28 const base::FilePath& file_path,
29 google_apis::drive::Property::Visibility visibility,
30 const std::string& key,
31 const std::string& value,
32 ResourceEntry* entry) {
33 using google_apis::drive::Property;
34 FileError error = metadata->GetResourceEntryByPath(file_path, entry);
35 if (error != FILE_ERROR_OK)
36 return error;
38 Property_Visibility proto_visibility = Property_Visibility_PRIVATE;
39 switch (visibility) {
40 case Property::VISIBILITY_PRIVATE:
41 proto_visibility = Property_Visibility_PRIVATE;
42 break;
43 case Property::VISIBILITY_PUBLIC:
44 proto_visibility = Property_Visibility_PUBLIC;
45 break;
48 ::drive::Property* property_to_update = nullptr;
49 for (auto& property : *entry->mutable_new_properties()) {
50 if (property.visibility() == proto_visibility && property.key() == key) {
51 // Exactly the same property exists, so don't update the local state.
52 if (property.value() == value)
53 return FILE_ERROR_EXISTS;
54 property_to_update = &property;
55 break;
59 // If no property to update has been found, then add a new one.
60 if (!property_to_update)
61 property_to_update = entry->mutable_new_properties()->Add();
63 property_to_update->set_visibility(proto_visibility);
64 property_to_update->set_key(key);
65 property_to_update->set_value(value);
66 entry->set_metadata_edit_state(ResourceEntry::DIRTY);
67 entry->set_modification_date(base::Time::Now().ToInternalValue());
69 return metadata->RefreshEntry(*entry);
72 } // namespace
74 SetPropertyOperation::SetPropertyOperation(
75 base::SequencedTaskRunner* blocking_task_runner,
76 OperationDelegate* delegate,
77 internal::ResourceMetadata* metadata)
78 : blocking_task_runner_(blocking_task_runner),
79 delegate_(delegate),
80 metadata_(metadata),
81 weak_ptr_factory_(this) {
84 SetPropertyOperation::~SetPropertyOperation() {
87 void SetPropertyOperation::SetProperty(
88 const base::FilePath& file_path,
89 google_apis::drive::Property::Visibility visibility,
90 const std::string& key,
91 const std::string& value,
92 const FileOperationCallback& callback) {
93 DCHECK_CURRENTLY_ON(BrowserThread::UI);
94 DCHECK(!callback.is_null());
96 ResourceEntry* entry = new ResourceEntry;
97 base::PostTaskAndReplyWithResult(
98 blocking_task_runner_.get(), FROM_HERE,
99 base::Bind(&UpdateLocalState, metadata_, file_path, visibility, key,
100 value, entry),
101 base::Bind(&SetPropertyOperation::SetPropertyAfterUpdateLocalState,
102 weak_ptr_factory_.GetWeakPtr(), callback, base::Owned(entry)));
105 void SetPropertyOperation::SetPropertyAfterUpdateLocalState(
106 const FileOperationCallback& callback,
107 const ResourceEntry* entry,
108 FileError result) {
109 DCHECK_CURRENTLY_ON(BrowserThread::UI);
110 DCHECK(!callback.is_null());
112 if (result == FILE_ERROR_OK) {
113 // Do not notify about the file change, as properties are write only and
114 // cannot be read, so there is no visible change.
115 delegate_->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED),
116 entry->local_id());
119 // Even if exists, return success, as the set property operation always
120 // overwrites existing values.
121 callback.Run(result == FILE_ERROR_EXISTS ? FILE_ERROR_OK : result);
124 } // namespace file_system
125 } // namespace drive