Chromium Code Reviews| 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 max_length /= display_info.GetDIPScale(); | |
| 51 // For tablet, having an image larger than 600dip does not make sense, because | |
| 52 // we are showing it inside of a dialog, which only takes about 3/4 the | |
| 53 // display width. This restriction will not affect phones (we defined phones | |
| 54 // to be some devices having one dimension less than 600dip). | |
| 55 max_length = std::min(max_length, 600); | |
| 56 DCHECK(max_length > 0); | |
| 57 max_size_.reset(new gfx::Size(max_length, max_length)); | |
| 58 } | |
| 59 | |
| 60 BookmarkImageServiceAndroid::~BookmarkImageServiceAndroid() { | |
| 36 } | 61 } |
| 37 | 62 |
| 38 void BookmarkImageServiceAndroid::RetrieveSalientImage( | 63 void BookmarkImageServiceAndroid::RetrieveSalientImage( |
| 39 const GURL& page_url, | 64 const GURL& page_url, |
| 40 const GURL& image_url, | 65 const GURL& image_url, |
| 41 const std::string& referrer, | 66 const std::string& referrer, |
| 42 net::URLRequest::ReferrerPolicy referrer_policy, | 67 net::URLRequest::ReferrerPolicy referrer_policy, |
| 43 bool update_bookmark) { | 68 bool update_bookmark) { |
| 44 const BookmarkNode* bookmark = | 69 const BookmarkNode* bookmark = |
| 45 enhanced_bookmark_model_->bookmark_model() | 70 enhanced_bookmark_model_->bookmark_model() |
| 46 ->GetMostRecentlyAddedUserNodeForURL(page_url); | 71 ->GetMostRecentlyAddedUserNodeForURL(page_url); |
| 47 if (!bookmark || !image_url.is_valid()) { | 72 if (!bookmark || !image_url.is_valid()) { |
| 48 ProcessNewImage(page_url, update_bookmark, gfx::Image(), image_url); | 73 ProcessNewImage(page_url, update_bookmark, image_url, gfx::Image()); |
| 49 return; | 74 return; |
| 50 } | 75 } |
| 51 | 76 |
| 52 BitmapFetcherHandler* bitmap_fetcher_handler = | 77 BitmapFetcherHandler* bitmap_fetcher_handler = |
| 53 new BitmapFetcherHandler(this, image_url); | 78 new BitmapFetcherHandler(this, image_url); |
| 54 bitmap_fetcher_handler->Start( | 79 bitmap_fetcher_handler->Start( |
| 55 browser_context_, referrer, referrer_policy, | 80 browser_context_, referrer, referrer_policy, |
| 56 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES, | 81 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES, |
| 57 update_bookmark, page_url); | 82 update_bookmark, page_url); |
| 58 } | 83 } |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 referrer_policy = net::URLRequest::NEVER_CLEAR_REFERRER; | 202 referrer_policy = net::URLRequest::NEVER_CLEAR_REFERRER; |
| 178 break; | 203 break; |
| 179 default: | 204 default: |
| 180 NOTREACHED(); | 205 NOTREACHED(); |
| 181 } | 206 } |
| 182 } | 207 } |
| 183 RetrieveSalientImage(page_url, GURL(image_url), referrer.url.spec(), | 208 RetrieveSalientImage(page_url, GURL(image_url), referrer.url.spec(), |
| 184 referrer_policy, update_bookmark); | 209 referrer_policy, update_bookmark); |
| 185 } | 210 } |
| 186 | 211 |
| 212 gfx::Image BookmarkImageServiceAndroid::ResizeImage(gfx::Image image) { | |
| 213 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 214 | |
| 215 gfx::Image resized_uimage = image; | |
|
Kibeom Kim (inactive)
2015/02/12 23:24:15
Q: what does |uimage| stand for?
Ian Wen
2015/02/13 00:52:41
Typo. Done.
| |
| 216 if (max_size_->width() > 0 && max_size_->height() > 0 && | |
| 217 (image.Width() > max_size_->width() || | |
| 218 image.Height() > max_size_->height())) { | |
| 219 float resize_ratio = | |
| 220 std::min(((float)max_size_->width()) / image.Width(), | |
| 221 ((float)max_size_->height()) / image.Height()); | |
| 222 // +0.5f is for correct rounding. Without it, it's possible that dest_width | |
| 223 // is smaller than max_size_.Width() by one. | |
| 224 int dest_width = static_cast<int>(resize_ratio * image.Width() + 0.5f); | |
| 225 int dest_height = static_cast<int>(resize_ratio * image.Height() + 0.5f); | |
|
Kibeom Kim (inactive)
2015/02/12 23:24:15
Eventually, I think it's good to share computing d
| |
| 226 | |
| 227 resized_uimage = | |
| 228 gfx::Image::CreateFrom1xBitmap(skia::ImageOperations::Resize( | |
| 229 image.AsBitmap(), skia::ImageOperations::RESIZE_BEST, dest_width, | |
| 230 dest_height)); | |
| 231 resized_uimage.AsImageSkia().MakeThreadSafe(); | |
| 232 } | |
| 233 return resized_uimage; | |
| 234 } | |
| 235 | |
| 187 void BookmarkImageServiceAndroid::BitmapFetcherHandler::Start( | 236 void BookmarkImageServiceAndroid::BitmapFetcherHandler::Start( |
| 188 content::BrowserContext* browser_context, | 237 content::BrowserContext* browser_context, |
| 189 const std::string& referrer, | 238 const std::string& referrer, |
| 190 net::URLRequest::ReferrerPolicy referrer_policy, | 239 net::URLRequest::ReferrerPolicy referrer_policy, |
| 191 int load_flags, | 240 int load_flags, |
| 192 bool update_bookmark, | 241 bool update_bookmark, |
| 193 const GURL& page_url) { | 242 const GURL& page_url) { |
| 194 update_bookmark_ = update_bookmark; | 243 update_bookmark_ = update_bookmark; |
| 195 page_url_ = page_url; | 244 page_url_ = page_url; |
| 196 | 245 |
| 197 bitmap_fetcher_.Start(browser_context->GetRequestContext(), referrer, | 246 bitmap_fetcher_.Start(browser_context->GetRequestContext(), referrer, |
| 198 referrer_policy, load_flags); | 247 referrer_policy, load_flags); |
| 199 } | 248 } |
| 200 | 249 |
| 201 void BookmarkImageServiceAndroid::BitmapFetcherHandler::OnFetchComplete( | 250 void BookmarkImageServiceAndroid::BitmapFetcherHandler::OnFetchComplete( |
| 202 const GURL url, | 251 const GURL url, |
| 203 const SkBitmap* bitmap) { | 252 const SkBitmap* bitmap) { |
| 204 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 253 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 205 | 254 |
| 206 gfx::Image image; | 255 gfx::Image image; |
| 207 if (bitmap) { | 256 if (bitmap) { |
| 208 gfx::ImageSkia imageSkia = gfx::ImageSkia::CreateFrom1xBitmap(*bitmap); | 257 gfx::ImageSkia imageSkia = gfx::ImageSkia::CreateFrom1xBitmap(*bitmap); |
| 209 imageSkia.MakeThreadSafe(); | 258 imageSkia.MakeThreadSafe(); |
| 210 image = gfx::Image(imageSkia); | 259 image = gfx::Image(imageSkia); |
| 211 } | 260 } |
| 212 service_->ProcessNewImage(page_url_, update_bookmark_, image, url); | 261 service_->ProcessNewImage(page_url_, update_bookmark_, url, image); |
| 213 | 262 |
| 214 delete this; | 263 delete this; |
| 215 } | 264 } |
| 216 | 265 |
| 217 } // namespace enhanced_bookmarks | 266 } // namespace enhanced_bookmarks |
| OLD | NEW |