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, |
| 34 scoped_refptr<base::SequencedWorkerPool> pool) | |
| 31 : BookmarkImageService( | 35 : BookmarkImageService( |
| 32 browserContext->GetPath(), | 36 browserContext->GetPath(), |
| 33 EnhancedBookmarkModelFactory::GetForBrowserContext(browserContext), | 37 EnhancedBookmarkModelFactory::GetForBrowserContext(browserContext), |
| 34 make_scoped_refptr(content::BrowserThread::GetBlockingPool())), | 38 pool), |
| 35 browser_context_(browserContext) { | 39 browser_context_(browserContext), |
| 40 pool_(pool) { | |
| 41 // The images we're saving will be used locally. So it's wasteful to store | |
| 42 // images larger than the device resolution. | |
| 43 gfx::DeviceDisplayInfo display_info; | |
| 44 int max_length = std::min(display_info.GetPhysicalDisplayWidth(), | |
| 45 display_info.GetPhysicalDisplayHeight()); | |
| 46 // GetPhysicalDisplay*() returns 0 for pre-JB MR1. If so, fall back to the | |
| 47 // second best option we have. | |
| 48 if (max_length == 0) { | |
| 49 max_length = std::max(display_info.GetDisplayWidth(), | |
| 50 display_info.GetDisplayHeight()); | |
| 51 } | |
| 52 max_length /= display_info.GetDIPScale(); | |
|
Kibeom Kim (inactive)
2015/02/12 23:24:15
IIRC, gfx::Image's DPI concept doesn't depend on t
Ian Wen
2015/02/13 00:52:41
Yes you were right. The size of the dialog is alwa
| |
| 53 // For tablet, having an image larger than 600dip does not make sense, because | |
| 54 // we are showing it inside of a dialog, which only takes about 3/4 the | |
| 55 // display width. This restriction will not affect phones (we defined phones | |
| 56 // to be some devices having one dimension less than 600dip). | |
| 57 max_length = std::min(max_length, 600); | |
| 58 DCHECK(max_length > 0); | |
| 59 max_size_.reset(new gfx::Size(max_length, 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_uimage = image; | |
| 218 if (max_size_->width() > 0 && max_size_->height() > 0 && | |
| 219 (image.Width() > max_size_->width() || | |
| 220 image.Height() > max_size_->height())) { | |
| 221 float resize_ratio = | |
| 222 std::min(((float)max_size_->width()) / image.Width(), | |
| 223 ((float)max_size_->height()) / image.Height()); | |
| 224 // +0.5f is for correct rounding. Without it, it's possible that dest_width | |
| 225 // is smaller than max_size_.Width() by one. | |
| 226 int dest_width = static_cast<int>(resize_ratio * image.Width() + 0.5f); | |
| 227 int dest_height = static_cast<int>(resize_ratio * image.Height() + 0.5f); | |
| 228 | |
| 229 resized_uimage = | |
| 230 gfx::Image::CreateFrom1xBitmap(skia::ImageOperations::Resize( | |
| 231 image.AsBitmap(), skia::ImageOperations::RESIZE_BEST, dest_width, | |
| 232 dest_height)); | |
| 233 resized_uimage.AsImageSkia().MakeThreadSafe(); | |
| 234 } | |
| 235 return resized_uimage; | |
| 236 } | |
| 237 | |
| 187 void BookmarkImageServiceAndroid::BitmapFetcherHandler::Start( | 238 void BookmarkImageServiceAndroid::BitmapFetcherHandler::Start( |
| 188 content::BrowserContext* browser_context, | 239 content::BrowserContext* browser_context, |
| 189 const std::string& referrer, | 240 const std::string& referrer, |
| 190 net::URLRequest::ReferrerPolicy referrer_policy, | 241 net::URLRequest::ReferrerPolicy referrer_policy, |
| 191 int load_flags, | 242 int load_flags, |
| 192 bool update_bookmark, | 243 bool update_bookmark, |
| 193 const GURL& page_url) { | 244 const GURL& page_url) { |
| 194 update_bookmark_ = update_bookmark; | 245 update_bookmark_ = update_bookmark; |
| 195 page_url_ = page_url; | 246 page_url_ = page_url; |
| 196 | 247 |
| 197 bitmap_fetcher_.Start(browser_context->GetRequestContext(), referrer, | 248 bitmap_fetcher_.Start(browser_context->GetRequestContext(), referrer, |
| 198 referrer_policy, load_flags); | 249 referrer_policy, load_flags); |
| 199 } | 250 } |
| 200 | 251 |
| 201 void BookmarkImageServiceAndroid::BitmapFetcherHandler::OnFetchComplete( | 252 void BookmarkImageServiceAndroid::BitmapFetcherHandler::OnFetchComplete( |
| 202 const GURL url, | 253 const GURL url, |
| 203 const SkBitmap* bitmap) { | 254 const SkBitmap* bitmap) { |
| 204 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 255 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 205 | 256 |
| 206 gfx::Image image; | 257 gfx::Image image; |
| 207 if (bitmap) { | 258 if (bitmap) { |
| 208 gfx::ImageSkia imageSkia = gfx::ImageSkia::CreateFrom1xBitmap(*bitmap); | 259 gfx::ImageSkia imageSkia = gfx::ImageSkia::CreateFrom1xBitmap(*bitmap); |
| 209 imageSkia.MakeThreadSafe(); | 260 imageSkia.MakeThreadSafe(); |
| 210 image = gfx::Image(imageSkia); | 261 image = gfx::Image(imageSkia); |
| 211 } | 262 } |
| 212 service_->ProcessNewImage(page_url_, update_bookmark_, image, url); | 263 |
| 264 base::Callback<gfx::Image(void)> task = | |
|
noyau (Ping after 24h)
2015/02/12 08:34:42
Could you move this code in the superclass? With a
Ian Wen
2015/02/12 22:49:47
Done.
| |
| 265 base::Bind(&BookmarkImageServiceAndroid::ResizeImage, | |
| 266 base::Unretained(service_), image); | |
| 267 base::Callback<void(const gfx::Image&)> reply = | |
| 268 base::Bind(&BookmarkImageServiceAndroid::ProcessNewImage, | |
| 269 base::Unretained(service_), page_url_, update_bookmark_, url); | |
| 270 base::PostTaskAndReplyWithResult(service_->pool_.get(), FROM_HERE, task, | |
| 271 reply); | |
| 213 | 272 |
| 214 delete this; | 273 delete this; |
| 215 } | 274 } |
| 216 | 275 |
| 217 } // namespace enhanced_bookmarks | 276 } // namespace enhanced_bookmarks |
| OLD | NEW |