| 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" |
| 22 #include "ui/base/device_form_factor.h" |
| 21 #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::min(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::min(display_info.GetDisplayWidth(), |
| 48 display_info.GetDisplayHeight()); |
| 49 } |
| 50 |
| 51 if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET) { |
| 52 max_length = max_length / 2; |
| 53 } else if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_PHONE) { |
| 54 max_length = max_length * 3 / 4; |
| 55 } |
| 56 |
| 57 DCHECK(max_length > 0); |
| 58 max_size_.set_height(max_length); |
| 59 max_size_.set_width(max_length); |
| 60 } |
| 61 |
| 62 BookmarkImageServiceAndroid::~BookmarkImageServiceAndroid() { |
| 36 } | 63 } |
| 37 | 64 |
| 38 void BookmarkImageServiceAndroid::RetrieveSalientImage( | 65 void BookmarkImageServiceAndroid::RetrieveSalientImage( |
| 39 const GURL& page_url, | 66 const GURL& page_url, |
| 40 const GURL& image_url, | 67 const GURL& image_url, |
| 41 const std::string& referrer, | 68 const std::string& referrer, |
| 42 net::URLRequest::ReferrerPolicy referrer_policy, | 69 net::URLRequest::ReferrerPolicy referrer_policy, |
| 43 bool update_bookmark) { | 70 bool update_bookmark) { |
| 44 const BookmarkNode* bookmark = | 71 const BookmarkNode* bookmark = |
| 45 enhanced_bookmark_model_->bookmark_model() | 72 enhanced_bookmark_model_->bookmark_model() |
| 46 ->GetMostRecentlyAddedUserNodeForURL(page_url); | 73 ->GetMostRecentlyAddedUserNodeForURL(page_url); |
| 47 if (!bookmark || !image_url.is_valid()) { | 74 if (!bookmark || !image_url.is_valid()) { |
| 48 ProcessNewImage(page_url, update_bookmark, gfx::Image(), image_url); | 75 ProcessNewImage(page_url, update_bookmark, image_url, gfx::Image()); |
| 49 return; | 76 return; |
| 50 } | 77 } |
| 51 | 78 |
| 52 BitmapFetcherHandler* bitmap_fetcher_handler = | 79 BitmapFetcherHandler* bitmap_fetcher_handler = |
| 53 new BitmapFetcherHandler(this, image_url); | 80 new BitmapFetcherHandler(this, image_url); |
| 54 bitmap_fetcher_handler->Start( | 81 bitmap_fetcher_handler->Start( |
| 55 browser_context_, referrer, referrer_policy, | 82 browser_context_, referrer, referrer_policy, |
| 56 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES, | 83 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES, |
| 57 update_bookmark, page_url); | 84 update_bookmark, page_url); |
| 58 } | 85 } |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 referrer_policy = net::URLRequest::NEVER_CLEAR_REFERRER; | 204 referrer_policy = net::URLRequest::NEVER_CLEAR_REFERRER; |
| 178 break; | 205 break; |
| 179 default: | 206 default: |
| 180 NOTREACHED(); | 207 NOTREACHED(); |
| 181 } | 208 } |
| 182 } | 209 } |
| 183 RetrieveSalientImage(page_url, GURL(image_url), referrer.url.spec(), | 210 RetrieveSalientImage(page_url, GURL(image_url), referrer.url.spec(), |
| 184 referrer_policy, update_bookmark); | 211 referrer_policy, update_bookmark); |
| 185 } | 212 } |
| 186 | 213 |
| 214 gfx::Image BookmarkImageServiceAndroid::ResizeImage(gfx::Image image) { |
| 215 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 216 |
| 217 gfx::Image resized_image = image; |
| 218 if (image.Width() > max_size_.width() && |
| 219 image.Height() > max_size_.height()) { |
| 220 float resize_ratio = 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); |
| 226 |
| 227 resized_image = |
| 228 gfx::Image::CreateFrom1xBitmap(skia::ImageOperations::Resize( |
| 229 image.AsBitmap(), skia::ImageOperations::RESIZE_BEST, dest_width, |
| 230 dest_height)); |
| 231 resized_image.AsImageSkia().MakeThreadSafe(); |
| 232 } |
| 233 return resized_image; |
| 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 |