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" | |
9 #include "chrome/browser/favicon/favicon_download_helper_delegate.h" | |
10 #include "chrome/browser/ui/ash/launcher/browser_launcher_item_controller.h" | 8 #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" | 9 #include "content/public/browser/render_view_host.h" |
14 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
15 #include "content/public/browser/web_contents_delegate.h" | 11 #include "content/public/browser/web_contents_observer.h" |
12 #include "content/public/common/favicon_url.h" | |
jam
2012/12/03 21:59:39
nit: this is in the header, so don't add it here a
Cait (Slow)
2012/12/04 20:57:35
Done.
| |
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 |
16 using content::FaviconURL; | |
17 | |
19 namespace internal { | 18 namespace internal { |
20 | 19 |
21 const int kMaxBitmapSize = 256; | 20 const int kMaxBitmapSize = 256; |
22 | 21 |
23 //////////////////////////////////////////////////////////////////////////////// | 22 //////////////////////////////////////////////////////////////////////////////// |
24 // FaviconBitmapHandler fetchs all bitmaps with the 'icon' (or 'shortcut icon') | 23 // FaviconBitmapHandler fetchs all bitmaps with the 'icon' (or 'shortcut icon') |
25 // link tag, storing the one that best matches ash::kLauncherPreferredSize. | 24 // link tag, storing the one that best matches ash::kLauncherPreferredSize. |
26 // These icon bitmaps are not resized and are not cached beyond the lifetime | 25 // These icon bitmaps are not resized and are not cached beyond the lifetime |
27 // of the class. Bitmaps larger than kMaxBitmapSize are ignored. | 26 // of the class. Bitmaps larger than kMaxBitmapSize are ignored. |
28 | 27 |
29 class FaviconBitmapHandler : public FaviconDownloadHelperDelegate { | 28 class FaviconBitmapHandler : public content::WebContentsObserver { |
30 public: | 29 public: |
31 FaviconBitmapHandler(content::WebContents* web_contents, | 30 FaviconBitmapHandler(content::WebContents* web_contents, |
32 LauncherFaviconLoader::Delegate* delegate) | 31 LauncherFaviconLoader::Delegate* delegate) |
33 : delegate_(delegate) { | 32 : content::WebContentsObserver(web_contents), |
34 download_helper_.reset(new FaviconDownloadHelper(web_contents, this)); | 33 delegate_(delegate), |
34 web_contents_(web_contents) { | |
35 } | 35 } |
36 | 36 |
37 ~FaviconBitmapHandler() {} | 37 ~FaviconBitmapHandler() {} |
38 | 38 |
39 const SkBitmap& bitmap() const { return bitmap_; } | 39 const SkBitmap& bitmap() const { return bitmap_; } |
40 | 40 |
41 bool HasPendingDownloads() const; | 41 bool HasPendingDownloads() const; |
42 | 42 |
43 // FaviconDownloadHelperDelegate methods | 43 // content::WebContentObserver implementation. |
44 virtual void OnUpdateFaviconURL( | 44 virtual void DidUpdateFaviconURL( |
45 int32 page_id, | 45 int32 page_id, |
46 const std::vector<FaviconURL>& candidates) OVERRIDE; | 46 const std::vector<FaviconURL>& candidates) OVERRIDE; |
47 | 47 |
48 virtual void OnDidDownloadFavicon( | 48 private: |
49 int id, | 49 void DidDownloadFavicon( |
50 const GURL& image_url, | 50 int id, |
51 bool errored, | 51 const GURL& image_url, |
52 int requested_size, | 52 bool errored, |
53 const std::vector<SkBitmap>& bitmaps) OVERRIDE; | 53 int requested_size, |
54 const std::vector<SkBitmap>& bitmaps); | |
54 | 55 |
55 private: | |
56 void DownloadFavicon(const GURL& image_url); | |
57 void AddFavicon(const GURL& image_url, const SkBitmap& new_bitmap); | 56 void AddFavicon(const GURL& image_url, const SkBitmap& new_bitmap); |
58 | 57 |
59 LauncherFaviconLoader::Delegate* delegate_; | 58 LauncherFaviconLoader::Delegate* delegate_; |
60 | 59 |
61 scoped_ptr<FaviconDownloadHelper> download_helper_; | 60 content::WebContents* web_contents_; |
62 | 61 |
63 typedef std::set<GURL> UrlSet; | 62 typedef std::set<GURL> UrlSet; |
64 // Map of pending download urls. | 63 // Map of pending download urls. |
65 UrlSet pending_requests_; | 64 UrlSet pending_requests_; |
66 // Map of processed urls. | 65 // Map of processed urls. |
67 UrlSet processed_requests_; | 66 UrlSet processed_requests_; |
68 // Current bitmap and source url. | 67 // Current bitmap and source url. |
69 SkBitmap bitmap_; | 68 SkBitmap bitmap_; |
70 GURL bitmap_url_; | 69 GURL bitmap_url_; |
71 | 70 |
72 DISALLOW_COPY_AND_ASSIGN(FaviconBitmapHandler); | 71 DISALLOW_COPY_AND_ASSIGN(FaviconBitmapHandler); |
73 }; | 72 }; |
74 | 73 |
75 void FaviconBitmapHandler::OnUpdateFaviconURL( | 74 void FaviconBitmapHandler::DidUpdateFaviconURL( |
76 int32 page_id, | 75 int32 page_id, |
77 const std::vector<FaviconURL>& candidates) { | 76 const std::vector<FaviconURL>& candidates) { |
78 // This function receives a complete list of faviocn urls for the page. | 77 // 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 | 78 // 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 | 79 // 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. | 80 // and pending urls, but only until they are removed from the list. |
82 UrlSet new_pending, new_processed; | 81 UrlSet new_pending, new_processed; |
83 // Create a map of valid favicon urls. | 82 // Create a map of valid favicon urls. |
84 std::set<GURL> urls; | 83 std::set<GURL> urls; |
85 for (std::vector<FaviconURL>::const_iterator iter = candidates.begin(); | 84 for (std::vector<FaviconURL>::const_iterator iter = candidates.begin(); |
(...skipping 17 matching lines...) Expand all Loading... | |
103 bitmap_.reset(); | 102 bitmap_.reset(); |
104 } | 103 } |
105 // Request any new urls. | 104 // Request any new urls. |
106 for (std::set<GURL>::iterator iter = urls.begin(); | 105 for (std::set<GURL>::iterator iter = urls.begin(); |
107 iter != urls.end(); ++iter) { | 106 iter != urls.end(); ++iter) { |
108 if (processed_requests_.find(*iter) != processed_requests_.end()) | 107 if (processed_requests_.find(*iter) != processed_requests_.end()) |
109 continue; // Skip already processed downloads. | 108 continue; // Skip already processed downloads. |
110 if (pending_requests_.find(*iter) != pending_requests_.end()) | 109 if (pending_requests_.find(*iter) != pending_requests_.end()) |
111 continue; // Skip already pending downloads. | 110 continue; // Skip already pending downloads. |
112 pending_requests_.insert(*iter); | 111 pending_requests_.insert(*iter); |
113 download_helper_->DownloadFavicon(*iter, 0); | 112 web_contents_->DownloadFavicon(*iter, 0, |
113 base::Bind(&FaviconBitmapHandler::DidDownloadFavicon, | |
114 base::Unretained(this))); | |
jam
2012/12/03 21:59:39
ditto re lifetime
| |
114 } | 115 } |
115 } | 116 } |
116 | 117 |
117 void FaviconBitmapHandler::OnDidDownloadFavicon( | 118 bool FaviconBitmapHandler::HasPendingDownloads() const { |
119 return !pending_requests_.empty(); | |
120 } | |
121 | |
122 void FaviconBitmapHandler::DidDownloadFavicon( | |
118 int id, | 123 int id, |
119 const GURL& image_url, | 124 const GURL& image_url, |
120 bool errored, | 125 bool errored, |
121 int requested_size, | 126 int requested_size, |
122 const std::vector<SkBitmap>& bitmaps) { | 127 const std::vector<SkBitmap>& bitmaps) { |
123 UrlSet::iterator iter = pending_requests_.find(image_url); | 128 UrlSet::iterator iter = pending_requests_.find(image_url); |
124 if (iter == pending_requests_.end()) { | 129 if (iter == pending_requests_.end()) { |
125 // Updates are received for all downloads; ignore unrequested urls. | 130 // Updates are received for all downloads; ignore unrequested urls. |
126 return; | 131 return; |
127 } | 132 } |
128 pending_requests_.erase(iter); | 133 pending_requests_.erase(iter); |
129 | 134 |
130 // Favicon bitmaps are ordered by decreasing width. | 135 // Favicon bitmaps are ordered by decreasing width. |
131 if (!errored && !bitmaps.empty()) | 136 if (!errored && !bitmaps.empty()) |
132 AddFavicon(image_url, bitmaps[0]); | 137 AddFavicon(image_url, bitmaps[0]); |
133 } | 138 } |
134 | 139 |
135 bool FaviconBitmapHandler::HasPendingDownloads() const { | |
136 return !pending_requests_.empty(); | |
137 } | |
138 | |
139 void FaviconBitmapHandler::AddFavicon(const GURL& image_url, | 140 void FaviconBitmapHandler::AddFavicon(const GURL& image_url, |
140 const SkBitmap& new_bitmap) { | 141 const SkBitmap& new_bitmap) { |
141 processed_requests_.insert(image_url); | 142 processed_requests_.insert(image_url); |
142 if (new_bitmap.height() > kMaxBitmapSize || | 143 if (new_bitmap.height() > kMaxBitmapSize || |
143 new_bitmap.width() > kMaxBitmapSize) | 144 new_bitmap.width() > kMaxBitmapSize) |
144 return; | 145 return; |
145 if (new_bitmap.height() < ash::kLauncherPreferredSize) | 146 if (new_bitmap.height() < ash::kLauncherPreferredSize) |
146 return; | 147 return; |
147 if (!bitmap_.isNull()) { | 148 if (!bitmap_.isNull()) { |
148 // We want the smallest icon that is large enough. | 149 // We want the smallest icon that is large enough. |
(...skipping 19 matching lines...) Expand all Loading... | |
168 LauncherFaviconLoader::~LauncherFaviconLoader() { | 169 LauncherFaviconLoader::~LauncherFaviconLoader() { |
169 } | 170 } |
170 | 171 |
171 SkBitmap LauncherFaviconLoader::GetFavicon() const { | 172 SkBitmap LauncherFaviconLoader::GetFavicon() const { |
172 return favicon_handler_->bitmap(); | 173 return favicon_handler_->bitmap(); |
173 } | 174 } |
174 | 175 |
175 bool LauncherFaviconLoader::HasPendingDownloads() const { | 176 bool LauncherFaviconLoader::HasPendingDownloads() const { |
176 return favicon_handler_->HasPendingDownloads(); | 177 return favicon_handler_->HasPendingDownloads(); |
177 } | 178 } |
OLD | NEW |