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 #include "google_apis/drive/drive_api_requests.h"
8 #include "base/callback.h"
9 #include "base/json/json_writer.h"
10 #include "base/location.h"
11 #include "base/sequenced_task_runner.h"
12 #include "base/task_runner_util.h"
13 #include "base/values.h"
14 #include "google_apis/drive/request_sender.h"
15 #include "google_apis/drive/request_util.h"
16 #include "google_apis/drive/time_util.h"
17 #include "net/base/url_util.h"
19 namespace google_apis
{
23 // Parses the JSON value to FileResource instance and runs |callback| on the
24 // UI thread once parsing is done.
25 // This is customized version of ParseJsonAndRun defined above to adapt the
26 // remaining response type.
27 void ParseFileResourceWithUploadRangeAndRun(const UploadRangeCallback
& callback
,
28 const UploadRangeResponse
& response
,
29 scoped_ptr
<base::Value
> value
) {
30 DCHECK(!callback
.is_null());
32 scoped_ptr
<FileResource
> file_resource
;
34 file_resource
= FileResource::CreateFrom(*value
);
37 UploadRangeResponse(DRIVE_PARSE_ERROR
,
38 response
.start_position_received
,
39 response
.end_position_received
),
40 scoped_ptr
<FileResource
>());
45 callback
.Run(response
, file_resource
.Pass());
48 // Attaches |properties| to the |request_body| if |properties| is not empty.
49 // |request_body| must not be NULL.
50 void AttachProperties(const Properties
& properties
,
51 base::DictionaryValue
* request_body
) {
53 if (properties
.empty())
56 base::ListValue
* const properties_value
= new base::ListValue
;
57 for (const auto& property
: properties
) {
58 base::DictionaryValue
* const property_value
= new base::DictionaryValue
;
59 std::string visibility_as_string
;
60 switch (property
.visibility()) {
61 case Property::VISIBILITY_PRIVATE
:
62 visibility_as_string
= "PRIVATE";
64 case Property::VISIBILITY_PUBLIC
:
65 visibility_as_string
= "PUBLIC";
68 property_value
->SetString("visibility", visibility_as_string
);
69 property_value
->SetString("key", property
.key());
70 property_value
->SetString("value", property
.value());
71 properties_value
->Append(property_value
);
73 request_body
->Set("properties", properties_value
);
76 // Creates metadata JSON string for multipart uploading.
77 // All the values are optional. If the value is empty or null, the value does
78 // not appear in the metadata.
79 std::string
CreateMultipartUploadMetadataJson(
80 const std::string
& title
,
81 const std::string
& parent_resource_id
,
82 const base::Time
& modified_date
,
83 const base::Time
& last_viewed_by_me_date
,
84 const Properties
& properties
) {
85 base::DictionaryValue root
;
87 root
.SetString("title", title
);
90 if (!parent_resource_id
.empty()) {
91 scoped_ptr
<base::ListValue
> parents(new base::ListValue
);
93 google_apis::util::CreateParentValue(parent_resource_id
).release());
94 root
.Set("parents", parents
.release());
97 if (!modified_date
.is_null()) {
98 root
.SetString("modifiedDate",
99 google_apis::util::FormatTimeAsString(modified_date
));
102 if (!last_viewed_by_me_date
.is_null()) {
103 root
.SetString("lastViewedByMeDate", google_apis::util::FormatTimeAsString(
104 last_viewed_by_me_date
));
107 AttachProperties(properties
, &root
);
108 std::string json_string
;
109 base::JSONWriter::Write(&root
, &json_string
);
115 Property::Property() : visibility_(VISIBILITY_PRIVATE
) {
118 Property::~Property() {
121 //============================ DriveApiPartialFieldRequest ====================
123 DriveApiPartialFieldRequest::DriveApiPartialFieldRequest(
124 RequestSender
* sender
) : UrlFetchRequestBase(sender
) {
127 DriveApiPartialFieldRequest::~DriveApiPartialFieldRequest() {
130 GURL
DriveApiPartialFieldRequest::GetURL() const {
131 GURL url
= GetURLInternal();
132 if (!fields_
.empty())
133 url
= net::AppendOrReplaceQueryParameter(url
, "fields", fields_
);
137 //=============================== FilesGetRequest =============================
139 FilesGetRequest::FilesGetRequest(
140 RequestSender
* sender
,
141 const DriveApiUrlGenerator
& url_generator
,
142 bool use_internal_endpoint
,
143 const FileResourceCallback
& callback
)
144 : DriveApiDataRequest
<FileResource
>(sender
, callback
),
145 url_generator_(url_generator
),
146 use_internal_endpoint_(use_internal_endpoint
) {
147 DCHECK(!callback
.is_null());
150 FilesGetRequest::~FilesGetRequest() {}
152 GURL
FilesGetRequest::GetURLInternal() const {
153 return url_generator_
.GetFilesGetUrl(file_id_
,
154 use_internal_endpoint_
,
158 //============================ FilesAuthorizeRequest ===========================
160 FilesAuthorizeRequest::FilesAuthorizeRequest(
161 RequestSender
* sender
,
162 const DriveApiUrlGenerator
& url_generator
,
163 const FileResourceCallback
& callback
)
164 : DriveApiDataRequest
<FileResource
>(sender
, callback
),
165 url_generator_(url_generator
) {
166 DCHECK(!callback
.is_null());
169 FilesAuthorizeRequest::~FilesAuthorizeRequest() {}
171 net::URLFetcher::RequestType
FilesAuthorizeRequest::GetRequestType() const {
172 return net::URLFetcher::POST
;
175 GURL
FilesAuthorizeRequest::GetURLInternal() const {
176 return url_generator_
.GetFilesAuthorizeUrl(file_id_
, app_id_
);
179 //============================ FilesInsertRequest ============================
181 FilesInsertRequest::FilesInsertRequest(
182 RequestSender
* sender
,
183 const DriveApiUrlGenerator
& url_generator
,
184 const FileResourceCallback
& callback
)
185 : DriveApiDataRequest
<FileResource
>(sender
, callback
),
186 url_generator_(url_generator
) {
187 DCHECK(!callback
.is_null());
190 FilesInsertRequest::~FilesInsertRequest() {}
192 net::URLFetcher::RequestType
FilesInsertRequest::GetRequestType() const {
193 return net::URLFetcher::POST
;
196 bool FilesInsertRequest::GetContentData(std::string
* upload_content_type
,
197 std::string
* upload_content
) {
198 *upload_content_type
= util::kContentTypeApplicationJson
;
200 base::DictionaryValue root
;
202 if (!last_viewed_by_me_date_
.is_null()) {
203 root
.SetString("lastViewedByMeDate",
204 util::FormatTimeAsString(last_viewed_by_me_date_
));
207 if (!mime_type_
.empty())
208 root
.SetString("mimeType", mime_type_
);
210 if (!modified_date_
.is_null())
211 root
.SetString("modifiedDate", util::FormatTimeAsString(modified_date_
));
213 if (!parents_
.empty()) {
214 base::ListValue
* parents_value
= new base::ListValue
;
215 for (size_t i
= 0; i
< parents_
.size(); ++i
) {
216 base::DictionaryValue
* parent
= new base::DictionaryValue
;
217 parent
->SetString("id", parents_
[i
]);
218 parents_value
->Append(parent
);
220 root
.Set("parents", parents_value
);
224 root
.SetString("title", title_
);
226 AttachProperties(properties_
, &root
);
227 base::JSONWriter::Write(&root
, upload_content
);
229 DVLOG(1) << "FilesInsert data: " << *upload_content_type
<< ", ["
230 << *upload_content
<< "]";
234 GURL
FilesInsertRequest::GetURLInternal() const {
235 return url_generator_
.GetFilesInsertUrl();
238 //============================== FilesPatchRequest ============================
240 FilesPatchRequest::FilesPatchRequest(
241 RequestSender
* sender
,
242 const DriveApiUrlGenerator
& url_generator
,
243 const FileResourceCallback
& callback
)
244 : DriveApiDataRequest
<FileResource
>(sender
, callback
),
245 url_generator_(url_generator
),
246 set_modified_date_(false),
247 update_viewed_date_(true) {
248 DCHECK(!callback
.is_null());
251 FilesPatchRequest::~FilesPatchRequest() {}
253 net::URLFetcher::RequestType
FilesPatchRequest::GetRequestType() const {
254 return net::URLFetcher::PATCH
;
257 std::vector
<std::string
> FilesPatchRequest::GetExtraRequestHeaders() const {
258 std::vector
<std::string
> headers
;
259 headers
.push_back(util::kIfMatchAllHeader
);
263 GURL
FilesPatchRequest::GetURLInternal() const {
264 return url_generator_
.GetFilesPatchUrl(
265 file_id_
, set_modified_date_
, update_viewed_date_
);
268 bool FilesPatchRequest::GetContentData(std::string
* upload_content_type
,
269 std::string
* upload_content
) {
270 if (title_
.empty() &&
271 modified_date_
.is_null() &&
272 last_viewed_by_me_date_
.is_null() &&
276 *upload_content_type
= util::kContentTypeApplicationJson
;
278 base::DictionaryValue root
;
280 root
.SetString("title", title_
);
282 if (!modified_date_
.is_null())
283 root
.SetString("modifiedDate", util::FormatTimeAsString(modified_date_
));
285 if (!last_viewed_by_me_date_
.is_null()) {
286 root
.SetString("lastViewedByMeDate",
287 util::FormatTimeAsString(last_viewed_by_me_date_
));
290 if (!parents_
.empty()) {
291 base::ListValue
* parents_value
= new base::ListValue
;
292 for (size_t i
= 0; i
< parents_
.size(); ++i
) {
293 base::DictionaryValue
* parent
= new base::DictionaryValue
;
294 parent
->SetString("id", parents_
[i
]);
295 parents_value
->Append(parent
);
297 root
.Set("parents", parents_value
);
300 AttachProperties(properties_
, &root
);
301 base::JSONWriter::Write(&root
, upload_content
);
303 DVLOG(1) << "FilesPatch data: " << *upload_content_type
<< ", ["
304 << *upload_content
<< "]";
308 //============================= FilesCopyRequest ==============================
310 FilesCopyRequest::FilesCopyRequest(
311 RequestSender
* sender
,
312 const DriveApiUrlGenerator
& url_generator
,
313 const FileResourceCallback
& callback
)
314 : DriveApiDataRequest
<FileResource
>(sender
, callback
),
315 url_generator_(url_generator
) {
316 DCHECK(!callback
.is_null());
319 FilesCopyRequest::~FilesCopyRequest() {
322 net::URLFetcher::RequestType
FilesCopyRequest::GetRequestType() const {
323 return net::URLFetcher::POST
;
326 GURL
FilesCopyRequest::GetURLInternal() const {
327 return url_generator_
.GetFilesCopyUrl(file_id_
);
330 bool FilesCopyRequest::GetContentData(std::string
* upload_content_type
,
331 std::string
* upload_content
) {
332 if (parents_
.empty() && title_
.empty())
335 *upload_content_type
= util::kContentTypeApplicationJson
;
337 base::DictionaryValue root
;
339 if (!modified_date_
.is_null())
340 root
.SetString("modifiedDate", util::FormatTimeAsString(modified_date_
));
342 if (!parents_
.empty()) {
343 base::ListValue
* parents_value
= new base::ListValue
;
344 for (size_t i
= 0; i
< parents_
.size(); ++i
) {
345 base::DictionaryValue
* parent
= new base::DictionaryValue
;
346 parent
->SetString("id", parents_
[i
]);
347 parents_value
->Append(parent
);
349 root
.Set("parents", parents_value
);
353 root
.SetString("title", title_
);
355 base::JSONWriter::Write(&root
, upload_content
);
356 DVLOG(1) << "FilesCopy data: " << *upload_content_type
<< ", ["
357 << *upload_content
<< "]";
361 //============================= FilesListRequest =============================
363 FilesListRequest::FilesListRequest(
364 RequestSender
* sender
,
365 const DriveApiUrlGenerator
& url_generator
,
366 const FileListCallback
& callback
)
367 : DriveApiDataRequest
<FileList
>(sender
, callback
),
368 url_generator_(url_generator
),
370 DCHECK(!callback
.is_null());
373 FilesListRequest::~FilesListRequest() {}
375 GURL
FilesListRequest::GetURLInternal() const {
376 return url_generator_
.GetFilesListUrl(max_results_
, page_token_
, q_
);
379 //======================== FilesListNextPageRequest =========================
381 FilesListNextPageRequest::FilesListNextPageRequest(
382 RequestSender
* sender
,
383 const FileListCallback
& callback
)
384 : DriveApiDataRequest
<FileList
>(sender
, callback
) {
385 DCHECK(!callback
.is_null());
388 FilesListNextPageRequest::~FilesListNextPageRequest() {
391 GURL
FilesListNextPageRequest::GetURLInternal() const {
395 //============================ FilesDeleteRequest =============================
397 FilesDeleteRequest::FilesDeleteRequest(
398 RequestSender
* sender
,
399 const DriveApiUrlGenerator
& url_generator
,
400 const EntryActionCallback
& callback
)
401 : EntryActionRequest(sender
, callback
),
402 url_generator_(url_generator
) {
403 DCHECK(!callback
.is_null());
406 FilesDeleteRequest::~FilesDeleteRequest() {}
408 net::URLFetcher::RequestType
FilesDeleteRequest::GetRequestType() const {
409 return net::URLFetcher::DELETE_REQUEST
;
412 GURL
FilesDeleteRequest::GetURL() const {
413 return url_generator_
.GetFilesDeleteUrl(file_id_
);
416 std::vector
<std::string
> FilesDeleteRequest::GetExtraRequestHeaders() const {
417 std::vector
<std::string
> headers(
418 EntryActionRequest::GetExtraRequestHeaders());
419 headers
.push_back(util::GenerateIfMatchHeader(etag_
));
423 //============================ FilesTrashRequest =============================
425 FilesTrashRequest::FilesTrashRequest(
426 RequestSender
* sender
,
427 const DriveApiUrlGenerator
& url_generator
,
428 const FileResourceCallback
& callback
)
429 : DriveApiDataRequest
<FileResource
>(sender
, callback
),
430 url_generator_(url_generator
) {
431 DCHECK(!callback
.is_null());
434 FilesTrashRequest::~FilesTrashRequest() {}
436 net::URLFetcher::RequestType
FilesTrashRequest::GetRequestType() const {
437 return net::URLFetcher::POST
;
440 GURL
FilesTrashRequest::GetURLInternal() const {
441 return url_generator_
.GetFilesTrashUrl(file_id_
);
444 //============================== AboutGetRequest =============================
446 AboutGetRequest::AboutGetRequest(
447 RequestSender
* sender
,
448 const DriveApiUrlGenerator
& url_generator
,
449 const AboutResourceCallback
& callback
)
450 : DriveApiDataRequest
<AboutResource
>(sender
, callback
),
451 url_generator_(url_generator
) {
452 DCHECK(!callback
.is_null());
455 AboutGetRequest::~AboutGetRequest() {}
457 GURL
AboutGetRequest::GetURLInternal() const {
458 return url_generator_
.GetAboutGetUrl();
461 //============================ ChangesListRequest ===========================
463 ChangesListRequest::ChangesListRequest(
464 RequestSender
* sender
,
465 const DriveApiUrlGenerator
& url_generator
,
466 const ChangeListCallback
& callback
)
467 : DriveApiDataRequest
<ChangeList
>(sender
, callback
),
468 url_generator_(url_generator
),
469 include_deleted_(true),
471 start_change_id_(0) {
472 DCHECK(!callback
.is_null());
475 ChangesListRequest::~ChangesListRequest() {}
477 GURL
ChangesListRequest::GetURLInternal() const {
478 return url_generator_
.GetChangesListUrl(
479 include_deleted_
, max_results_
, page_token_
, start_change_id_
);
482 //======================== ChangesListNextPageRequest =========================
484 ChangesListNextPageRequest::ChangesListNextPageRequest(
485 RequestSender
* sender
,
486 const ChangeListCallback
& callback
)
487 : DriveApiDataRequest
<ChangeList
>(sender
, callback
) {
488 DCHECK(!callback
.is_null());
491 ChangesListNextPageRequest::~ChangesListNextPageRequest() {
494 GURL
ChangesListNextPageRequest::GetURLInternal() const {
498 //============================== AppsListRequest ===========================
500 AppsListRequest::AppsListRequest(
501 RequestSender
* sender
,
502 const DriveApiUrlGenerator
& url_generator
,
503 bool use_internal_endpoint
,
504 const AppListCallback
& callback
)
505 : DriveApiDataRequest
<AppList
>(sender
, callback
),
506 url_generator_(url_generator
),
507 use_internal_endpoint_(use_internal_endpoint
) {
508 DCHECK(!callback
.is_null());
511 AppsListRequest::~AppsListRequest() {}
513 GURL
AppsListRequest::GetURLInternal() const {
514 return url_generator_
.GetAppsListUrl(use_internal_endpoint_
);
517 //============================== AppsDeleteRequest ===========================
519 AppsDeleteRequest::AppsDeleteRequest(RequestSender
* sender
,
520 const DriveApiUrlGenerator
& url_generator
,
521 const EntryActionCallback
& callback
)
522 : EntryActionRequest(sender
, callback
),
523 url_generator_(url_generator
) {
524 DCHECK(!callback
.is_null());
527 AppsDeleteRequest::~AppsDeleteRequest() {}
529 net::URLFetcher::RequestType
AppsDeleteRequest::GetRequestType() const {
530 return net::URLFetcher::DELETE_REQUEST
;
533 GURL
AppsDeleteRequest::GetURL() const {
534 return url_generator_
.GetAppsDeleteUrl(app_id_
);
537 //========================== ChildrenInsertRequest ============================
539 ChildrenInsertRequest::ChildrenInsertRequest(
540 RequestSender
* sender
,
541 const DriveApiUrlGenerator
& url_generator
,
542 const EntryActionCallback
& callback
)
543 : EntryActionRequest(sender
, callback
),
544 url_generator_(url_generator
) {
545 DCHECK(!callback
.is_null());
548 ChildrenInsertRequest::~ChildrenInsertRequest() {}
550 net::URLFetcher::RequestType
ChildrenInsertRequest::GetRequestType() const {
551 return net::URLFetcher::POST
;
554 GURL
ChildrenInsertRequest::GetURL() const {
555 return url_generator_
.GetChildrenInsertUrl(folder_id_
);
558 bool ChildrenInsertRequest::GetContentData(std::string
* upload_content_type
,
559 std::string
* upload_content
) {
560 *upload_content_type
= util::kContentTypeApplicationJson
;
562 base::DictionaryValue root
;
563 root
.SetString("id", id_
);
565 base::JSONWriter::Write(&root
, upload_content
);
566 DVLOG(1) << "InsertResource data: " << *upload_content_type
<< ", ["
567 << *upload_content
<< "]";
571 //========================== ChildrenDeleteRequest ============================
573 ChildrenDeleteRequest::ChildrenDeleteRequest(
574 RequestSender
* sender
,
575 const DriveApiUrlGenerator
& url_generator
,
576 const EntryActionCallback
& callback
)
577 : EntryActionRequest(sender
, callback
),
578 url_generator_(url_generator
) {
579 DCHECK(!callback
.is_null());
582 ChildrenDeleteRequest::~ChildrenDeleteRequest() {}
584 net::URLFetcher::RequestType
ChildrenDeleteRequest::GetRequestType() const {
585 return net::URLFetcher::DELETE_REQUEST
;
588 GURL
ChildrenDeleteRequest::GetURL() const {
589 return url_generator_
.GetChildrenDeleteUrl(child_id_
, folder_id_
);
592 //======================= InitiateUploadNewFileRequest =======================
594 InitiateUploadNewFileRequest::InitiateUploadNewFileRequest(
595 RequestSender
* sender
,
596 const DriveApiUrlGenerator
& url_generator
,
597 const std::string
& content_type
,
598 int64 content_length
,
599 const std::string
& parent_resource_id
,
600 const std::string
& title
,
601 const InitiateUploadCallback
& callback
)
602 : InitiateUploadRequestBase(sender
,
606 url_generator_(url_generator
),
607 parent_resource_id_(parent_resource_id
),
611 InitiateUploadNewFileRequest::~InitiateUploadNewFileRequest() {}
613 GURL
InitiateUploadNewFileRequest::GetURL() const {
614 return url_generator_
.GetInitiateUploadNewFileUrl(!modified_date_
.is_null());
617 net::URLFetcher::RequestType
618 InitiateUploadNewFileRequest::GetRequestType() const {
619 return net::URLFetcher::POST
;
622 bool InitiateUploadNewFileRequest::GetContentData(
623 std::string
* upload_content_type
,
624 std::string
* upload_content
) {
625 *upload_content_type
= util::kContentTypeApplicationJson
;
627 base::DictionaryValue root
;
628 root
.SetString("title", title_
);
631 scoped_ptr
<base::ListValue
> parents(new base::ListValue
);
632 parents
->Append(util::CreateParentValue(parent_resource_id_
).release());
633 root
.Set("parents", parents
.release());
635 if (!modified_date_
.is_null())
636 root
.SetString("modifiedDate", util::FormatTimeAsString(modified_date_
));
638 if (!last_viewed_by_me_date_
.is_null()) {
639 root
.SetString("lastViewedByMeDate",
640 util::FormatTimeAsString(last_viewed_by_me_date_
));
643 AttachProperties(properties_
, &root
);
644 base::JSONWriter::Write(&root
, upload_content
);
646 DVLOG(1) << "InitiateUploadNewFile data: " << *upload_content_type
<< ", ["
647 << *upload_content
<< "]";
651 //===================== InitiateUploadExistingFileRequest ====================
653 InitiateUploadExistingFileRequest::InitiateUploadExistingFileRequest(
654 RequestSender
* sender
,
655 const DriveApiUrlGenerator
& url_generator
,
656 const std::string
& content_type
,
657 int64 content_length
,
658 const std::string
& resource_id
,
659 const std::string
& etag
,
660 const InitiateUploadCallback
& callback
)
661 : InitiateUploadRequestBase(sender
,
665 url_generator_(url_generator
),
666 resource_id_(resource_id
),
670 InitiateUploadExistingFileRequest::~InitiateUploadExistingFileRequest() {}
672 GURL
InitiateUploadExistingFileRequest::GetURL() const {
673 return url_generator_
.GetInitiateUploadExistingFileUrl(
674 resource_id_
, !modified_date_
.is_null());
677 net::URLFetcher::RequestType
678 InitiateUploadExistingFileRequest::GetRequestType() const {
679 return net::URLFetcher::PUT
;
682 std::vector
<std::string
>
683 InitiateUploadExistingFileRequest::GetExtraRequestHeaders() const {
684 std::vector
<std::string
> headers(
685 InitiateUploadRequestBase::GetExtraRequestHeaders());
686 headers
.push_back(util::GenerateIfMatchHeader(etag_
));
690 bool InitiateUploadExistingFileRequest::GetContentData(
691 std::string
* upload_content_type
,
692 std::string
* upload_content
) {
693 base::DictionaryValue root
;
694 if (!parent_resource_id_
.empty()) {
695 scoped_ptr
<base::ListValue
> parents(new base::ListValue
);
696 parents
->Append(util::CreateParentValue(parent_resource_id_
).release());
697 root
.Set("parents", parents
.release());
701 root
.SetString("title", title_
);
703 if (!modified_date_
.is_null())
704 root
.SetString("modifiedDate", util::FormatTimeAsString(modified_date_
));
706 if (!last_viewed_by_me_date_
.is_null()) {
707 root
.SetString("lastViewedByMeDate",
708 util::FormatTimeAsString(last_viewed_by_me_date_
));
711 AttachProperties(properties_
, &root
);
715 *upload_content_type
= util::kContentTypeApplicationJson
;
716 base::JSONWriter::Write(&root
, upload_content
);
717 DVLOG(1) << "InitiateUploadExistingFile data: " << *upload_content_type
718 << ", [" << *upload_content
<< "]";
722 //============================ ResumeUploadRequest ===========================
724 ResumeUploadRequest::ResumeUploadRequest(
725 RequestSender
* sender
,
726 const GURL
& upload_location
,
727 int64 start_position
,
729 int64 content_length
,
730 const std::string
& content_type
,
731 const base::FilePath
& local_file_path
,
732 const UploadRangeCallback
& callback
,
733 const ProgressCallback
& progress_callback
)
734 : ResumeUploadRequestBase(sender
,
742 progress_callback_(progress_callback
) {
743 DCHECK(!callback_
.is_null());
746 ResumeUploadRequest::~ResumeUploadRequest() {}
748 void ResumeUploadRequest::OnRangeRequestComplete(
749 const UploadRangeResponse
& response
,
750 scoped_ptr
<base::Value
> value
) {
751 DCHECK(CalledOnValidThread());
752 ParseFileResourceWithUploadRangeAndRun(callback_
, response
, value
.Pass());
755 void ResumeUploadRequest::OnURLFetchUploadProgress(
756 const net::URLFetcher
* source
, int64 current
, int64 total
) {
757 if (!progress_callback_
.is_null())
758 progress_callback_
.Run(current
, total
);
761 //========================== GetUploadStatusRequest ==========================
763 GetUploadStatusRequest::GetUploadStatusRequest(
764 RequestSender
* sender
,
765 const GURL
& upload_url
,
766 int64 content_length
,
767 const UploadRangeCallback
& callback
)
768 : GetUploadStatusRequestBase(sender
,
771 callback_(callback
) {
772 DCHECK(!callback
.is_null());
775 GetUploadStatusRequest::~GetUploadStatusRequest() {}
777 void GetUploadStatusRequest::OnRangeRequestComplete(
778 const UploadRangeResponse
& response
,
779 scoped_ptr
<base::Value
> value
) {
780 DCHECK(CalledOnValidThread());
781 ParseFileResourceWithUploadRangeAndRun(callback_
, response
, value
.Pass());
784 //======================= MultipartUploadNewFileRequest =======================
786 MultipartUploadNewFileRequest::MultipartUploadNewFileRequest(
787 RequestSender
* sender
,
788 const std::string
& title
,
789 const std::string
& parent_resource_id
,
790 const std::string
& content_type
,
791 int64 content_length
,
792 const base::Time
& modified_date
,
793 const base::Time
& last_viewed_by_me_date
,
794 const base::FilePath
& local_file_path
,
795 const Properties
& properties
,
796 const DriveApiUrlGenerator
& url_generator
,
797 const FileResourceCallback
& callback
,
798 const ProgressCallback
& progress_callback
)
799 : MultipartUploadRequestBase(
801 CreateMultipartUploadMetadataJson(title
,
804 last_viewed_by_me_date
,
811 has_modified_date_(!modified_date
.is_null()),
812 url_generator_(url_generator
) {
815 MultipartUploadNewFileRequest::~MultipartUploadNewFileRequest() {
818 GURL
MultipartUploadNewFileRequest::GetURL() const {
819 return url_generator_
.GetMultipartUploadNewFileUrl(has_modified_date_
);
822 net::URLFetcher::RequestType
MultipartUploadNewFileRequest::GetRequestType()
824 return net::URLFetcher::POST
;
827 //======================= MultipartUploadExistingFileRequest ===================
829 MultipartUploadExistingFileRequest::MultipartUploadExistingFileRequest(
830 RequestSender
* sender
,
831 const std::string
& title
,
832 const std::string
& resource_id
,
833 const std::string
& parent_resource_id
,
834 const std::string
& content_type
,
835 int64 content_length
,
836 const base::Time
& modified_date
,
837 const base::Time
& last_viewed_by_me_date
,
838 const base::FilePath
& local_file_path
,
839 const std::string
& etag
,
840 const Properties
& properties
,
841 const DriveApiUrlGenerator
& url_generator
,
842 const FileResourceCallback
& callback
,
843 const ProgressCallback
& progress_callback
)
844 : MultipartUploadRequestBase(
846 CreateMultipartUploadMetadataJson(title
,
849 last_viewed_by_me_date
,
856 resource_id_(resource_id
),
858 has_modified_date_(!modified_date
.is_null()),
859 url_generator_(url_generator
) {
862 MultipartUploadExistingFileRequest::~MultipartUploadExistingFileRequest() {
865 std::vector
<std::string
>
866 MultipartUploadExistingFileRequest::GetExtraRequestHeaders() const {
867 std::vector
<std::string
> headers(
868 MultipartUploadRequestBase::GetExtraRequestHeaders());
869 headers
.push_back(util::GenerateIfMatchHeader(etag_
));
873 GURL
MultipartUploadExistingFileRequest::GetURL() const {
874 return url_generator_
.GetMultipartUploadExistingFileUrl(resource_id_
,
878 net::URLFetcher::RequestType
879 MultipartUploadExistingFileRequest::GetRequestType() const {
880 return net::URLFetcher::PUT
;
883 //========================== DownloadFileRequest ==========================
885 DownloadFileRequest::DownloadFileRequest(
886 RequestSender
* sender
,
887 const DriveApiUrlGenerator
& url_generator
,
888 const std::string
& resource_id
,
889 const base::FilePath
& output_file_path
,
890 const DownloadActionCallback
& download_action_callback
,
891 const GetContentCallback
& get_content_callback
,
892 const ProgressCallback
& progress_callback
)
893 : DownloadFileRequestBase(
895 download_action_callback
,
896 get_content_callback
,
898 url_generator
.GenerateDownloadFileUrl(resource_id
),
902 DownloadFileRequest::~DownloadFileRequest() {
905 //========================== PermissionsInsertRequest ==========================
907 PermissionsInsertRequest::PermissionsInsertRequest(
908 RequestSender
* sender
,
909 const DriveApiUrlGenerator
& url_generator
,
910 const EntryActionCallback
& callback
)
911 : EntryActionRequest(sender
, callback
),
912 url_generator_(url_generator
),
913 type_(PERMISSION_TYPE_USER
),
914 role_(PERMISSION_ROLE_READER
) {
917 PermissionsInsertRequest::~PermissionsInsertRequest() {
920 GURL
PermissionsInsertRequest::GetURL() const {
921 return url_generator_
.GetPermissionsInsertUrl(id_
);
924 net::URLFetcher::RequestType
925 PermissionsInsertRequest::GetRequestType() const {
926 return net::URLFetcher::POST
;
929 bool PermissionsInsertRequest::GetContentData(std::string
* upload_content_type
,
930 std::string
* upload_content
) {
931 *upload_content_type
= util::kContentTypeApplicationJson
;
933 base::DictionaryValue root
;
935 case PERMISSION_TYPE_ANYONE
:
936 root
.SetString("type", "anyone");
938 case PERMISSION_TYPE_DOMAIN
:
939 root
.SetString("type", "domain");
941 case PERMISSION_TYPE_GROUP
:
942 root
.SetString("type", "group");
944 case PERMISSION_TYPE_USER
:
945 root
.SetString("type", "user");
949 case PERMISSION_ROLE_OWNER
:
950 root
.SetString("role", "owner");
952 case PERMISSION_ROLE_READER
:
953 root
.SetString("role", "reader");
955 case PERMISSION_ROLE_WRITER
:
956 root
.SetString("role", "writer");
958 case PERMISSION_ROLE_COMMENTER
:
959 root
.SetString("role", "reader");
961 base::ListValue
* list
= new base::ListValue
;
962 list
->AppendString("commenter");
963 root
.Set("additionalRoles", list
);
967 root
.SetString("value", value_
);
968 base::JSONWriter::Write(&root
, upload_content
);
972 //========================== BatchUploadRequest ==========================
974 BatchUploadRequest::BatchUploadRequest(
975 RequestSender
* sender
,
976 const DriveApiUrlGenerator
& url_generator
)
977 : UrlFetchRequestBase(sender
),
979 url_generator_(url_generator
),
980 weak_ptr_factory_(this) {
983 BatchUploadRequest::~BatchUploadRequest() {
984 for (const auto& child
: child_requests_
) {
985 // Request will be deleted in RequestFinished method.
986 sender_
->RequestFinished(child
.request
);
990 void BatchUploadRequest::AddRequest(UrlFetchRequestBase
* request
) {
992 child_requests_
.push_back(BatchUploadChildEntry(request
));
995 void BatchUploadRequest::Commit() {
999 base::WeakPtr
<BatchUploadRequest
>
1000 BatchUploadRequest::GetWeakPtrAsBatchUploadRequest() {
1001 return weak_ptr_factory_
.GetWeakPtr();
1004 GURL
BatchUploadRequest::GetURL() const {
1009 void BatchUploadRequest::ProcessURLFetchResults(const net::URLFetcher
* source
) {
1013 void BatchUploadRequest::RunCallbackOnPrematureFailure(DriveApiErrorCode code
) {
1017 } // namespace drive
1018 } // namespace google_apis