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/webui/favicon_source.h" | 5 #include "chrome/browser/ui/webui/favicon_source.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "chrome/browser/favicon/favicon_service_factory.h" | 9 #include "chrome/browser/favicon/favicon_service_factory.h" |
10 #include "chrome/browser/history/top_sites.h" | 10 #include "chrome/browser/history/top_sites.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
13 #include "chrome/browser/ui/webui/web_ui_util.h" | 12 #include "chrome/browser/ui/webui/web_ui_util.h" |
14 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
15 #include "grit/locale_settings.h" | 14 #include "grit/locale_settings.h" |
16 #include "grit/ui_resources.h" | 15 #include "grit/ui_resources.h" |
17 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
18 #include "ui/base/layout.h" | 17 #include "ui/base/layout.h" |
19 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
20 | 19 |
| 20 FaviconSource::IconRequest::IconRequest() |
| 21 : size_in_dip(gfx::kFaviconSize), |
| 22 scale_factor(ui::SCALE_FACTOR_NONE) { |
| 23 } |
| 24 |
| 25 FaviconSource::IconRequest::IconRequest( |
| 26 const content::URLDataSource::GotDataCallback& cb, |
| 27 const std::string& path, |
| 28 int size, |
| 29 ui::ScaleFactor scale) |
| 30 : callback(cb), |
| 31 request_path(path), |
| 32 size_in_dip(size), |
| 33 scale_factor(scale) { |
| 34 } |
| 35 |
| 36 FaviconSource::IconRequest::~IconRequest() { |
| 37 } |
| 38 |
21 FaviconSource::FaviconSource(Profile* profile, IconType type) | 39 FaviconSource::FaviconSource(Profile* profile, IconType type) |
22 : profile_(profile->GetOriginalProfile()), | 40 : profile_(profile->GetOriginalProfile()), |
23 icon_types_(type == FAVICON ? history::FAVICON : | 41 icon_types_(type == FAVICON ? history::FAVICON : |
24 history::TOUCH_PRECOMPOSED_ICON | history::TOUCH_ICON | | 42 history::TOUCH_PRECOMPOSED_ICON | history::TOUCH_ICON | |
25 history::FAVICON) { | 43 history::FAVICON) { |
26 } | 44 } |
27 | 45 |
28 FaviconSource::~FaviconSource() { | 46 FaviconSource::~FaviconSource() { |
29 } | 47 } |
30 | 48 |
31 std::string FaviconSource::GetSource() { | 49 std::string FaviconSource::GetSource() { |
32 return icon_types_ == history::FAVICON ? | 50 return icon_types_ == history::FAVICON ? |
33 chrome::kChromeUIFaviconHost : chrome::kChromeUITouchIconHost; | 51 chrome::kChromeUIFaviconHost : chrome::kChromeUITouchIconHost; |
34 } | 52 } |
35 | 53 |
36 void FaviconSource::StartDataRequest(const std::string& path, | 54 void FaviconSource::StartDataRequest( |
37 bool is_incognito, | 55 const std::string& path, |
38 int request_id) { | 56 bool is_incognito, |
| 57 const content::URLDataSource::GotDataCallback& callback) { |
39 FaviconService* favicon_service = | 58 FaviconService* favicon_service = |
40 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 59 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
41 if (!favicon_service || path.empty()) { | 60 if (!favicon_service || path.empty()) { |
42 SendDefaultResponse(IconRequest(request_id, | 61 SendDefaultResponse(IconRequest(callback, |
43 "", | 62 "", |
44 16, | 63 16, |
45 ui::SCALE_FACTOR_100P)); | 64 ui::SCALE_FACTOR_100P)); |
46 return; | 65 return; |
47 } | 66 } |
48 | 67 |
49 int size_in_dip = gfx::kFaviconSize; | 68 int size_in_dip = gfx::kFaviconSize; |
50 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_100P; | 69 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_100P; |
51 | 70 |
52 if (path.size() > 8 && | 71 if (path.size() > 8 && |
53 (path.substr(0, 8) == "iconurl/" || path.substr(0, 8) == "iconurl@")) { | 72 (path.substr(0, 8) == "iconurl/" || path.substr(0, 8) == "iconurl@")) { |
54 size_t prefix_length = 8; | 73 size_t prefix_length = 8; |
55 // Optional scale factor appended to iconurl, which may be @1x or @2x. | 74 // Optional scale factor appended to iconurl, which may be @1x or @2x. |
56 if (path.at(7) == '@') { | 75 if (path.at(7) == '@') { |
57 size_t slash = path.find("/"); | 76 size_t slash = path.find("/"); |
58 std::string scale_str = path.substr(8, slash - 8); | 77 std::string scale_str = path.substr(8, slash - 8); |
59 web_ui_util::ParseScaleFactor(scale_str, &scale_factor); | 78 web_ui_util::ParseScaleFactor(scale_str, &scale_factor); |
60 prefix_length = slash + 1; | 79 prefix_length = slash + 1; |
61 } | 80 } |
62 // TODO(michaelbai): Change GetRawFavicon to support combination of | 81 // TODO(michaelbai): Change GetRawFavicon to support combination of |
63 // IconType. | 82 // IconType. |
64 favicon_service->GetRawFavicon( | 83 favicon_service->GetRawFavicon( |
65 GURL(path.substr(prefix_length)), | 84 GURL(path.substr(prefix_length)), |
66 history::FAVICON, | 85 history::FAVICON, |
67 size_in_dip, | 86 size_in_dip, |
68 scale_factor, | 87 scale_factor, |
69 base::Bind(&FaviconSource::OnFaviconDataAvailable, | 88 base::Bind(&FaviconSource::OnFaviconDataAvailable, |
70 base::Unretained(this), | 89 base::Unretained(this), |
71 IconRequest(request_id, | 90 IconRequest(callback, |
72 path.substr(prefix_length), | 91 path.substr(prefix_length), |
73 size_in_dip, | 92 size_in_dip, |
74 scale_factor)), | 93 scale_factor)), |
75 &cancelable_task_tracker_); | 94 &cancelable_task_tracker_); |
76 } else { | 95 } else { |
77 GURL url; | 96 GURL url; |
78 if (path.size() > 5 && path.substr(0, 5) == "size/") { | 97 if (path.size() > 5 && path.substr(0, 5) == "size/") { |
79 size_t slash = path.find("/", 5); | 98 size_t slash = path.find("/", 5); |
80 size_t scale_delimiter = path.find("@", 5); | 99 size_t scale_delimiter = path.find("@", 5); |
81 std::string size = path.substr(5, slash - 5); | 100 std::string size = path.substr(5, slash - 5); |
(...skipping 26 matching lines...) Expand all Loading... |
108 url = GURL(originalUrl).GetOrigin(); | 127 url = GURL(originalUrl).GetOrigin(); |
109 } else { | 128 } else { |
110 url = GURL(path); | 129 url = GURL(path); |
111 } | 130 } |
112 } | 131 } |
113 | 132 |
114 // Intercept requests for prepopulated pages. | 133 // Intercept requests for prepopulated pages. |
115 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) { | 134 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) { |
116 if (url.spec() == | 135 if (url.spec() == |
117 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) { | 136 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) { |
118 url_data_source()->SendResponse(request_id, | 137 callback.Run( |
119 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( | 138 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( |
120 history::kPrepopulatedPages[i].favicon_id, | 139 history::kPrepopulatedPages[i].favicon_id, |
121 scale_factor)); | 140 scale_factor)); |
122 return; | 141 return; |
123 } | 142 } |
124 } | 143 } |
125 | 144 |
126 favicon_service->GetRawFaviconForURL( | 145 favicon_service->GetRawFaviconForURL( |
127 FaviconService::FaviconForURLParams( | 146 FaviconService::FaviconForURLParams( |
128 profile_, url, icon_types_, size_in_dip), | 147 profile_, url, icon_types_, size_in_dip), |
129 scale_factor, | 148 scale_factor, |
130 base::Bind(&FaviconSource::OnFaviconDataAvailable, | 149 base::Bind(&FaviconSource::OnFaviconDataAvailable, |
131 base::Unretained(this), | 150 base::Unretained(this), |
132 IconRequest(request_id, | 151 IconRequest(callback, |
133 url.spec(), | 152 url.spec(), |
134 size_in_dip, | 153 size_in_dip, |
135 scale_factor)), | 154 scale_factor)), |
136 &cancelable_task_tracker_); | 155 &cancelable_task_tracker_); |
137 } | 156 } |
138 } | 157 } |
139 | 158 |
140 std::string FaviconSource::GetMimeType(const std::string&) const { | 159 std::string FaviconSource::GetMimeType(const std::string&) const { |
141 // We need to explicitly return a mime type, otherwise if the user tries to | 160 // We need to explicitly return a mime type, otherwise if the user tries to |
142 // drag the image they get no extension. | 161 // drag the image they get no extension. |
(...skipping 10 matching lines...) Expand all Loading... |
153 // No additional checks to locate the favicon resource in the base | 172 // No additional checks to locate the favicon resource in the base |
154 // implementation. | 173 // implementation. |
155 return false; | 174 return false; |
156 } | 175 } |
157 | 176 |
158 void FaviconSource::OnFaviconDataAvailable( | 177 void FaviconSource::OnFaviconDataAvailable( |
159 const IconRequest& request, | 178 const IconRequest& request, |
160 const history::FaviconBitmapResult& bitmap_result) { | 179 const history::FaviconBitmapResult& bitmap_result) { |
161 if (bitmap_result.is_valid()) { | 180 if (bitmap_result.is_valid()) { |
162 // Forward the data along to the networking system. | 181 // Forward the data along to the networking system. |
163 url_data_source()->SendResponse( | 182 request.callback.Run(bitmap_result.bitmap_data); |
164 request.request_id, bitmap_result.bitmap_data); | |
165 } else if (!HandleMissingResource(request)) { | 183 } else if (!HandleMissingResource(request)) { |
166 SendDefaultResponse(request); | 184 SendDefaultResponse(request); |
167 } | 185 } |
168 } | 186 } |
169 | 187 |
170 void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { | 188 void FaviconSource::SendDefaultResponse(const IconRequest& icon_request) { |
171 int favicon_index; | 189 int favicon_index; |
172 int resource_id; | 190 int resource_id; |
173 switch (icon_request.size_in_dip) { | 191 switch (icon_request.size_in_dip) { |
174 case 64: | 192 case 64: |
(...skipping 11 matching lines...) Expand all Loading... |
186 } | 204 } |
187 base::RefCountedMemory* default_favicon = default_favicons_[favicon_index]; | 205 base::RefCountedMemory* default_favicon = default_favicons_[favicon_index]; |
188 | 206 |
189 if (!default_favicon) { | 207 if (!default_favicon) { |
190 ui::ScaleFactor scale_factor = icon_request.scale_factor; | 208 ui::ScaleFactor scale_factor = icon_request.scale_factor; |
191 default_favicon = ResourceBundle::GetSharedInstance() | 209 default_favicon = ResourceBundle::GetSharedInstance() |
192 .LoadDataResourceBytesForScale(resource_id, scale_factor); | 210 .LoadDataResourceBytesForScale(resource_id, scale_factor); |
193 default_favicons_[favicon_index] = default_favicon; | 211 default_favicons_[favicon_index] = default_favicon; |
194 } | 212 } |
195 | 213 |
196 url_data_source()->SendResponse(icon_request.request_id, default_favicon); | 214 icon_request.callback.Run(default_favicon); |
197 } | 215 } |
OLD | NEW |