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 "chrome/browser/search/suggestions/thumbnail_manager.h" | 5 #include "chrome/browser/search/suggestions/thumbnail_manager.h" |
6 | 6 |
7 #include "base/memory/ref_counted_memory.h" | 7 #include "base/memory/ref_counted_memory.h" |
8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
9 #include "net/base/load_flags.h" | 9 #include "net/base/load_flags.h" |
10 #include "net/url_request/url_request_context_getter.h" | 10 #include "net/url_request/url_request_context_getter.h" |
11 #include "ui/gfx/codec/jpeg_codec.h" | 11 #include "ui/gfx/codec/jpeg_codec.h" |
12 | 12 |
13 using leveldb_proto::ProtoDatabase; | 13 using leveldb_proto::ProtoDatabase; |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // From JPEG-encoded bytes to SkBitmap. | 17 // From JPEG-encoded bytes to SkBitmap. |
18 SkBitmap* DecodeThumbnail(const std::vector<unsigned char>& encoded_data) { | 18 SkBitmap* DecodeThumbnail(const std::vector<unsigned char>& encoded_data) { |
19 return gfx::JPEGCodec::Decode(&encoded_data[0], encoded_data.size()); | 19 return gfx::JPEGCodec::Decode(&encoded_data[0], encoded_data.size()); |
20 } | 20 } |
21 } | 21 |
| 22 } // namespace |
22 | 23 |
23 namespace suggestions { | 24 namespace suggestions { |
24 | 25 |
25 ThumbnailManager::ThumbnailManager() : weak_ptr_factory_(this) {} | 26 ThumbnailManager::ThumbnailManager() : weak_ptr_factory_(this) {} |
26 | 27 |
27 ThumbnailManager::ThumbnailManager( | 28 ThumbnailManager::ThumbnailManager( |
28 net::URLRequestContextGetter* url_request_context, | 29 net::URLRequestContextGetter* url_request_context, |
29 scoped_ptr<ProtoDatabase<ThumbnailData> > database, | 30 scoped_ptr<ProtoDatabase<ThumbnailData> > database, |
30 const base::FilePath& database_dir) | 31 const base::FilePath& database_dir) |
31 : url_request_context_(url_request_context), | 32 : url_request_context_(url_request_context), |
32 database_(database.Pass()), | 33 database_(database.Pass()), |
33 database_ready_(false), | 34 database_ready_(false), |
34 weak_ptr_factory_(this) { | 35 weak_ptr_factory_(this) { |
35 database_->Init(database_dir, base::Bind(&ThumbnailManager::OnDatabaseInit, | 36 database_->Init(database_dir, base::Bind(&ThumbnailManager::OnDatabaseInit, |
36 weak_ptr_factory_.GetWeakPtr())); | 37 weak_ptr_factory_.GetWeakPtr())); |
37 } | 38 } |
38 | 39 |
39 ThumbnailManager::~ThumbnailManager() {} | 40 ThumbnailManager::~ThumbnailManager() {} |
40 | 41 |
41 ThumbnailManager::ThumbnailRequest::ThumbnailRequest() : fetcher(NULL) {} | 42 ThumbnailManager::ThumbnailRequest::ThumbnailRequest() : fetcher(NULL) {} |
42 | 43 |
43 ThumbnailManager::ThumbnailRequest::ThumbnailRequest(chrome::BitmapFetcher* f) | 44 ThumbnailManager::ThumbnailRequest::ThumbnailRequest(chrome::BitmapFetcher* f) |
44 : fetcher(f) {} | 45 : fetcher(f) {} |
45 | 46 |
46 ThumbnailManager::ThumbnailRequest::~ThumbnailRequest() { delete fetcher; } | 47 ThumbnailManager::ThumbnailRequest::~ThumbnailRequest() { delete fetcher; } |
47 | 48 |
48 void ThumbnailManager::InitializeThumbnailMap( | 49 void ThumbnailManager::Initialize(const SuggestionsProfile& suggestions) { |
49 const SuggestionsProfile& suggestions) { | |
50 thumbnail_url_map_.clear(); | 50 thumbnail_url_map_.clear(); |
51 for (int i = 0; i < suggestions.suggestions_size(); ++i) { | 51 for (int i = 0; i < suggestions.suggestions_size(); ++i) { |
52 const ChromeSuggestion& suggestion = suggestions.suggestions(i); | 52 const ChromeSuggestion& suggestion = suggestions.suggestions(i); |
53 if (suggestion.has_thumbnail()) { | 53 if (suggestion.has_thumbnail()) { |
54 thumbnail_url_map_[GURL(suggestion.url())] = GURL(suggestion.thumbnail()); | 54 thumbnail_url_map_[GURL(suggestion.url())] = GURL(suggestion.thumbnail()); |
55 } | 55 } |
56 } | 56 } |
57 } | 57 } |
58 | 58 |
59 void ThumbnailManager::GetPageThumbnail( | 59 void ThumbnailManager::GetImageForURL( |
60 const GURL& url, | 60 const GURL& url, |
61 base::Callback<void(const GURL&, const SkBitmap*)> callback) { | 61 base::Callback<void(const GURL&, const SkBitmap*)> callback) { |
62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
63 // If |url| is not found in |thumbnail_url_map_|, then invoke |callback| with | 63 // If |url| is not found in |thumbnail_url_map_|, then invoke |callback| with |
64 // NULL since there is no associated thumbnail for this |url|. | 64 // NULL since there is no associated thumbnail for this |url|. |
65 GURL thumbnail_url; | 65 GURL thumbnail_url; |
66 if (!GetThumbnailURL(url, &thumbnail_url)) { | 66 if (!GetThumbnailURL(url, &thumbnail_url)) { |
67 callback.Run(url, NULL); | 67 callback.Run(url, NULL); |
68 return; | 68 return; |
69 } | 69 } |
70 | 70 |
71 // |database_| can be null if something went wrong in initialization. | 71 // |database_| can be NULL if something went wrong in initialization. |
72 if (database_.get() && !database_ready_) { | 72 if (database_.get() && !database_ready_) { |
73 // Once database is initialized, it will serve pending requests from either | 73 // Once database is initialized, it will serve pending requests from either |
74 // cache or network. | 74 // cache or network. |
75 QueueCacheRequest(url, thumbnail_url, callback); | 75 QueueCacheRequest(url, thumbnail_url, callback); |
76 return; | 76 return; |
77 } | 77 } |
78 | 78 |
79 ServeFromCacheOrNetwork(url, thumbnail_url, callback); | 79 ServeFromCacheOrNetwork(url, thumbnail_url, callback); |
80 } | 80 } |
81 | 81 |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 if (!bitmap.readyToDraw() || bitmap.isNull()) { | 271 if (!bitmap.readyToDraw() || bitmap.isNull()) { |
272 return false; | 272 return false; |
273 } | 273 } |
274 return gfx::JPEGCodec::Encode( | 274 return gfx::JPEGCodec::Encode( |
275 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 275 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
276 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), | 276 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), |
277 bitmap.width() * bitmap.bytesPerPixel(), 100, dest); | 277 bitmap.width() * bitmap.bytesPerPixel(), 100, dest); |
278 } | 278 } |
279 | 279 |
280 } // namespace suggestions | 280 } // namespace suggestions |
OLD | NEW |