Add an extension override bubble and warning box for proxy extensions. (2nd attempt...
[chromium-blink-merge.git] / google_apis / drive / drive_api_requests.cc
bloba63757112b86ca9f1fa85525fd098d973edbbf36
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"
7 #include "base/bind.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 {
21 namespace {
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.
28 template<typename T>
29 void ParseJsonAndRun(
30 const base::Callback<void(GDataErrorCode, scoped_ptr<T>)>& callback,
31 GDataErrorCode error,
32 scoped_ptr<base::Value> value) {
33 DCHECK(!callback.is_null());
35 scoped_ptr<T> resource;
36 if (value) {
37 resource = T::CreateFrom(*value);
38 if (!resource) {
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.
49 template<typename T>
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.
56 template<typename T>
57 void ParseJsonOnBlockingPoolAndRunAfterBlockingPoolTask(
58 const base::Callback<void(GDataErrorCode, scoped_ptr<T>)>& callback,
59 GDataErrorCode error, scoped_ptr<T> value) {
60 if (!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.
69 template<typename T>
70 void ParseJsonOnBlockingPoolAndRun(
71 scoped_refptr<base::TaskRunner> blocking_task_runner,
72 const base::Callback<void(GDataErrorCode, scoped_ptr<T>)>& callback,
73 GDataErrorCode error,
74 scoped_ptr<base::Value> value) {
75 DCHECK(!callback.is_null());
77 if (!value) {
78 callback.Run(error, scoped_ptr<T>());
79 return;
82 base::PostTaskAndReplyWithResult(
83 blocking_task_runner,
84 FROM_HERE,
85 base::Bind(&ParseJsonOnBlockingPool<T>, base::Passed(&value)),
86 base::Bind(&ParseJsonOnBlockingPoolAndRunAfterBlockingPoolTask<T>,
87 callback, error));
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;
101 if (value) {
102 file_resource = FileResource::CreateFrom(*value);
103 if (!file_resource) {
104 callback.Run(
105 UploadRangeResponse(GDATA_PARSE_ERROR,
106 response.start_position_received,
107 response.end_position_received),
108 scoped_ptr<FileResource>());
109 return;
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();
125 } // namespace
127 namespace drive {
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_);
143 return url;
146 //=============================== FilesGetRequest =============================
148 FilesGetRequest::FilesGetRequest(
149 RequestSender* sender,
150 const DriveApiUrlGenerator& url_generator,
151 const FileResourceCallback& callback)
152 : DriveApiDataRequest(
153 sender,
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(
172 sender,
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(
195 sender,
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);
234 if (!title_.empty())
235 root.SetString("title", title_);
237 base::JSONWriter::Write(&root, upload_content);
238 DVLOG(1) << "FilesInsert data: " << *upload_content_type << ", ["
239 << *upload_content << "]";
240 return true;
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(
254 sender,
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);
271 return headers;
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() &&
284 parents_.empty())
285 return false;
287 *upload_content_type = kContentTypeApplicationJson;
289 base::DictionaryValue root;
290 if (!title_.empty())
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 << "]";
314 return true;
317 //============================= FilesCopyRequest ==============================
319 FilesCopyRequest::FilesCopyRequest(
320 RequestSender* sender,
321 const DriveApiUrlGenerator& url_generator,
322 const FileResourceCallback& callback)
323 : DriveApiDataRequest(
324 sender,
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())
344 return false;
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);
363 if (!title_.empty())
364 root.SetString("title", title_);
366 base::JSONWriter::Write(&root, upload_content);
367 DVLOG(1) << "FilesCopy data: " << *upload_content_type << ", ["
368 << *upload_content << "]";
369 return true;
372 //============================= FilesListRequest =============================
374 FilesListRequest::FilesListRequest(
375 RequestSender* sender,
376 const DriveApiUrlGenerator& url_generator,
377 const FileListCallback& callback)
378 : DriveApiDataRequest(
379 sender,
380 base::Bind(&ParseJsonOnBlockingPoolAndRun<FileList>,
381 make_scoped_refptr(sender->blocking_task_runner()),
382 callback)),
383 url_generator_(url_generator),
384 max_results_(100) {
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(
400 sender,
401 base::Bind(&ParseJsonOnBlockingPoolAndRun<FileList>,
402 make_scoped_refptr(sender->blocking_task_runner()),
403 callback)) {
404 DCHECK(!callback.is_null());
407 FilesListNextPageRequest::~FilesListNextPageRequest() {
410 GURL FilesListNextPageRequest::GetURLInternal() const {
411 return next_link_;
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_));
439 return headers;
442 //============================ FilesTrashRequest =============================
444 FilesTrashRequest::FilesTrashRequest(
445 RequestSender* sender,
446 const DriveApiUrlGenerator& url_generator,
447 const FileResourceCallback& callback)
448 : DriveApiDataRequest(
449 sender,
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(
472 sender,
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(
491 sender,
492 base::Bind(&ParseJsonOnBlockingPoolAndRun<ChangeList>,
493 make_scoped_refptr(sender->blocking_task_runner()),
494 callback)),
495 url_generator_(url_generator),
496 include_deleted_(true),
497 max_results_(100),
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(
515 sender,
516 base::Bind(&ParseJsonOnBlockingPoolAndRun<ChangeList>,
517 make_scoped_refptr(sender->blocking_task_runner()),
518 callback)) {
519 DCHECK(!callback.is_null());
522 ChangesListNextPageRequest::~ChangesListNextPageRequest() {
525 GURL ChangesListNextPageRequest::GetURLInternal() const {
526 return next_link_;
529 //============================== AppsListRequest ===========================
531 AppsListRequest::AppsListRequest(
532 RequestSender* sender,
533 const DriveApiUrlGenerator& url_generator,
534 bool use_internal_endpoint,
535 const AppListCallback& callback)
536 : DriveApiDataRequest(
537 sender,
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 << "]";
601 return true;
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,
636 callback,
637 content_type,
638 content_length),
639 url_generator_(url_generator),
640 parent_resource_id_(parent_resource_id),
641 title_(title) {
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_);
663 // Fill parent link.
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 << "]";
680 return true;
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,
694 callback,
695 content_type,
696 content_length),
697 url_generator_(url_generator),
698 resource_id_(resource_id),
699 etag_(etag) {
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_));
719 return headers;
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());
732 if (!title_.empty())
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_));
743 if (root.empty())
744 return false;
746 *upload_content_type = kContentTypeApplicationJson;
747 base::JSONWriter::Write(&root, upload_content);
748 DVLOG(1) << "InitiateUploadExistingFile data: " << *upload_content_type
749 << ", [" << *upload_content << "]";
750 return true;
753 //============================ ResumeUploadRequest ===========================
755 ResumeUploadRequest::ResumeUploadRequest(
756 RequestSender* sender,
757 const GURL& upload_location,
758 int64 start_position,
759 int64 end_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,
766 upload_location,
767 start_position,
768 end_position,
769 content_length,
770 content_type,
771 local_file_path),
772 callback_(callback),
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,
800 upload_url,
801 content_length),
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(
826 sender,
827 download_action_callback,
828 get_content_callback,
829 progress_callback,
830 url_generator.GenerateDownloadFileUrl(resource_id),
831 output_file_path) {
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;
866 switch (type_) {
867 case PERMISSION_TYPE_ANYONE:
868 root.SetString("type", "anyone");
869 break;
870 case PERMISSION_TYPE_DOMAIN:
871 root.SetString("type", "domain");
872 break;
873 case PERMISSION_TYPE_GROUP:
874 root.SetString("type", "group");
875 break;
876 case PERMISSION_TYPE_USER:
877 root.SetString("type", "user");
878 break;
880 switch (role_) {
881 case PERMISSION_ROLE_OWNER:
882 root.SetString("role", "owner");
883 break;
884 case PERMISSION_ROLE_READER:
885 root.SetString("role", "reader");
886 break;
887 case PERMISSION_ROLE_WRITER:
888 root.SetString("role", "writer");
889 break;
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);
897 break;
899 root.SetString("value", value_);
900 base::JSONWriter::Write(&root, upload_content);
901 return true;
904 } // namespace drive
905 } // namespace google_apis