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/drive_api_parser.h"
15 #include "google_apis/drive/request_sender.h"
16 #include "google_apis/drive/request_util.h"
17 #include "google_apis/drive/time_util.h"
18 #include "net/base/url_util.h"
20 namespace google_apis
{
23 const char kContentTypeApplicationJson
[] = "application/json";
24 const char kParentLinkKind
[] = "drive#fileLink";
26 // Parses the JSON value to a resource typed |T| and runs |callback| on the UI
27 // thread once parsing is done.
30 const base::Callback
<void(GDataErrorCode
, scoped_ptr
<T
>)>& callback
,
32 scoped_ptr
<base::Value
> value
) {
33 DCHECK(!callback
.is_null());
35 scoped_ptr
<T
> resource
;
37 resource
= T::CreateFrom(*value
);
39 // Failed to parse the JSON value, although the JSON value is available,
40 // so let the callback know the parsing error.
41 error
= GDATA_PARSE_ERROR
;
45 callback
.Run(error
, resource
.Pass());
48 // Thin adapter of T::CreateFrom.
50 scoped_ptr
<T
> ParseJsonOnBlockingPool(scoped_ptr
<base::Value
> value
) {
51 return T::CreateFrom(*value
);
54 // Runs |callback| with given |error| and |value|. If |value| is null,
55 // overwrites |error| to GDATA_PARSE_ERROR.
57 void ParseJsonOnBlockingPoolAndRunAfterBlockingPoolTask(
58 const base::Callback
<void(GDataErrorCode
, scoped_ptr
<T
>)>& callback
,
59 GDataErrorCode error
, scoped_ptr
<T
> value
) {
61 error
= GDATA_PARSE_ERROR
;
62 callback
.Run(error
, value
.Pass());
65 // Parses the JSON value to a resource typed |T| and runs |callback| on
66 // blocking pool, and then run on the current thread.
67 // TODO(hidehiko): Move this and ParseJsonAndRun defined above into base with
68 // merging the tasks running on blocking pool into one.
70 void ParseJsonOnBlockingPoolAndRun(
71 scoped_refptr
<base::TaskRunner
> blocking_task_runner
,
72 const base::Callback
<void(GDataErrorCode
, scoped_ptr
<T
>)>& callback
,
74 scoped_ptr
<base::Value
> value
) {
75 DCHECK(!callback
.is_null());
78 callback
.Run(error
, scoped_ptr
<T
>());
82 base::PostTaskAndReplyWithResult(
85 base::Bind(&ParseJsonOnBlockingPool
<T
>, base::Passed(&value
)),
86 base::Bind(&ParseJsonOnBlockingPoolAndRunAfterBlockingPoolTask
<T
>,
90 // Parses the JSON value to FileResource instance and runs |callback| on the
91 // UI thread once parsing is done.
92 // This is customized version of ParseJsonAndRun defined above to adapt the
93 // remaining response type.
94 void ParseFileResourceWithUploadRangeAndRun(
95 const drive::UploadRangeCallback
& callback
,
96 const UploadRangeResponse
& response
,
97 scoped_ptr
<base::Value
> value
) {
98 DCHECK(!callback
.is_null());
100 scoped_ptr
<FileResource
> file_resource
;
102 file_resource
= FileResource::CreateFrom(*value
);
103 if (!file_resource
) {
105 UploadRangeResponse(GDATA_PARSE_ERROR
,
106 response
.start_position_received
,
107 response
.end_position_received
),
108 scoped_ptr
<FileResource
>());
113 callback
.Run(response
, file_resource
.Pass());
116 // Creates a Parents value which can be used as a part of request body.
117 scoped_ptr
<base::DictionaryValue
> CreateParentValue(
118 const std::string
& file_id
) {
119 scoped_ptr
<base::DictionaryValue
> parent(new base::DictionaryValue
);
120 parent
->SetString("kind", kParentLinkKind
);
121 parent
->SetString("id", file_id
);
122 return parent
.Pass();
129 //============================ DriveApiDataRequest ===========================
131 DriveApiDataRequest::DriveApiDataRequest(RequestSender
* sender
,
132 const GetDataCallback
& callback
)
133 : GetDataRequest(sender
, callback
) {
136 DriveApiDataRequest::~DriveApiDataRequest() {
139 GURL
DriveApiDataRequest::GetURL() const {
140 GURL url
= GetURLInternal();
141 if (!fields_
.empty())
142 url
= net::AppendOrReplaceQueryParameter(url
, "fields", fields_
);
146 //=============================== FilesGetRequest =============================
148 FilesGetRequest::FilesGetRequest(
149 RequestSender
* sender
,
150 const DriveApiUrlGenerator
& url_generator
,
151 const FileResourceCallback
& callback
)
152 : DriveApiDataRequest(
154 base::Bind(&ParseJsonAndRun
<FileResource
>, callback
)),
155 url_generator_(url_generator
) {
156 DCHECK(!callback
.is_null());
159 FilesGetRequest::~FilesGetRequest() {}
161 GURL
FilesGetRequest::GetURLInternal() const {
162 return url_generator_
.GetFilesGetUrl(file_id_
);
165 //============================ FilesAuthorizeRequest ===========================
167 FilesAuthorizeRequest::FilesAuthorizeRequest(
168 RequestSender
* sender
,
169 const DriveApiUrlGenerator
& url_generator
,
170 const FileResourceCallback
& callback
)
171 : DriveApiDataRequest(
173 base::Bind(&ParseJsonAndRun
<FileResource
>, callback
)),
174 url_generator_(url_generator
) {
175 DCHECK(!callback
.is_null());
178 FilesAuthorizeRequest::~FilesAuthorizeRequest() {}
180 net::URLFetcher::RequestType
FilesAuthorizeRequest::GetRequestType() const {
181 return net::URLFetcher::POST
;
184 GURL
FilesAuthorizeRequest::GetURLInternal() const {
185 return url_generator_
.GetFilesAuthorizeUrl(file_id_
, app_id_
);
188 //============================ FilesInsertRequest ============================
190 FilesInsertRequest::FilesInsertRequest(
191 RequestSender
* sender
,
192 const DriveApiUrlGenerator
& url_generator
,
193 const FileResourceCallback
& callback
)
194 : DriveApiDataRequest(
196 base::Bind(&ParseJsonAndRun
<FileResource
>, callback
)),
197 url_generator_(url_generator
) {
198 DCHECK(!callback
.is_null());
201 FilesInsertRequest::~FilesInsertRequest() {}
203 net::URLFetcher::RequestType
FilesInsertRequest::GetRequestType() const {
204 return net::URLFetcher::POST
;
207 bool FilesInsertRequest::GetContentData(std::string
* upload_content_type
,
208 std::string
* upload_content
) {
209 *upload_content_type
= kContentTypeApplicationJson
;
211 base::DictionaryValue root
;
213 if (!last_viewed_by_me_date_
.is_null()) {
214 root
.SetString("lastViewedByMeDate",
215 util::FormatTimeAsString(last_viewed_by_me_date_
));
218 if (!mime_type_
.empty())
219 root
.SetString("mimeType", mime_type_
);
221 if (!modified_date_
.is_null())
222 root
.SetString("modifiedDate", util::FormatTimeAsString(modified_date_
));
224 if (!parents_
.empty()) {
225 base::ListValue
* parents_value
= new base::ListValue
;
226 for (size_t i
= 0; i
< parents_
.size(); ++i
) {
227 base::DictionaryValue
* parent
= new base::DictionaryValue
;
228 parent
->SetString("id", parents_
[i
]);
229 parents_value
->Append(parent
);
231 root
.Set("parents", parents_value
);
235 root
.SetString("title", title_
);
237 base::JSONWriter::Write(&root
, upload_content
);
238 DVLOG(1) << "FilesInsert data: " << *upload_content_type
<< ", ["
239 << *upload_content
<< "]";
243 GURL
FilesInsertRequest::GetURLInternal() const {
244 return url_generator_
.GetFilesInsertUrl();
247 //============================== FilesPatchRequest ============================
249 FilesPatchRequest::FilesPatchRequest(
250 RequestSender
* sender
,
251 const DriveApiUrlGenerator
& url_generator
,
252 const FileResourceCallback
& callback
)
253 : DriveApiDataRequest(
255 base::Bind(&ParseJsonAndRun
<FileResource
>, callback
)),
256 url_generator_(url_generator
),
257 set_modified_date_(false),
258 update_viewed_date_(true) {
259 DCHECK(!callback
.is_null());
262 FilesPatchRequest::~FilesPatchRequest() {}
264 net::URLFetcher::RequestType
FilesPatchRequest::GetRequestType() const {
265 return net::URLFetcher::PATCH
;
268 std::vector
<std::string
> FilesPatchRequest::GetExtraRequestHeaders() const {
269 std::vector
<std::string
> headers
;
270 headers
.push_back(util::kIfMatchAllHeader
);
274 GURL
FilesPatchRequest::GetURLInternal() const {
275 return url_generator_
.GetFilesPatchUrl(
276 file_id_
, set_modified_date_
, update_viewed_date_
);
279 bool FilesPatchRequest::GetContentData(std::string
* upload_content_type
,
280 std::string
* upload_content
) {
281 if (title_
.empty() &&
282 modified_date_
.is_null() &&
283 last_viewed_by_me_date_
.is_null() &&
287 *upload_content_type
= kContentTypeApplicationJson
;
289 base::DictionaryValue root
;
291 root
.SetString("title", title_
);
293 if (!modified_date_
.is_null())
294 root
.SetString("modifiedDate", util::FormatTimeAsString(modified_date_
));
296 if (!last_viewed_by_me_date_
.is_null()) {
297 root
.SetString("lastViewedByMeDate",
298 util::FormatTimeAsString(last_viewed_by_me_date_
));
301 if (!parents_
.empty()) {
302 base::ListValue
* parents_value
= new base::ListValue
;
303 for (size_t i
= 0; i
< parents_
.size(); ++i
) {
304 base::DictionaryValue
* parent
= new base::DictionaryValue
;
305 parent
->SetString("id", parents_
[i
]);
306 parents_value
->Append(parent
);
308 root
.Set("parents", parents_value
);
311 base::JSONWriter::Write(&root
, upload_content
);
312 DVLOG(1) << "FilesPatch data: " << *upload_content_type
<< ", ["
313 << *upload_content
<< "]";
317 //============================= FilesCopyRequest ==============================
319 FilesCopyRequest::FilesCopyRequest(
320 RequestSender
* sender
,
321 const DriveApiUrlGenerator
& url_generator
,
322 const FileResourceCallback
& callback
)
323 : DriveApiDataRequest(
325 base::Bind(&ParseJsonAndRun
<FileResource
>, callback
)),
326 url_generator_(url_generator
) {
327 DCHECK(!callback
.is_null());
330 FilesCopyRequest::~FilesCopyRequest() {
333 net::URLFetcher::RequestType
FilesCopyRequest::GetRequestType() const {
334 return net::URLFetcher::POST
;
337 GURL
FilesCopyRequest::GetURLInternal() const {
338 return url_generator_
.GetFilesCopyUrl(file_id_
);
341 bool FilesCopyRequest::GetContentData(std::string
* upload_content_type
,
342 std::string
* upload_content
) {
343 if (parents_
.empty() && title_
.empty())
346 *upload_content_type
= kContentTypeApplicationJson
;
348 base::DictionaryValue root
;
350 if (!modified_date_
.is_null())
351 root
.SetString("modifiedDate", util::FormatTimeAsString(modified_date_
));
353 if (!parents_
.empty()) {
354 base::ListValue
* parents_value
= new base::ListValue
;
355 for (size_t i
= 0; i
< parents_
.size(); ++i
) {
356 base::DictionaryValue
* parent
= new base::DictionaryValue
;
357 parent
->SetString("id", parents_
[i
]);
358 parents_value
->Append(parent
);
360 root
.Set("parents", parents_value
);
364 root
.SetString("title", title_
);
366 base::JSONWriter::Write(&root
, upload_content
);
367 DVLOG(1) << "FilesCopy data: " << *upload_content_type
<< ", ["
368 << *upload_content
<< "]";
372 //============================= FilesListRequest =============================
374 FilesListRequest::FilesListRequest(
375 RequestSender
* sender
,
376 const DriveApiUrlGenerator
& url_generator
,
377 const FileListCallback
& callback
)
378 : DriveApiDataRequest(
380 base::Bind(&ParseJsonOnBlockingPoolAndRun
<FileList
>,
381 make_scoped_refptr(sender
->blocking_task_runner()),
383 url_generator_(url_generator
),
385 DCHECK(!callback
.is_null());
388 FilesListRequest::~FilesListRequest() {}
390 GURL
FilesListRequest::GetURLInternal() const {
391 return url_generator_
.GetFilesListUrl(max_results_
, page_token_
, q_
);
394 //======================== FilesListNextPageRequest =========================
396 FilesListNextPageRequest::FilesListNextPageRequest(
397 RequestSender
* sender
,
398 const FileListCallback
& callback
)
399 : DriveApiDataRequest(
401 base::Bind(&ParseJsonOnBlockingPoolAndRun
<FileList
>,
402 make_scoped_refptr(sender
->blocking_task_runner()),
404 DCHECK(!callback
.is_null());
407 FilesListNextPageRequest::~FilesListNextPageRequest() {
410 GURL
FilesListNextPageRequest::GetURLInternal() const {
414 //============================ FilesDeleteRequest =============================
416 FilesDeleteRequest::FilesDeleteRequest(
417 RequestSender
* sender
,
418 const DriveApiUrlGenerator
& url_generator
,
419 const EntryActionCallback
& callback
)
420 : EntryActionRequest(sender
, callback
),
421 url_generator_(url_generator
) {
422 DCHECK(!callback
.is_null());
425 FilesDeleteRequest::~FilesDeleteRequest() {}
427 net::URLFetcher::RequestType
FilesDeleteRequest::GetRequestType() const {
428 return net::URLFetcher::DELETE_REQUEST
;
431 GURL
FilesDeleteRequest::GetURL() const {
432 return url_generator_
.GetFilesDeleteUrl(file_id_
);
435 std::vector
<std::string
> FilesDeleteRequest::GetExtraRequestHeaders() const {
436 std::vector
<std::string
> headers(
437 EntryActionRequest::GetExtraRequestHeaders());
438 headers
.push_back(util::GenerateIfMatchHeader(etag_
));
442 //============================ FilesTrashRequest =============================
444 FilesTrashRequest::FilesTrashRequest(
445 RequestSender
* sender
,
446 const DriveApiUrlGenerator
& url_generator
,
447 const FileResourceCallback
& callback
)
448 : DriveApiDataRequest(
450 base::Bind(&ParseJsonAndRun
<FileResource
>, callback
)),
451 url_generator_(url_generator
) {
452 DCHECK(!callback
.is_null());
455 FilesTrashRequest::~FilesTrashRequest() {}
457 net::URLFetcher::RequestType
FilesTrashRequest::GetRequestType() const {
458 return net::URLFetcher::POST
;
461 GURL
FilesTrashRequest::GetURLInternal() const {
462 return url_generator_
.GetFilesTrashUrl(file_id_
);
465 //============================== AboutGetRequest =============================
467 AboutGetRequest::AboutGetRequest(
468 RequestSender
* sender
,
469 const DriveApiUrlGenerator
& url_generator
,
470 const AboutResourceCallback
& callback
)
471 : DriveApiDataRequest(
473 base::Bind(&ParseJsonAndRun
<AboutResource
>, callback
)),
474 url_generator_(url_generator
) {
475 DCHECK(!callback
.is_null());
478 AboutGetRequest::~AboutGetRequest() {}
480 GURL
AboutGetRequest::GetURLInternal() const {
481 return url_generator_
.GetAboutGetUrl();
484 //============================ ChangesListRequest ===========================
486 ChangesListRequest::ChangesListRequest(
487 RequestSender
* sender
,
488 const DriveApiUrlGenerator
& url_generator
,
489 const ChangeListCallback
& callback
)
490 : DriveApiDataRequest(
492 base::Bind(&ParseJsonOnBlockingPoolAndRun
<ChangeList
>,
493 make_scoped_refptr(sender
->blocking_task_runner()),
495 url_generator_(url_generator
),
496 include_deleted_(true),
498 start_change_id_(0) {
499 DCHECK(!callback
.is_null());
502 ChangesListRequest::~ChangesListRequest() {}
504 GURL
ChangesListRequest::GetURLInternal() const {
505 return url_generator_
.GetChangesListUrl(
506 include_deleted_
, max_results_
, page_token_
, start_change_id_
);
509 //======================== ChangesListNextPageRequest =========================
511 ChangesListNextPageRequest::ChangesListNextPageRequest(
512 RequestSender
* sender
,
513 const ChangeListCallback
& callback
)
514 : DriveApiDataRequest(
516 base::Bind(&ParseJsonOnBlockingPoolAndRun
<ChangeList
>,
517 make_scoped_refptr(sender
->blocking_task_runner()),
519 DCHECK(!callback
.is_null());
522 ChangesListNextPageRequest::~ChangesListNextPageRequest() {
525 GURL
ChangesListNextPageRequest::GetURLInternal() const {
529 //============================== AppsListRequest ===========================
531 AppsListRequest::AppsListRequest(
532 RequestSender
* sender
,
533 const DriveApiUrlGenerator
& url_generator
,
534 bool use_internal_endpoint
,
535 const AppListCallback
& callback
)
536 : DriveApiDataRequest(
538 base::Bind(&ParseJsonAndRun
<AppList
>, callback
)),
539 url_generator_(url_generator
),
540 use_internal_endpoint_(use_internal_endpoint
) {
541 DCHECK(!callback
.is_null());
544 AppsListRequest::~AppsListRequest() {}
546 GURL
AppsListRequest::GetURLInternal() const {
547 return url_generator_
.GetAppsListUrl(use_internal_endpoint_
);
550 //============================== AppsDeleteRequest ===========================
552 AppsDeleteRequest::AppsDeleteRequest(RequestSender
* sender
,
553 const DriveApiUrlGenerator
& url_generator
,
554 const EntryActionCallback
& callback
)
555 : EntryActionRequest(sender
, callback
),
556 url_generator_(url_generator
) {
557 DCHECK(!callback
.is_null());
560 AppsDeleteRequest::~AppsDeleteRequest() {}
562 net::URLFetcher::RequestType
AppsDeleteRequest::GetRequestType() const {
563 return net::URLFetcher::DELETE_REQUEST
;
566 GURL
AppsDeleteRequest::GetURL() const {
567 return url_generator_
.GetAppsDeleteUrl(app_id_
);
570 //========================== ChildrenInsertRequest ============================
572 ChildrenInsertRequest::ChildrenInsertRequest(
573 RequestSender
* sender
,
574 const DriveApiUrlGenerator
& url_generator
,
575 const EntryActionCallback
& callback
)
576 : EntryActionRequest(sender
, callback
),
577 url_generator_(url_generator
) {
578 DCHECK(!callback
.is_null());
581 ChildrenInsertRequest::~ChildrenInsertRequest() {}
583 net::URLFetcher::RequestType
ChildrenInsertRequest::GetRequestType() const {
584 return net::URLFetcher::POST
;
587 GURL
ChildrenInsertRequest::GetURL() const {
588 return url_generator_
.GetChildrenInsertUrl(folder_id_
);
591 bool ChildrenInsertRequest::GetContentData(std::string
* upload_content_type
,
592 std::string
* upload_content
) {
593 *upload_content_type
= kContentTypeApplicationJson
;
595 base::DictionaryValue root
;
596 root
.SetString("id", id_
);
598 base::JSONWriter::Write(&root
, upload_content
);
599 DVLOG(1) << "InsertResource data: " << *upload_content_type
<< ", ["
600 << *upload_content
<< "]";
604 //========================== ChildrenDeleteRequest ============================
606 ChildrenDeleteRequest::ChildrenDeleteRequest(
607 RequestSender
* sender
,
608 const DriveApiUrlGenerator
& url_generator
,
609 const EntryActionCallback
& callback
)
610 : EntryActionRequest(sender
, callback
),
611 url_generator_(url_generator
) {
612 DCHECK(!callback
.is_null());
615 ChildrenDeleteRequest::~ChildrenDeleteRequest() {}
617 net::URLFetcher::RequestType
ChildrenDeleteRequest::GetRequestType() const {
618 return net::URLFetcher::DELETE_REQUEST
;
621 GURL
ChildrenDeleteRequest::GetURL() const {
622 return url_generator_
.GetChildrenDeleteUrl(child_id_
, folder_id_
);
625 //======================= InitiateUploadNewFileRequest =======================
627 InitiateUploadNewFileRequest::InitiateUploadNewFileRequest(
628 RequestSender
* sender
,
629 const DriveApiUrlGenerator
& url_generator
,
630 const std::string
& content_type
,
631 int64 content_length
,
632 const std::string
& parent_resource_id
,
633 const std::string
& title
,
634 const InitiateUploadCallback
& callback
)
635 : InitiateUploadRequestBase(sender
,
639 url_generator_(url_generator
),
640 parent_resource_id_(parent_resource_id
),
644 InitiateUploadNewFileRequest::~InitiateUploadNewFileRequest() {}
646 GURL
InitiateUploadNewFileRequest::GetURL() const {
647 return url_generator_
.GetInitiateUploadNewFileUrl(!modified_date_
.is_null());
650 net::URLFetcher::RequestType
651 InitiateUploadNewFileRequest::GetRequestType() const {
652 return net::URLFetcher::POST
;
655 bool InitiateUploadNewFileRequest::GetContentData(
656 std::string
* upload_content_type
,
657 std::string
* upload_content
) {
658 *upload_content_type
= kContentTypeApplicationJson
;
660 base::DictionaryValue root
;
661 root
.SetString("title", title_
);
664 scoped_ptr
<base::ListValue
> parents(new base::ListValue
);
665 parents
->Append(CreateParentValue(parent_resource_id_
).release());
666 root
.Set("parents", parents
.release());
668 if (!modified_date_
.is_null())
669 root
.SetString("modifiedDate", util::FormatTimeAsString(modified_date_
));
671 if (!last_viewed_by_me_date_
.is_null()) {
672 root
.SetString("lastViewedByMeDate",
673 util::FormatTimeAsString(last_viewed_by_me_date_
));
676 base::JSONWriter::Write(&root
, upload_content
);
678 DVLOG(1) << "InitiateUploadNewFile data: " << *upload_content_type
<< ", ["
679 << *upload_content
<< "]";
683 //===================== InitiateUploadExistingFileRequest ====================
685 InitiateUploadExistingFileRequest::InitiateUploadExistingFileRequest(
686 RequestSender
* sender
,
687 const DriveApiUrlGenerator
& url_generator
,
688 const std::string
& content_type
,
689 int64 content_length
,
690 const std::string
& resource_id
,
691 const std::string
& etag
,
692 const InitiateUploadCallback
& callback
)
693 : InitiateUploadRequestBase(sender
,
697 url_generator_(url_generator
),
698 resource_id_(resource_id
),
702 InitiateUploadExistingFileRequest::~InitiateUploadExistingFileRequest() {}
704 GURL
InitiateUploadExistingFileRequest::GetURL() const {
705 return url_generator_
.GetInitiateUploadExistingFileUrl(
706 resource_id_
, !modified_date_
.is_null());
709 net::URLFetcher::RequestType
710 InitiateUploadExistingFileRequest::GetRequestType() const {
711 return net::URLFetcher::PUT
;
714 std::vector
<std::string
>
715 InitiateUploadExistingFileRequest::GetExtraRequestHeaders() const {
716 std::vector
<std::string
> headers(
717 InitiateUploadRequestBase::GetExtraRequestHeaders());
718 headers
.push_back(util::GenerateIfMatchHeader(etag_
));
722 bool InitiateUploadExistingFileRequest::GetContentData(
723 std::string
* upload_content_type
,
724 std::string
* upload_content
) {
725 base::DictionaryValue root
;
726 if (!parent_resource_id_
.empty()) {
727 scoped_ptr
<base::ListValue
> parents(new base::ListValue
);
728 parents
->Append(CreateParentValue(parent_resource_id_
).release());
729 root
.Set("parents", parents
.release());
733 root
.SetString("title", title_
);
735 if (!modified_date_
.is_null())
736 root
.SetString("modifiedDate", util::FormatTimeAsString(modified_date_
));
738 if (!last_viewed_by_me_date_
.is_null()) {
739 root
.SetString("lastViewedByMeDate",
740 util::FormatTimeAsString(last_viewed_by_me_date_
));
746 *upload_content_type
= kContentTypeApplicationJson
;
747 base::JSONWriter::Write(&root
, upload_content
);
748 DVLOG(1) << "InitiateUploadExistingFile data: " << *upload_content_type
749 << ", [" << *upload_content
<< "]";
753 //============================ ResumeUploadRequest ===========================
755 ResumeUploadRequest::ResumeUploadRequest(
756 RequestSender
* sender
,
757 const GURL
& upload_location
,
758 int64 start_position
,
760 int64 content_length
,
761 const std::string
& content_type
,
762 const base::FilePath
& local_file_path
,
763 const UploadRangeCallback
& callback
,
764 const ProgressCallback
& progress_callback
)
765 : ResumeUploadRequestBase(sender
,
773 progress_callback_(progress_callback
) {
774 DCHECK(!callback_
.is_null());
777 ResumeUploadRequest::~ResumeUploadRequest() {}
779 void ResumeUploadRequest::OnRangeRequestComplete(
780 const UploadRangeResponse
& response
,
781 scoped_ptr
<base::Value
> value
) {
782 DCHECK(CalledOnValidThread());
783 ParseFileResourceWithUploadRangeAndRun(callback_
, response
, value
.Pass());
786 void ResumeUploadRequest::OnURLFetchUploadProgress(
787 const net::URLFetcher
* source
, int64 current
, int64 total
) {
788 if (!progress_callback_
.is_null())
789 progress_callback_
.Run(current
, total
);
792 //========================== GetUploadStatusRequest ==========================
794 GetUploadStatusRequest::GetUploadStatusRequest(
795 RequestSender
* sender
,
796 const GURL
& upload_url
,
797 int64 content_length
,
798 const UploadRangeCallback
& callback
)
799 : GetUploadStatusRequestBase(sender
,
802 callback_(callback
) {
803 DCHECK(!callback
.is_null());
806 GetUploadStatusRequest::~GetUploadStatusRequest() {}
808 void GetUploadStatusRequest::OnRangeRequestComplete(
809 const UploadRangeResponse
& response
,
810 scoped_ptr
<base::Value
> value
) {
811 DCHECK(CalledOnValidThread());
812 ParseFileResourceWithUploadRangeAndRun(callback_
, response
, value
.Pass());
815 //========================== DownloadFileRequest ==========================
817 DownloadFileRequest::DownloadFileRequest(
818 RequestSender
* sender
,
819 const DriveApiUrlGenerator
& url_generator
,
820 const std::string
& resource_id
,
821 const base::FilePath
& output_file_path
,
822 const DownloadActionCallback
& download_action_callback
,
823 const GetContentCallback
& get_content_callback
,
824 const ProgressCallback
& progress_callback
)
825 : DownloadFileRequestBase(
827 download_action_callback
,
828 get_content_callback
,
830 url_generator
.GenerateDownloadFileUrl(resource_id
),
834 DownloadFileRequest::~DownloadFileRequest() {
837 //========================== PermissionsInsertRequest ==========================
839 PermissionsInsertRequest::PermissionsInsertRequest(
840 RequestSender
* sender
,
841 const DriveApiUrlGenerator
& url_generator
,
842 const EntryActionCallback
& callback
)
843 : EntryActionRequest(sender
, callback
),
844 url_generator_(url_generator
),
845 type_(PERMISSION_TYPE_USER
),
846 role_(PERMISSION_ROLE_READER
) {
849 PermissionsInsertRequest::~PermissionsInsertRequest() {
852 GURL
PermissionsInsertRequest::GetURL() const {
853 return url_generator_
.GetPermissionsInsertUrl(id_
);
856 net::URLFetcher::RequestType
857 PermissionsInsertRequest::GetRequestType() const {
858 return net::URLFetcher::POST
;
861 bool PermissionsInsertRequest::GetContentData(std::string
* upload_content_type
,
862 std::string
* upload_content
) {
863 *upload_content_type
= kContentTypeApplicationJson
;
865 base::DictionaryValue root
;
867 case PERMISSION_TYPE_ANYONE
:
868 root
.SetString("type", "anyone");
870 case PERMISSION_TYPE_DOMAIN
:
871 root
.SetString("type", "domain");
873 case PERMISSION_TYPE_GROUP
:
874 root
.SetString("type", "group");
876 case PERMISSION_TYPE_USER
:
877 root
.SetString("type", "user");
881 case PERMISSION_ROLE_OWNER
:
882 root
.SetString("role", "owner");
884 case PERMISSION_ROLE_READER
:
885 root
.SetString("role", "reader");
887 case PERMISSION_ROLE_WRITER
:
888 root
.SetString("role", "writer");
890 case PERMISSION_ROLE_COMMENTER
:
891 root
.SetString("role", "reader");
893 base::ListValue
* list
= new base::ListValue
;
894 list
->AppendString("commenter");
895 root
.Set("additionalRoles", list
);
899 root
.SetString("value", value_
);
900 base::JSONWriter::Write(&root
, upload_content
);
905 } // namespace google_apis