| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "chrome/browser/extensions/blob_reader.h" | 5 #include "chrome/browser/extensions/blob_reader.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "net/http/http_request_headers.h" | 12 #include "net/http/http_request_headers.h" |
| 13 #include "net/http/http_response_headers.h" | 13 #include "net/http/http_response_headers.h" |
| 14 #include "net/url_request/url_fetcher.h" | 14 #include "net/url_request/url_fetcher.h" |
| 15 #include "net/url_request/url_request_context.h" | 15 #include "net/url_request/url_request_context.h" |
| 16 #include "net/url_request/url_request_context_getter.h" | 16 #include "net/url_request/url_request_context_getter.h" |
| 17 | 17 |
| 18 BlobReader::BlobReader(Profile* profile, | 18 BlobReader::BlobReader(Profile* profile, |
| 19 const std::string& blob_uuid, | 19 const std::string& blob_uuid, |
| 20 BlobReadCallback callback) | 20 BlobReadCallback callback) |
| 21 : callback_(callback) { | 21 : callback_(callback) { |
| 22 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 22 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 23 GURL blob_url; | 23 GURL blob_url; |
| 24 if (StartsWithASCII(blob_uuid, "blob:blobinternal", true)) { | 24 if (StartsWithASCII(blob_uuid, "blob:blobinternal", true)) { |
| 25 // TODO(michaeln): remove support for deprecated blob urls | 25 // TODO(michaeln): remove support for deprecated blob urls |
| 26 blob_url = GURL(blob_uuid); | 26 blob_url = GURL(blob_uuid); |
| 27 } else { | 27 } else { |
| 28 blob_url = GURL(std::string("blob:uuid/") + blob_uuid); | 28 blob_url = GURL(std::string("blob:uuid/") + blob_uuid); |
| 29 } | 29 } |
| 30 DCHECK(blob_url.is_valid()); | 30 DCHECK(blob_url.is_valid()); |
| 31 fetcher_.reset(net::URLFetcher::Create( | 31 fetcher_.reset(net::URLFetcher::Create( |
| 32 blob_url, net::URLFetcher::GET, | 32 blob_url, net::URLFetcher::GET, |
| 33 this)); | 33 this)); |
| 34 fetcher_->SetRequestContext(profile->GetRequestContext()); | 34 fetcher_->SetRequestContext(profile->GetRequestContext()); |
| 35 } | 35 } |
| 36 | 36 |
| 37 BlobReader::~BlobReader() { | 37 BlobReader::~BlobReader() { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); } |
| 38 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 39 } | |
| 40 | 38 |
| 41 void BlobReader::SetByteRange(int64 offset, int64 length) { | 39 void BlobReader::SetByteRange(int64 offset, int64 length) { |
| 42 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 43 CHECK_GE(offset, 0); | 41 CHECK_GE(offset, 0); |
| 44 CHECK_GT(length, 0); | 42 CHECK_GT(length, 0); |
| 45 CHECK_LE(offset, kint64max - length); | 43 CHECK_LE(offset, kint64max - length); |
| 46 | 44 |
| 47 net::HttpRequestHeaders headers; | 45 net::HttpRequestHeaders headers; |
| 48 headers.SetHeader( | 46 headers.SetHeader( |
| 49 net::HttpRequestHeaders::kRange, | 47 net::HttpRequestHeaders::kRange, |
| 50 base::StringPrintf("bytes=%" PRId64 "-%" PRId64, offset, | 48 base::StringPrintf("bytes=%" PRId64 "-%" PRId64, offset, |
| 51 offset + length - 1)); | 49 offset + length - 1)); |
| 52 fetcher_->SetExtraRequestHeaders(headers.ToString()); | 50 fetcher_->SetExtraRequestHeaders(headers.ToString()); |
| 53 } | 51 } |
| 54 | 52 |
| 55 void BlobReader::Start() { | 53 void BlobReader::Start() { |
| 56 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 54 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 57 fetcher_->Start(); | 55 fetcher_->Start(); |
| 58 } | 56 } |
| 59 | 57 |
| 60 // Overridden from net::URLFetcherDelegate. | 58 // Overridden from net::URLFetcherDelegate. |
| 61 void BlobReader::OnURLFetchComplete(const net::URLFetcher* source) { | 59 void BlobReader::OnURLFetchComplete(const net::URLFetcher* source) { |
| 62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 63 scoped_ptr<std::string> response(new std::string); | 61 scoped_ptr<std::string> response(new std::string); |
| 64 int64 first = 0, last = 0, length = 0; | 62 int64 first = 0, last = 0, length = 0; |
| 65 source->GetResponseAsString(response.get()); | 63 source->GetResponseAsString(response.get()); |
| 66 source->GetResponseHeaders()->GetContentRange(&first, &last, &length); | 64 source->GetResponseHeaders()->GetContentRange(&first, &last, &length); |
| 67 callback_.Run(response.Pass(), length); | 65 callback_.Run(response.Pass(), length); |
| 68 | 66 |
| 69 delete this; | 67 delete this; |
| 70 } | 68 } |
| OLD | NEW |