| 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/google_apis/drive_api_operations.h" | 5 #include "chrome/browser/google_apis/drive_api_operations.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 8 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 #include "chrome/browser/google_apis/drive_api_parser.h" | 11 #include "chrome/browser/google_apis/drive_api_parser.h" |
| 11 #include "chrome/browser/google_apis/operation_util.h" | 12 #include "chrome/browser/google_apis/operation_util.h" |
| 12 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 13 | 14 |
| 14 using content::BrowserThread; | 15 using content::BrowserThread; |
| 15 | 16 |
| 16 namespace google_apis { | 17 namespace google_apis { |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 const char kContentTypeApplicationJson[] = "application/json"; | 20 const char kContentTypeApplicationJson[] = "application/json"; |
| 20 const char kDirectoryMimeType[] = "application/vnd.google-apps.folder"; | 21 const char kDirectoryMimeType[] = "application/vnd.google-apps.folder"; |
| 21 | 22 |
| 22 // Parses the JSON value to AboutResource and runs |callback| on the UI | 23 // Parses the JSON value to a resource typed |T| and runs |callback| on the UI |
| 23 // thread once parsing is done. | 24 // thread once parsing is done. |
| 24 void ParseAboutResourceAndRun( | 25 template<typename T> |
| 25 const GetAboutResourceCallback& callback, | 26 void ParseJsonAndRun( |
| 27 const base::Callback<void(GDataErrorCode, scoped_ptr<T>)>& callback, |
| 26 GDataErrorCode error, | 28 GDataErrorCode error, |
| 27 scoped_ptr<base::Value> value) { | 29 scoped_ptr<base::Value> value) { |
| 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 29 DCHECK(!callback.is_null()); | 31 DCHECK(!callback.is_null()); |
| 30 | 32 |
| 31 scoped_ptr<AboutResource> about_resource; | 33 scoped_ptr<T> resource; |
| 32 if (value.get()) { | 34 if (value.get()) { |
| 33 about_resource = AboutResource::CreateFrom(*value); | 35 resource = T::CreateFrom(*value); |
| 34 if (!about_resource) { | 36 if (!resource) { |
| 35 // Failed to parse the JSON value (although the JSON value is available), | 37 // Failed to parse the JSON value, although the JSON value is available, |
| 36 // so let callback know the parsing error. | 38 // so let the callback know the parsing error. |
| 37 error = GDATA_PARSE_ERROR; | 39 error = GDATA_PARSE_ERROR; |
| 38 } | 40 } |
| 39 } | 41 } |
| 40 | 42 |
| 41 callback.Run(error, about_resource.Pass()); | 43 callback.Run(error, resource.Pass()); |
| 42 } | 44 } |
| 43 | 45 |
| 44 } // namespace | 46 } // namespace |
| 45 | 47 |
| 46 //============================== GetAboutOperation ============================= | 48 //============================== GetAboutOperation ============================= |
| 47 | 49 |
| 48 GetAboutOperation::GetAboutOperation( | 50 GetAboutOperation::GetAboutOperation( |
| 49 OperationRegistry* registry, | 51 OperationRegistry* registry, |
| 50 net::URLRequestContextGetter* url_request_context_getter, | 52 net::URLRequestContextGetter* url_request_context_getter, |
| 51 const DriveApiUrlGenerator& url_generator, | 53 const DriveApiUrlGenerator& url_generator, |
| 52 const GetAboutResourceCallback& callback) | 54 const GetAboutResourceCallback& callback) |
| 53 : GetDataOperation(registry, url_request_context_getter, | 55 : GetDataOperation(registry, url_request_context_getter, |
| 54 base::Bind(&ParseAboutResourceAndRun, callback)), | 56 base::Bind(&ParseJsonAndRun<AboutResource>, callback)), |
| 55 url_generator_(url_generator) { | 57 url_generator_(url_generator) { |
| 56 DCHECK(!callback.is_null()); | 58 DCHECK(!callback.is_null()); |
| 57 } | 59 } |
| 58 | 60 |
| 59 GetAboutOperation::~GetAboutOperation() {} | 61 GetAboutOperation::~GetAboutOperation() {} |
| 60 | 62 |
| 61 GURL GetAboutOperation::GetURL() const { | 63 GURL GetAboutOperation::GetURL() const { |
| 62 return url_generator_.GetAboutUrl(); | 64 return url_generator_.GetAboutUrl(); |
| 63 } | 65 } |
| 64 | 66 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 return url_generator_.GetFilelistUrl(url_, search_string_); | 126 return url_generator_.GetFilelistUrl(url_, search_string_); |
| 125 } | 127 } |
| 126 | 128 |
| 127 //=============================== GetFlieOperation ============================= | 129 //=============================== GetFlieOperation ============================= |
| 128 | 130 |
| 129 GetFileOperation::GetFileOperation( | 131 GetFileOperation::GetFileOperation( |
| 130 OperationRegistry* registry, | 132 OperationRegistry* registry, |
| 131 net::URLRequestContextGetter* url_request_context_getter, | 133 net::URLRequestContextGetter* url_request_context_getter, |
| 132 const DriveApiUrlGenerator& url_generator, | 134 const DriveApiUrlGenerator& url_generator, |
| 133 const std::string& file_id, | 135 const std::string& file_id, |
| 134 const GetDataCallback& callback) | 136 const FileResourceCallback& callback) |
| 135 : GetDataOperation(registry, url_request_context_getter, callback), | 137 : GetDataOperation(registry, url_request_context_getter, |
| 138 base::Bind(&ParseJsonAndRun<FileResource>, callback)), |
| 136 url_generator_(url_generator), | 139 url_generator_(url_generator), |
| 137 file_id_(file_id) { | 140 file_id_(file_id) { |
| 138 DCHECK(!callback.is_null()); | 141 DCHECK(!callback.is_null()); |
| 139 } | 142 } |
| 140 | 143 |
| 141 GetFileOperation::~GetFileOperation() {} | 144 GetFileOperation::~GetFileOperation() {} |
| 142 | 145 |
| 143 GURL GetFileOperation::GetURL() const { | 146 GURL GetFileOperation::GetURL() const { |
| 144 return url_generator_.GetFileUrl(file_id_); | 147 return url_generator_.GetFileUrl(file_id_); |
| 145 } | 148 } |
| 146 | 149 |
| 147 namespace drive { | 150 namespace drive { |
| 148 | 151 |
| 149 //========================== CreateDirectoryOperation ========================== | 152 //========================== CreateDirectoryOperation ========================== |
| 150 | 153 |
| 151 CreateDirectoryOperation::CreateDirectoryOperation( | 154 CreateDirectoryOperation::CreateDirectoryOperation( |
| 152 OperationRegistry* registry, | 155 OperationRegistry* registry, |
| 153 net::URLRequestContextGetter* url_request_context_getter, | 156 net::URLRequestContextGetter* url_request_context_getter, |
| 154 const DriveApiUrlGenerator& url_generator, | 157 const DriveApiUrlGenerator& url_generator, |
| 155 const std::string& parent_resource_id, | 158 const std::string& parent_resource_id, |
| 156 const std::string& directory_name, | 159 const std::string& directory_name, |
| 157 const GetDataCallback& callback) | 160 const FileResourceCallback& callback) |
| 158 : GetDataOperation(registry, url_request_context_getter, callback), | 161 : GetDataOperation(registry, url_request_context_getter, |
| 162 base::Bind(&ParseJsonAndRun<FileResource>, callback)), |
| 159 url_generator_(url_generator), | 163 url_generator_(url_generator), |
| 160 parent_resource_id_(parent_resource_id), | 164 parent_resource_id_(parent_resource_id), |
| 161 directory_name_(directory_name) { | 165 directory_name_(directory_name) { |
| 162 DCHECK(!callback.is_null()); | 166 DCHECK(!callback.is_null()); |
| 163 } | 167 } |
| 164 | 168 |
| 165 CreateDirectoryOperation::~CreateDirectoryOperation() {} | 169 CreateDirectoryOperation::~CreateDirectoryOperation() {} |
| 166 | 170 |
| 167 GURL CreateDirectoryOperation::GetURL() const { | 171 GURL CreateDirectoryOperation::GetURL() const { |
| 168 if (parent_resource_id_.empty() || directory_name_.empty()) { | 172 if (parent_resource_id_.empty() || directory_name_.empty()) { |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 return url_generator_.GetChildrenUrlForRemoval( | 332 return url_generator_.GetChildrenUrlForRemoval( |
| 329 parent_resource_id_, resource_id_); | 333 parent_resource_id_, resource_id_); |
| 330 } | 334 } |
| 331 | 335 |
| 332 net::URLFetcher::RequestType DeleteResourceOperation::GetRequestType() const { | 336 net::URLFetcher::RequestType DeleteResourceOperation::GetRequestType() const { |
| 333 return net::URLFetcher::DELETE_REQUEST; | 337 return net::URLFetcher::DELETE_REQUEST; |
| 334 } | 338 } |
| 335 | 339 |
| 336 } // namespace drive | 340 } // namespace drive |
| 337 } // namespace google_apis | 341 } // namespace google_apis |
| OLD | NEW |