OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/json/json_parser.h" | 5 #include "base/json/json_parser.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" | 8 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" |
9 #include "chrome/browser/enhanced_bookmarks/android/bookmark_image_service_andro id.h" | 9 #include "chrome/browser/enhanced_bookmarks/android/bookmark_image_service_andro id.h" |
10 #include "chrome/browser/enhanced_bookmarks/enhanced_bookmark_model_factory.h" | 10 #include "chrome/browser/enhanced_bookmarks/enhanced_bookmark_model_factory.h" |
11 #include "chrome/grit/browser_resources.h" | 11 #include "chrome/grit/browser_resources.h" |
12 #include "components/bookmarks/browser/bookmark_model.h" | 12 #include "components/bookmarks/browser/bookmark_model.h" |
13 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h" | 13 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h" |
14 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/navigation_entry.h" | 16 #include "content/public/browser/navigation_entry.h" |
17 #include "content/public/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
19 #include "content/public/common/referrer.h" | 19 #include "content/public/common/referrer.h" |
20 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
21 #include "skia/ext/image_operations.h" | |
21 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
23 #include "ui/base/resource/resource_bundle.h" | |
24 #include "ui/gfx/android/device_display_info.h" | |
22 #include "ui/gfx/image/image_skia.h" | 25 #include "ui/gfx/image/image_skia.h" |
23 | 26 |
24 using content::Referrer; | 27 using content::Referrer; |
25 using bookmarks::BookmarkNode; | 28 using bookmarks::BookmarkNode; |
26 | 29 |
27 namespace enhanced_bookmarks { | 30 namespace enhanced_bookmarks { |
28 | 31 |
29 BookmarkImageServiceAndroid::BookmarkImageServiceAndroid( | 32 BookmarkImageServiceAndroid::BookmarkImageServiceAndroid( |
30 content::BrowserContext* browserContext) | 33 content::BrowserContext* browserContext) |
31 : BookmarkImageService( | 34 : BookmarkImageService( |
32 browserContext->GetPath(), | 35 browserContext->GetPath(), |
33 EnhancedBookmarkModelFactory::GetForBrowserContext(browserContext), | 36 EnhancedBookmarkModelFactory::GetForBrowserContext(browserContext), |
34 make_scoped_refptr(content::BrowserThread::GetBlockingPool())), | 37 make_scoped_refptr(content::BrowserThread::GetBlockingPool())), |
35 browser_context_(browserContext) { | 38 browser_context_(browserContext) { |
39 // The images we're saving will be used locally. So it's wasteful to store | |
40 // images larger than the device resolution. | |
41 gfx::DeviceDisplayInfo display_info; | |
42 int max_length = std::max(display_info.GetPhysicalDisplayWidth(), | |
43 display_info.GetPhysicalDisplayHeight()); | |
44 // GetPhysicalDisplay*() returns 0 for pre-JB MR1. If so, fall back to the | |
45 // second best option we have. | |
46 if (max_length == 0) { | |
47 max_length = std::max(display_info.GetDisplayWidth(), | |
48 display_info.GetDisplayHeight()); | |
49 } | |
50 // For tablet, having an image larger than 500dp does not make sense, because | |
51 // we are showing it inside of a dialog, which only takes about 3/4 the | |
52 // display width. This restriction will not affect phones (we defined phones | |
53 // to be some devices having one dimension less than 600dip). | |
54 max_length = std::min(max_length, int(500 * display_info.GetDIPScale())); | |
noyau (Ping after 24h)
2015/02/13 09:57:42
The comment says 600, the code uses 500.
Is the s
Ian Wen
2015/02/13 22:51:24
500 was used intentionally, because I wanted to ma
| |
55 DCHECK(max_length > 0); | |
56 max_size_.reset(new gfx::Size(max_length, max_length)); | |
57 } | |
58 | |
59 BookmarkImageServiceAndroid::~BookmarkImageServiceAndroid() { | |
36 } | 60 } |
37 | 61 |
38 void BookmarkImageServiceAndroid::RetrieveSalientImage( | 62 void BookmarkImageServiceAndroid::RetrieveSalientImage( |
39 const GURL& page_url, | 63 const GURL& page_url, |
40 const GURL& image_url, | 64 const GURL& image_url, |
41 const std::string& referrer, | 65 const std::string& referrer, |
42 net::URLRequest::ReferrerPolicy referrer_policy, | 66 net::URLRequest::ReferrerPolicy referrer_policy, |
43 bool update_bookmark) { | 67 bool update_bookmark) { |
44 const BookmarkNode* bookmark = | 68 const BookmarkNode* bookmark = |
45 enhanced_bookmark_model_->bookmark_model() | 69 enhanced_bookmark_model_->bookmark_model() |
46 ->GetMostRecentlyAddedUserNodeForURL(page_url); | 70 ->GetMostRecentlyAddedUserNodeForURL(page_url); |
47 if (!bookmark || !image_url.is_valid()) { | 71 if (!bookmark || !image_url.is_valid()) { |
48 ProcessNewImage(page_url, update_bookmark, gfx::Image(), image_url); | 72 ProcessNewImage(page_url, update_bookmark, image_url, gfx::Image()); |
49 return; | 73 return; |
50 } | 74 } |
51 | 75 |
52 BitmapFetcherHandler* bitmap_fetcher_handler = | 76 BitmapFetcherHandler* bitmap_fetcher_handler = |
53 new BitmapFetcherHandler(this, image_url); | 77 new BitmapFetcherHandler(this, image_url); |
54 bitmap_fetcher_handler->Start( | 78 bitmap_fetcher_handler->Start( |
55 browser_context_, referrer, referrer_policy, | 79 browser_context_, referrer, referrer_policy, |
56 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES, | 80 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES, |
57 update_bookmark, page_url); | 81 update_bookmark, page_url); |
58 } | 82 } |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 referrer_policy = net::URLRequest::NEVER_CLEAR_REFERRER; | 201 referrer_policy = net::URLRequest::NEVER_CLEAR_REFERRER; |
178 break; | 202 break; |
179 default: | 203 default: |
180 NOTREACHED(); | 204 NOTREACHED(); |
181 } | 205 } |
182 } | 206 } |
183 RetrieveSalientImage(page_url, GURL(image_url), referrer.url.spec(), | 207 RetrieveSalientImage(page_url, GURL(image_url), referrer.url.spec(), |
184 referrer_policy, update_bookmark); | 208 referrer_policy, update_bookmark); |
185 } | 209 } |
186 | 210 |
211 gfx::Image BookmarkImageServiceAndroid::ResizeImage(gfx::Image image) { | |
212 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
213 | |
214 gfx::Image resized_image = image; | |
215 if (max_size_->width() > 0 && max_size_->height() > 0 && | |
noyau (Ping after 24h)
2015/02/13 09:57:42
You've already done a DCHECK for this condition in
Ian Wen
2015/02/13 22:51:24
Done.
| |
216 (image.Width() > max_size_->width() || | |
217 image.Height() > max_size_->height())) { | |
noyau (Ping after 24h)
2015/02/13 09:57:42
If you resize an image that is really tall, or an
Ian Wen
2015/02/13 22:51:24
Done.
| |
218 float resize_ratio = | |
219 std::min(((float)max_size_->width()) / image.Width(), | |
220 ((float)max_size_->height()) / image.Height()); | |
221 // +0.5f is for correct rounding. Without it, it's possible that dest_width | |
222 // is smaller than max_size_.Width() by one. | |
223 int dest_width = static_cast<int>(resize_ratio * image.Width() + 0.5f); | |
224 int dest_height = static_cast<int>(resize_ratio * image.Height() + 0.5f); | |
225 | |
226 resized_image = | |
227 gfx::Image::CreateFrom1xBitmap(skia::ImageOperations::Resize( | |
228 image.AsBitmap(), skia::ImageOperations::RESIZE_BEST, dest_width, | |
229 dest_height)); | |
230 resized_image.AsImageSkia().MakeThreadSafe(); | |
231 } | |
232 return resized_image; | |
233 } | |
234 | |
187 void BookmarkImageServiceAndroid::BitmapFetcherHandler::Start( | 235 void BookmarkImageServiceAndroid::BitmapFetcherHandler::Start( |
188 content::BrowserContext* browser_context, | 236 content::BrowserContext* browser_context, |
189 const std::string& referrer, | 237 const std::string& referrer, |
190 net::URLRequest::ReferrerPolicy referrer_policy, | 238 net::URLRequest::ReferrerPolicy referrer_policy, |
191 int load_flags, | 239 int load_flags, |
192 bool update_bookmark, | 240 bool update_bookmark, |
193 const GURL& page_url) { | 241 const GURL& page_url) { |
194 update_bookmark_ = update_bookmark; | 242 update_bookmark_ = update_bookmark; |
195 page_url_ = page_url; | 243 page_url_ = page_url; |
196 | 244 |
197 bitmap_fetcher_.Start(browser_context->GetRequestContext(), referrer, | 245 bitmap_fetcher_.Start(browser_context->GetRequestContext(), referrer, |
198 referrer_policy, load_flags); | 246 referrer_policy, load_flags); |
199 } | 247 } |
200 | 248 |
201 void BookmarkImageServiceAndroid::BitmapFetcherHandler::OnFetchComplete( | 249 void BookmarkImageServiceAndroid::BitmapFetcherHandler::OnFetchComplete( |
202 const GURL url, | 250 const GURL url, |
203 const SkBitmap* bitmap) { | 251 const SkBitmap* bitmap) { |
204 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 252 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
205 | 253 |
206 gfx::Image image; | 254 gfx::Image image; |
207 if (bitmap) { | 255 if (bitmap) { |
208 gfx::ImageSkia imageSkia = gfx::ImageSkia::CreateFrom1xBitmap(*bitmap); | 256 gfx::ImageSkia imageSkia = gfx::ImageSkia::CreateFrom1xBitmap(*bitmap); |
209 imageSkia.MakeThreadSafe(); | 257 imageSkia.MakeThreadSafe(); |
210 image = gfx::Image(imageSkia); | 258 image = gfx::Image(imageSkia); |
211 } | 259 } |
212 service_->ProcessNewImage(page_url_, update_bookmark_, image, url); | 260 service_->ProcessNewImage(page_url_, update_bookmark_, url, image); |
213 | 261 |
214 delete this; | 262 delete this; |
215 } | 263 } |
216 | 264 |
217 } // namespace enhanced_bookmarks | 265 } // namespace enhanced_bookmarks |
OLD | NEW |