Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Unified Diff: ios/web/public/image_fetcher/image_data_fetcher.mm

Issue 2701843002: Remove ImageDataFetcher from iOS. (Closed)
Patch Set: Remove mock Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ios/web/public/image_fetcher/image_data_fetcher.mm
diff --git a/ios/web/public/image_fetcher/image_data_fetcher.mm b/ios/web/public/image_fetcher/image_data_fetcher.mm
deleted file mode 100644
index 11a237a12ce0908b0f2f66a4d39b7f48e477dd93..0000000000000000000000000000000000000000
--- a/ios/web/public/image_fetcher/image_data_fetcher.mm
+++ /dev/null
@@ -1,179 +0,0 @@
-// Copyright 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.
-
-#import "ios/web/public/image_fetcher/image_data_fetcher.h"
-
-#import <Foundation/Foundation.h>
-#include <stddef.h>
-
-#include "base/bind.h"
-#include "base/location.h"
-#import "base/mac/scoped_nsobject.h"
-#include "base/task_runner.h"
-#include "base/task_runner_util.h"
-#import "ios/web/public/image_fetcher/webp_decoder.h"
-#include "net/base/load_flags.h"
-#include "net/http/http_response_headers.h"
-#include "net/url_request/url_fetcher.h"
-#include "net/url_request/url_request_context_getter.h"
-
-#if !defined(__has_feature) || !__has_feature(objc_arc)
-#error "This file requires ARC support."
-#endif
-
-namespace {
-
-class WebpDecoderDelegate : public webp_transcode::WebpDecoder::Delegate {
- public:
- NSData* data() const { return decoded_image_; }
-
- // WebpDecoder::Delegate methods
- void OnFinishedDecoding(bool success) override {
- if (!success)
- decoded_image_.reset();
- }
- void SetImageFeatures(
- size_t total_size,
- webp_transcode::WebpDecoder::DecodedImageFormat format) override {
- decoded_image_.reset([[NSMutableData alloc] initWithCapacity:total_size]);
- }
- void OnDataDecoded(NSData* data) override {
- DCHECK(decoded_image_);
- [decoded_image_ appendData:data];
- }
-
- private:
- ~WebpDecoderDelegate() override {}
- base::scoped_nsobject<NSMutableData> decoded_image_;
-};
-
-// Content-type header for WebP images.
-static const char kWEBPMimeType[] = "image/webp";
-
-// Returns a NSData object containing the decoded image.
-// Returns nil in case of failure.
-base::scoped_nsobject<NSData> DecodeWebpImage(
- const base::scoped_nsobject<NSData>& webp_image) {
- scoped_refptr<WebpDecoderDelegate> delegate(new WebpDecoderDelegate);
- scoped_refptr<webp_transcode::WebpDecoder> decoder(
- new webp_transcode::WebpDecoder(delegate.get()));
- decoder->OnDataReceived(webp_image);
- DLOG_IF(ERROR, !delegate->data()) << "WebP image decoding failed.";
- return base::scoped_nsobject<NSData>(delegate->data());
-}
-
-} // namespace
-
-namespace web {
-
-ImageDataFetcher::ImageDataFetcher(
- const scoped_refptr<base::TaskRunner>& task_runner)
- : request_context_getter_(nullptr),
- task_runner_(task_runner),
- weak_factory_(this) {
- DCHECK(task_runner_.get());
-}
-
-ImageDataFetcher::~ImageDataFetcher() {
- // Delete all the entries in the |downloads_in_progress_| map. This will in
- // turn cancel all of the requests.
- for (const auto& pair : downloads_in_progress_) {
- delete pair.first;
- }
-}
-
-void ImageDataFetcher::StartDownload(
- const GURL& url,
- ImageFetchedCallback callback,
- const std::string& referrer,
- net::URLRequest::ReferrerPolicy referrer_policy) {
- DCHECK(request_context_getter_.get());
- net::URLFetcher* fetcher =
- net::URLFetcher::Create(url, net::URLFetcher::GET, this).release();
- downloads_in_progress_[fetcher] = [callback copy];
- fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
- net::LOAD_DO_NOT_SAVE_COOKIES |
- net::LOAD_DO_NOT_SEND_AUTH_DATA);
- fetcher->SetRequestContext(request_context_getter_.get());
- fetcher->SetReferrer(referrer);
- fetcher->SetReferrerPolicy(referrer_policy);
- fetcher->Start();
-}
-
-void ImageDataFetcher::StartDownload(const GURL& url,
- ImageFetchedCallback callback) {
- ImageDataFetcher::StartDownload(url, callback, std::string(),
- net::URLRequest::NEVER_CLEAR_REFERRER);
-}
-
-// Delegate callback that is called when URLFetcher completes. If the image
-// was fetched successfully, creates a new NSData and returns it to the
-// callback, otherwise returns nil to the callback.
-void ImageDataFetcher::OnURLFetchComplete(const net::URLFetcher* fetcher) {
- if (downloads_in_progress_.find(fetcher) == downloads_in_progress_.end()) {
- LOG(ERROR) << "Received callback for unknown URLFetcher " << fetcher;
- return;
- }
-
- // Ensures that |fetcher| will be deleted in the event of early return.
- std::unique_ptr<const net::URLFetcher> fetcher_deleter(fetcher);
-
- // Retrieves the callback and ensures that it will be deleted in the event
- // of early return.
- base::mac::ScopedBlock<ImageFetchedCallback> callback(
- downloads_in_progress_[fetcher]);
-
- // Remove |fetcher| from the map.
- downloads_in_progress_.erase(fetcher);
-
- // Make sure the request was successful. For "data" requests, the response
- // code has no meaning, because there is no actual server (data is encoded
- // directly in the URL). In that case, set the response code to 200 (OK).
- const GURL& original_url = fetcher->GetOriginalURL();
- const int http_response_code =
- original_url.SchemeIs("data") ? 200 : fetcher->GetResponseCode();
- if (http_response_code != 200) {
- (callback.get())(original_url, http_response_code, nil);
- return;
- }
-
- std::string response;
- if (!fetcher->GetResponseAsString(&response)) {
- (callback.get())(original_url, http_response_code, nil);
- return;
- }
-
- // Create a NSData from the returned data and notify the callback.
- base::scoped_nsobject<NSData> data([[NSData alloc]
- initWithBytes:reinterpret_cast<const unsigned char*>(response.data())
- length:response.size()]);
-
- if (fetcher->GetResponseHeaders()) {
- std::string mime_type;
- fetcher->GetResponseHeaders()->GetMimeType(&mime_type);
- if (mime_type == kWEBPMimeType) {
- base::PostTaskAndReplyWithResult(
- task_runner_.get(), FROM_HERE, base::Bind(&DecodeWebpImage, data),
- base::Bind(&ImageDataFetcher::RunCallback, weak_factory_.GetWeakPtr(),
- callback, original_url, http_response_code));
- return;
- }
- }
- (callback.get())(original_url, http_response_code, data);
-}
-
-void ImageDataFetcher::RunCallback(
- const base::mac::ScopedBlock<ImageFetchedCallback>& callback,
- const GURL& url,
- int http_response_code,
- NSData* data) {
- (callback.get())(url, http_response_code, data);
-}
-
-void ImageDataFetcher::SetRequestContextGetter(
- const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) {
- request_context_getter_ = request_context_getter;
-}
-
-} // namespace web
« no previous file with comments | « ios/web/public/image_fetcher/image_data_fetcher.h ('k') | ios/web/public/image_fetcher/image_data_fetcher_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698