OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ui/ash/launcher/launcher_favicon_loader.h" | 5 #include "chrome/browser/ui/ash/launcher/launcher_favicon_loader.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/browser/favicon/favicon_download_helper.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "chrome/browser/favicon/favicon_download_helper_delegate.h" | |
10 #include "chrome/browser/ui/ash/launcher/browser_launcher_item_controller.h" | 9 #include "chrome/browser/ui/ash/launcher/browser_launcher_item_controller.h" |
11 #include "chrome/common/favicon_url.h" | |
12 #include "chrome/common/icon_messages.h" | |
13 #include "content/public/browser/render_view_host.h" | 10 #include "content/public/browser/render_view_host.h" |
14 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
15 #include "content/public/browser/web_contents_delegate.h" | 12 #include "content/public/browser/web_contents_observer.h" |
16 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
17 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
18 | 15 |
19 namespace internal { | 16 namespace internal { |
20 | 17 |
21 const int kMaxBitmapSize = 256; | 18 const int kMaxBitmapSize = 256; |
22 | 19 |
23 //////////////////////////////////////////////////////////////////////////////// | 20 //////////////////////////////////////////////////////////////////////////////// |
24 // FaviconBitmapHandler fetchs all bitmaps with the 'icon' (or 'shortcut icon') | 21 // FaviconBitmapHandler fetchs all bitmaps with the 'icon' (or 'shortcut icon') |
25 // link tag, storing the one that best matches ash::kLauncherPreferredSize. | 22 // link tag, storing the one that best matches ash::kLauncherPreferredSize. |
26 // These icon bitmaps are not resized and are not cached beyond the lifetime | 23 // These icon bitmaps are not resized and are not cached beyond the lifetime |
27 // of the class. Bitmaps larger than kMaxBitmapSize are ignored. | 24 // of the class. Bitmaps larger than kMaxBitmapSize are ignored. |
28 | 25 |
29 class FaviconBitmapHandler : public FaviconDownloadHelperDelegate { | 26 class FaviconBitmapHandler : public content::WebContentsObserver { |
30 public: | 27 public: |
31 FaviconBitmapHandler(content::WebContents* web_contents, | 28 FaviconBitmapHandler(content::WebContents* web_contents, |
32 LauncherFaviconLoader::Delegate* delegate) | 29 LauncherFaviconLoader::Delegate* delegate) |
33 : delegate_(delegate) { | 30 : content::WebContentsObserver(web_contents), |
34 download_helper_.reset(new FaviconDownloadHelper(web_contents, this)); | 31 delegate_(delegate), |
| 32 web_contents_(web_contents), |
| 33 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
35 } | 34 } |
36 | 35 |
37 ~FaviconBitmapHandler() {} | 36 ~FaviconBitmapHandler() {} |
38 | 37 |
39 const SkBitmap& bitmap() const { return bitmap_; } | 38 const SkBitmap& bitmap() const { return bitmap_; } |
40 | 39 |
41 bool HasPendingDownloads() const; | 40 bool HasPendingDownloads() const; |
42 | 41 |
43 // FaviconDownloadHelperDelegate methods | 42 // content::WebContentObserver implementation. |
44 virtual void OnUpdateFaviconURL( | 43 virtual void DidUpdateFaviconURL( |
45 int32 page_id, | 44 int32 page_id, |
46 const std::vector<FaviconURL>& candidates) OVERRIDE; | 45 const std::vector<content::FaviconURL>& candidates) OVERRIDE; |
47 | |
48 virtual void OnDidDownloadFavicon( | |
49 int id, | |
50 const GURL& image_url, | |
51 bool errored, | |
52 int requested_size, | |
53 const std::vector<SkBitmap>& bitmaps) OVERRIDE; | |
54 | 46 |
55 private: | 47 private: |
56 void DownloadFavicon(const GURL& image_url); | 48 void DidDownloadFavicon( |
| 49 int id, |
| 50 const GURL& image_url, |
| 51 bool errored, |
| 52 int requested_size, |
| 53 const std::vector<SkBitmap>& bitmaps); |
| 54 |
57 void AddFavicon(const GURL& image_url, const SkBitmap& new_bitmap); | 55 void AddFavicon(const GURL& image_url, const SkBitmap& new_bitmap); |
58 | 56 |
59 LauncherFaviconLoader::Delegate* delegate_; | 57 LauncherFaviconLoader::Delegate* delegate_; |
60 | 58 |
61 scoped_ptr<FaviconDownloadHelper> download_helper_; | 59 content::WebContents* web_contents_; |
62 | 60 |
63 typedef std::set<GURL> UrlSet; | 61 typedef std::set<GURL> UrlSet; |
64 // Map of pending download urls. | 62 // Map of pending download urls. |
65 UrlSet pending_requests_; | 63 UrlSet pending_requests_; |
66 // Map of processed urls. | 64 // Map of processed urls. |
67 UrlSet processed_requests_; | 65 UrlSet processed_requests_; |
68 // Current bitmap and source url. | 66 // Current bitmap and source url. |
69 SkBitmap bitmap_; | 67 SkBitmap bitmap_; |
70 GURL bitmap_url_; | 68 GURL bitmap_url_; |
71 | 69 |
| 70 base::WeakPtrFactory<FaviconBitmapHandler> weak_ptr_factory_; |
| 71 |
72 DISALLOW_COPY_AND_ASSIGN(FaviconBitmapHandler); | 72 DISALLOW_COPY_AND_ASSIGN(FaviconBitmapHandler); |
73 }; | 73 }; |
74 | 74 |
75 void FaviconBitmapHandler::OnUpdateFaviconURL( | 75 void FaviconBitmapHandler::DidUpdateFaviconURL( |
76 int32 page_id, | 76 int32 page_id, |
77 const std::vector<FaviconURL>& candidates) { | 77 const std::vector<content::FaviconURL>& candidates) { |
78 // This function receives a complete list of faviocn urls for the page. | 78 // This function receives a complete list of faviocn urls for the page. |
79 // It may get called multiple times with the same list, and will also get | 79 // It may get called multiple times with the same list, and will also get |
80 // called any time an item is added or removed. As such, we track processed | 80 // called any time an item is added or removed. As such, we track processed |
81 // and pending urls, but only until they are removed from the list. | 81 // and pending urls, but only until they are removed from the list. |
82 UrlSet new_pending, new_processed; | 82 UrlSet new_pending, new_processed; |
83 // Create a map of valid favicon urls. | 83 // Create a map of valid favicon urls. |
84 std::set<GURL> urls; | 84 std::set<GURL> urls; |
85 for (std::vector<FaviconURL>::const_iterator iter = candidates.begin(); | 85 std::vector<content::FaviconURL>::const_iterator iter; |
86 iter != candidates.end(); ++iter) { | 86 for (iter = candidates.begin(); iter != candidates.end(); ++iter) { |
87 if (iter->icon_type != FaviconURL::FAVICON) | 87 if (iter->icon_type != content::FaviconURL::FAVICON) |
88 continue; | 88 continue; |
89 const GURL& url = iter->icon_url; | 89 const GURL& url = iter->icon_url; |
90 if (url.is_valid()) | 90 if (url.is_valid()) |
91 urls.insert(url); | 91 urls.insert(url); |
92 // Preserve matching pending requests amd processed requests. | 92 // Preserve matching pending requests amd processed requests. |
93 if (pending_requests_.find(url) != pending_requests_.end()) | 93 if (pending_requests_.find(url) != pending_requests_.end()) |
94 new_pending.insert(url); | 94 new_pending.insert(url); |
95 if (processed_requests_.find(url) != processed_requests_.end()) | 95 if (processed_requests_.find(url) != processed_requests_.end()) |
96 new_processed.insert(url); | 96 new_processed.insert(url); |
97 } | 97 } |
98 pending_requests_ = new_pending; | 98 pending_requests_ = new_pending; |
99 processed_requests_ = new_processed; | 99 processed_requests_ = new_processed; |
100 // Reset bitmap_ if no longer valid (i.e. not in the list of urls). | 100 // Reset bitmap_ if no longer valid (i.e. not in the list of urls). |
101 if (urls.find(bitmap_url_) == urls.end()) { | 101 if (urls.find(bitmap_url_) == urls.end()) { |
102 bitmap_url_ = GURL(); | 102 bitmap_url_ = GURL(); |
103 bitmap_.reset(); | 103 bitmap_.reset(); |
104 } | 104 } |
105 // Request any new urls. | 105 // Request any new urls. |
106 for (std::set<GURL>::iterator iter = urls.begin(); | 106 for (std::set<GURL>::iterator iter = urls.begin(); |
107 iter != urls.end(); ++iter) { | 107 iter != urls.end(); ++iter) { |
108 if (processed_requests_.find(*iter) != processed_requests_.end()) | 108 if (processed_requests_.find(*iter) != processed_requests_.end()) |
109 continue; // Skip already processed downloads. | 109 continue; // Skip already processed downloads. |
110 if (pending_requests_.find(*iter) != pending_requests_.end()) | 110 if (pending_requests_.find(*iter) != pending_requests_.end()) |
111 continue; // Skip already pending downloads. | 111 continue; // Skip already pending downloads. |
112 pending_requests_.insert(*iter); | 112 pending_requests_.insert(*iter); |
113 download_helper_->DownloadFavicon(*iter, 0); | 113 web_contents_->DownloadFavicon(*iter, 0, |
| 114 base::Bind(&FaviconBitmapHandler::DidDownloadFavicon, |
| 115 weak_ptr_factory_.GetWeakPtr())); |
114 } | 116 } |
115 } | 117 } |
116 | 118 |
117 void FaviconBitmapHandler::OnDidDownloadFavicon( | 119 bool FaviconBitmapHandler::HasPendingDownloads() const { |
| 120 return !pending_requests_.empty(); |
| 121 } |
| 122 |
| 123 void FaviconBitmapHandler::DidDownloadFavicon( |
118 int id, | 124 int id, |
119 const GURL& image_url, | 125 const GURL& image_url, |
120 bool errored, | 126 bool errored, |
121 int requested_size, | 127 int requested_size, |
122 const std::vector<SkBitmap>& bitmaps) { | 128 const std::vector<SkBitmap>& bitmaps) { |
123 UrlSet::iterator iter = pending_requests_.find(image_url); | 129 UrlSet::iterator iter = pending_requests_.find(image_url); |
124 if (iter == pending_requests_.end()) { | 130 if (iter == pending_requests_.end()) { |
125 // Updates are received for all downloads; ignore unrequested urls. | 131 // Updates are received for all downloads; ignore unrequested urls. |
126 return; | 132 return; |
127 } | 133 } |
128 pending_requests_.erase(iter); | 134 pending_requests_.erase(iter); |
129 | 135 |
130 // Favicon bitmaps are ordered by decreasing width. | 136 // Favicon bitmaps are ordered by decreasing width. |
131 if (!errored && !bitmaps.empty()) | 137 if (!errored && !bitmaps.empty()) |
132 AddFavicon(image_url, bitmaps[0]); | 138 AddFavicon(image_url, bitmaps[0]); |
133 } | 139 } |
134 | 140 |
135 bool FaviconBitmapHandler::HasPendingDownloads() const { | |
136 return !pending_requests_.empty(); | |
137 } | |
138 | |
139 void FaviconBitmapHandler::AddFavicon(const GURL& image_url, | 141 void FaviconBitmapHandler::AddFavicon(const GURL& image_url, |
140 const SkBitmap& new_bitmap) { | 142 const SkBitmap& new_bitmap) { |
141 processed_requests_.insert(image_url); | 143 processed_requests_.insert(image_url); |
142 if (new_bitmap.height() > kMaxBitmapSize || | 144 if (new_bitmap.height() > kMaxBitmapSize || |
143 new_bitmap.width() > kMaxBitmapSize) | 145 new_bitmap.width() > kMaxBitmapSize) |
144 return; | 146 return; |
145 if (new_bitmap.height() < ash::kLauncherPreferredSize) | 147 if (new_bitmap.height() < ash::kLauncherPreferredSize) |
146 return; | 148 return; |
147 if (!bitmap_.isNull()) { | 149 if (!bitmap_.isNull()) { |
148 // We want the smallest icon that is large enough. | 150 // We want the smallest icon that is large enough. |
(...skipping 19 matching lines...) Expand all Loading... |
168 LauncherFaviconLoader::~LauncherFaviconLoader() { | 170 LauncherFaviconLoader::~LauncherFaviconLoader() { |
169 } | 171 } |
170 | 172 |
171 SkBitmap LauncherFaviconLoader::GetFavicon() const { | 173 SkBitmap LauncherFaviconLoader::GetFavicon() const { |
172 return favicon_handler_->bitmap(); | 174 return favicon_handler_->bitmap(); |
173 } | 175 } |
174 | 176 |
175 bool LauncherFaviconLoader::HasPendingDownloads() const { | 177 bool LauncherFaviconLoader::HasPendingDownloads() const { |
176 return favicon_handler_->HasPendingDownloads(); | 178 return favicon_handler_->HasPendingDownloads(); |
177 } | 179 } |
OLD | NEW |