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
3 // found in the LICENSE file.
5 #ifndef GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_
6 #define GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_
11 #include "base/callback_forward.h"
12 #include "base/location.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/sequenced_task_runner.h"
15 #include "base/task_runner_util.h"
16 #include "base/time/time.h"
17 #include "base/values.h"
18 #include "google_apis/drive/base_requests.h"
19 #include "google_apis/drive/drive_api_parser.h"
20 #include "google_apis/drive/drive_api_url_generator.h"
21 #include "google_apis/drive/drive_common_callbacks.h"
23 namespace google_apis
{
25 // Callback used for requests that the server returns FileList data
26 // formatted into JSON value.
27 typedef base::Callback
<void(DriveApiErrorCode error
,
28 scoped_ptr
<FileList
> entry
)> FileListCallback
;
30 // Callback used for requests that the server returns ChangeList data
31 // formatted into JSON value.
32 typedef base::Callback
<void(DriveApiErrorCode error
,
33 scoped_ptr
<ChangeList
> entry
)> ChangeListCallback
;
37 // Represents a property for a file or a directory.
38 // https://developers.google.com/drive/v2/reference/properties
44 // Visibility of the property. Either limited to the same client, or to any.
45 enum Visibility
{ VISIBILITY_PRIVATE
, VISIBILITY_PUBLIC
};
47 // Whether the property is public or private.
48 Visibility
visibility() const { return visibility_
; }
50 // Name of the property.
51 const std::string
& key() const { return key_
; }
53 // Value of the property.
54 const std::string
& value() const { return value_
; }
56 void set_visibility(Visibility visibility
) { visibility_
= visibility
; }
57 void set_key(const std::string
& key
) { key_
= key
; }
58 void set_value(const std::string
& value
) { value_
= value
; }
61 Visibility visibility_
;
66 // List of properties for a single file or a directory.
67 typedef std::vector
<Property
> Properties
;
69 // Child response embedded in multipart parent response.
70 struct MultipartHttpResponse
{
71 MultipartHttpResponse();
72 ~MultipartHttpResponse();
73 DriveApiErrorCode code
;
77 // Splits multipart |response| into |parts|. Each part must be HTTP sub-response
78 // of drive batch request. |content_type| is a value of Content-Type response
79 // header. Returns true on succcess.
80 bool ParseMultipartResponse(const std::string
& content_type
,
81 const std::string
& response
,
82 std::vector
<MultipartHttpResponse
>* parts
);
84 //============================ DriveApiPartialFieldRequest ====================
86 // This is base class of the Drive API related requests. All Drive API requests
87 // support partial request (to improve the performance). The function can be
88 // shared among the Drive API requests.
89 // See also https://developers.google.com/drive/performance
90 class DriveApiPartialFieldRequest
: public UrlFetchRequestBase
{
92 explicit DriveApiPartialFieldRequest(RequestSender
* sender
);
93 ~DriveApiPartialFieldRequest() override
;
95 // Optional parameter.
96 const std::string
& fields() const { return fields_
; }
97 void set_fields(const std::string
& fields
) { fields_
= fields
; }
100 // UrlFetchRequestBase overrides.
101 GURL
GetURL() const override
;
103 // Derived classes should override GetURLInternal instead of GetURL()
105 virtual GURL
GetURLInternal() const = 0;
110 DISALLOW_COPY_AND_ASSIGN(DriveApiPartialFieldRequest
);
113 //============================ DriveApiDataRequest ===========================
115 // The base class of Drive API related requests that receive a JSON response
116 // representing |DataType|.
117 template<class DataType
>
118 class DriveApiDataRequest
: public DriveApiPartialFieldRequest
{
120 typedef base::Callback
<void(DriveApiErrorCode error
,
121 scoped_ptr
<DataType
> data
)> Callback
;
123 // |callback| is called when the request finishes either by success or by
124 // failure. On success, a JSON Value object is passed. It must not be null.
125 DriveApiDataRequest(RequestSender
* sender
, const Callback
& callback
)
126 : DriveApiPartialFieldRequest(sender
),
128 weak_ptr_factory_(this) {
129 DCHECK(!callback_
.is_null());
131 ~DriveApiDataRequest() override
{}
134 // UrlFetchRequestBase overrides.
135 void ProcessURLFetchResults(const net::URLFetcher
* source
) override
{
136 DriveApiErrorCode error
= GetErrorCode();
140 base::PostTaskAndReplyWithResult(
141 blocking_task_runner(),
143 base::Bind(&DriveApiDataRequest::Parse
, response_writer()->data()),
144 base::Bind(&DriveApiDataRequest::OnDataParsed
,
145 weak_ptr_factory_
.GetWeakPtr(), error
));
148 RunCallbackOnPrematureFailure(error
);
149 OnProcessURLFetchResultsComplete();
154 void RunCallbackOnPrematureFailure(DriveApiErrorCode error
) override
{
155 callback_
.Run(error
, scoped_ptr
<DataType
>());
159 // Parses the |json| string by using DataType::CreateFrom.
160 static scoped_ptr
<DataType
> Parse(const std::string
& json
) {
161 scoped_ptr
<base::Value
> value
= ParseJson(json
);
162 return value
? DataType::CreateFrom(*value
) : scoped_ptr
<DataType
>();
165 // Receives the parsed result and invokes the callback.
166 void OnDataParsed(DriveApiErrorCode error
, scoped_ptr
<DataType
> value
) {
168 error
= DRIVE_PARSE_ERROR
;
169 callback_
.Run(error
, value
.Pass());
170 OnProcessURLFetchResultsComplete();
173 const Callback callback_
;
175 // Note: This should remain the last member so it'll be destroyed and
176 // invalidate its weak pointers before any other members are destroyed.
177 base::WeakPtrFactory
<DriveApiDataRequest
> weak_ptr_factory_
;
179 DISALLOW_COPY_AND_ASSIGN(DriveApiDataRequest
);
182 //=============================== FilesGetRequest =============================
184 // This class performs the request for fetching a file.
185 // This request is mapped to
186 // https://developers.google.com/drive/v2/reference/files/get
187 class FilesGetRequest
: public DriveApiDataRequest
<FileResource
> {
189 FilesGetRequest(RequestSender
* sender
,
190 const DriveApiUrlGenerator
& url_generator
,
191 bool use_internal_endpoint
,
192 const FileResourceCallback
& callback
);
193 ~FilesGetRequest() override
;
195 // Required parameter.
196 const std::string
& file_id() const { return file_id_
; }
197 void set_file_id(const std::string
& file_id
) { file_id_
= file_id
; }
199 // Optional parameter.
200 const GURL
& embed_origin() const { return embed_origin_
; }
201 void set_embed_origin(const GURL
& embed_origin
) {
202 embed_origin_
= embed_origin
;
206 // Overridden from DriveApiDataRequest.
207 GURL
GetURLInternal() const override
;
210 const DriveApiUrlGenerator url_generator_
;
211 const bool use_internal_endpoint_
;
212 std::string file_id_
;
215 DISALLOW_COPY_AND_ASSIGN(FilesGetRequest
);
218 //============================ FilesAuthorizeRequest ===========================
220 // This class performs request for authorizing an app to access a file.
221 // This request is mapped to /drive/v2internal/file/authorize internal endpoint.
222 class FilesAuthorizeRequest
: public DriveApiDataRequest
<FileResource
> {
224 FilesAuthorizeRequest(RequestSender
* sender
,
225 const DriveApiUrlGenerator
& url_generator
,
226 const FileResourceCallback
& callback
);
227 ~FilesAuthorizeRequest() override
;
229 // Required parameter.
230 const std::string
& file_id() const { return file_id_
; }
231 void set_file_id(const std::string
& file_id
) { file_id_
= file_id
; }
232 const std::string
& app_id() const { return app_id_
; }
233 void set_app_id(const std::string
& app_id
) { app_id_
= app_id
; }
236 // Overridden from GetDataRequest.
237 net::URLFetcher::RequestType
GetRequestType() const override
;
239 // Overridden from DriveApiDataRequest.
240 GURL
GetURLInternal() const override
;
243 const DriveApiUrlGenerator url_generator_
;
244 std::string file_id_
;
247 DISALLOW_COPY_AND_ASSIGN(FilesAuthorizeRequest
);
250 //============================ FilesInsertRequest =============================
252 // This class performs the request for creating a resource.
253 // This request is mapped to
254 // https://developers.google.com/drive/v2/reference/files/insert
255 // See also https://developers.google.com/drive/manage-uploads and
256 // https://developers.google.com/drive/folder
257 class FilesInsertRequest
: public DriveApiDataRequest
<FileResource
> {
259 FilesInsertRequest(RequestSender
* sender
,
260 const DriveApiUrlGenerator
& url_generator
,
261 const FileResourceCallback
& callback
);
262 ~FilesInsertRequest() override
;
264 // Optional request body.
265 const base::Time
& last_viewed_by_me_date() const {
266 return last_viewed_by_me_date_
;
268 void set_last_viewed_by_me_date(const base::Time
& last_viewed_by_me_date
) {
269 last_viewed_by_me_date_
= last_viewed_by_me_date
;
272 const std::string
& mime_type() const { return mime_type_
; }
273 void set_mime_type(const std::string
& mime_type
) {
274 mime_type_
= mime_type
;
277 const base::Time
& modified_date() const { return modified_date_
; }
278 void set_modified_date(const base::Time
& modified_date
) {
279 modified_date_
= modified_date
;
282 const std::vector
<std::string
>& parents() const { return parents_
; }
283 void add_parent(const std::string
& parent
) { parents_
.push_back(parent
); }
285 const std::string
& title() const { return title_
; }
286 void set_title(const std::string
& title
) { title_
= title
; }
288 const Properties
& properties() const { return properties_
; }
289 void set_properties(const Properties
& properties
) {
290 properties_
= properties
;
294 // Overridden from GetDataRequest.
295 net::URLFetcher::RequestType
GetRequestType() const override
;
296 bool GetContentData(std::string
* upload_content_type
,
297 std::string
* upload_content
) override
;
299 // Overridden from DriveApiDataRequest.
300 GURL
GetURLInternal() const override
;
303 const DriveApiUrlGenerator url_generator_
;
305 base::Time last_viewed_by_me_date_
;
306 std::string mime_type_
;
307 base::Time modified_date_
;
308 std::vector
<std::string
> parents_
;
310 Properties properties_
;
312 DISALLOW_COPY_AND_ASSIGN(FilesInsertRequest
);
315 //============================== FilesPatchRequest ============================
317 // This class performs the request for patching file metadata.
318 // This request is mapped to
319 // https://developers.google.com/drive/v2/reference/files/patch
320 class FilesPatchRequest
: public DriveApiDataRequest
<FileResource
> {
322 FilesPatchRequest(RequestSender
* sender
,
323 const DriveApiUrlGenerator
& url_generator
,
324 const FileResourceCallback
& callback
);
325 ~FilesPatchRequest() override
;
327 // Required parameter.
328 const std::string
& file_id() const { return file_id_
; }
329 void set_file_id(const std::string
& file_id
) { file_id_
= file_id
; }
331 // Optional parameter.
332 bool set_modified_date() const { return set_modified_date_
; }
333 void set_set_modified_date(bool set_modified_date
) {
334 set_modified_date_
= set_modified_date
;
337 bool update_viewed_date() const { return update_viewed_date_
; }
338 void set_update_viewed_date(bool update_viewed_date
) {
339 update_viewed_date_
= update_viewed_date
;
342 // Optional request body.
343 // Note: "Files: patch" accepts any "Files resource" data, but this class
344 // only supports limited members of it for now. We can extend it upon
346 const std::string
& title() const { return title_
; }
347 void set_title(const std::string
& title
) { title_
= title
; }
349 const base::Time
& modified_date() const { return modified_date_
; }
350 void set_modified_date(const base::Time
& modified_date
) {
351 modified_date_
= modified_date
;
354 const base::Time
& last_viewed_by_me_date() const {
355 return last_viewed_by_me_date_
;
357 void set_last_viewed_by_me_date(const base::Time
& last_viewed_by_me_date
) {
358 last_viewed_by_me_date_
= last_viewed_by_me_date
;
361 const std::vector
<std::string
>& parents() const { return parents_
; }
362 void add_parent(const std::string
& parent
) { parents_
.push_back(parent
); }
364 const Properties
& properties() const { return properties_
; }
365 void set_properties(const Properties
& properties
) {
366 properties_
= properties
;
370 // Overridden from URLFetchRequestBase.
371 net::URLFetcher::RequestType
GetRequestType() const override
;
372 std::vector
<std::string
> GetExtraRequestHeaders() const override
;
373 bool GetContentData(std::string
* upload_content_type
,
374 std::string
* upload_content
) override
;
376 // Overridden from DriveApiDataRequest.
377 GURL
GetURLInternal() const override
;
380 const DriveApiUrlGenerator url_generator_
;
382 std::string file_id_
;
383 bool set_modified_date_
;
384 bool update_viewed_date_
;
387 base::Time modified_date_
;
388 base::Time last_viewed_by_me_date_
;
389 std::vector
<std::string
> parents_
;
390 Properties properties_
;
392 DISALLOW_COPY_AND_ASSIGN(FilesPatchRequest
);
395 //============================= FilesCopyRequest ==============================
397 // This class performs the request for copying a resource.
398 // This request is mapped to
399 // https://developers.google.com/drive/v2/reference/files/copy
400 class FilesCopyRequest
: public DriveApiDataRequest
<FileResource
> {
402 // Upon completion, |callback| will be called. |callback| must not be null.
403 FilesCopyRequest(RequestSender
* sender
,
404 const DriveApiUrlGenerator
& url_generator
,
405 const FileResourceCallback
& callback
);
406 ~FilesCopyRequest() override
;
408 // Required parameter.
409 const std::string
& file_id() const { return file_id_
; }
410 void set_file_id(const std::string
& file_id
) { file_id_
= file_id
; }
412 // Optional request body.
413 const std::vector
<std::string
>& parents() const { return parents_
; }
414 void add_parent(const std::string
& parent
) { parents_
.push_back(parent
); }
416 const base::Time
& modified_date() const { return modified_date_
; }
417 void set_modified_date(const base::Time
& modified_date
) {
418 modified_date_
= modified_date
;
421 const std::string
& title() const { return title_
; }
422 void set_title(const std::string
& title
) { title_
= title
; }
425 // Overridden from URLFetchRequestBase.
426 net::URLFetcher::RequestType
GetRequestType() const override
;
427 bool GetContentData(std::string
* upload_content_type
,
428 std::string
* upload_content
) override
;
430 // Overridden from DriveApiDataRequest.
431 GURL
GetURLInternal() const override
;
434 const DriveApiUrlGenerator url_generator_
;
436 std::string file_id_
;
437 base::Time modified_date_
;
438 std::vector
<std::string
> parents_
;
441 DISALLOW_COPY_AND_ASSIGN(FilesCopyRequest
);
444 //============================= FilesListRequest =============================
446 // This class performs the request for fetching FileList.
447 // The result may contain only first part of the result. The remaining result
448 // should be able to be fetched by ContinueGetFileListRequest defined below,
449 // or by FilesListRequest with setting page token.
450 // This request is mapped to
451 // https://developers.google.com/drive/v2/reference/files/list
452 class FilesListRequest
: public DriveApiDataRequest
<FileList
> {
454 FilesListRequest(RequestSender
* sender
,
455 const DriveApiUrlGenerator
& url_generator
,
456 const FileListCallback
& callback
);
457 ~FilesListRequest() override
;
459 // Optional parameter
460 int max_results() const { return max_results_
; }
461 void set_max_results(int max_results
) { max_results_
= max_results
; }
463 const std::string
& page_token() const { return page_token_
; }
464 void set_page_token(const std::string
& page_token
) {
465 page_token_
= page_token
;
468 const std::string
& q() const { return q_
; }
469 void set_q(const std::string
& q
) { q_
= q
; }
472 // Overridden from DriveApiDataRequest.
473 GURL
GetURLInternal() const override
;
476 const DriveApiUrlGenerator url_generator_
;
478 std::string page_token_
;
481 DISALLOW_COPY_AND_ASSIGN(FilesListRequest
);
484 //========================= FilesListNextPageRequest ==========================
486 // There are two ways to obtain next pages of "Files: list" result (if paged).
487 // 1) Set pageToken and all params used for the initial request.
488 // 2) Use URL in the nextLink field in the previous response.
489 // This class implements 2)'s request.
490 class FilesListNextPageRequest
: public DriveApiDataRequest
<FileList
> {
492 FilesListNextPageRequest(RequestSender
* sender
,
493 const FileListCallback
& callback
);
494 ~FilesListNextPageRequest() override
;
496 const GURL
& next_link() const { return next_link_
; }
497 void set_next_link(const GURL
& next_link
) { next_link_
= next_link
; }
500 // Overridden from DriveApiDataRequest.
501 GURL
GetURLInternal() const override
;
506 DISALLOW_COPY_AND_ASSIGN(FilesListNextPageRequest
);
509 //============================= FilesDeleteRequest =============================
511 // This class performs the request for deleting a resource.
512 // This request is mapped to
513 // https://developers.google.com/drive/v2/reference/files/delete
514 class FilesDeleteRequest
: public EntryActionRequest
{
516 FilesDeleteRequest(RequestSender
* sender
,
517 const DriveApiUrlGenerator
& url_generator
,
518 const EntryActionCallback
& callback
);
519 ~FilesDeleteRequest() override
;
521 // Required parameter.
522 const std::string
& file_id() const { return file_id_
; }
523 void set_file_id(const std::string
& file_id
) { file_id_
= file_id
; }
524 void set_etag(const std::string
& etag
) { etag_
= etag
; }
527 // Overridden from UrlFetchRequestBase.
528 net::URLFetcher::RequestType
GetRequestType() const override
;
529 GURL
GetURL() const override
;
530 std::vector
<std::string
> GetExtraRequestHeaders() const override
;
533 const DriveApiUrlGenerator url_generator_
;
534 std::string file_id_
;
537 DISALLOW_COPY_AND_ASSIGN(FilesDeleteRequest
);
540 //============================= FilesTrashRequest ==============================
542 // This class performs the request for trashing a resource.
543 // This request is mapped to
544 // https://developers.google.com/drive/v2/reference/files/trash
545 class FilesTrashRequest
: public DriveApiDataRequest
<FileResource
> {
547 FilesTrashRequest(RequestSender
* sender
,
548 const DriveApiUrlGenerator
& url_generator
,
549 const FileResourceCallback
& callback
);
550 ~FilesTrashRequest() override
;
552 // Required parameter.
553 const std::string
& file_id() const { return file_id_
; }
554 void set_file_id(const std::string
& file_id
) { file_id_
= file_id
; }
557 // Overridden from UrlFetchRequestBase.
558 net::URLFetcher::RequestType
GetRequestType() const override
;
560 // Overridden from DriveApiDataRequest.
561 GURL
GetURLInternal() const override
;
564 const DriveApiUrlGenerator url_generator_
;
565 std::string file_id_
;
567 DISALLOW_COPY_AND_ASSIGN(FilesTrashRequest
);
570 //============================== AboutGetRequest =============================
572 // This class performs the request for fetching About data.
573 // This request is mapped to
574 // https://developers.google.com/drive/v2/reference/about/get
575 class AboutGetRequest
: public DriveApiDataRequest
<AboutResource
> {
577 AboutGetRequest(RequestSender
* sender
,
578 const DriveApiUrlGenerator
& url_generator
,
579 const AboutResourceCallback
& callback
);
580 ~AboutGetRequest() override
;
583 // Overridden from DriveApiDataRequest.
584 GURL
GetURLInternal() const override
;
587 const DriveApiUrlGenerator url_generator_
;
589 DISALLOW_COPY_AND_ASSIGN(AboutGetRequest
);
592 //============================ ChangesListRequest ============================
594 // This class performs the request for fetching ChangeList.
595 // The result may contain only first part of the result. The remaining result
596 // should be able to be fetched by ContinueGetFileListRequest defined below.
597 // or by ChangesListRequest with setting page token.
598 // This request is mapped to
599 // https://developers.google.com/drive/v2/reference/changes/list
600 class ChangesListRequest
: public DriveApiDataRequest
<ChangeList
> {
602 ChangesListRequest(RequestSender
* sender
,
603 const DriveApiUrlGenerator
& url_generator
,
604 const ChangeListCallback
& callback
);
605 ~ChangesListRequest() override
;
607 // Optional parameter
608 bool include_deleted() const { return include_deleted_
; }
609 void set_include_deleted(bool include_deleted
) {
610 include_deleted_
= include_deleted
;
613 int max_results() const { return max_results_
; }
614 void set_max_results(int max_results
) { max_results_
= max_results
; }
616 const std::string
& page_token() const { return page_token_
; }
617 void set_page_token(const std::string
& page_token
) {
618 page_token_
= page_token
;
621 int64
start_change_id() const { return start_change_id_
; }
622 void set_start_change_id(int64 start_change_id
) {
623 start_change_id_
= start_change_id
;
627 // Overridden from DriveApiDataRequest.
628 GURL
GetURLInternal() const override
;
631 const DriveApiUrlGenerator url_generator_
;
632 bool include_deleted_
;
634 std::string page_token_
;
635 int64 start_change_id_
;
637 DISALLOW_COPY_AND_ASSIGN(ChangesListRequest
);
640 //======================== ChangesListNextPageRequest =========================
642 // There are two ways to obtain next pages of "Changes: list" result (if paged).
643 // 1) Set pageToken and all params used for the initial request.
644 // 2) Use URL in the nextLink field in the previous response.
645 // This class implements 2)'s request.
646 class ChangesListNextPageRequest
: public DriveApiDataRequest
<ChangeList
> {
648 ChangesListNextPageRequest(RequestSender
* sender
,
649 const ChangeListCallback
& callback
);
650 ~ChangesListNextPageRequest() override
;
652 const GURL
& next_link() const { return next_link_
; }
653 void set_next_link(const GURL
& next_link
) { next_link_
= next_link
; }
656 // Overridden from DriveApiDataRequest.
657 GURL
GetURLInternal() const override
;
662 DISALLOW_COPY_AND_ASSIGN(ChangesListNextPageRequest
);
665 //============================= AppsListRequest ============================
667 // This class performs the request for fetching AppList.
668 // This request is mapped to
669 // https://developers.google.com/drive/v2/reference/apps/list
670 class AppsListRequest
: public DriveApiDataRequest
<AppList
> {
672 AppsListRequest(RequestSender
* sender
,
673 const DriveApiUrlGenerator
& url_generator
,
674 bool use_internal_endpoint
,
675 const AppListCallback
& callback
);
676 ~AppsListRequest() override
;
679 // Overridden from DriveApiDataRequest.
680 GURL
GetURLInternal() const override
;
683 const DriveApiUrlGenerator url_generator_
;
684 const bool use_internal_endpoint_
;
686 DISALLOW_COPY_AND_ASSIGN(AppsListRequest
);
689 //============================= AppsDeleteRequest ==============================
691 // This class performs the request for deleting a Drive app.
692 // This request is mapped to
693 // https://developers.google.com/drive/v2/reference/files/trash
694 class AppsDeleteRequest
: public EntryActionRequest
{
696 AppsDeleteRequest(RequestSender
* sender
,
697 const DriveApiUrlGenerator
& url_generator
,
698 const EntryActionCallback
& callback
);
699 ~AppsDeleteRequest() override
;
701 // Required parameter.
702 const std::string
& app_id() const { return app_id_
; }
703 void set_app_id(const std::string
& app_id
) { app_id_
= app_id
; }
706 // Overridden from UrlFetchRequestBase.
707 net::URLFetcher::RequestType
GetRequestType() const override
;
708 GURL
GetURL() const override
;
711 const DriveApiUrlGenerator url_generator_
;
714 DISALLOW_COPY_AND_ASSIGN(AppsDeleteRequest
);
717 //========================== ChildrenInsertRequest ============================
719 // This class performs the request for inserting a resource to a directory.
720 // This request is mapped to
721 // https://developers.google.com/drive/v2/reference/children/insert
722 class ChildrenInsertRequest
: public EntryActionRequest
{
724 ChildrenInsertRequest(RequestSender
* sender
,
725 const DriveApiUrlGenerator
& url_generator
,
726 const EntryActionCallback
& callback
);
727 ~ChildrenInsertRequest() override
;
729 // Required parameter.
730 const std::string
& folder_id() const { return folder_id_
; }
731 void set_folder_id(const std::string
& folder_id
) {
732 folder_id_
= folder_id
;
736 const std::string
& id() const { return id_
; }
737 void set_id(const std::string
& id
) { id_
= id
; }
740 // UrlFetchRequestBase overrides.
741 net::URLFetcher::RequestType
GetRequestType() const override
;
742 GURL
GetURL() const override
;
743 bool GetContentData(std::string
* upload_content_type
,
744 std::string
* upload_content
) override
;
747 const DriveApiUrlGenerator url_generator_
;
748 std::string folder_id_
;
751 DISALLOW_COPY_AND_ASSIGN(ChildrenInsertRequest
);
754 //========================== ChildrenDeleteRequest ============================
756 // This class performs the request for removing a resource from a directory.
757 // This request is mapped to
758 // https://developers.google.com/drive/v2/reference/children/delete
759 class ChildrenDeleteRequest
: public EntryActionRequest
{
761 // |callback| must not be null.
762 ChildrenDeleteRequest(RequestSender
* sender
,
763 const DriveApiUrlGenerator
& url_generator
,
764 const EntryActionCallback
& callback
);
765 ~ChildrenDeleteRequest() override
;
767 // Required parameter.
768 const std::string
& child_id() const { return child_id_
; }
769 void set_child_id(const std::string
& child_id
) {
770 child_id_
= child_id
;
773 const std::string
& folder_id() const { return folder_id_
; }
774 void set_folder_id(const std::string
& folder_id
) {
775 folder_id_
= folder_id
;
779 // UrlFetchRequestBase overrides.
780 net::URLFetcher::RequestType
GetRequestType() const override
;
781 GURL
GetURL() const override
;
784 const DriveApiUrlGenerator url_generator_
;
785 std::string child_id_
;
786 std::string folder_id_
;
788 DISALLOW_COPY_AND_ASSIGN(ChildrenDeleteRequest
);
791 //======================= InitiateUploadNewFileRequest =======================
793 // This class performs the request for initiating the upload of a new file.
794 class InitiateUploadNewFileRequest
: public InitiateUploadRequestBase
{
796 // |parent_resource_id| should be the resource id of the parent directory.
797 // |title| should be set.
798 // See also the comments of InitiateUploadRequestBase for more details
799 // about the other parameters.
800 InitiateUploadNewFileRequest(RequestSender
* sender
,
801 const DriveApiUrlGenerator
& url_generator
,
802 const std::string
& content_type
,
803 int64 content_length
,
804 const std::string
& parent_resource_id
,
805 const std::string
& title
,
806 const InitiateUploadCallback
& callback
);
807 ~InitiateUploadNewFileRequest() override
;
809 // Optional parameters.
810 const base::Time
& modified_date() const { return modified_date_
; }
811 void set_modified_date(const base::Time
& modified_date
) {
812 modified_date_
= modified_date
;
814 const base::Time
& last_viewed_by_me_date() const {
815 return last_viewed_by_me_date_
;
817 void set_last_viewed_by_me_date(const base::Time
& last_viewed_by_me_date
) {
818 last_viewed_by_me_date_
= last_viewed_by_me_date
;
820 const Properties
& properties() const { return properties_
; }
821 void set_properties(const Properties
& properties
) {
822 properties_
= properties
;
826 // UrlFetchRequestBase overrides.
827 GURL
GetURL() const override
;
828 net::URLFetcher::RequestType
GetRequestType() const override
;
829 bool GetContentData(std::string
* upload_content_type
,
830 std::string
* upload_content
) override
;
833 const DriveApiUrlGenerator url_generator_
;
834 const std::string parent_resource_id_
;
835 const std::string title_
;
837 base::Time modified_date_
;
838 base::Time last_viewed_by_me_date_
;
839 Properties properties_
;
841 DISALLOW_COPY_AND_ASSIGN(InitiateUploadNewFileRequest
);
844 //==================== InitiateUploadExistingFileRequest =====================
846 // This class performs the request for initiating the upload of an existing
848 class InitiateUploadExistingFileRequest
: public InitiateUploadRequestBase
{
850 // |upload_url| should be the upload_url() of the file
851 // (resumable-create-media URL)
852 // |etag| should be set if it is available to detect the upload confliction.
853 // See also the comments of InitiateUploadRequestBase for more details
854 // about the other parameters.
855 InitiateUploadExistingFileRequest(RequestSender
* sender
,
856 const DriveApiUrlGenerator
& url_generator
,
857 const std::string
& content_type
,
858 int64 content_length
,
859 const std::string
& resource_id
,
860 const std::string
& etag
,
861 const InitiateUploadCallback
& callback
);
862 ~InitiateUploadExistingFileRequest() override
;
864 // Optional parameters.
865 const std::string
& parent_resource_id() const { return parent_resource_id_
; }
866 void set_parent_resource_id(const std::string
& parent_resource_id
) {
867 parent_resource_id_
= parent_resource_id
;
869 const std::string
& title() const { return title_
; }
870 void set_title(const std::string
& title
) { title_
= title
; }
871 const base::Time
& modified_date() const { return modified_date_
; }
872 void set_modified_date(const base::Time
& modified_date
) {
873 modified_date_
= modified_date
;
875 const base::Time
& last_viewed_by_me_date() const {
876 return last_viewed_by_me_date_
;
878 void set_last_viewed_by_me_date(const base::Time
& last_viewed_by_me_date
) {
879 last_viewed_by_me_date_
= last_viewed_by_me_date
;
881 const Properties
& properties() const { return properties_
; }
882 void set_properties(const Properties
& properties
) {
883 properties_
= properties
;
887 // UrlFetchRequestBase overrides.
888 GURL
GetURL() const override
;
889 net::URLFetcher::RequestType
GetRequestType() const override
;
890 std::vector
<std::string
> GetExtraRequestHeaders() const override
;
891 bool GetContentData(std::string
* upload_content_type
,
892 std::string
* upload_content
) override
;
895 const DriveApiUrlGenerator url_generator_
;
896 const std::string resource_id_
;
897 const std::string etag_
;
899 std::string parent_resource_id_
;
901 base::Time modified_date_
;
902 base::Time last_viewed_by_me_date_
;
903 Properties properties_
;
905 DISALLOW_COPY_AND_ASSIGN(InitiateUploadExistingFileRequest
);
908 // Callback used for ResumeUpload() and GetUploadStatus().
909 typedef base::Callback
<void(
910 const UploadRangeResponse
& response
,
911 scoped_ptr
<FileResource
> new_resource
)> UploadRangeCallback
;
913 //============================ ResumeUploadRequest ===========================
915 // Performs the request for resuming the upload of a file.
916 class ResumeUploadRequest
: public ResumeUploadRequestBase
{
918 // See also ResumeUploadRequestBase's comment for parameters meaning.
919 // |callback| must not be null. |progress_callback| may be null.
920 ResumeUploadRequest(RequestSender
* sender
,
921 const GURL
& upload_location
,
922 int64 start_position
,
924 int64 content_length
,
925 const std::string
& content_type
,
926 const base::FilePath
& local_file_path
,
927 const UploadRangeCallback
& callback
,
928 const ProgressCallback
& progress_callback
);
929 ~ResumeUploadRequest() override
;
932 // UploadRangeRequestBase overrides.
933 void OnRangeRequestComplete(const UploadRangeResponse
& response
,
934 scoped_ptr
<base::Value
> value
) override
;
935 // content::UrlFetcherDelegate overrides.
936 void OnURLFetchUploadProgress(const net::URLFetcher
* source
,
938 int64 total
) override
;
941 const UploadRangeCallback callback_
;
942 const ProgressCallback progress_callback_
;
944 DISALLOW_COPY_AND_ASSIGN(ResumeUploadRequest
);
947 //========================== GetUploadStatusRequest ==========================
949 // Performs the request to fetch the current upload status of a file.
950 class GetUploadStatusRequest
: public GetUploadStatusRequestBase
{
952 // See also GetUploadStatusRequestBase's comment for parameters meaning.
953 // |callback| must not be null.
954 GetUploadStatusRequest(RequestSender
* sender
,
955 const GURL
& upload_url
,
956 int64 content_length
,
957 const UploadRangeCallback
& callback
);
958 ~GetUploadStatusRequest() override
;
961 // UploadRangeRequestBase overrides.
962 void OnRangeRequestComplete(const UploadRangeResponse
& response
,
963 scoped_ptr
<base::Value
> value
) override
;
966 const UploadRangeCallback callback_
;
968 DISALLOW_COPY_AND_ASSIGN(GetUploadStatusRequest
);
971 //======================= MultipartUploadNewFileDelegate =======================
973 // This class performs the request for initiating the upload of a new file.
974 class MultipartUploadNewFileDelegate
: public MultipartUploadRequestBase
{
976 // |parent_resource_id| should be the resource id of the parent directory.
977 // |title| should be set.
978 // See also the comments of MultipartUploadRequestBase for more details
979 // about the other parameters.
980 MultipartUploadNewFileDelegate(base::SequencedTaskRunner
* task_runner
,
981 const std::string
& title
,
982 const std::string
& parent_resource_id
,
983 const std::string
& content_type
,
984 int64 content_length
,
985 const base::Time
& modified_date
,
986 const base::Time
& last_viewed_by_me_date
,
987 const base::FilePath
& local_file_path
,
988 const Properties
& properties
,
989 const DriveApiUrlGenerator
& url_generator
,
990 const FileResourceCallback
& callback
,
991 const ProgressCallback
& progress_callback
);
992 ~MultipartUploadNewFileDelegate() override
;
995 // UrlFetchRequestBase overrides.
996 GURL
GetURL() const override
;
997 net::URLFetcher::RequestType
GetRequestType() const override
;
1000 const bool has_modified_date_
;
1001 const DriveApiUrlGenerator url_generator_
;
1003 DISALLOW_COPY_AND_ASSIGN(MultipartUploadNewFileDelegate
);
1006 //====================== MultipartUploadExistingFileDelegate ===================
1008 // This class performs the request for initiating the upload of a new file.
1009 class MultipartUploadExistingFileDelegate
: public MultipartUploadRequestBase
{
1011 // |parent_resource_id| should be the resource id of the parent directory.
1012 // |title| should be set.
1013 // See also the comments of MultipartUploadRequestBase for more details
1014 // about the other parameters.
1015 MultipartUploadExistingFileDelegate(
1016 base::SequencedTaskRunner
* task_runner
,
1017 const std::string
& title
,
1018 const std::string
& resource_id
,
1019 const std::string
& parent_resource_id
,
1020 const std::string
& content_type
,
1021 int64 content_length
,
1022 const base::Time
& modified_date
,
1023 const base::Time
& last_viewed_by_me_date
,
1024 const base::FilePath
& local_file_path
,
1025 const std::string
& etag
,
1026 const Properties
& properties
,
1027 const DriveApiUrlGenerator
& url_generator
,
1028 const FileResourceCallback
& callback
,
1029 const ProgressCallback
& progress_callback
);
1030 ~MultipartUploadExistingFileDelegate() override
;
1033 // UrlFetchRequestBase overrides.
1034 std::vector
<std::string
> GetExtraRequestHeaders() const override
;
1035 GURL
GetURL() const override
;
1036 net::URLFetcher::RequestType
GetRequestType() const override
;
1039 const std::string resource_id_
;
1040 const std::string etag_
;
1041 const bool has_modified_date_
;
1042 const DriveApiUrlGenerator url_generator_
;
1044 DISALLOW_COPY_AND_ASSIGN(MultipartUploadExistingFileDelegate
);
1047 //========================== DownloadFileRequest ==========================
1049 // This class performs the request for downloading of a specified file.
1050 class DownloadFileRequest
: public DownloadFileRequestBase
{
1052 // See also DownloadFileRequestBase's comment for parameters meaning.
1053 DownloadFileRequest(RequestSender
* sender
,
1054 const DriveApiUrlGenerator
& url_generator
,
1055 const std::string
& resource_id
,
1056 const base::FilePath
& output_file_path
,
1057 const DownloadActionCallback
& download_action_callback
,
1058 const GetContentCallback
& get_content_callback
,
1059 const ProgressCallback
& progress_callback
);
1060 ~DownloadFileRequest() override
;
1062 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequest
);
1065 //========================== PermissionsInsertRequest ==========================
1067 // Enumeration type for specifying type of permissions.
1068 enum PermissionType
{
1069 PERMISSION_TYPE_ANYONE
,
1070 PERMISSION_TYPE_DOMAIN
,
1071 PERMISSION_TYPE_GROUP
,
1072 PERMISSION_TYPE_USER
,
1075 // Enumeration type for specifying the role of permissions.
1076 enum PermissionRole
{
1077 PERMISSION_ROLE_OWNER
,
1078 PERMISSION_ROLE_READER
,
1079 PERMISSION_ROLE_WRITER
,
1080 PERMISSION_ROLE_COMMENTER
,
1083 // This class performs the request for adding permission on a specified file.
1084 class PermissionsInsertRequest
: public EntryActionRequest
{
1086 // See https://developers.google.com/drive/v2/reference/permissions/insert.
1087 PermissionsInsertRequest(RequestSender
* sender
,
1088 const DriveApiUrlGenerator
& url_generator
,
1089 const EntryActionCallback
& callback
);
1090 ~PermissionsInsertRequest() override
;
1092 void set_id(const std::string
& id
) { id_
= id
; }
1093 void set_type(PermissionType type
) { type_
= type
; }
1094 void set_role(PermissionRole role
) { role_
= role
; }
1095 void set_value(const std::string
& value
) { value_
= value
; }
1097 // UrlFetchRequestBase overrides.
1098 GURL
GetURL() const override
;
1099 net::URLFetcher::RequestType
GetRequestType() const override
;
1100 bool GetContentData(std::string
* upload_content_type
,
1101 std::string
* upload_content
) override
;
1104 const DriveApiUrlGenerator url_generator_
;
1106 PermissionType type_
;
1107 PermissionRole role_
;
1110 DISALLOW_COPY_AND_ASSIGN(PermissionsInsertRequest
);
1113 //======================= SingleBatchableDelegateRequest =======================
1115 // Request that is operated by single BatchableDelegate.
1116 class SingleBatchableDelegateRequest
: public UrlFetchRequestBase
{
1118 // The instance takes ownership of |delegate|.
1119 SingleBatchableDelegateRequest(RequestSender
* sender
,
1120 BatchableDelegate
* delegate
);
1121 ~SingleBatchableDelegateRequest() override
;
1124 GURL
GetURL() const override
;
1125 net::URLFetcher::RequestType
GetRequestType() const override
;
1126 std::vector
<std::string
> GetExtraRequestHeaders() const override
;
1127 void Prepare(const PrepareCallback
& callback
) override
;
1128 bool GetContentData(std::string
* upload_content_type
,
1129 std::string
* upload_content
) override
;
1130 void RunCallbackOnPrematureFailure(DriveApiErrorCode code
) override
;
1131 void ProcessURLFetchResults(const net::URLFetcher
* source
) override
;
1132 void OnURLFetchUploadProgress(const net::URLFetcher
* source
,
1134 int64 total
) override
;
1135 scoped_ptr
<BatchableDelegate
> delegate_
;
1137 // Note: This should remain the last member so it'll be destroyed and
1138 // invalidate its weak pointers before any other members are destroyed.
1139 base::WeakPtrFactory
<SingleBatchableDelegateRequest
> weak_ptr_factory_
;
1141 DISALLOW_COPY_AND_ASSIGN(SingleBatchableDelegateRequest
);
1144 //========================== BatchUploadRequest ==========================
1146 class BatchUploadChildEntry
{
1148 explicit BatchUploadChildEntry(BatchableDelegate
* request
);
1149 ~BatchUploadChildEntry();
1150 scoped_ptr
<BatchableDelegate
> request
;
1156 DISALLOW_COPY_AND_ASSIGN(BatchUploadChildEntry
);
1159 class BatchUploadRequest
: public UrlFetchRequestBase
{
1161 BatchUploadRequest(RequestSender
* sender
,
1162 const DriveApiUrlGenerator
& url_generator
);
1163 ~BatchUploadRequest() override
;
1165 // Adds request to the batch request. The instance takes ownership of
1167 void AddRequest(BatchableDelegate
* request
);
1169 // Completes building batch upload request, and starts to send the request to
1170 // server. Must add at least one request before calling |Commit|.
1173 // Obtains weak pointer of this.
1174 base::WeakPtr
<BatchUploadRequest
> GetWeakPtrAsBatchUploadRequest();
1176 // Set boundary. Only tests can use this method.
1177 void SetBoundaryForTesting(const std::string
& boundary
);
1179 // Obtains reference to RequestSender that owns the request.
1180 RequestSender
* sender() const { return sender_
; }
1182 // Obtains URLGenerator.
1183 const DriveApiUrlGenerator
& url_generator() const { return url_generator_
; }
1185 // UrlFetchRequestBase overrides.
1186 void Prepare(const PrepareCallback
& callback
) override
;
1187 void Cancel() override
;
1188 GURL
GetURL() const override
;
1189 net::URLFetcher::RequestType
GetRequestType() const override
;
1190 std::vector
<std::string
> GetExtraRequestHeaders() const override
;
1191 bool GetContentData(std::string
* upload_content_type
,
1192 std::string
* upload_content
) override
;
1193 void ProcessURLFetchResults(const net::URLFetcher
* source
) override
;
1194 void RunCallbackOnPrematureFailure(DriveApiErrorCode code
) override
;
1196 // content::UrlFetcherDelegate overrides.
1197 void OnURLFetchUploadProgress(const net::URLFetcher
* source
,
1199 int64 total
) override
;
1202 typedef void* RequestID
;
1203 // Obtains corresponding child entry of |request_id|. Returns NULL if the
1204 // entry is not found.
1205 ScopedVector
<BatchUploadChildEntry
>::iterator
GetChildEntry(
1206 RequestID request_id
);
1208 // Called after child requests' |Prepare| method.
1209 void OnChildRequestPrepared(RequestID request_id
, DriveApiErrorCode result
);
1211 // Complete |Prepare| if possible.
1212 void MayCompletePrepare();
1214 // Process result for each child.
1215 void ProcessURLFetchResultsForChild(RequestID id
, const std::string
& body
);
1217 RequestSender
* const sender_
;
1218 const DriveApiUrlGenerator url_generator_
;
1219 ScopedVector
<BatchUploadChildEntry
> child_requests_
;
1221 PrepareCallback prepare_callback_
;
1224 // Boundary of multipart body.
1225 std::string boundary_
;
1227 // Multipart of child requests.
1228 ContentTypeAndData upload_content_
;
1230 // Last reported progress value.
1231 int64 last_progress_value_
;
1233 // Note: This should remain the last member so it'll be destroyed and
1234 // invalidate its weak pointers before any other members are destroyed.
1235 base::WeakPtrFactory
<BatchUploadRequest
> weak_ptr_factory_
;
1237 DISALLOW_COPY_AND_ASSIGN(BatchUploadRequest
);
1240 } // namespace drive
1241 } // namespace google_apis
1243 #endif // GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_