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

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

Issue 23477038: Misc clean up for Drive API service and requests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 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 | chrome/browser/google_apis/drive_api_requests.h » ('j') | 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/drive_api_service.h" 5 #include "chrome/browser/drive/drive_api_service.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 using google_apis::ResourceEntry; 51 using google_apis::ResourceEntry;
52 using google_apis::ResourceList; 52 using google_apis::ResourceList;
53 using google_apis::UploadRangeCallback; 53 using google_apis::UploadRangeCallback;
54 using google_apis::UploadRangeResponse; 54 using google_apis::UploadRangeResponse;
55 using google_apis::drive::AboutGetRequest; 55 using google_apis::drive::AboutGetRequest;
56 using google_apis::drive::AppsListRequest; 56 using google_apis::drive::AppsListRequest;
57 using google_apis::drive::ChangesListRequest; 57 using google_apis::drive::ChangesListRequest;
58 using google_apis::drive::ChangesListNextPageRequest; 58 using google_apis::drive::ChangesListNextPageRequest;
59 using google_apis::drive::ChildrenDeleteRequest; 59 using google_apis::drive::ChildrenDeleteRequest;
60 using google_apis::drive::ChildrenInsertRequest; 60 using google_apis::drive::ChildrenInsertRequest;
61 using google_apis::drive::ContinueGetFileListRequest;
62 using google_apis::drive::DownloadFileRequest; 61 using google_apis::drive::DownloadFileRequest;
63 using google_apis::drive::FilesCopyRequest; 62 using google_apis::drive::FilesCopyRequest;
64 using google_apis::drive::FilesGetRequest; 63 using google_apis::drive::FilesGetRequest;
65 using google_apis::drive::FilesInsertRequest; 64 using google_apis::drive::FilesInsertRequest;
66 using google_apis::drive::FilesPatchRequest; 65 using google_apis::drive::FilesPatchRequest;
67 using google_apis::drive::FilesListRequest; 66 using google_apis::drive::FilesListRequest;
68 using google_apis::drive::FilesListNextPageRequest; 67 using google_apis::drive::FilesListNextPageRequest;
69 using google_apis::drive::FilesTrashRequest; 68 using google_apis::drive::FilesTrashRequest;
70 using google_apis::drive::GetUploadStatusRequest; 69 using google_apis::drive::GetUploadStatusRequest;
71 using google_apis::drive::InitiateUploadExistingFileRequest; 70 using google_apis::drive::InitiateUploadExistingFileRequest;
(...skipping 12 matching lines...) Expand all
84 // Mime type to create a directory. 83 // Mime type to create a directory.
85 const char kFolderMimeType[] = "application/vnd.google-apps.folder"; 84 const char kFolderMimeType[] = "application/vnd.google-apps.folder";
86 85
87 // Expected max number of files resources in a http request. 86 // Expected max number of files resources in a http request.
88 // Be careful not to use something too small because it might overload the 87 // Be careful not to use something too small because it might overload the
89 // server. Be careful not to use something too large because it takes longer 88 // server. Be careful not to use something too large because it takes longer
90 // time to fetch the result without UI response. 89 // time to fetch the result without UI response.
91 const int kMaxNumFilesResourcePerRequest = 500; 90 const int kMaxNumFilesResourcePerRequest = 500;
92 const int kMaxNumFilesResourcePerRequestForSearch = 50; 91 const int kMaxNumFilesResourcePerRequestForSearch = 50;
93 92
94 scoped_ptr<ResourceList> ParseChangeListJsonToResourceList(
95 scoped_ptr<base::Value> value) {
96 scoped_ptr<ChangeList> change_list(ChangeList::CreateFrom(*value));
97 if (!change_list) {
98 return scoped_ptr<ResourceList>();
99 }
100
101 return ResourceList::CreateFromChangeList(*change_list);
102 }
103
104 scoped_ptr<ResourceList> ParseFileListJsonToResourceList(
105 scoped_ptr<base::Value> value) {
106 scoped_ptr<FileList> file_list(FileList::CreateFrom(*value));
107 if (!file_list) {
108 return scoped_ptr<ResourceList>();
109 }
110
111 return ResourceList::CreateFromFileList(*file_list);
112 }
113
114 // Parses JSON value representing either ChangeList or FileList into
115 // ResourceList.
116 scoped_ptr<ResourceList> ParseResourceListOnBlockingPool(
117 scoped_ptr<base::Value> value) {
118 DCHECK(value);
119
120 // Dispatch the parsing based on kind field.
121 if (ChangeList::HasChangeListKind(*value)) {
122 return ParseChangeListJsonToResourceList(value.Pass());
123 }
124 if (FileList::HasFileListKind(*value)) {
125 return ParseFileListJsonToResourceList(value.Pass());
126 }
127
128 // The value type is unknown, so give up to parse and return an error.
129 return scoped_ptr<ResourceList>();
130 }
131
132 // Callback invoked when the parsing of resource list is completed, 93 // Callback invoked when the parsing of resource list is completed,
133 // regardless whether it is succeeded or not. 94 // regardless whether it is succeeded or not.
134 void DidParseResourceListOnBlockingPool( 95 void DidConvertToResourceListOnBlockingPool(
135 const GetResourceListCallback& callback, 96 const GetResourceListCallback& callback,
136 scoped_ptr<ResourceList> resource_list) { 97 scoped_ptr<ResourceList> resource_list) {
137 GDataErrorCode error = resource_list ? HTTP_SUCCESS : GDATA_PARSE_ERROR; 98 GDataErrorCode error = resource_list ? HTTP_SUCCESS : GDATA_PARSE_ERROR;
138 callback.Run(error, resource_list.Pass()); 99 callback.Run(error, resource_list.Pass());
139 } 100 }
140 101
141 // Sends a task to parse the JSON value into ResourceList on blocking pool,
142 // with a callback which is called when the task is done.
143 void ParseResourceListOnBlockingPoolAndRun(
144 scoped_refptr<base::TaskRunner> blocking_task_runner,
145 const GetResourceListCallback& callback,
146 GDataErrorCode error,
147 scoped_ptr<base::Value> value) {
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
149 DCHECK(!callback.is_null());
150
151 if (error != HTTP_SUCCESS) {
152 // An error occurs, so run callback immediately.
153 callback.Run(error, scoped_ptr<ResourceList>());
154 return;
155 }
156
157 base::PostTaskAndReplyWithResult(
158 blocking_task_runner.get(),
159 FROM_HERE,
160 base::Bind(&ParseResourceListOnBlockingPool, base::Passed(&value)),
161 base::Bind(&DidParseResourceListOnBlockingPool, callback));
162 }
163
164 // Converts the FileResource value to ResourceEntry and runs |callback| on the 102 // Converts the FileResource value to ResourceEntry and runs |callback| on the
165 // UI thread. 103 // UI thread.
166 void ConvertFileEntryToResourceEntryAndRun( 104 void ConvertFileEntryToResourceEntryAndRun(
167 const GetResourceEntryCallback& callback, 105 const GetResourceEntryCallback& callback,
168 GDataErrorCode error, 106 GDataErrorCode error,
169 scoped_ptr<FileResource> value) { 107 scoped_ptr<FileResource> value) {
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
171 DCHECK(!callback.is_null()); 109 DCHECK(!callback.is_null());
172 110
173 if (!value) { 111 if (!value) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 if (!value) { 143 if (!value) {
206 callback.Run(error, scoped_ptr<ResourceList>()); 144 callback.Run(error, scoped_ptr<ResourceList>());
207 return; 145 return;
208 } 146 }
209 147
210 // Convert the value on blocking pool. 148 // Convert the value on blocking pool.
211 base::PostTaskAndReplyWithResult( 149 base::PostTaskAndReplyWithResult(
212 blocking_task_runner.get(), 150 blocking_task_runner.get(),
213 FROM_HERE, 151 FROM_HERE,
214 base::Bind(&ConvertFileListToResourceList, base::Passed(&value)), 152 base::Bind(&ConvertFileListToResourceList, base::Passed(&value)),
215 base::Bind(&DidParseResourceListOnBlockingPool, callback)); 153 base::Bind(&DidConvertToResourceListOnBlockingPool, callback));
216 } 154 }
217 155
218 // Thin adapter of CreateFromChangeList. 156 // Thin adapter of CreateFromChangeList.
219 scoped_ptr<ResourceList> ConvertChangeListToResourceList( 157 scoped_ptr<ResourceList> ConvertChangeListToResourceList(
220 scoped_ptr<ChangeList> change_list) { 158 scoped_ptr<ChangeList> change_list) {
221 return ResourceList::CreateFromChangeList(*change_list); 159 return ResourceList::CreateFromChangeList(*change_list);
222 } 160 }
223 161
224 // Converts the FileList value to ResourceList on blocking pool and runs 162 // Converts the FileList value to ResourceList on blocking pool and runs
225 // |callback| on the UI thread. 163 // |callback| on the UI thread.
226 void ConvertChangeListToResourceListOnBlockingPoolAndRun( 164 void ConvertChangeListToResourceListOnBlockingPoolAndRun(
227 scoped_refptr<base::TaskRunner> blocking_task_runner, 165 scoped_refptr<base::TaskRunner> blocking_task_runner,
228 const GetResourceListCallback& callback, 166 const GetResourceListCallback& callback,
229 GDataErrorCode error, 167 GDataErrorCode error,
230 scoped_ptr<ChangeList> value) { 168 scoped_ptr<ChangeList> value) {
231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
232 DCHECK(!callback.is_null()); 170 DCHECK(!callback.is_null());
233 171
234 if (!value) { 172 if (!value) {
235 callback.Run(error, scoped_ptr<ResourceList>()); 173 callback.Run(error, scoped_ptr<ResourceList>());
236 return; 174 return;
237 } 175 }
238 176
239 // Convert the value on blocking pool. 177 // Convert the value on blocking pool.
240 base::PostTaskAndReplyWithResult( 178 base::PostTaskAndReplyWithResult(
241 blocking_task_runner.get(), 179 blocking_task_runner.get(),
242 FROM_HERE, 180 FROM_HERE,
243 base::Bind(&ConvertChangeListToResourceList, base::Passed(&value)), 181 base::Bind(&ConvertChangeListToResourceList, base::Passed(&value)),
244 base::Bind(&DidParseResourceListOnBlockingPool, callback)); 182 base::Bind(&DidConvertToResourceListOnBlockingPool, callback));
245 } 183 }
246 184
247 // Parses the FileResource value to ResourceEntry for upload range request, 185 // Converts the FileResource value to ResourceEntry for upload range request,
248 // and runs |callback| on the UI thread. 186 // and runs |callback| on the UI thread.
249 void ParseResourceEntryForUploadRangeAndRun( 187 void ConvertFileResourceToResourceEntryForUploadRangeAndRun(
250 const UploadRangeCallback& callback, 188 const UploadRangeCallback& callback,
251 const UploadRangeResponse& response, 189 const UploadRangeResponse& response,
252 scoped_ptr<FileResource> value) { 190 scoped_ptr<FileResource> value) {
253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
254 DCHECK(!callback.is_null()); 192 DCHECK(!callback.is_null());
255 193
256 if (!value) { 194 if (!value) {
257 callback.Run(response, scoped_ptr<ResourceEntry>()); 195 callback.Run(response, scoped_ptr<ResourceEntry>());
258 return; 196 return;
259 } 197 }
(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 719
782 return sender_->StartRequestWithRetry( 720 return sender_->StartRequestWithRetry(
783 new ResumeUploadRequest( 721 new ResumeUploadRequest(
784 sender_.get(), 722 sender_.get(),
785 upload_url, 723 upload_url,
786 start_position, 724 start_position,
787 end_position, 725 end_position,
788 content_length, 726 content_length,
789 content_type, 727 content_type,
790 local_file_path, 728 local_file_path,
791 base::Bind(&ParseResourceEntryForUploadRangeAndRun, callback), 729 base::Bind(&ConvertFileResourceToResourceEntryForUploadRangeAndRun,
730 callback),
792 progress_callback)); 731 progress_callback));
793 } 732 }
794 733
795 CancelCallback DriveAPIService::GetUploadStatus( 734 CancelCallback DriveAPIService::GetUploadStatus(
796 const GURL& upload_url, 735 const GURL& upload_url,
797 int64 content_length, 736 int64 content_length,
798 const UploadRangeCallback& callback) { 737 const UploadRangeCallback& callback) {
799 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 738 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
800 DCHECK(!callback.is_null()); 739 DCHECK(!callback.is_null());
801 740
802 return sender_->StartRequestWithRetry(new GetUploadStatusRequest( 741 return sender_->StartRequestWithRetry(new GetUploadStatusRequest(
803 sender_.get(), 742 sender_.get(),
804 upload_url, 743 upload_url,
805 content_length, 744 content_length,
806 base::Bind(&ParseResourceEntryForUploadRangeAndRun, callback))); 745 base::Bind(&ConvertFileResourceToResourceEntryForUploadRangeAndRun,
746 callback)));
807 } 747 }
808 748
809 CancelCallback DriveAPIService::AuthorizeApp( 749 CancelCallback DriveAPIService::AuthorizeApp(
810 const std::string& resource_id, 750 const std::string& resource_id,
811 const std::string& app_id, 751 const std::string& app_id,
812 const AuthorizeAppCallback& callback) { 752 const AuthorizeAppCallback& callback) {
813 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 753 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
814 DCHECK(!callback.is_null()); 754 DCHECK(!callback.is_null());
815 755
816 FilesGetRequest* request = new FilesGetRequest( 756 FilesGetRequest* request = new FilesGetRequest(
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
893 if (CanSendRequest()) { 833 if (CanSendRequest()) {
894 FOR_EACH_OBSERVER( 834 FOR_EACH_OBSERVER(
895 DriveServiceObserver, observers_, OnReadyToSendRequests()); 835 DriveServiceObserver, observers_, OnReadyToSendRequests());
896 } else if (!HasRefreshToken()) { 836 } else if (!HasRefreshToken()) {
897 FOR_EACH_OBSERVER( 837 FOR_EACH_OBSERVER(
898 DriveServiceObserver, observers_, OnRefreshTokenInvalid()); 838 DriveServiceObserver, observers_, OnRefreshTokenInvalid());
899 } 839 }
900 } 840 }
901 841
902 } // namespace drive 842 } // namespace drive
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/google_apis/drive_api_requests.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698