1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "google_apis/drive/drive_api_requests.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/json/json_writer.h"
#include "base/location.h"
#include "base/sequenced_task_runner.h"
#include "base/task_runner_util.h"
#include "base/values.h"
#include "google_apis/drive/drive_api_parser.h"
#include "google_apis/drive/request_sender.h"
#include "google_apis/drive/request_util.h"
#include "google_apis/drive/time_util.h"
#include "net/base/url_util.h"
namespace google_apis {
namespace {
const char kContentTypeApplicationJson[] = "application/json";
const char kParentLinkKind[] = "drive#fileLink";
// Parses the JSON value to a resource typed |T| and runs |callback| on the UI
// thread once parsing is done.
template<typename T>
void ParseJsonAndRun(
const base::Callback<void(GDataErrorCode, scoped_ptr<T>)>& callback,
GDataErrorCode error,
scoped_ptr<base::Value> value) {
DCHECK(!callback.is_null());
scoped_ptr<T> resource;
if (value) {
resource = T::CreateFrom(*value);
if (!resource) {
// Failed to parse the JSON value, although the JSON value is available,
// so let the callback know the parsing error.
error = GDATA_PARSE_ERROR;
}
}
callback.Run(error, resource.Pass());
}
// Thin adapter of T::CreateFrom.
template<typename T>
scoped_ptr<T> ParseJsonOnBlockingPool(scoped_ptr<base::Value> value) {
return T::CreateFrom(*value);
}
// Runs |callback| with given |error| and |value|. If |value| is null,
// overwrites |error| to GDATA_PARSE_ERROR.
template<typename T>
void ParseJsonOnBlockingPoolAndRunAfterBlockingPoolTask(
const base::Callback<void(GDataErrorCode, scoped_ptr<T>)>& callback,
GDataErrorCode error, scoped_ptr<T> value) {
if (!value)
error = GDATA_PARSE_ERROR;
callback.Run(error, value.Pass());
}
// Parses the JSON value to a resource typed |T| and runs |callback| on
// blocking pool, and then run on the current thread.
// TODO(hidehiko): Move this and ParseJsonAndRun defined above into base with
// merging the tasks running on blocking pool into one.
template<typename T>
void ParseJsonOnBlockingPoolAndRun(
scoped_refptr<base::TaskRunner> blocking_task_runner,
const base::Callback<void(GDataErrorCode, scoped_ptr<T>)>& callback,
GDataErrorCode error,
scoped_ptr<base::Value> value) {
DCHECK(!callback.is_null());
if (!value) {
callback.Run(error, scoped_ptr<T>());
return;
}
base::PostTaskAndReplyWithResult(
blocking_task_runner,
FROM_HERE,
base::Bind(&ParseJsonOnBlockingPool<T>, base::Passed(&value)),
base::Bind(&ParseJsonOnBlockingPoolAndRunAfterBlockingPoolTask<T>,
callback, error));
}
// Parses the JSON value to FileResource instance and runs |callback| on the
// UI thread once parsing is done.
// This is customized version of ParseJsonAndRun defined above to adapt the
// remaining response type.
void ParseFileResourceWithUploadRangeAndRun(
const drive::UploadRangeCallback& callback,
const UploadRangeResponse& response,
scoped_ptr<base::Value> value) {
DCHECK(!callback.is_null());
scoped_ptr<FileResource> file_resource;
if (value) {
file_resource = FileResource::CreateFrom(*value);
if (!file_resource) {
callback.Run(
UploadRangeResponse(GDATA_PARSE_ERROR,
response.start_position_received,
response.end_position_received),
scoped_ptr<FileResource>());
return;
}
}
callback.Run(response, file_resource.Pass());
}
// Creates a Parents value which can be used as a part of request body.
scoped_ptr<base::DictionaryValue> CreateParentValue(
const std::string& file_id) {
scoped_ptr<base::DictionaryValue> parent(new base::DictionaryValue);
parent->SetString("kind", kParentLinkKind);
parent->SetString("id", file_id);
return parent.Pass();
}
} // namespace
namespace drive {
//============================ DriveApiDataRequest ===========================
DriveApiDataRequest::DriveApiDataRequest(RequestSender* sender,
const GetDataCallback& callback)
: GetDataRequest(sender, callback) {
}
DriveApiDataRequest::~DriveApiDataRequest() {
}
GURL DriveApiDataRequest::GetURL() const {
GURL url = GetURLInternal();
if (!fields_.empty())
url = net::AppendOrReplaceQueryParameter(url, "fields", fields_);
return url;
}
//=============================== FilesGetRequest =============================
FilesGetRequest::FilesGetRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const FileResourceCallback& callback)
: DriveApiDataRequest(
sender,
base::Bind(&ParseJsonAndRun<FileResource>, callback)),
url_generator_(url_generator) {
DCHECK(!callback.is_null());
}
FilesGetRequest::~FilesGetRequest() {}
GURL FilesGetRequest::GetURLInternal() const {
return url_generator_.GetFilesGetUrl(file_id_);
}
//============================ FilesAuthorizeRequest ===========================
FilesAuthorizeRequest::FilesAuthorizeRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const FileResourceCallback& callback)
: DriveApiDataRequest(
sender,
base::Bind(&ParseJsonAndRun<FileResource>, callback)),
url_generator_(url_generator) {
DCHECK(!callback.is_null());
}
FilesAuthorizeRequest::~FilesAuthorizeRequest() {}
net::URLFetcher::RequestType FilesAuthorizeRequest::GetRequestType() const {
return net::URLFetcher::POST;
}
GURL FilesAuthorizeRequest::GetURLInternal() const {
return url_generator_.GetFilesAuthorizeUrl(file_id_, app_id_);
}
//============================ FilesInsertRequest ============================
FilesInsertRequest::FilesInsertRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const FileResourceCallback& callback)
: DriveApiDataRequest(
sender,
base::Bind(&ParseJsonAndRun<FileResource>, callback)),
url_generator_(url_generator) {
DCHECK(!callback.is_null());
}
FilesInsertRequest::~FilesInsertRequest() {}
net::URLFetcher::RequestType FilesInsertRequest::GetRequestType() const {
return net::URLFetcher::POST;
}
bool FilesInsertRequest::GetContentData(std::string* upload_content_type,
std::string* upload_content) {
*upload_content_type = kContentTypeApplicationJson;
base::DictionaryValue root;
if (!mime_type_.empty())
root.SetString("mimeType", mime_type_);
if (!parents_.empty()) {
base::ListValue* parents_value = new base::ListValue;
for (size_t i = 0; i < parents_.size(); ++i) {
base::DictionaryValue* parent = new base::DictionaryValue;
parent->SetString("id", parents_[i]);
parents_value->Append(parent);
}
root.Set("parents", parents_value);
}
if (!title_.empty())
root.SetString("title", title_);
base::JSONWriter::Write(&root, upload_content);
DVLOG(1) << "FilesInsert data: " << *upload_content_type << ", ["
<< *upload_content << "]";
return true;
}
GURL FilesInsertRequest::GetURLInternal() const {
return url_generator_.GetFilesInsertUrl();
}
//============================== FilesPatchRequest ============================
FilesPatchRequest::FilesPatchRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const FileResourceCallback& callback)
: DriveApiDataRequest(
sender,
base::Bind(&ParseJsonAndRun<FileResource>, callback)),
url_generator_(url_generator),
set_modified_date_(false),
update_viewed_date_(true) {
DCHECK(!callback.is_null());
}
FilesPatchRequest::~FilesPatchRequest() {}
net::URLFetcher::RequestType FilesPatchRequest::GetRequestType() const {
return net::URLFetcher::PATCH;
}
std::vector<std::string> FilesPatchRequest::GetExtraRequestHeaders() const {
std::vector<std::string> headers;
headers.push_back(util::kIfMatchAllHeader);
return headers;
}
GURL FilesPatchRequest::GetURLInternal() const {
return url_generator_.GetFilesPatchUrl(
file_id_, set_modified_date_, update_viewed_date_);
}
bool FilesPatchRequest::GetContentData(std::string* upload_content_type,
std::string* upload_content) {
if (title_.empty() &&
modified_date_.is_null() &&
last_viewed_by_me_date_.is_null() &&
parents_.empty())
return false;
*upload_content_type = kContentTypeApplicationJson;
base::DictionaryValue root;
if (!title_.empty())
root.SetString("title", title_);
if (!modified_date_.is_null())
root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_));
if (!last_viewed_by_me_date_.is_null()) {
root.SetString("lastViewedByMeDate",
util::FormatTimeAsString(last_viewed_by_me_date_));
}
if (!parents_.empty()) {
base::ListValue* parents_value = new base::ListValue;
for (size_t i = 0; i < parents_.size(); ++i) {
base::DictionaryValue* parent = new base::DictionaryValue;
parent->SetString("id", parents_[i]);
parents_value->Append(parent);
}
root.Set("parents", parents_value);
}
base::JSONWriter::Write(&root, upload_content);
DVLOG(1) << "FilesPatch data: " << *upload_content_type << ", ["
<< *upload_content << "]";
return true;
}
//============================= FilesCopyRequest ==============================
FilesCopyRequest::FilesCopyRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const FileResourceCallback& callback)
: DriveApiDataRequest(
sender,
base::Bind(&ParseJsonAndRun<FileResource>, callback)),
url_generator_(url_generator) {
DCHECK(!callback.is_null());
}
FilesCopyRequest::~FilesCopyRequest() {
}
net::URLFetcher::RequestType FilesCopyRequest::GetRequestType() const {
return net::URLFetcher::POST;
}
GURL FilesCopyRequest::GetURLInternal() const {
return url_generator_.GetFilesCopyUrl(file_id_);
}
bool FilesCopyRequest::GetContentData(std::string* upload_content_type,
std::string* upload_content) {
if (parents_.empty() && title_.empty())
return false;
*upload_content_type = kContentTypeApplicationJson;
base::DictionaryValue root;
if (!modified_date_.is_null())
root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_));
if (!parents_.empty()) {
base::ListValue* parents_value = new base::ListValue;
for (size_t i = 0; i < parents_.size(); ++i) {
base::DictionaryValue* parent = new base::DictionaryValue;
parent->SetString("id", parents_[i]);
parents_value->Append(parent);
}
root.Set("parents", parents_value);
}
if (!title_.empty())
root.SetString("title", title_);
base::JSONWriter::Write(&root, upload_content);
DVLOG(1) << "FilesCopy data: " << *upload_content_type << ", ["
<< *upload_content << "]";
return true;
}
//============================= FilesListRequest =============================
FilesListRequest::FilesListRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const FileListCallback& callback)
: DriveApiDataRequest(
sender,
base::Bind(&ParseJsonOnBlockingPoolAndRun<FileList>,
make_scoped_refptr(sender->blocking_task_runner()),
callback)),
url_generator_(url_generator),
max_results_(100) {
DCHECK(!callback.is_null());
}
FilesListRequest::~FilesListRequest() {}
GURL FilesListRequest::GetURLInternal() const {
return url_generator_.GetFilesListUrl(max_results_, page_token_, q_);
}
//======================== FilesListNextPageRequest =========================
FilesListNextPageRequest::FilesListNextPageRequest(
RequestSender* sender,
const FileListCallback& callback)
: DriveApiDataRequest(
sender,
base::Bind(&ParseJsonOnBlockingPoolAndRun<FileList>,
make_scoped_refptr(sender->blocking_task_runner()),
callback)) {
DCHECK(!callback.is_null());
}
FilesListNextPageRequest::~FilesListNextPageRequest() {
}
GURL FilesListNextPageRequest::GetURLInternal() const {
return next_link_;
}
//============================ FilesDeleteRequest =============================
FilesDeleteRequest::FilesDeleteRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const EntryActionCallback& callback)
: EntryActionRequest(sender, callback),
url_generator_(url_generator) {
DCHECK(!callback.is_null());
}
FilesDeleteRequest::~FilesDeleteRequest() {}
net::URLFetcher::RequestType FilesDeleteRequest::GetRequestType() const {
return net::URLFetcher::DELETE_REQUEST;
}
GURL FilesDeleteRequest::GetURL() const {
return url_generator_.GetFilesDeleteUrl(file_id_);
}
std::vector<std::string> FilesDeleteRequest::GetExtraRequestHeaders() const {
std::vector<std::string> headers(
EntryActionRequest::GetExtraRequestHeaders());
headers.push_back(util::GenerateIfMatchHeader(etag_));
return headers;
}
//============================ FilesTrashRequest =============================
FilesTrashRequest::FilesTrashRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const FileResourceCallback& callback)
: DriveApiDataRequest(
sender,
base::Bind(&ParseJsonAndRun<FileResource>, callback)),
url_generator_(url_generator) {
DCHECK(!callback.is_null());
}
FilesTrashRequest::~FilesTrashRequest() {}
net::URLFetcher::RequestType FilesTrashRequest::GetRequestType() const {
return net::URLFetcher::POST;
}
GURL FilesTrashRequest::GetURLInternal() const {
return url_generator_.GetFilesTrashUrl(file_id_);
}
//============================== AboutGetRequest =============================
AboutGetRequest::AboutGetRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const AboutResourceCallback& callback)
: DriveApiDataRequest(
sender,
base::Bind(&ParseJsonAndRun<AboutResource>, callback)),
url_generator_(url_generator) {
DCHECK(!callback.is_null());
}
AboutGetRequest::~AboutGetRequest() {}
GURL AboutGetRequest::GetURLInternal() const {
return url_generator_.GetAboutGetUrl();
}
//============================ ChangesListRequest ===========================
ChangesListRequest::ChangesListRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const ChangeListCallback& callback)
: DriveApiDataRequest(
sender,
base::Bind(&ParseJsonOnBlockingPoolAndRun<ChangeList>,
make_scoped_refptr(sender->blocking_task_runner()),
callback)),
url_generator_(url_generator),
include_deleted_(true),
max_results_(100),
start_change_id_(0) {
DCHECK(!callback.is_null());
}
ChangesListRequest::~ChangesListRequest() {}
GURL ChangesListRequest::GetURLInternal() const {
return url_generator_.GetChangesListUrl(
include_deleted_, max_results_, page_token_, start_change_id_);
}
//======================== ChangesListNextPageRequest =========================
ChangesListNextPageRequest::ChangesListNextPageRequest(
RequestSender* sender,
const ChangeListCallback& callback)
: DriveApiDataRequest(
sender,
base::Bind(&ParseJsonOnBlockingPoolAndRun<ChangeList>,
make_scoped_refptr(sender->blocking_task_runner()),
callback)) {
DCHECK(!callback.is_null());
}
ChangesListNextPageRequest::~ChangesListNextPageRequest() {
}
GURL ChangesListNextPageRequest::GetURLInternal() const {
return next_link_;
}
//============================== AppsListRequest ===========================
AppsListRequest::AppsListRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const AppListCallback& callback)
: DriveApiDataRequest(
sender,
base::Bind(&ParseJsonAndRun<AppList>, callback)),
url_generator_(url_generator) {
DCHECK(!callback.is_null());
}
AppsListRequest::~AppsListRequest() {}
GURL AppsListRequest::GetURLInternal() const {
return url_generator_.GetAppsListUrl();
}
//============================== AppsDeleteRequest ===========================
AppsDeleteRequest::AppsDeleteRequest(RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const EntryActionCallback& callback)
: EntryActionRequest(sender, callback),
url_generator_(url_generator) {
DCHECK(!callback.is_null());
}
AppsDeleteRequest::~AppsDeleteRequest() {}
net::URLFetcher::RequestType AppsDeleteRequest::GetRequestType() const {
return net::URLFetcher::DELETE_REQUEST;
}
GURL AppsDeleteRequest::GetURL() const {
return url_generator_.GetAppsDeleteUrl(app_id_);
}
//========================== ChildrenInsertRequest ============================
ChildrenInsertRequest::ChildrenInsertRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const EntryActionCallback& callback)
: EntryActionRequest(sender, callback),
url_generator_(url_generator) {
DCHECK(!callback.is_null());
}
ChildrenInsertRequest::~ChildrenInsertRequest() {}
net::URLFetcher::RequestType ChildrenInsertRequest::GetRequestType() const {
return net::URLFetcher::POST;
}
GURL ChildrenInsertRequest::GetURL() const {
return url_generator_.GetChildrenInsertUrl(folder_id_);
}
bool ChildrenInsertRequest::GetContentData(std::string* upload_content_type,
std::string* upload_content) {
*upload_content_type = kContentTypeApplicationJson;
base::DictionaryValue root;
root.SetString("id", id_);
base::JSONWriter::Write(&root, upload_content);
DVLOG(1) << "InsertResource data: " << *upload_content_type << ", ["
<< *upload_content << "]";
return true;
}
//========================== ChildrenDeleteRequest ============================
ChildrenDeleteRequest::ChildrenDeleteRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const EntryActionCallback& callback)
: EntryActionRequest(sender, callback),
url_generator_(url_generator) {
DCHECK(!callback.is_null());
}
ChildrenDeleteRequest::~ChildrenDeleteRequest() {}
net::URLFetcher::RequestType ChildrenDeleteRequest::GetRequestType() const {
return net::URLFetcher::DELETE_REQUEST;
}
GURL ChildrenDeleteRequest::GetURL() const {
return url_generator_.GetChildrenDeleteUrl(child_id_, folder_id_);
}
//======================= InitiateUploadNewFileRequest =======================
InitiateUploadNewFileRequest::InitiateUploadNewFileRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const std::string& content_type,
int64 content_length,
const std::string& parent_resource_id,
const std::string& title,
const InitiateUploadCallback& callback)
: InitiateUploadRequestBase(sender,
callback,
content_type,
content_length),
url_generator_(url_generator),
parent_resource_id_(parent_resource_id),
title_(title) {
}
InitiateUploadNewFileRequest::~InitiateUploadNewFileRequest() {}
GURL InitiateUploadNewFileRequest::GetURL() const {
return url_generator_.GetInitiateUploadNewFileUrl(!modified_date_.is_null());
}
net::URLFetcher::RequestType
InitiateUploadNewFileRequest::GetRequestType() const {
return net::URLFetcher::POST;
}
bool InitiateUploadNewFileRequest::GetContentData(
std::string* upload_content_type,
std::string* upload_content) {
*upload_content_type = kContentTypeApplicationJson;
base::DictionaryValue root;
root.SetString("title", title_);
// Fill parent link.
scoped_ptr<base::ListValue> parents(new base::ListValue);
parents->Append(CreateParentValue(parent_resource_id_).release());
root.Set("parents", parents.release());
if (!modified_date_.is_null())
root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_));
if (!last_viewed_by_me_date_.is_null()) {
root.SetString("lastViewedByMeDate",
util::FormatTimeAsString(last_viewed_by_me_date_));
}
base::JSONWriter::Write(&root, upload_content);
DVLOG(1) << "InitiateUploadNewFile data: " << *upload_content_type << ", ["
<< *upload_content << "]";
return true;
}
//===================== InitiateUploadExistingFileRequest ====================
InitiateUploadExistingFileRequest::InitiateUploadExistingFileRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const std::string& content_type,
int64 content_length,
const std::string& resource_id,
const std::string& etag,
const InitiateUploadCallback& callback)
: InitiateUploadRequestBase(sender,
callback,
content_type,
content_length),
url_generator_(url_generator),
resource_id_(resource_id),
etag_(etag) {
}
InitiateUploadExistingFileRequest::~InitiateUploadExistingFileRequest() {}
GURL InitiateUploadExistingFileRequest::GetURL() const {
return url_generator_.GetInitiateUploadExistingFileUrl(
resource_id_, !modified_date_.is_null());
}
net::URLFetcher::RequestType
InitiateUploadExistingFileRequest::GetRequestType() const {
return net::URLFetcher::PUT;
}
std::vector<std::string>
InitiateUploadExistingFileRequest::GetExtraRequestHeaders() const {
std::vector<std::string> headers(
InitiateUploadRequestBase::GetExtraRequestHeaders());
headers.push_back(util::GenerateIfMatchHeader(etag_));
return headers;
}
bool InitiateUploadExistingFileRequest::GetContentData(
std::string* upload_content_type,
std::string* upload_content) {
base::DictionaryValue root;
if (!parent_resource_id_.empty()) {
scoped_ptr<base::ListValue> parents(new base::ListValue);
parents->Append(CreateParentValue(parent_resource_id_).release());
root.Set("parents", parents.release());
}
if (!title_.empty())
root.SetString("title", title_);
if (!modified_date_.is_null())
root.SetString("modifiedDate", util::FormatTimeAsString(modified_date_));
if (!last_viewed_by_me_date_.is_null()) {
root.SetString("lastViewedByMeDate",
util::FormatTimeAsString(last_viewed_by_me_date_));
}
if (root.empty())
return false;
*upload_content_type = kContentTypeApplicationJson;
base::JSONWriter::Write(&root, upload_content);
DVLOG(1) << "InitiateUploadExistingFile data: " << *upload_content_type
<< ", [" << *upload_content << "]";
return true;
}
//============================ ResumeUploadRequest ===========================
ResumeUploadRequest::ResumeUploadRequest(
RequestSender* sender,
const GURL& upload_location,
int64 start_position,
int64 end_position,
int64 content_length,
const std::string& content_type,
const base::FilePath& local_file_path,
const UploadRangeCallback& callback,
const ProgressCallback& progress_callback)
: ResumeUploadRequestBase(sender,
upload_location,
start_position,
end_position,
content_length,
content_type,
local_file_path),
callback_(callback),
progress_callback_(progress_callback) {
DCHECK(!callback_.is_null());
}
ResumeUploadRequest::~ResumeUploadRequest() {}
void ResumeUploadRequest::OnRangeRequestComplete(
const UploadRangeResponse& response,
scoped_ptr<base::Value> value) {
DCHECK(CalledOnValidThread());
ParseFileResourceWithUploadRangeAndRun(callback_, response, value.Pass());
}
void ResumeUploadRequest::OnURLFetchUploadProgress(
const net::URLFetcher* source, int64 current, int64 total) {
if (!progress_callback_.is_null())
progress_callback_.Run(current, total);
}
//========================== GetUploadStatusRequest ==========================
GetUploadStatusRequest::GetUploadStatusRequest(
RequestSender* sender,
const GURL& upload_url,
int64 content_length,
const UploadRangeCallback& callback)
: GetUploadStatusRequestBase(sender,
upload_url,
content_length),
callback_(callback) {
DCHECK(!callback.is_null());
}
GetUploadStatusRequest::~GetUploadStatusRequest() {}
void GetUploadStatusRequest::OnRangeRequestComplete(
const UploadRangeResponse& response,
scoped_ptr<base::Value> value) {
DCHECK(CalledOnValidThread());
ParseFileResourceWithUploadRangeAndRun(callback_, response, value.Pass());
}
//========================== DownloadFileRequest ==========================
DownloadFileRequest::DownloadFileRequest(
RequestSender* sender,
const DriveApiUrlGenerator& url_generator,
const std::string& resource_id,
const base::FilePath& output_file_path,
const DownloadActionCallback& download_action_callback,
const GetContentCallback& get_content_callback,
const ProgressCallback& progress_callback)
: DownloadFileRequestBase(
sender,
download_action_callback,
get_content_callback,
progress_callback,
url_generator.GenerateDownloadFileUrl(resource_id),
output_file_path) {
}
DownloadFileRequest::~DownloadFileRequest() {
}
} // namespace drive
} // namespace google_apis
|