Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(494)

Side by Side Diff: chrome/browser/drive/fake_drive_service.cc

Issue 486543002: [Drive] Handle error cases earlier in FakeDriveService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/json/json_string_value_serializer.h" 10 #include "base/json/json_string_value_serializer.h"
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 UploadRangeResponse(error, 113 UploadRangeResponse(error,
114 start_position, 114 start_position,
115 end_position), 115 end_position),
116 base::Passed(&entry))); 116 base::Passed(&entry)));
117 } 117 }
118 118
119 void FileListCallbackAdapter(const FileListCallback& callback, 119 void FileListCallbackAdapter(const FileListCallback& callback,
120 GDataErrorCode error, 120 GDataErrorCode error,
121 scoped_ptr<ChangeList> change_list) { 121 scoped_ptr<ChangeList> change_list) {
122 scoped_ptr<FileList> file_list; 122 scoped_ptr<FileList> file_list;
123 if (change_list) { 123 if (!change_list) {
124 file_list.reset(new FileList); 124 callback.Run(error, file_list.Pass());
125 file_list->set_next_link(change_list->next_link()); 125 return;
126 for (size_t i = 0; i < change_list->items().size(); ++i) { 126 }
127 const ChangeResource& entry = *change_list->items()[i]; 127
128 if (entry.file()) 128 file_list.reset(new FileList);
129 file_list->mutable_items()->push_back(new FileResource(*entry.file())); 129 file_list->set_next_link(change_list->next_link());
130 } 130 for (size_t i = 0; i < change_list->items().size(); ++i) {
131 const ChangeResource& entry = *change_list->items()[i];
132 if (entry.file())
133 file_list->mutable_items()->push_back(new FileResource(*entry.file()));
131 } 134 }
132 callback.Run(error, file_list.Pass()); 135 callback.Run(error, file_list.Pass());
133 } 136 }
134 137
135 bool UserHasWriteAccess(google_apis::drive::PermissionRole user_permission) { 138 bool UserHasWriteAccess(google_apis::drive::PermissionRole user_permission) {
136 switch (user_permission) { 139 switch (user_permission) {
137 case google_apis::drive::PERMISSION_ROLE_OWNER: 140 case google_apis::drive::PERMISSION_ROLE_OWNER:
138 case google_apis::drive::PERMISSION_ROLE_WRITER: 141 case google_apis::drive::PERMISSION_ROLE_WRITER:
139 return true; 142 return true;
140 case google_apis::drive::PERMISSION_ROLE_READER: 143 case google_apis::drive::PERMISSION_ROLE_READER:
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 609 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
607 DCHECK(!callback.is_null()); 610 DCHECK(!callback.is_null());
608 611
609 if (offline_) { 612 if (offline_) {
610 base::MessageLoop::current()->PostTask( 613 base::MessageLoop::current()->PostTask(
611 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION)); 614 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION));
612 return CancelCallback(); 615 return CancelCallback();
613 } 616 }
614 617
615 EntryInfo* entry = FindEntryByResourceId(resource_id); 618 EntryInfo* entry = FindEntryByResourceId(resource_id);
616 if (entry) { 619 if (!entry) {
617 ChangeResource* change = &entry->change_resource;
618 const FileResource* file = change->file();
619 if (change->is_deleted()) {
620 base::MessageLoop::current()->PostTask(
621 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND));
622 return CancelCallback();
623 }
624
625 if (!etag.empty() && etag != file->etag()) {
626 base::MessageLoop::current()->PostTask(
627 FROM_HERE, base::Bind(callback, HTTP_PRECONDITION));
628 return CancelCallback();
629 }
630
631 if (entry->user_permission != google_apis::drive::PERMISSION_ROLE_OWNER) {
632 base::MessageLoop::current()->PostTask(
633 FROM_HERE, base::Bind(callback, HTTP_FORBIDDEN));
634 return CancelCallback();
635 }
636
637 change->set_deleted(true);
638 AddNewChangestamp(change);
639 change->set_file(scoped_ptr<FileResource>());
640 base::MessageLoop::current()->PostTask( 620 base::MessageLoop::current()->PostTask(
641 FROM_HERE, base::Bind(callback, HTTP_NO_CONTENT)); 621 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND));
642 return CancelCallback(); 622 return CancelCallback();
643 } 623 }
644 624
625 ChangeResource* change = &entry->change_resource;
626 const FileResource* file = change->file();
627 if (change->is_deleted()) {
628 base::MessageLoop::current()->PostTask(
629 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND));
630 return CancelCallback();
631 }
632
633 if (!etag.empty() && etag != file->etag()) {
634 base::MessageLoop::current()->PostTask(
635 FROM_HERE, base::Bind(callback, HTTP_PRECONDITION));
636 return CancelCallback();
637 }
638
639 if (entry->user_permission != google_apis::drive::PERMISSION_ROLE_OWNER) {
640 base::MessageLoop::current()->PostTask(
641 FROM_HERE, base::Bind(callback, HTTP_FORBIDDEN));
642 return CancelCallback();
643 }
644
645 change->set_deleted(true);
646 AddNewChangestamp(change);
647 change->set_file(scoped_ptr<FileResource>());
645 base::MessageLoop::current()->PostTask( 648 base::MessageLoop::current()->PostTask(
646 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND)); 649 FROM_HERE, base::Bind(callback, HTTP_NO_CONTENT));
647 return CancelCallback(); 650 return CancelCallback();
648 } 651 }
649 652
650 CancelCallback FakeDriveService::TrashResource( 653 CancelCallback FakeDriveService::TrashResource(
651 const std::string& resource_id, 654 const std::string& resource_id,
652 const EntryActionCallback& callback) { 655 const EntryActionCallback& callback) {
653 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 656 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
654 DCHECK(!callback.is_null()); 657 DCHECK(!callback.is_null());
655 658
656 if (offline_) { 659 if (offline_) {
657 base::MessageLoop::current()->PostTask( 660 base::MessageLoop::current()->PostTask(
658 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION)); 661 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION));
659 return CancelCallback(); 662 return CancelCallback();
660 } 663 }
661 664
662 EntryInfo* entry = FindEntryByResourceId(resource_id); 665 EntryInfo* entry = FindEntryByResourceId(resource_id);
663 if (entry) { 666 if (!entry) {
664 ChangeResource* change = &entry->change_resource;
665 FileResource* file = change->mutable_file();
666 GDataErrorCode error = google_apis::GDATA_OTHER_ERROR;
667 if (change->is_deleted() || file->labels().is_trashed()) {
668 error = HTTP_NOT_FOUND;
669 } else if (entry->user_permission !=
670 google_apis::drive::PERMISSION_ROLE_OWNER) {
671 error = HTTP_FORBIDDEN;
672 } else {
673 file->mutable_labels()->set_trashed(true);
674 AddNewChangestamp(change);
675 error = HTTP_SUCCESS;
676 }
677 base::MessageLoop::current()->PostTask( 667 base::MessageLoop::current()->PostTask(
678 FROM_HERE, base::Bind(callback, error)); 668 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND));
679 return CancelCallback(); 669 return CancelCallback();
680 } 670 }
681 671
672 ChangeResource* change = &entry->change_resource;
673 FileResource* file = change->mutable_file();
674 if (change->is_deleted() || file->labels().is_trashed()) {
675 base::MessageLoop::current()->PostTask(
676 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND));
677 return CancelCallback();
678 }
679
680 if (entry->user_permission != google_apis::drive::PERMISSION_ROLE_OWNER) {
681 base::MessageLoop::current()->PostTask(
682 FROM_HERE, base::Bind(callback, HTTP_FORBIDDEN));
683 return CancelCallback();
684 }
685
686 file->mutable_labels()->set_trashed(true);
687 AddNewChangestamp(change);
682 base::MessageLoop::current()->PostTask( 688 base::MessageLoop::current()->PostTask(
683 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND)); 689 FROM_HERE, base::Bind(callback, HTTP_SUCCESS));
684 return CancelCallback(); 690 return CancelCallback();
685 } 691 }
686 692
687 CancelCallback FakeDriveService::DownloadFile( 693 CancelCallback FakeDriveService::DownloadFile(
688 const base::FilePath& local_cache_path, 694 const base::FilePath& local_cache_path,
689 const std::string& resource_id, 695 const std::string& resource_id,
690 const DownloadActionCallback& download_action_callback, 696 const DownloadActionCallback& download_action_callback,
691 const GetContentCallback& get_content_callback, 697 const GetContentCallback& get_content_callback,
692 const ProgressCallback& progress_callback) { 698 const ProgressCallback& progress_callback) {
693 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 699 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
(...skipping 27 matching lines...) Expand all
721 const int64 size = std::min(kBlockSize, file_size - i); 727 const int64 size = std::min(kBlockSize, file_size - i);
722 scoped_ptr<std::string> content_for_callback( 728 scoped_ptr<std::string> content_for_callback(
723 new std::string(content_data.substr(i, size))); 729 new std::string(content_data.substr(i, size)));
724 base::MessageLoopProxy::current()->PostTask( 730 base::MessageLoopProxy::current()->PostTask(
725 FROM_HERE, 731 FROM_HERE,
726 base::Bind(get_content_callback, HTTP_SUCCESS, 732 base::Bind(get_content_callback, HTTP_SUCCESS,
727 base::Passed(&content_for_callback))); 733 base::Passed(&content_for_callback)));
728 } 734 }
729 } 735 }
730 736
731 if (test_util::WriteStringToFile(local_cache_path, content_data)) { 737 if (!test_util::WriteStringToFile(local_cache_path, content_data)) {
732 if (!progress_callback.is_null()) { 738 // Failed to write the content.
733 // See also the comment in ResumeUpload(). For testing that clients
734 // can handle the case progress_callback is called multiple times,
735 // here we invoke the callback twice.
736 base::MessageLoopProxy::current()->PostTask(
737 FROM_HERE,
738 base::Bind(progress_callback, file_size / 2, file_size));
739 base::MessageLoopProxy::current()->PostTask(
740 FROM_HERE,
741 base::Bind(progress_callback, file_size, file_size));
742 }
743 base::MessageLoopProxy::current()->PostTask( 739 base::MessageLoopProxy::current()->PostTask(
744 FROM_HERE, 740 FROM_HERE,
745 base::Bind(download_action_callback, 741 base::Bind(download_action_callback,
746 HTTP_SUCCESS, 742 GDATA_FILE_ERROR, base::FilePath()));
747 local_cache_path));
748 return CancelCallback(); 743 return CancelCallback();
749 } 744 }
750 745
751 // Failed to write the content. 746 if (!progress_callback.is_null()) {
747 // See also the comment in ResumeUpload(). For testing that clients
748 // can handle the case progress_callback is called multiple times,
749 // here we invoke the callback twice.
750 base::MessageLoopProxy::current()->PostTask(
751 FROM_HERE,
752 base::Bind(progress_callback, file_size / 2, file_size));
753 base::MessageLoopProxy::current()->PostTask(
754 FROM_HERE,
755 base::Bind(progress_callback, file_size, file_size));
756 }
752 base::MessageLoopProxy::current()->PostTask( 757 base::MessageLoopProxy::current()->PostTask(
753 FROM_HERE, 758 FROM_HERE,
754 base::Bind(download_action_callback, GDATA_FILE_ERROR, base::FilePath())); 759 base::Bind(download_action_callback,
760 HTTP_SUCCESS,
761 local_cache_path));
755 return CancelCallback(); 762 return CancelCallback();
756 } 763 }
757 764
758 CancelCallback FakeDriveService::CopyResource( 765 CancelCallback FakeDriveService::CopyResource(
759 const std::string& resource_id, 766 const std::string& resource_id,
760 const std::string& in_parent_resource_id, 767 const std::string& in_parent_resource_id,
761 const std::string& new_title, 768 const std::string& new_title,
762 const base::Time& last_modified, 769 const base::Time& last_modified,
763 const FileResourceCallback& callback) { 770 const FileResourceCallback& callback) {
764 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 771 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
765 DCHECK(!callback.is_null()); 772 DCHECK(!callback.is_null());
766 773
767 if (offline_) { 774 if (offline_) {
768 base::MessageLoop::current()->PostTask( 775 base::MessageLoop::current()->PostTask(
769 FROM_HERE, 776 FROM_HERE,
770 base::Bind(callback, 777 base::Bind(callback,
771 GDATA_NO_CONNECTION, 778 GDATA_NO_CONNECTION,
772 base::Passed(scoped_ptr<FileResource>()))); 779 base::Passed(scoped_ptr<FileResource>())));
773 return CancelCallback(); 780 return CancelCallback();
774 } 781 }
775 782
776 const std::string& parent_resource_id = in_parent_resource_id.empty() ? 783 const std::string& parent_resource_id = in_parent_resource_id.empty() ?
777 GetRootResourceId() : in_parent_resource_id; 784 GetRootResourceId() : in_parent_resource_id;
778 785
779 EntryInfo* entry = FindEntryByResourceId(resource_id); 786 EntryInfo* entry = FindEntryByResourceId(resource_id);
780 if (entry) { 787 if (!entry) {
781 // Make a copy and set the new resource ID and the new title.
782 scoped_ptr<EntryInfo> copied_entry(new EntryInfo);
783 copied_entry->content_data = entry->content_data;
784 copied_entry->share_url = entry->share_url;
785 copied_entry->change_resource.set_file(
786 make_scoped_ptr(new FileResource(*entry->change_resource.file())));
787
788 ChangeResource* new_change = &copied_entry->change_resource;
789 FileResource* new_file = new_change->mutable_file();
790 const std::string new_resource_id = GetNewResourceId();
791 new_change->set_file_id(new_resource_id);
792 new_file->set_file_id(new_resource_id);
793 new_file->set_title(new_title);
794
795 ParentReference parent;
796 parent.set_file_id(parent_resource_id);
797 parent.set_parent_link(GetFakeLinkUrl(parent_resource_id));
798 std::vector<ParentReference> parents;
799 parents.push_back(parent);
800 *new_file->mutable_parents() = parents;
801
802 if (!last_modified.is_null())
803 new_file->set_modified_date(last_modified);
804
805 AddNewChangestamp(new_change);
806 UpdateETag(new_file);
807
808 // Add the new entry to the map.
809 entries_[new_resource_id] = copied_entry.release();
810
811 base::MessageLoop::current()->PostTask( 788 base::MessageLoop::current()->PostTask(
812 FROM_HERE, 789 FROM_HERE,
813 base::Bind(callback, 790 base::Bind(callback, HTTP_NOT_FOUND,
814 HTTP_SUCCESS, 791 base::Passed(scoped_ptr<FileResource>())));
815 base::Passed(make_scoped_ptr(new FileResource(*new_file)))));
816 return CancelCallback(); 792 return CancelCallback();
817 } 793 }
818 794
795 // Make a copy and set the new resource ID and the new title.
796 scoped_ptr<EntryInfo> copied_entry(new EntryInfo);
797 copied_entry->content_data = entry->content_data;
798 copied_entry->share_url = entry->share_url;
799 copied_entry->change_resource.set_file(
800 make_scoped_ptr(new FileResource(*entry->change_resource.file())));
801
802 ChangeResource* new_change = &copied_entry->change_resource;
803 FileResource* new_file = new_change->mutable_file();
804 const std::string new_resource_id = GetNewResourceId();
805 new_change->set_file_id(new_resource_id);
806 new_file->set_file_id(new_resource_id);
807 new_file->set_title(new_title);
808
809 ParentReference parent;
810 parent.set_file_id(parent_resource_id);
811 parent.set_parent_link(GetFakeLinkUrl(parent_resource_id));
812 std::vector<ParentReference> parents;
813 parents.push_back(parent);
814 *new_file->mutable_parents() = parents;
815
816 if (!last_modified.is_null())
817 new_file->set_modified_date(last_modified);
818
819 AddNewChangestamp(new_change);
820 UpdateETag(new_file);
821
822 // Add the new entry to the map.
823 entries_[new_resource_id] = copied_entry.release();
824
819 base::MessageLoop::current()->PostTask( 825 base::MessageLoop::current()->PostTask(
820 FROM_HERE, 826 FROM_HERE,
821 base::Bind(callback, HTTP_NOT_FOUND, 827 base::Bind(callback,
822 base::Passed(scoped_ptr<FileResource>()))); 828 HTTP_SUCCESS,
829 base::Passed(make_scoped_ptr(new FileResource(*new_file)))));
823 return CancelCallback(); 830 return CancelCallback();
824 } 831 }
825 832
826 CancelCallback FakeDriveService::UpdateResource( 833 CancelCallback FakeDriveService::UpdateResource(
827 const std::string& resource_id, 834 const std::string& resource_id,
828 const std::string& parent_resource_id, 835 const std::string& parent_resource_id,
829 const std::string& new_title, 836 const std::string& new_title,
830 const base::Time& last_modified, 837 const base::Time& last_modified,
831 const base::Time& last_viewed_by_me, 838 const base::Time& last_viewed_by_me,
832 const google_apis::FileResourceCallback& callback) { 839 const google_apis::FileResourceCallback& callback) {
833 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 840 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
834 DCHECK(!callback.is_null()); 841 DCHECK(!callback.is_null());
835 842
836 if (offline_) { 843 if (offline_) {
837 base::MessageLoop::current()->PostTask( 844 base::MessageLoop::current()->PostTask(
838 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION, 845 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION,
839 base::Passed(scoped_ptr<FileResource>()))); 846 base::Passed(scoped_ptr<FileResource>())));
840 return CancelCallback(); 847 return CancelCallback();
841 } 848 }
842 849
843 EntryInfo* entry = FindEntryByResourceId(resource_id); 850 EntryInfo* entry = FindEntryByResourceId(resource_id);
844 if (entry) { 851 if (!entry) {
845 if (!UserHasWriteAccess(entry->user_permission)) {
846 base::MessageLoop::current()->PostTask(
847 FROM_HERE,
848 base::Bind(callback, HTTP_FORBIDDEN,
849 base::Passed(scoped_ptr<FileResource>())));
850 return CancelCallback();
851 }
852
853 ChangeResource* change = &entry->change_resource;
854 FileResource* file = change->mutable_file();
855
856 if (!new_title.empty())
857 file->set_title(new_title);
858
859 // Set parent if necessary.
860 if (!parent_resource_id.empty()) {
861 ParentReference parent;
862 parent.set_file_id(parent_resource_id);
863 parent.set_parent_link(GetFakeLinkUrl(parent_resource_id));
864
865 std::vector<ParentReference> parents;
866 parents.push_back(parent);
867 *file->mutable_parents() = parents;
868 }
869
870 if (!last_modified.is_null())
871 file->set_modified_date(last_modified);
872
873 if (!last_viewed_by_me.is_null())
874 file->set_last_viewed_by_me_date(last_viewed_by_me);
875
876 AddNewChangestamp(change);
877 UpdateETag(file);
878
879 base::MessageLoop::current()->PostTask( 852 base::MessageLoop::current()->PostTask(
880 FROM_HERE, 853 FROM_HERE,
881 base::Bind(callback, HTTP_SUCCESS, 854 base::Bind(callback, HTTP_NOT_FOUND,
882 base::Passed(make_scoped_ptr(new FileResource(*file))))); 855 base::Passed(scoped_ptr<FileResource>())));
883 return CancelCallback(); 856 return CancelCallback();
884 } 857 }
885 858
859 if (!UserHasWriteAccess(entry->user_permission)) {
860 base::MessageLoop::current()->PostTask(
861 FROM_HERE,
862 base::Bind(callback, HTTP_FORBIDDEN,
863 base::Passed(scoped_ptr<FileResource>())));
864 return CancelCallback();
865 }
866
867 ChangeResource* change = &entry->change_resource;
868 FileResource* file = change->mutable_file();
869
870 if (!new_title.empty())
871 file->set_title(new_title);
872
873 // Set parent if necessary.
874 if (!parent_resource_id.empty()) {
875 ParentReference parent;
876 parent.set_file_id(parent_resource_id);
877 parent.set_parent_link(GetFakeLinkUrl(parent_resource_id));
878
879 std::vector<ParentReference> parents;
880 parents.push_back(parent);
881 *file->mutable_parents() = parents;
882 }
883
884 if (!last_modified.is_null())
885 file->set_modified_date(last_modified);
886
887 if (!last_viewed_by_me.is_null())
888 file->set_last_viewed_by_me_date(last_viewed_by_me);
889
890 AddNewChangestamp(change);
891 UpdateETag(file);
892
886 base::MessageLoop::current()->PostTask( 893 base::MessageLoop::current()->PostTask(
887 FROM_HERE, 894 FROM_HERE,
888 base::Bind(callback, HTTP_NOT_FOUND, 895 base::Bind(callback, HTTP_SUCCESS,
889 base::Passed(scoped_ptr<FileResource>()))); 896 base::Passed(make_scoped_ptr(new FileResource(*file)))));
890 return CancelCallback(); 897 return CancelCallback();
891 } 898 }
892 899
893 CancelCallback FakeDriveService::AddResourceToDirectory( 900 CancelCallback FakeDriveService::AddResourceToDirectory(
894 const std::string& parent_resource_id, 901 const std::string& parent_resource_id,
895 const std::string& resource_id, 902 const std::string& resource_id,
896 const EntryActionCallback& callback) { 903 const EntryActionCallback& callback) {
897 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 904 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
898 DCHECK(!callback.is_null()); 905 DCHECK(!callback.is_null());
899 906
900 if (offline_) { 907 if (offline_) {
901 base::MessageLoop::current()->PostTask( 908 base::MessageLoop::current()->PostTask(
902 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION)); 909 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION));
903 return CancelCallback(); 910 return CancelCallback();
904 } 911 }
905 912
906 EntryInfo* entry = FindEntryByResourceId(resource_id); 913 EntryInfo* entry = FindEntryByResourceId(resource_id);
907 if (entry) { 914 if (!entry) {
908 ChangeResource* change = &entry->change_resource;
909 // On the real Drive server, resources do not necessary shape a tree
910 // structure. That is, each resource can have multiple parent.
911 // We mimic the behavior here; AddResourceToDirectoy just adds
912 // one more parent, not overwriting old ones.
913 ParentReference parent;
914 parent.set_file_id(parent_resource_id);
915 parent.set_parent_link(GetFakeLinkUrl(parent_resource_id));
916 change->mutable_file()->mutable_parents()->push_back(parent);
917
918 AddNewChangestamp(change);
919 base::MessageLoop::current()->PostTask( 915 base::MessageLoop::current()->PostTask(
920 FROM_HERE, base::Bind(callback, HTTP_SUCCESS)); 916 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND));
921 return CancelCallback(); 917 return CancelCallback();
922 } 918 }
923 919
920 ChangeResource* change = &entry->change_resource;
921 // On the real Drive server, resources do not necessary shape a tree
922 // structure. That is, each resource can have multiple parent.
923 // We mimic the behavior here; AddResourceToDirectoy just adds
924 // one more parent, not overwriting old ones.
925 ParentReference parent;
926 parent.set_file_id(parent_resource_id);
927 parent.set_parent_link(GetFakeLinkUrl(parent_resource_id));
928 change->mutable_file()->mutable_parents()->push_back(parent);
929
930 AddNewChangestamp(change);
924 base::MessageLoop::current()->PostTask( 931 base::MessageLoop::current()->PostTask(
925 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND)); 932 FROM_HERE, base::Bind(callback, HTTP_SUCCESS));
926 return CancelCallback(); 933 return CancelCallback();
927 } 934 }
928 935
929 CancelCallback FakeDriveService::RemoveResourceFromDirectory( 936 CancelCallback FakeDriveService::RemoveResourceFromDirectory(
930 const std::string& parent_resource_id, 937 const std::string& parent_resource_id,
931 const std::string& resource_id, 938 const std::string& resource_id,
932 const EntryActionCallback& callback) { 939 const EntryActionCallback& callback) {
933 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 940 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
934 DCHECK(!callback.is_null()); 941 DCHECK(!callback.is_null());
935 942
936 if (offline_) { 943 if (offline_) {
937 base::MessageLoop::current()->PostTask( 944 base::MessageLoop::current()->PostTask(
938 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION)); 945 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION));
939 return CancelCallback(); 946 return CancelCallback();
940 } 947 }
941 948
942 EntryInfo* entry = FindEntryByResourceId(resource_id); 949 EntryInfo* entry = FindEntryByResourceId(resource_id);
943 if (entry) { 950 if (!entry) {
944 ChangeResource* change = &entry->change_resource; 951 base::MessageLoop::current()->PostTask(
945 FileResource* file = change->mutable_file(); 952 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND));
946 std::vector<ParentReference>* parents = file->mutable_parents(); 953 return CancelCallback();
947 for (size_t i = 0; i < parents->size(); ++i) { 954 }
948 if ((*parents)[i].file_id() == parent_resource_id) { 955
949 parents->erase(parents->begin() + i); 956 ChangeResource* change = &entry->change_resource;
950 AddNewChangestamp(change); 957 FileResource* file = change->mutable_file();
951 base::MessageLoop::current()->PostTask( 958 std::vector<ParentReference>* parents = file->mutable_parents();
952 FROM_HERE, base::Bind(callback, HTTP_NO_CONTENT)); 959 for (size_t i = 0; i < parents->size(); ++i) {
953 return CancelCallback(); 960 if ((*parents)[i].file_id() == parent_resource_id) {
954 } 961 parents->erase(parents->begin() + i);
962 AddNewChangestamp(change);
963 base::MessageLoop::current()->PostTask(
964 FROM_HERE, base::Bind(callback, HTTP_NO_CONTENT));
965 return CancelCallback();
955 } 966 }
956 } 967 }
957 968
958 base::MessageLoop::current()->PostTask( 969 base::MessageLoop::current()->PostTask(
959 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND)); 970 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND));
960 return CancelCallback(); 971 return CancelCallback();
961 } 972 }
962 973
963 CancelCallback FakeDriveService::AddNewDirectory( 974 CancelCallback FakeDriveService::AddNewDirectory(
964 const std::string& parent_resource_id, 975 const std::string& parent_resource_id,
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
1201 app_id.c_str()))); 1212 app_id.c_str())));
1202 return CancelCallback(); 1213 return CancelCallback();
1203 } 1214 }
1204 1215
1205 CancelCallback FakeDriveService::UninstallApp( 1216 CancelCallback FakeDriveService::UninstallApp(
1206 const std::string& app_id, 1217 const std::string& app_id,
1207 const google_apis::EntryActionCallback& callback) { 1218 const google_apis::EntryActionCallback& callback) {
1208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1209 DCHECK(!callback.is_null()); 1220 DCHECK(!callback.is_null());
1210 1221
1222 if (offline_) {
1223 base::MessageLoop::current()->PostTask(
1224 FROM_HERE,
1225 base::Bind(callback, google_apis::GDATA_NO_CONNECTION));
1226 return CancelCallback();
1227 }
1228
1211 // Find app_id from app_info_value_ and delete. 1229 // Find app_id from app_info_value_ and delete.
1212 google_apis::GDataErrorCode error = google_apis::HTTP_NOT_FOUND; 1230 base::ListValue* items = NULL;
1213 if (offline_) { 1231 if (!app_info_value_->GetList("items", &items)) {
1214 error = google_apis::GDATA_NO_CONNECTION; 1232 base::MessageLoop::current()->PostTask(
1215 } else { 1233 FROM_HERE,
1216 base::ListValue* items = NULL; 1234 base::Bind(callback, google_apis::HTTP_NOT_FOUND));
1217 if (app_info_value_->GetList("items", &items)) { 1235 return CancelCallback();
1218 for (size_t i = 0; i < items->GetSize(); ++i) { 1236 }
1219 base::DictionaryValue* item = NULL; 1237
1220 std::string id; 1238 for (size_t i = 0; i < items->GetSize(); ++i) {
1221 if (items->GetDictionary(i, &item) && item->GetString("id", &id) && 1239 base::DictionaryValue* item = NULL;
1222 id == app_id) { 1240 std::string id;
1223 if (items->Remove(i, NULL)) 1241 if (items->GetDictionary(i, &item) && item->GetString("id", &id) &&
1224 error = google_apis::HTTP_NO_CONTENT; 1242 id == app_id) {
1225 break; 1243 base::MessageLoop::current()->PostTask(
1226 } 1244 FROM_HERE,
1227 } 1245 base::Bind(callback,
1246 items->Remove(i, NULL) ? google_apis::HTTP_NO_CONTENT
1247 : google_apis::HTTP_NOT_FOUND));
1248 return CancelCallback();
1228 } 1249 }
1229 } 1250 }
1230 1251
1231 base::MessageLoop::current()->PostTask(FROM_HERE, 1252 base::MessageLoop::current()->PostTask(
1232 base::Bind(callback, error)); 1253 FROM_HERE,
1254 base::Bind(callback, google_apis::HTTP_NOT_FOUND));
1233 return CancelCallback(); 1255 return CancelCallback();
1234 } 1256 }
1235 1257
1236 void FakeDriveService::AddNewFile(const std::string& content_type, 1258 void FakeDriveService::AddNewFile(const std::string& content_type,
1237 const std::string& content_data, 1259 const std::string& content_data,
1238 const std::string& parent_resource_id, 1260 const std::string& parent_resource_id,
1239 const std::string& title, 1261 const std::string& title,
1240 bool shared_with_me, 1262 bool shared_with_me,
1241 const FileResourceCallback& callback) { 1263 const FileResourceCallback& callback) {
1242 AddNewFileWithResourceId("", content_type, content_data, parent_resource_id, 1264 AddNewFileWithResourceId("", content_type, content_data, parent_resource_id,
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
1613 google_apis::drive::PermissionRole role, 1635 google_apis::drive::PermissionRole role,
1614 const google_apis::EntryActionCallback& callback) { 1636 const google_apis::EntryActionCallback& callback) {
1615 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 1637 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1616 DCHECK(!callback.is_null()); 1638 DCHECK(!callback.is_null());
1617 1639
1618 NOTREACHED(); 1640 NOTREACHED();
1619 return CancelCallback(); 1641 return CancelCallback();
1620 } 1642 }
1621 1643
1622 } // namespace drive 1644 } // namespace drive
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698