OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/renderer/image_downloader/image_downloader_base.h" | 5 #include "content/renderer/image_downloader/image_downloader_base.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" |
12 #include "content/child/image_decoder.h" | 13 #include "content/child/image_decoder.h" |
13 #include "content/public/renderer/render_frame.h" | 14 #include "content/public/renderer/render_frame.h" |
14 #include "content/public/renderer/render_thread.h" | 15 #include "content/public/renderer/render_thread.h" |
15 #include "content/renderer/fetchers/multi_resolution_image_resource_fetcher.h" | 16 #include "content/renderer/fetchers/multi_resolution_image_resource_fetcher.h" |
16 #include "net/base/data_url.h" | 17 #include "net/base/data_url.h" |
17 #include "third_party/WebKit/public/platform/WebCachePolicy.h" | 18 #include "third_party/WebKit/public/platform/WebCachePolicy.h" |
18 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 19 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
19 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 20 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
20 #include "ui/gfx/favicon_size.h" | 21 #include "ui/gfx/favicon_size.h" |
21 #include "ui/gfx/geometry/size.h" | 22 #include "ui/gfx/geometry/size.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 } | 87 } |
87 | 88 |
88 bool ImageDownloaderBase::FetchImage(const GURL& image_url, | 89 bool ImageDownloaderBase::FetchImage(const GURL& image_url, |
89 bool is_favicon, | 90 bool is_favicon, |
90 bool bypass_cache, | 91 bool bypass_cache, |
91 const DownloadCallback& callback) { | 92 const DownloadCallback& callback) { |
92 blink::WebLocalFrame* frame = render_frame()->GetWebFrame(); | 93 blink::WebLocalFrame* frame = render_frame()->GetWebFrame(); |
93 DCHECK(frame); | 94 DCHECK(frame); |
94 | 95 |
95 // Create an image resource fetcher and assign it with a call back object. | 96 // Create an image resource fetcher and assign it with a call back object. |
96 image_fetchers_.push_back(new MultiResolutionImageResourceFetcher( | 97 image_fetchers_.push_back( |
97 image_url, frame, 0, is_favicon ? WebURLRequest::RequestContextFavicon | 98 base::MakeUnique<MultiResolutionImageResourceFetcher>( |
98 : WebURLRequest::RequestContextImage, | 99 image_url, frame, 0, |
99 bypass_cache ? WebCachePolicy::BypassingCache | 100 is_favicon ? WebURLRequest::RequestContextFavicon |
100 : WebCachePolicy::UseProtocolCachePolicy, | 101 : WebURLRequest::RequestContextImage, |
101 base::Bind(&ImageDownloaderBase::DidFetchImage, base::Unretained(this), | 102 bypass_cache ? WebCachePolicy::BypassingCache |
102 callback))); | 103 : WebCachePolicy::UseProtocolCachePolicy, |
| 104 base::Bind(&ImageDownloaderBase::DidFetchImage, |
| 105 base::Unretained(this), callback))); |
103 return true; | 106 return true; |
104 } | 107 } |
105 | 108 |
106 void ImageDownloaderBase::DidFetchImage( | 109 void ImageDownloaderBase::DidFetchImage( |
107 const DownloadCallback& callback, | 110 const DownloadCallback& callback, |
108 MultiResolutionImageResourceFetcher* fetcher, | 111 MultiResolutionImageResourceFetcher* fetcher, |
109 const std::vector<SkBitmap>& images) { | 112 const std::vector<SkBitmap>& images) { |
110 int32_t http_status_code = fetcher->http_status_code(); | 113 int32_t http_status_code = fetcher->http_status_code(); |
111 | 114 |
112 // Remove the image fetcher from our pending list. We're in the callback from | 115 // Remove the image fetcher from our pending list. We're in the callback from |
113 // MultiResolutionImageResourceFetcher, best to delay deletion. | 116 // MultiResolutionImageResourceFetcher, best to delay deletion. |
114 ImageResourceFetcherList::iterator iter = | 117 for (auto iter = image_fetchers_.begin(); iter != image_fetchers_.end(); |
115 std::find(image_fetchers_.begin(), image_fetchers_.end(), fetcher); | 118 ++iter) { |
116 if (iter != image_fetchers_.end()) { | 119 if (iter->get() == fetcher) { |
117 image_fetchers_.weak_erase(iter); | 120 iter->release(); |
118 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, fetcher); | 121 image_fetchers_.erase(iter); |
| 122 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, fetcher); |
| 123 break; |
| 124 } |
119 } | 125 } |
120 | 126 |
121 // |this| may be destructed after callback is run. | 127 // |this| may be destructed after callback is run. |
122 callback.Run(http_status_code, images); | 128 callback.Run(http_status_code, images); |
123 } | 129 } |
124 | 130 |
125 void ImageDownloaderBase::OnDestruct() { | 131 void ImageDownloaderBase::OnDestruct() { |
126 for (auto* fetchers : image_fetchers_) { | 132 for (const auto& fetchers : image_fetchers_) { |
127 // Will run callbacks with an empty image vector. | 133 // Will run callbacks with an empty image vector. |
128 fetchers->OnRenderFrameDestruct(); | 134 fetchers->OnRenderFrameDestruct(); |
129 } | 135 } |
130 } | 136 } |
131 | 137 |
132 } // namespace content | 138 } // namespace content |
OLD | NEW |