| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // This file provides base classes used to issue HTTP requests for Google | 5 // This file provides base classes used to issue HTTP requests for Google |
| 6 // APIs. | 6 // APIs. |
| 7 | 7 |
| 8 #ifndef GOOGLE_APIS_DRIVE_BASE_REQUESTS_H_ | 8 #ifndef GOOGLE_APIS_DRIVE_BASE_REQUESTS_H_ |
| 9 #define GOOGLE_APIS_DRIVE_BASE_REQUESTS_H_ | 9 #define GOOGLE_APIS_DRIVE_BASE_REQUESTS_H_ |
| 10 | 10 |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 | 253 |
| 254 base::ThreadChecker thread_checker_; | 254 base::ThreadChecker thread_checker_; |
| 255 | 255 |
| 256 // Note: This should remain the last member so it'll be destroyed and | 256 // Note: This should remain the last member so it'll be destroyed and |
| 257 // invalidate its weak pointers before any other members are destroyed. | 257 // invalidate its weak pointers before any other members are destroyed. |
| 258 base::WeakPtrFactory<UrlFetchRequestBase> weak_ptr_factory_; | 258 base::WeakPtrFactory<UrlFetchRequestBase> weak_ptr_factory_; |
| 259 | 259 |
| 260 DISALLOW_COPY_AND_ASSIGN(UrlFetchRequestBase); | 260 DISALLOW_COPY_AND_ASSIGN(UrlFetchRequestBase); |
| 261 }; | 261 }; |
| 262 | 262 |
| 263 //============================ BatchableRequestBase ============================ | 263 //============================ BatchableDelegate ============================ |
| 264 | 264 |
| 265 class BatchableRequestBase : public UrlFetchRequestBase { | 265 // Delegate to be used by |SingleBatchableDelegateRequest| and |
| 266 // |BatchUploadRequest|. |
| 267 class BatchableDelegate { |
| 266 public: | 268 public: |
| 267 explicit BatchableRequestBase(RequestSender* sender) : | 269 virtual ~BatchableDelegate() {} |
| 268 UrlFetchRequestBase(sender) {} | |
| 269 | 270 |
| 270 GURL GetURL() const override = 0; | 271 // See UrlFetchRequestBase. |
| 271 net::URLFetcher::RequestType GetRequestType() const override; | 272 virtual GURL GetURL() const = 0; |
| 272 std::vector<std::string> GetExtraRequestHeaders() const override; | 273 virtual net::URLFetcher::RequestType GetRequestType() const = 0; |
| 273 void Prepare(const PrepareCallback& callback) override; | 274 virtual std::vector<std::string> GetExtraRequestHeaders() const = 0; |
| 274 bool GetContentData(std::string* upload_content_type, | 275 virtual void Prepare(const PrepareCallback& callback) = 0; |
| 275 std::string* upload_content) override; | 276 virtual bool GetContentData(std::string* upload_content_type, |
| 276 void RunCallbackOnPrematureFailure(DriveApiErrorCode code) override = 0; | 277 std::string* upload_content) = 0; |
| 277 virtual void ProcessURLFetchResults( | |
| 278 DriveApiErrorCode code, const std::string& body) = 0; | |
| 279 | 278 |
| 280 private: | 279 // Notifies result of the request. Usually, it parses the |code| and |
| 281 void ProcessURLFetchResults(const net::URLFetcher* source) final; | 280 // |response_body|, then notifies the parsed value to client code of the |
| 281 // API. |callback| must be called on completion. The instance must not |
| 282 // do anything after calling |callback| since the instance may be deleted in |
| 283 // |callback|. |
| 284 virtual void NotifyResult(DriveApiErrorCode code, |
| 285 const std::string& response_body, |
| 286 const base::Closure& callback) = 0; |
| 287 |
| 288 // Notifies error. Unlike |NotifyResult|, it must report error |
| 289 // synchronously. The instance may be deleted just after calling |
| 290 // NotifyError. |
| 291 virtual void NotifyError(DriveApiErrorCode code) = 0; |
| 292 |
| 293 // Notifies progress. |
| 294 virtual void NotifyUploadProgress(const net::URLFetcher* source, |
| 295 int64 current, |
| 296 int64 total) = 0; |
| 282 }; | 297 }; |
| 283 | 298 |
| 284 //============================ EntryActionRequest ============================ | 299 //============================ EntryActionRequest ============================ |
| 285 | 300 |
| 286 // Callback type for requests that return only error status, like: Delete/Move. | 301 // Callback type for requests that return only error status, like: Delete/Move. |
| 287 typedef base::Callback<void(DriveApiErrorCode error)> EntryActionCallback; | 302 typedef base::Callback<void(DriveApiErrorCode error)> EntryActionCallback; |
| 288 | 303 |
| 289 // This class performs a simple action over a given entry (document/file). | 304 // This class performs a simple action over a given entry (document/file). |
| 290 // It is meant to be used for requests that return no JSON blobs. | 305 // It is meant to be used for requests that return no JSON blobs. |
| 291 class EntryActionRequest : public UrlFetchRequestBase { | 306 class EntryActionRequest : public UrlFetchRequestBase { |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 private: | 506 private: |
| 492 const int64 content_length_; | 507 const int64 content_length_; |
| 493 | 508 |
| 494 DISALLOW_COPY_AND_ASSIGN(GetUploadStatusRequestBase); | 509 DISALLOW_COPY_AND_ASSIGN(GetUploadStatusRequestBase); |
| 495 }; | 510 }; |
| 496 | 511 |
| 497 //=========================== MultipartUploadRequestBase======================= | 512 //=========================== MultipartUploadRequestBase======================= |
| 498 | 513 |
| 499 // This class provides base implementation for performing the request for | 514 // This class provides base implementation for performing the request for |
| 500 // uploading a file by multipart body. | 515 // uploading a file by multipart body. |
| 501 class MultipartUploadRequestBase : public BatchableRequestBase { | 516 class MultipartUploadRequestBase : public BatchableDelegate { |
| 502 public: | 517 public: |
| 503 // Set boundary. Only tests can use this method. | 518 // Set boundary. Only tests can use this method. |
| 504 void SetBoundaryForTesting(const std::string& boundary); | 519 void SetBoundaryForTesting(const std::string& boundary); |
| 505 | 520 |
| 506 protected: | 521 protected: |
| 507 // |callback| will be called with the file resource.upload URL. | 522 // |callback| will be called with the file resource.upload URL. |
| 508 // |content_type| and |content_length| should be the attributes of the | 523 // |content_type| and |content_length| should be the attributes of the |
| 509 // uploading file. Other parameters are optional and can be empty or null | 524 // uploading file. Other parameters are optional and can be empty or null |
| 510 // depending on Upload URL provided by the subclasses. | 525 // depending on Upload URL provided by the subclasses. |
| 511 MultipartUploadRequestBase(RequestSender* sender, | 526 MultipartUploadRequestBase(base::SequencedTaskRunner* blocking_task_runner, |
| 512 const std::string& metadata_json, | 527 const std::string& metadata_json, |
| 513 const std::string& content_type, | 528 const std::string& content_type, |
| 514 int64 content_length, | 529 int64 content_length, |
| 515 const base::FilePath& local_file_path, | 530 const base::FilePath& local_file_path, |
| 516 const FileResourceCallback& callback, | 531 const FileResourceCallback& callback, |
| 517 const ProgressCallback& progress_callback); | 532 const ProgressCallback& progress_callback); |
| 518 ~MultipartUploadRequestBase() override; | 533 ~MultipartUploadRequestBase() override; |
| 519 | 534 |
| 520 // Overridden from UrlFetchRequestBase. | 535 // BatchableDelegate. |
| 536 std::vector<std::string> GetExtraRequestHeaders() const override; |
| 521 void Prepare(const PrepareCallback& callback) override; | 537 void Prepare(const PrepareCallback& callback) override; |
| 522 bool GetContentData(std::string* upload_content_type, | 538 bool GetContentData(std::string* upload_content_type, |
| 523 std::string* upload_content) override; | 539 std::string* upload_content) override; |
| 524 void ProcessURLFetchResults( | 540 void NotifyResult(DriveApiErrorCode code, |
| 525 DriveApiErrorCode code, const std::string& body) override; | 541 const std::string& body, |
| 526 void RunCallbackOnPrematureFailure(DriveApiErrorCode code) override; | 542 const base::Closure& callback) override; |
| 527 | 543 void NotifyError(DriveApiErrorCode code) override; |
| 528 // content::UrlFetcherDelegate overrides. | 544 void NotifyUploadProgress(const net::URLFetcher* source, |
| 529 void OnURLFetchUploadProgress(const net::URLFetcher* source, | 545 int64 current, |
| 530 int64 current, | 546 int64 total) override; |
| 531 int64 total) override; | |
| 532 | |
| 533 // Parses the response value and invokes |callback_| with |FileResource|. | 547 // Parses the response value and invokes |callback_| with |FileResource|. |
| 534 void OnDataParsed(DriveApiErrorCode code, scoped_ptr<base::Value> value); | 548 void OnDataParsed(DriveApiErrorCode code, |
| 549 const base::Closure& callback, |
| 550 scoped_ptr<base::Value> value); |
| 535 | 551 |
| 536 private: | 552 private: |
| 537 // Continues to rest part of |Start| method after determining boundary string | 553 // Continues to rest part of |Start| method after determining boundary string |
| 538 // of multipart/related. | 554 // of multipart/related. |
| 539 void OnPrepareUploadContent(const PrepareCallback& callback, | 555 void OnPrepareUploadContent(const PrepareCallback& callback, |
| 540 std::string* upload_content_type, | 556 std::string* upload_content_type, |
| 541 std::string* upload_content_data, | 557 std::string* upload_content_data, |
| 542 bool result); | 558 bool result); |
| 543 | 559 |
| 560 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 544 const std::string metadata_json_; | 561 const std::string metadata_json_; |
| 545 const std::string content_type_; | 562 const std::string content_type_; |
| 546 const base::FilePath local_path_; | 563 const base::FilePath local_path_; |
| 547 const FileResourceCallback callback_; | 564 const FileResourceCallback callback_; |
| 548 const ProgressCallback progress_callback_; | 565 const ProgressCallback progress_callback_; |
| 549 | 566 |
| 550 // Boundary of multipart body. | 567 // Boundary of multipart body. |
| 551 std::string boundary_; | 568 std::string boundary_; |
| 552 | 569 |
| 553 // Upload content of multipart body. | 570 // Upload content of multipart body. |
| 554 std::string upload_content_type_; | 571 std::string upload_content_type_; |
| 555 std::string upload_content_data_; | 572 std::string upload_content_data_; |
| 556 | 573 |
| 574 base::ThreadChecker thread_checker_; |
| 575 |
| 557 // Note: This should remain the last member so it'll be destroyed and | 576 // Note: This should remain the last member so it'll be destroyed and |
| 558 // invalidate its weak pointers before any other members are destroyed. | 577 // invalidate its weak pointers before any other members are destroyed. |
| 559 base::WeakPtrFactory<MultipartUploadRequestBase> weak_ptr_factory_; | 578 base::WeakPtrFactory<MultipartUploadRequestBase> weak_ptr_factory_; |
| 560 | 579 |
| 561 DISALLOW_COPY_AND_ASSIGN(MultipartUploadRequestBase); | 580 DISALLOW_COPY_AND_ASSIGN(MultipartUploadRequestBase); |
| 562 }; | 581 }; |
| 563 | 582 |
| 564 //============================ DownloadFileRequest =========================== | 583 //============================ DownloadFileRequest =========================== |
| 565 | 584 |
| 566 // Callback type for receiving the completion of DownloadFileRequest. | 585 // Callback type for receiving the completion of DownloadFileRequest. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 const ProgressCallback progress_callback_; | 635 const ProgressCallback progress_callback_; |
| 617 const GURL download_url_; | 636 const GURL download_url_; |
| 618 const base::FilePath output_file_path_; | 637 const base::FilePath output_file_path_; |
| 619 | 638 |
| 620 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequestBase); | 639 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequestBase); |
| 621 }; | 640 }; |
| 622 | 641 |
| 623 } // namespace google_apis | 642 } // namespace google_apis |
| 624 | 643 |
| 625 #endif // GOOGLE_APIS_DRIVE_BASE_REQUESTS_H_ | 644 #endif // GOOGLE_APIS_DRIVE_BASE_REQUESTS_H_ |
| OLD | NEW |