| Index: chrome/browser/google_apis/gdata_wapi_operations.cc
|
| diff --git a/chrome/browser/google_apis/gdata_wapi_operations.cc b/chrome/browser/google_apis/gdata_wapi_operations.cc
|
| index d3ddc261a7807252e58857c44ee1466a9a5f9e54..f0e78f03aa9ede958c5a737437f3522be4dae23b 100644
|
| --- a/chrome/browser/google_apis/gdata_wapi_operations.cc
|
| +++ b/chrome/browser/google_apis/gdata_wapi_operations.cc
|
| @@ -88,6 +88,7 @@ ResumeUploadParams::ResumeUploadParams(
|
| int64 content_length,
|
| const std::string& content_type,
|
| scoped_refptr<net::IOBuffer> buf,
|
| + int64 buffer_offset,
|
| const GURL& upload_location,
|
| const FilePath& drive_file_path) : upload_mode(upload_mode),
|
| start_position(start_position),
|
| @@ -95,6 +96,7 @@ ResumeUploadParams::ResumeUploadParams(
|
| content_length(content_length),
|
| content_type(content_type),
|
| buf(buf),
|
| + buffer_offset(buffer_offset),
|
| upload_location(upload_location),
|
| drive_file_path(drive_file_path) {
|
| }
|
| @@ -670,34 +672,14 @@ bool InitiateUploadOperation::GetContentData(std::string* upload_content_type,
|
| return true;
|
| }
|
|
|
| -//============================ ResumeUploadOperation ===========================
|
| +namespace {
|
|
|
| -ResumeUploadOperation::ResumeUploadOperation(
|
| - OperationRegistry* registry,
|
| - net::URLRequestContextGetter* url_request_context_getter,
|
| +bool ProcessURLFetchResultsImpl(
|
| + GDataErrorCode code,
|
| + const URLFetcher* source,
|
| + const FilePath& drive_file_path,
|
| const ResumeUploadCallback& callback,
|
| - const ResumeUploadParams& params)
|
| - : UrlFetchOperationBase(registry,
|
| - url_request_context_getter,
|
| - OPERATION_UPLOAD,
|
| - params.drive_file_path),
|
| - callback_(callback),
|
| - params_(params),
|
| - last_chunk_completed_(false),
|
| - ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
|
| - DCHECK(!callback_.is_null());
|
| -}
|
| -
|
| -ResumeUploadOperation::~ResumeUploadOperation() {}
|
| -
|
| -GURL ResumeUploadOperation::GetURL() const {
|
| - // This is very tricky to get json from this operation. To do that, &alt=json
|
| - // has to be appended not here but in InitiateUploadOperation::GetURL().
|
| - return params_.upload_location;
|
| -}
|
| -
|
| -void ResumeUploadOperation::ProcessURLFetchResults(const URLFetcher* source) {
|
| - GDataErrorCode code = GetErrorCode(source);
|
| + const ParseJsonCallback& json_callback) {
|
| net::HttpResponseHeaders* hdrs = source->GetResponseHeaders();
|
|
|
| if (code == HTTP_RESUME_INCOMPLETE) {
|
| @@ -719,44 +701,37 @@ void ResumeUploadOperation::ProcessURLFetchResults(const URLFetcher* source) {
|
| end_position_received = ranges[0].last_byte_position() + 1;
|
| }
|
| }
|
| - DVLOG(1) << "Got response for [" << params_.drive_file_path.value()
|
| + DVLOG(1) << "Got response for [" << drive_file_path.value()
|
| << "]: code=" << code
|
| << ", range_hdr=[" << range_received
|
| << "], range_parsed=" << start_position_received
|
| << "," << end_position_received;
|
|
|
| - callback_.Run(ResumeUploadResponse(code,
|
| - start_position_received,
|
| - end_position_received),
|
| - scoped_ptr<ResourceEntry>());
|
| + callback.Run(ResumeUploadResponse(code,
|
| + start_position_received,
|
| + end_position_received),
|
| + scoped_ptr<ResourceEntry>());
|
| + return true;
|
| + }
|
|
|
| - OnProcessURLFetchResultsComplete(true);
|
| - } else {
|
| - // There might be explanation of unexpected error code in response.
|
| - std::string response_content;
|
| - source->GetResponseAsString(&response_content);
|
| - DVLOG(1) << "Got response for [" << params_.drive_file_path.value()
|
| - << "]: code=" << code
|
| - << ", content=[\n" << response_content << "\n]";
|
| + // There might be explanation of unexpected error code in response.
|
| + std::string response_content;
|
| + source->GetResponseAsString(&response_content);
|
| + DVLOG(1) << "Got response for [" << drive_file_path.value()
|
| + << "]: code=" << code
|
| + << ", content=[\n" << response_content << "\n]";
|
|
|
| - ParseJson(response_content,
|
| - base::Bind(&ResumeUploadOperation::OnDataParsed,
|
| - weak_ptr_factory_.GetWeakPtr(),
|
| - code));
|
| - }
|
| + ParseJson(response_content, json_callback);
|
| + return false;
|
| }
|
|
|
| -void ResumeUploadOperation::OnDataParsed(GDataErrorCode code,
|
| - scoped_ptr<base::Value> value) {
|
| +bool OnDataParsedImpl(
|
| + UploadMode upload_mode,
|
| + const ResumeUploadCallback& callback,
|
| + GDataErrorCode code,
|
| + scoped_ptr<base::Value> value) {
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
|
|
| - // For a new file, HTTP_CREATED is returned.
|
| - // For an existing file, HTTP_SUCCESS is returned.
|
| - if ((params_.upload_mode == UPLOAD_NEW_FILE && code == HTTP_CREATED) ||
|
| - (params_.upload_mode == UPLOAD_EXISTING_FILE && code == HTTP_SUCCESS)) {
|
| - last_chunk_completed_ = true;
|
| - }
|
| -
|
| scoped_ptr<ResourceEntry> entry;
|
| if (value.get())
|
| entry = ResourceEntry::ExtractAndParse(*(value.get()));
|
| @@ -764,7 +739,57 @@ void ResumeUploadOperation::OnDataParsed(GDataErrorCode code,
|
| if (!entry.get())
|
| LOG(WARNING) << "Invalid entry received on upload.";
|
|
|
| - callback_.Run(ResumeUploadResponse(code, -1, -1), entry.Pass());
|
| + callback.Run(ResumeUploadResponse(code, -1, -1), entry.Pass());
|
| +
|
| + // For a new file, HTTP_CREATED is returned.
|
| + // For an existing file, HTTP_SUCCESS is returned.
|
| + return (upload_mode == UPLOAD_NEW_FILE && code == HTTP_CREATED) ||
|
| + (upload_mode == UPLOAD_EXISTING_FILE && code == HTTP_SUCCESS);
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +//============================ ResumeUploadOperation ===========================
|
| +
|
| +ResumeUploadOperation::ResumeUploadOperation(
|
| + OperationRegistry* registry,
|
| + net::URLRequestContextGetter* url_request_context_getter,
|
| + const ResumeUploadCallback& callback,
|
| + const ResumeUploadParams& params)
|
| + : UrlFetchOperationBase(registry,
|
| + url_request_context_getter,
|
| + OPERATION_UPLOAD,
|
| + params.drive_file_path),
|
| + callback_(callback),
|
| + params_(params),
|
| + last_chunk_completed_(false),
|
| + ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
|
| + DCHECK(!callback_.is_null());
|
| +}
|
| +
|
| +ResumeUploadOperation::~ResumeUploadOperation() {}
|
| +
|
| +GURL ResumeUploadOperation::GetURL() const {
|
| + // This is very tricky to get json from this operation. To do that, &alt=json
|
| + // has to be appended not here but in InitiateUploadOperation::GetURL().
|
| + return params_.upload_location;
|
| +}
|
| +
|
| +void ResumeUploadOperation::ProcessURLFetchResults(const URLFetcher* source) {
|
| + GDataErrorCode code = GetErrorCode(source);
|
| + if (ProcessURLFetchResultsImpl(
|
| + code, source, params_.drive_file_path, callback_,
|
| + base::Bind(&ResumeUploadOperation::OnDataParsed,
|
| + weak_ptr_factory_.GetWeakPtr(),
|
| + code))) {
|
| + OnProcessURLFetchResultsComplete(true);
|
| + }
|
| +}
|
| +
|
| +void ResumeUploadOperation::OnDataParsed(GDataErrorCode code,
|
| + scoped_ptr<base::Value> value) {
|
| + last_chunk_completed_ = OnDataParsedImpl(
|
| + params_.upload_mode, callback_, code, value.Pass());
|
| OnProcessURLFetchResultsComplete(last_chunk_completed_);
|
| }
|
|
|
| @@ -819,7 +844,7 @@ std::vector<std::string> ResumeUploadOperation::GetExtraRequestHeaders() const {
|
| bool ResumeUploadOperation::GetContentData(std::string* upload_content_type,
|
| std::string* upload_content) {
|
| *upload_content_type = params_.content_type;
|
| - *upload_content = std::string(params_.buf->data(),
|
| + *upload_content = std::string(params_.buf->data() + params_.buffer_offset,
|
| params_.end_position - params_.start_position);
|
| return true;
|
| }
|
| @@ -830,4 +855,75 @@ void ResumeUploadOperation::OnURLFetchUploadProgress(
|
| NotifyProgress(params_.start_position + current, params_.content_length);
|
| }
|
|
|
| +//============================ GetUploadStateOperation =========================
|
| +
|
| +GetUploadStateOperation::GetUploadStateOperation(
|
| + OperationRegistry* registry,
|
| + net::URLRequestContextGetter* url_request_context_getter,
|
| + const ResumeUploadCallback& callback,
|
| + const UploadMode upload_mode,
|
| + const FilePath& drive_file_path,
|
| + const GURL& upload_url,
|
| + int64 content_length)
|
| + : UrlFetchOperationBase(registry, url_request_context_getter),
|
| + callback_(callback),
|
| + upload_mode_(upload_mode),
|
| + drive_file_path_(drive_file_path),
|
| + upload_url_(upload_url),
|
| + content_length_(content_length),
|
| + ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
|
| +}
|
| +
|
| +GetUploadStateOperation::~GetUploadStateOperation() {
|
| +}
|
| +
|
| +GURL GetUploadStateOperation::GetURL() const {
|
| + return upload_url_;
|
| +}
|
| +
|
| +net::URLFetcher::RequestType GetUploadStateOperation::GetRequestType() const {
|
| + return net::URLFetcher::PUT_WITHOUT_BODY;
|
| +}
|
| +
|
| +void GetUploadStateOperation::ProcessURLFetchResults(
|
| + const net::URLFetcher* source) {
|
| + GDataErrorCode code = GetErrorCode(source);
|
| + if (ProcessURLFetchResultsImpl(
|
| + code, source, drive_file_path_, callback_,
|
| + base::Bind(&GetUploadStateOperation::OnDataParsed,
|
| + weak_ptr_factory_.GetWeakPtr(),
|
| + code))) {
|
| + OnProcessURLFetchResultsComplete(true);
|
| + }
|
| +}
|
| +
|
| +void GetUploadStateOperation::RunCallbackOnPrematureFailure(
|
| + GDataErrorCode code) {
|
| + callback_.Run(ResumeUploadResponse(code, 0, 0), scoped_ptr<ResourceEntry>());
|
| +}
|
| +
|
| +std::vector<std::string>
|
| +GetUploadStateOperation::GetExtraRequestHeaders() const {
|
| + // The header looks like
|
| + // Content-Range: bytes */<content_length>
|
| + // for example:
|
| + // Content-Range: bytes */13851821
|
| + // Use * for unknown/streaming content length.
|
| + // The header takes inclusive range, so we adjust by "end_position - 1".
|
| + DCHECK_GE(content_length_, -1);
|
| +
|
| + std::vector<std::string> headers;
|
| + headers.push_back(
|
| + std::string(kUploadContentRange) + "*/" +
|
| + (content_length_ == -1 ? "*" : base::Int64ToString(content_length_)));
|
| + return headers;
|
| +}
|
| +
|
| +void GetUploadStateOperation::OnDataParsed(
|
| + GDataErrorCode code, scoped_ptr<base::Value> value) {
|
| + OnProcessURLFetchResultsComplete(
|
| + OnDataParsedImpl(
|
| + upload_mode_, callback_, code, value.Pass()));
|
| +}
|
| +
|
| } // namespace google_apis
|
|
|