| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/drive/fake_drive_service.h" | 5 #include "chrome/browser/drive/fake_drive_service.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 end_position), | 119 end_position), |
| 120 base::Passed(&entry))); | 120 base::Passed(&entry))); |
| 121 } | 121 } |
| 122 | 122 |
| 123 void EntryActionCallbackAdapter( | 123 void EntryActionCallbackAdapter( |
| 124 const EntryActionCallback& callback, | 124 const EntryActionCallback& callback, |
| 125 GDataErrorCode error, scoped_ptr<ResourceEntry> resource_entry) { | 125 GDataErrorCode error, scoped_ptr<ResourceEntry> resource_entry) { |
| 126 callback.Run(error); | 126 callback.Run(error); |
| 127 } | 127 } |
| 128 | 128 |
| 129 void ClearPropatiesForPermanentDelete(DictionaryValue* entry) { | 129 void ClearPropatiesForPermanentDelete(base::DictionaryValue* entry) { |
| 130 scoped_ptr<DictionaryValue> new_entry(new DictionaryValue); | 130 scoped_ptr<base::DictionaryValue> new_entry(new base::DictionaryValue); |
| 131 const char* kPreservedProperties[] = { | 131 const char* kPreservedProperties[] = { |
| 132 "docs$removed", "docs$changestamp", "gd$resourceId", "id", "updated" | 132 "docs$removed", "docs$changestamp", "gd$resourceId", "id", "updated" |
| 133 }; | 133 }; |
| 134 | 134 |
| 135 for (size_t i = 0; i < arraysize(kPreservedProperties); ++i) { | 135 for (size_t i = 0; i < arraysize(kPreservedProperties); ++i) { |
| 136 const char* key = kPreservedProperties[i]; | 136 const char* key = kPreservedProperties[i]; |
| 137 scoped_ptr<Value> value; | 137 scoped_ptr<base::Value> value; |
| 138 if (entry->Remove(key, &value)) | 138 if (entry->Remove(key, &value)) |
| 139 new_entry->Set(key, value.release()); | 139 new_entry->Set(key, value.release()); |
| 140 } | 140 } |
| 141 | 141 |
| 142 entry->Swap(new_entry.get()); | 142 entry->Swap(new_entry.get()); |
| 143 } | 143 } |
| 144 | 144 |
| 145 } // namespace | 145 } // namespace |
| 146 | 146 |
| 147 struct FakeDriveService::UploadSession { | 147 struct FakeDriveService::UploadSession { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 193 } | 193 } |
| 194 | 194 |
| 195 FakeDriveService::~FakeDriveService() { | 195 FakeDriveService::~FakeDriveService() { |
| 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 197 } | 197 } |
| 198 | 198 |
| 199 bool FakeDriveService::LoadResourceListForWapi( | 199 bool FakeDriveService::LoadResourceListForWapi( |
| 200 const std::string& relative_path) { | 200 const std::string& relative_path) { |
| 201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 202 scoped_ptr<Value> raw_value = test_util::LoadJSONFile(relative_path); | 202 scoped_ptr<base::Value> raw_value = test_util::LoadJSONFile(relative_path); |
| 203 base::DictionaryValue* as_dict = NULL; | 203 base::DictionaryValue* as_dict = NULL; |
| 204 scoped_ptr<base::Value> feed; | 204 scoped_ptr<base::Value> feed; |
| 205 base::DictionaryValue* feed_as_dict = NULL; | 205 base::DictionaryValue* feed_as_dict = NULL; |
| 206 | 206 |
| 207 // Extract the "feed" from the raw value and take the ownership. | 207 // Extract the "feed" from the raw value and take the ownership. |
| 208 // Note that Remove() transfers the ownership to |feed|. | 208 // Note that Remove() transfers the ownership to |feed|. |
| 209 if (raw_value->GetAsDictionary(&as_dict) && | 209 if (raw_value->GetAsDictionary(&as_dict) && |
| 210 as_dict->Remove("feed", &feed) && | 210 as_dict->Remove("feed", &feed) && |
| 211 feed->GetAsDictionary(&feed_as_dict)) { | 211 feed->GetAsDictionary(&feed_as_dict)) { |
| 212 ignore_result(feed.release()); | 212 ignore_result(feed.release()); |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 } | 599 } |
| 600 | 600 |
| 601 std::string entry_etag; | 601 std::string entry_etag; |
| 602 entry->GetString("gd$etag", &entry_etag); | 602 entry->GetString("gd$etag", &entry_etag); |
| 603 if (!etag.empty() && entry_etag != etag) { | 603 if (!etag.empty() && entry_etag != etag) { |
| 604 base::MessageLoop::current()->PostTask( | 604 base::MessageLoop::current()->PostTask( |
| 605 FROM_HERE, base::Bind(callback, HTTP_PRECONDITION)); | 605 FROM_HERE, base::Bind(callback, HTTP_PRECONDITION)); |
| 606 return CancelCallback(); | 606 return CancelCallback(); |
| 607 } | 607 } |
| 608 | 608 |
| 609 entry->Set("docs$removed", new DictionaryValue); | 609 entry->Set("docs$removed", new base::DictionaryValue); |
| 610 AddNewChangestamp(entry); | 610 AddNewChangestamp(entry); |
| 611 ClearPropatiesForPermanentDelete(entry); | 611 ClearPropatiesForPermanentDelete(entry); |
| 612 base::MessageLoop::current()->PostTask( | 612 base::MessageLoop::current()->PostTask( |
| 613 FROM_HERE, base::Bind(callback, HTTP_NO_CONTENT)); | 613 FROM_HERE, base::Bind(callback, HTTP_NO_CONTENT)); |
| 614 return CancelCallback(); | 614 return CancelCallback(); |
| 615 } | 615 } |
| 616 } | 616 } |
| 617 } | 617 } |
| 618 | 618 |
| 619 base::MessageLoop::current()->PostTask( | 619 base::MessageLoop::current()->PostTask( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 639 for (size_t i = 0; i < entries->GetSize(); ++i) { | 639 for (size_t i = 0; i < entries->GetSize(); ++i) { |
| 640 base::DictionaryValue* entry = NULL; | 640 base::DictionaryValue* entry = NULL; |
| 641 std::string current_resource_id; | 641 std::string current_resource_id; |
| 642 if (entries->GetDictionary(i, &entry) && | 642 if (entries->GetDictionary(i, &entry) && |
| 643 entry->GetString("gd$resourceId.$t", ¤t_resource_id) && | 643 entry->GetString("gd$resourceId.$t", ¤t_resource_id) && |
| 644 resource_id == current_resource_id) { | 644 resource_id == current_resource_id) { |
| 645 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; | 645 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; |
| 646 if (entry->HasKey("gd$deleted") || entry->HasKey("docs$removed")) { | 646 if (entry->HasKey("gd$deleted") || entry->HasKey("docs$removed")) { |
| 647 error = HTTP_NOT_FOUND; | 647 error = HTTP_NOT_FOUND; |
| 648 } else { | 648 } else { |
| 649 entry->Set("gd$deleted", new DictionaryValue); | 649 entry->Set("gd$deleted", new base::DictionaryValue); |
| 650 AddNewChangestamp(entry); | 650 AddNewChangestamp(entry); |
| 651 error = HTTP_SUCCESS; | 651 error = HTTP_SUCCESS; |
| 652 } | 652 } |
| 653 base::MessageLoop::current()->PostTask( | 653 base::MessageLoop::current()->PostTask( |
| 654 FROM_HERE, base::Bind(callback, error)); | 654 FROM_HERE, base::Bind(callback, error)); |
| 655 return CancelCallback(); | 655 return CancelCallback(); |
| 656 } | 656 } |
| 657 } | 657 } |
| 658 } | 658 } |
| 659 | 659 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 base::ListValue* entries = NULL; | 768 base::ListValue* entries = NULL; |
| 769 // Go through entries and copy the one that matches |resource_id|. | 769 // Go through entries and copy the one that matches |resource_id|. |
| 770 if (resource_list_value_->GetList("entry", &entries)) { | 770 if (resource_list_value_->GetList("entry", &entries)) { |
| 771 for (size_t i = 0; i < entries->GetSize(); ++i) { | 771 for (size_t i = 0; i < entries->GetSize(); ++i) { |
| 772 base::DictionaryValue* entry = NULL; | 772 base::DictionaryValue* entry = NULL; |
| 773 std::string current_resource_id; | 773 std::string current_resource_id; |
| 774 if (entries->GetDictionary(i, &entry) && | 774 if (entries->GetDictionary(i, &entry) && |
| 775 entry->GetString("gd$resourceId.$t", ¤t_resource_id) && | 775 entry->GetString("gd$resourceId.$t", ¤t_resource_id) && |
| 776 resource_id == current_resource_id) { | 776 resource_id == current_resource_id) { |
| 777 // Make a copy and set the new resource ID and the new title. | 777 // Make a copy and set the new resource ID and the new title. |
| 778 scoped_ptr<DictionaryValue> copied_entry(entry->DeepCopy()); | 778 scoped_ptr<base::DictionaryValue> copied_entry(entry->DeepCopy()); |
| 779 copied_entry->SetString("gd$resourceId.$t", | 779 copied_entry->SetString("gd$resourceId.$t", |
| 780 resource_id + "_copied"); | 780 resource_id + "_copied"); |
| 781 copied_entry->SetString("title.$t", new_title); | 781 copied_entry->SetString("title.$t", new_title); |
| 782 | 782 |
| 783 // Reset parent directory. | 783 // Reset parent directory. |
| 784 base::ListValue* links = NULL; | 784 base::ListValue* links = NULL; |
| 785 if (!copied_entry->GetList("link", &links)) { | 785 if (!copied_entry->GetList("link", &links)) { |
| 786 links = new base::ListValue; | 786 links = new base::ListValue; |
| 787 copied_entry->Set("link", links); | 787 copied_entry->Set("link", links); |
| 788 } | 788 } |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1090 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1090 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1091 DCHECK(!callback.is_null()); | 1091 DCHECK(!callback.is_null()); |
| 1092 | 1092 |
| 1093 if (offline_) { | 1093 if (offline_) { |
| 1094 base::MessageLoop::current()->PostTask( | 1094 base::MessageLoop::current()->PostTask( |
| 1095 FROM_HERE, | 1095 FROM_HERE, |
| 1096 base::Bind(callback, GDATA_NO_CONNECTION, GURL())); | 1096 base::Bind(callback, GDATA_NO_CONNECTION, GURL())); |
| 1097 return CancelCallback(); | 1097 return CancelCallback(); |
| 1098 } | 1098 } |
| 1099 | 1099 |
| 1100 DictionaryValue* entry = FindEntryByResourceId(resource_id); | 1100 base::DictionaryValue* entry = FindEntryByResourceId(resource_id); |
| 1101 if (!entry) { | 1101 if (!entry) { |
| 1102 base::MessageLoop::current()->PostTask( | 1102 base::MessageLoop::current()->PostTask( |
| 1103 FROM_HERE, | 1103 FROM_HERE, |
| 1104 base::Bind(callback, HTTP_NOT_FOUND, GURL())); | 1104 base::Bind(callback, HTTP_NOT_FOUND, GURL())); |
| 1105 return CancelCallback(); | 1105 return CancelCallback(); |
| 1106 } | 1106 } |
| 1107 | 1107 |
| 1108 std::string entry_etag; | 1108 std::string entry_etag; |
| 1109 entry->GetString("gd$etag", &entry_etag); | 1109 entry->GetString("gd$etag", &entry_etag); |
| 1110 if (!etag.empty() && etag != entry_etag) { | 1110 if (!etag.empty() && etag != entry_etag) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1198 session->uploaded_size = end_position; | 1198 session->uploaded_size = end_position; |
| 1199 completion_callback.Run(GDATA_FILE_ERROR, scoped_ptr<ResourceEntry>()); | 1199 completion_callback.Run(GDATA_FILE_ERROR, scoped_ptr<ResourceEntry>()); |
| 1200 return CancelCallback(); | 1200 return CancelCallback(); |
| 1201 } | 1201 } |
| 1202 session->uploaded_size = end_position; | 1202 session->uploaded_size = end_position; |
| 1203 | 1203 |
| 1204 // |resource_id| is empty if the upload is for new file. | 1204 // |resource_id| is empty if the upload is for new file. |
| 1205 if (session->resource_id.empty()) { | 1205 if (session->resource_id.empty()) { |
| 1206 DCHECK(!session->parent_resource_id.empty()); | 1206 DCHECK(!session->parent_resource_id.empty()); |
| 1207 DCHECK(!session->title.empty()); | 1207 DCHECK(!session->title.empty()); |
| 1208 const DictionaryValue* new_entry = AddNewEntry( | 1208 const base::DictionaryValue* new_entry = AddNewEntry( |
| 1209 session->content_type, | 1209 session->content_type, |
| 1210 content_data, | 1210 content_data, |
| 1211 session->parent_resource_id, | 1211 session->parent_resource_id, |
| 1212 session->title, | 1212 session->title, |
| 1213 false, // shared_with_me | 1213 false, // shared_with_me |
| 1214 "file"); | 1214 "file"); |
| 1215 if (!new_entry) { | 1215 if (!new_entry) { |
| 1216 completion_callback.Run(HTTP_NOT_FOUND, scoped_ptr<ResourceEntry>()); | 1216 completion_callback.Run(HTTP_NOT_FOUND, scoped_ptr<ResourceEntry>()); |
| 1217 return CancelCallback(); | 1217 return CancelCallback(); |
| 1218 } | 1218 } |
| 1219 | 1219 |
| 1220 completion_callback.Run(HTTP_CREATED, | 1220 completion_callback.Run(HTTP_CREATED, |
| 1221 ResourceEntry::CreateFrom(*new_entry)); | 1221 ResourceEntry::CreateFrom(*new_entry)); |
| 1222 return CancelCallback(); | 1222 return CancelCallback(); |
| 1223 } | 1223 } |
| 1224 | 1224 |
| 1225 DictionaryValue* entry = FindEntryByResourceId(session->resource_id); | 1225 base::DictionaryValue* entry = FindEntryByResourceId(session->resource_id); |
| 1226 if (!entry) { | 1226 if (!entry) { |
| 1227 completion_callback.Run(HTTP_NOT_FOUND, scoped_ptr<ResourceEntry>()); | 1227 completion_callback.Run(HTTP_NOT_FOUND, scoped_ptr<ResourceEntry>()); |
| 1228 return CancelCallback(); | 1228 return CancelCallback(); |
| 1229 } | 1229 } |
| 1230 | 1230 |
| 1231 std::string entry_etag; | 1231 std::string entry_etag; |
| 1232 entry->GetString("gd$etag", &entry_etag); | 1232 entry->GetString("gd$etag", &entry_etag); |
| 1233 if (entry_etag.empty() || session->etag != entry_etag) { | 1233 if (entry_etag.empty() || session->etag != entry_etag) { |
| 1234 completion_callback.Run(HTTP_PRECONDITION, scoped_ptr<ResourceEntry>()); | 1234 completion_callback.Run(HTTP_PRECONDITION, scoped_ptr<ResourceEntry>()); |
| 1235 return CancelCallback(); | 1235 return CancelCallback(); |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1556 UpdateETag(new_entry.get()); | 1556 UpdateETag(new_entry.get()); |
| 1557 | 1557 |
| 1558 base::Time published_date = | 1558 base::Time published_date = |
| 1559 base::Time() + base::TimeDelta::FromMilliseconds(++published_date_seq_); | 1559 base::Time() + base::TimeDelta::FromMilliseconds(++published_date_seq_); |
| 1560 new_entry->SetString( | 1560 new_entry->SetString( |
| 1561 "published.$t", | 1561 "published.$t", |
| 1562 google_apis::util::FormatTimeAsString(published_date)); | 1562 google_apis::util::FormatTimeAsString(published_date)); |
| 1563 | 1563 |
| 1564 // If there are no entries, prepare an empty entry to add. | 1564 // If there are no entries, prepare an empty entry to add. |
| 1565 if (!resource_list_value_->HasKey("entry")) | 1565 if (!resource_list_value_->HasKey("entry")) |
| 1566 resource_list_value_->Set("entry", new ListValue); | 1566 resource_list_value_->Set("entry", new base::ListValue); |
| 1567 | 1567 |
| 1568 base::DictionaryValue* raw_new_entry = new_entry.release(); | 1568 base::DictionaryValue* raw_new_entry = new_entry.release(); |
| 1569 base::ListValue* entries = NULL; | 1569 base::ListValue* entries = NULL; |
| 1570 if (resource_list_value_->GetList("entry", &entries)) | 1570 if (resource_list_value_->GetList("entry", &entries)) |
| 1571 entries->Append(raw_new_entry); | 1571 entries->Append(raw_new_entry); |
| 1572 | 1572 |
| 1573 return raw_new_entry; | 1573 return raw_new_entry; |
| 1574 } | 1574 } |
| 1575 | 1575 |
| 1576 void FakeDriveService::GetResourceListInternal( | 1576 void FakeDriveService::GetResourceListInternal( |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1696 FROM_HERE, | 1696 FROM_HERE, |
| 1697 base::Bind(callback, HTTP_SUCCESS, base::Passed(&resource_list))); | 1697 base::Bind(callback, HTTP_SUCCESS, base::Passed(&resource_list))); |
| 1698 } | 1698 } |
| 1699 | 1699 |
| 1700 GURL FakeDriveService::GetNewUploadSessionUrl() { | 1700 GURL FakeDriveService::GetNewUploadSessionUrl() { |
| 1701 return GURL("https://upload_session_url/" + | 1701 return GURL("https://upload_session_url/" + |
| 1702 base::Int64ToString(next_upload_sequence_number_++)); | 1702 base::Int64ToString(next_upload_sequence_number_++)); |
| 1703 } | 1703 } |
| 1704 | 1704 |
| 1705 } // namespace drive | 1705 } // namespace drive |
| OLD | NEW |