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