| 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 CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ | 8 #ifndef CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ |
| 9 #define CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ | 9 #define CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ |
| 10 | 10 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 public RequestRegistry::Request, | 90 public RequestRegistry::Request, |
| 91 public net::URLFetcherDelegate { | 91 public net::URLFetcherDelegate { |
| 92 public: | 92 public: |
| 93 // AuthenticatedRequestInterface overrides. | 93 // AuthenticatedRequestInterface overrides. |
| 94 virtual void Start(const std::string& access_token, | 94 virtual void Start(const std::string& access_token, |
| 95 const std::string& custom_user_agent, | 95 const std::string& custom_user_agent, |
| 96 const ReAuthenticateCallback& callback) OVERRIDE; | 96 const ReAuthenticateCallback& callback) OVERRIDE; |
| 97 virtual base::WeakPtr<AuthenticatedRequestInterface> GetWeakPtr() OVERRIDE; | 97 virtual base::WeakPtr<AuthenticatedRequestInterface> GetWeakPtr() OVERRIDE; |
| 98 | 98 |
| 99 protected: | 99 protected: |
| 100 UrlFetchRequestBase( | 100 UrlFetchRequestBase(RequestSender* runner, |
| 101 RequestSender* runner, | 101 net::URLRequestContextGetter* url_request_context_getter); |
| 102 net::URLRequestContextGetter* url_request_context_getter); | |
| 103 // Use this constructor when you need to implement requests that take a | |
| 104 // drive file path (ex. for downloading and uploading). | |
| 105 // |url_request_context_getter| is used to initialize URLFetcher. | |
| 106 // TODO(satorux): Remove the drive file path hack. crbug.com/163296 | |
| 107 UrlFetchRequestBase( | |
| 108 RequestSender* runner, | |
| 109 net::URLRequestContextGetter* url_request_context_getter, | |
| 110 const base::FilePath& drive_file_path); | |
| 111 virtual ~UrlFetchRequestBase(); | 102 virtual ~UrlFetchRequestBase(); |
| 112 | 103 |
| 113 // Gets URL for the request. | 104 // Gets URL for the request. |
| 114 virtual GURL GetURL() const = 0; | 105 virtual GURL GetURL() const = 0; |
| 115 | 106 |
| 116 // Returns the request type. A derived class should override this method | 107 // Returns the request type. A derived class should override this method |
| 117 // for a request type other than HTTP GET. | 108 // for a request type other than HTTP GET. |
| 118 virtual net::URLFetcher::RequestType GetRequestType() const; | 109 virtual net::URLFetcher::RequestType GetRequestType() const; |
| 119 | 110 |
| 120 // Returns the extra HTTP headers for the request. A derived class should | 111 // Returns the extra HTTP headers for the request. A derived class should |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 protected: | 279 protected: |
| 289 // |callback| will be called with the upload URL, where upload data is | 280 // |callback| will be called with the upload URL, where upload data is |
| 290 // uploaded to with ResumeUploadRequestBase. | 281 // uploaded to with ResumeUploadRequestBase. |
| 291 // |callback| must not be null. | 282 // |callback| must not be null. |
| 292 // |content_type| and |content_length| should be the attributes of the | 283 // |content_type| and |content_length| should be the attributes of the |
| 293 // uploading file. | 284 // uploading file. |
| 294 InitiateUploadRequestBase( | 285 InitiateUploadRequestBase( |
| 295 RequestSender* runner, | 286 RequestSender* runner, |
| 296 net::URLRequestContextGetter* url_request_context_getter, | 287 net::URLRequestContextGetter* url_request_context_getter, |
| 297 const InitiateUploadCallback& callback, | 288 const InitiateUploadCallback& callback, |
| 298 const base::FilePath& drive_file_path, | |
| 299 const std::string& content_type, | 289 const std::string& content_type, |
| 300 int64 content_length); | 290 int64 content_length); |
| 301 virtual ~InitiateUploadRequestBase(); | 291 virtual ~InitiateUploadRequestBase(); |
| 302 | 292 |
| 303 // UrlFetchRequestBase overrides. | 293 // UrlFetchRequestBase overrides. |
| 304 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; | 294 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; |
| 305 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; | 295 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; |
| 306 virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE; | 296 virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE; |
| 307 | 297 |
| 308 private: | 298 private: |
| 309 const InitiateUploadCallback callback_; | 299 const InitiateUploadCallback callback_; |
| 310 const base::FilePath drive_file_path_; | |
| 311 const std::string content_type_; | 300 const std::string content_type_; |
| 312 const int64 content_length_; | 301 const int64 content_length_; |
| 313 | 302 |
| 314 DISALLOW_COPY_AND_ASSIGN(InitiateUploadRequestBase); | 303 DISALLOW_COPY_AND_ASSIGN(InitiateUploadRequestBase); |
| 315 }; | 304 }; |
| 316 | 305 |
| 317 //========================== UploadRangeRequestBase ========================== | 306 //========================== UploadRangeRequestBase ========================== |
| 318 | 307 |
| 319 // Struct for response to ResumeUpload and GetUploadStatus. | 308 // Struct for response to ResumeUpload and GetUploadStatus. |
| 320 struct UploadRangeResponse { | 309 struct UploadRangeResponse { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 334 int64 start_position_received; | 323 int64 start_position_received; |
| 335 int64 end_position_received; | 324 int64 end_position_received; |
| 336 }; | 325 }; |
| 337 | 326 |
| 338 // Base class for a URL fetch request expecting the response containing the | 327 // Base class for a URL fetch request expecting the response containing the |
| 339 // current uploading range. This class processes the response containing | 328 // current uploading range. This class processes the response containing |
| 340 // "Range" header and invoke OnRangeRequestComplete. | 329 // "Range" header and invoke OnRangeRequestComplete. |
| 341 class UploadRangeRequestBase : public UrlFetchRequestBase { | 330 class UploadRangeRequestBase : public UrlFetchRequestBase { |
| 342 protected: | 331 protected: |
| 343 // |upload_location| is the URL of where to upload the file to. | 332 // |upload_location| is the URL of where to upload the file to. |
| 344 // |drive_file_path| is the path to the file seen in the UI. Not necessary | |
| 345 // for resuming an upload, but used for adding an entry to RequestRegistry. | |
| 346 // TODO(satorux): Remove the drive file path hack. crbug.com/163296 | |
| 347 UploadRangeRequestBase( | 333 UploadRangeRequestBase( |
| 348 RequestSender* runner, | 334 RequestSender* runner, |
| 349 net::URLRequestContextGetter* url_request_context_getter, | 335 net::URLRequestContextGetter* url_request_context_getter, |
| 350 const base::FilePath& drive_file_path, | |
| 351 const GURL& upload_url); | 336 const GURL& upload_url); |
| 352 virtual ~UploadRangeRequestBase(); | 337 virtual ~UploadRangeRequestBase(); |
| 353 | 338 |
| 354 // UrlFetchRequestBase overrides. | 339 // UrlFetchRequestBase overrides. |
| 355 virtual GURL GetURL() const OVERRIDE; | 340 virtual GURL GetURL() const OVERRIDE; |
| 356 virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE; | 341 virtual net::URLFetcher::RequestType GetRequestType() const OVERRIDE; |
| 357 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; | 342 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; |
| 358 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; | 343 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; |
| 359 | 344 |
| 360 // This method will be called when the request is done, regardless of | 345 // This method will be called when the request is done, regardless of |
| (...skipping 12 matching lines...) Expand all Loading... |
| 373 // Note: Subclasses should have responsibility to run some callback | 358 // Note: Subclasses should have responsibility to run some callback |
| 374 // in this method to notify the finish status to its clients (or ignore it | 359 // in this method to notify the finish status to its clients (or ignore it |
| 375 // under its responsibility). | 360 // under its responsibility). |
| 376 virtual void OnRangeRequestComplete( | 361 virtual void OnRangeRequestComplete( |
| 377 const UploadRangeResponse& response, scoped_ptr<base::Value> value) = 0; | 362 const UploadRangeResponse& response, scoped_ptr<base::Value> value) = 0; |
| 378 | 363 |
| 379 private: | 364 private: |
| 380 // Called when ParseJson() is completed. | 365 // Called when ParseJson() is completed. |
| 381 void OnDataParsed(GDataErrorCode code, scoped_ptr<base::Value> value); | 366 void OnDataParsed(GDataErrorCode code, scoped_ptr<base::Value> value); |
| 382 | 367 |
| 383 const base::FilePath drive_file_path_; | |
| 384 const GURL upload_url_; | 368 const GURL upload_url_; |
| 385 | 369 |
| 386 // Note: This should remain the last member so it'll be destroyed and | 370 // Note: This should remain the last member so it'll be destroyed and |
| 387 // invalidate its weak pointers before any other members are destroyed. | 371 // invalidate its weak pointers before any other members are destroyed. |
| 388 base::WeakPtrFactory<UploadRangeRequestBase> weak_ptr_factory_; | 372 base::WeakPtrFactory<UploadRangeRequestBase> weak_ptr_factory_; |
| 389 DISALLOW_COPY_AND_ASSIGN(UploadRangeRequestBase); | 373 DISALLOW_COPY_AND_ASSIGN(UploadRangeRequestBase); |
| 390 }; | 374 }; |
| 391 | 375 |
| 392 //========================== ResumeUploadRequestBase ========================= | 376 //========================== ResumeUploadRequestBase ========================= |
| 393 | 377 |
| 394 // This class performs the request for resuming the upload of a file. | 378 // This class performs the request for resuming the upload of a file. |
| 395 // More specifically, this request uploads a chunk of data carried in |buf| | 379 // More specifically, this request uploads a chunk of data carried in |buf| |
| 396 // of ResumeUploadResponseBase. This class is designed to share the | 380 // of ResumeUploadResponseBase. This class is designed to share the |
| 397 // implementation of upload resuming between GData WAPI and Drive API v2. | 381 // implementation of upload resuming between GData WAPI and Drive API v2. |
| 398 // The subclasses should implement OnRangeRequestComplete inherited by | 382 // The subclasses should implement OnRangeRequestComplete inherited by |
| 399 // UploadRangeRequestBase, because the type of the response should be | 383 // UploadRangeRequestBase, because the type of the response should be |
| 400 // different (although the format in the server response is JSON). | 384 // different (although the format in the server response is JSON). |
| 401 class ResumeUploadRequestBase : public UploadRangeRequestBase { | 385 class ResumeUploadRequestBase : public UploadRangeRequestBase { |
| 402 protected: | 386 protected: |
| 403 // |start_position| is the start of range of contents currently stored in | 387 // |start_position| is the start of range of contents currently stored in |
| 404 // |buf|. |end_position| is the end of range of contents currently stared in | 388 // |buf|. |end_position| is the end of range of contents currently stared in |
| 405 // |buf|. This is exclusive. For instance, if you are to upload the first | 389 // |buf|. This is exclusive. For instance, if you are to upload the first |
| 406 // 500 bytes of data, |start_position| is 0 and |end_position| is 500. | 390 // 500 bytes of data, |start_position| is 0 and |end_position| is 500. |
| 407 // |content_length| and |content_type| are the length and type of the | 391 // |content_length| and |content_type| are the length and type of the |
| 408 // file content to be uploaded respectively. | 392 // file content to be uploaded respectively. |
| 409 // |buf| holds current content to be uploaded. | 393 // |buf| holds current content to be uploaded. |
| 410 // See also UploadRangeRequestBase's comment for remaining parameters | 394 // See also UploadRangeRequestBase's comment for remaining parameters |
| 411 // meaining. | 395 // meaning. |
| 412 ResumeUploadRequestBase( | 396 ResumeUploadRequestBase( |
| 413 RequestSender* runner, | 397 RequestSender* runner, |
| 414 net::URLRequestContextGetter* url_request_context_getter, | 398 net::URLRequestContextGetter* url_request_context_getter, |
| 415 const base::FilePath& drive_file_path, | |
| 416 const GURL& upload_location, | 399 const GURL& upload_location, |
| 417 int64 start_position, | 400 int64 start_position, |
| 418 int64 end_position, | 401 int64 end_position, |
| 419 int64 content_length, | 402 int64 content_length, |
| 420 const std::string& content_type, | 403 const std::string& content_type, |
| 421 const base::FilePath& local_file_path); | 404 const base::FilePath& local_file_path); |
| 422 virtual ~ResumeUploadRequestBase(); | 405 virtual ~ResumeUploadRequestBase(); |
| 423 | 406 |
| 424 // UrlFetchRequestBase overrides. | 407 // UrlFetchRequestBase overrides. |
| 425 virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE; | 408 virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 450 // for the uploaded data, if a file has been completely uploaded. | 433 // for the uploaded data, if a file has been completely uploaded. |
| 451 // See also UploadRangeRequestBase. | 434 // See also UploadRangeRequestBase. |
| 452 class GetUploadStatusRequestBase : public UploadRangeRequestBase { | 435 class GetUploadStatusRequestBase : public UploadRangeRequestBase { |
| 453 public: | 436 public: |
| 454 // |content_length| is the whole data size to be uploaded. | 437 // |content_length| is the whole data size to be uploaded. |
| 455 // See also UploadRangeRequestBase's constructor comment for other | 438 // See also UploadRangeRequestBase's constructor comment for other |
| 456 // parameters. | 439 // parameters. |
| 457 GetUploadStatusRequestBase( | 440 GetUploadStatusRequestBase( |
| 458 RequestSender* runner, | 441 RequestSender* runner, |
| 459 net::URLRequestContextGetter* url_request_context_getter, | 442 net::URLRequestContextGetter* url_request_context_getter, |
| 460 const base::FilePath& drive_file_path, | |
| 461 const GURL& upload_url, | 443 const GURL& upload_url, |
| 462 int64 content_length); | 444 int64 content_length); |
| 463 virtual ~GetUploadStatusRequestBase(); | 445 virtual ~GetUploadStatusRequestBase(); |
| 464 | 446 |
| 465 protected: | 447 protected: |
| 466 // UrlFetchRequestBase overrides. | 448 // UrlFetchRequestBase overrides. |
| 467 virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE; | 449 virtual std::vector<std::string> GetExtraRequestHeaders() const OVERRIDE; |
| 468 | 450 |
| 469 private: | 451 private: |
| 470 const int64 content_length_; | 452 const int64 content_length_; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 494 // This callback is called when some part of the content is | 476 // This callback is called when some part of the content is |
| 495 // read. Used to read the download content progressively. May be null. | 477 // read. Used to read the download content progressively. May be null. |
| 496 // | 478 // |
| 497 // progress_callback: | 479 // progress_callback: |
| 498 // This callback is called for periodically reporting the number of bytes | 480 // This callback is called for periodically reporting the number of bytes |
| 499 // downloaded so far. May be null. | 481 // downloaded so far. May be null. |
| 500 // | 482 // |
| 501 // download_url: | 483 // download_url: |
| 502 // Specifies the target file to download. | 484 // Specifies the target file to download. |
| 503 // | 485 // |
| 504 // drive_file_path: | |
| 505 // Specifies the drive path of the target file. Shown in UI. | |
| 506 // TODO(satorux): Remove the drive file path hack. crbug.com/163296 | |
| 507 // | |
| 508 // output_file_path: | 486 // output_file_path: |
| 509 // Specifies the file path to save the downloaded file. | 487 // Specifies the file path to save the downloaded file. |
| 510 // | 488 // |
| 511 DownloadFileRequest( | 489 DownloadFileRequest( |
| 512 RequestSender* runner, | 490 RequestSender* runner, |
| 513 net::URLRequestContextGetter* url_request_context_getter, | 491 net::URLRequestContextGetter* url_request_context_getter, |
| 514 const DownloadActionCallback& download_action_callback, | 492 const DownloadActionCallback& download_action_callback, |
| 515 const GetContentCallback& get_content_callback, | 493 const GetContentCallback& get_content_callback, |
| 516 const ProgressCallback& progress_callback, | 494 const ProgressCallback& progress_callback, |
| 517 const GURL& download_url, | 495 const GURL& download_url, |
| 518 const base::FilePath& drive_file_path, | |
| 519 const base::FilePath& output_file_path); | 496 const base::FilePath& output_file_path); |
| 520 virtual ~DownloadFileRequest(); | 497 virtual ~DownloadFileRequest(); |
| 521 | 498 |
| 522 protected: | 499 protected: |
| 523 // UrlFetchRequestBase overrides. | 500 // UrlFetchRequestBase overrides. |
| 524 virtual GURL GetURL() const OVERRIDE; | 501 virtual GURL GetURL() const OVERRIDE; |
| 525 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; | 502 virtual void ProcessURLFetchResults(const net::URLFetcher* source) OVERRIDE; |
| 526 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; | 503 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE; |
| 527 | 504 |
| 528 // net::URLFetcherDelegate overrides. | 505 // net::URLFetcherDelegate overrides. |
| 529 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, | 506 virtual void OnURLFetchDownloadProgress(const net::URLFetcher* source, |
| 530 int64 current, int64 total) OVERRIDE; | 507 int64 current, int64 total) OVERRIDE; |
| 531 virtual bool ShouldSendDownloadData() OVERRIDE; | 508 virtual bool ShouldSendDownloadData() OVERRIDE; |
| 532 virtual void OnURLFetchDownloadData( | 509 virtual void OnURLFetchDownloadData( |
| 533 const net::URLFetcher* source, | 510 const net::URLFetcher* source, |
| 534 scoped_ptr<std::string> download_data) OVERRIDE; | 511 scoped_ptr<std::string> download_data) OVERRIDE; |
| 535 | 512 |
| 536 private: | 513 private: |
| 537 const DownloadActionCallback download_action_callback_; | 514 const DownloadActionCallback download_action_callback_; |
| 538 const GetContentCallback get_content_callback_; | 515 const GetContentCallback get_content_callback_; |
| 539 const ProgressCallback progress_callback_; | 516 const ProgressCallback progress_callback_; |
| 540 const GURL download_url_; | 517 const GURL download_url_; |
| 541 | 518 |
| 542 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequest); | 519 DISALLOW_COPY_AND_ASSIGN(DownloadFileRequest); |
| 543 }; | 520 }; |
| 544 | 521 |
| 545 } // namespace google_apis | 522 } // namespace google_apis |
| 546 | 523 |
| 547 #endif // CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ | 524 #endif // CHROME_BROWSER_GOOGLE_APIS_BASE_REQUESTS_H_ |
| OLD | NEW |