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/extensions/bookmark_app_helper.h" | 5 #include "chrome/browser/extensions/bookmark_app_helper.h" |
6 | 6 |
7 #include <cctype> | 7 #include <cctype> |
8 | 8 |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 registrar_.Add(this, | 176 registrar_.Add(this, |
177 chrome::NOTIFICATION_CRX_INSTALLER_DONE, | 177 chrome::NOTIFICATION_CRX_INSTALLER_DONE, |
178 content::Source<CrxInstaller>(crx_installer_.get())); | 178 content::Source<CrxInstaller>(crx_installer_.get())); |
179 | 179 |
180 registrar_.Add(this, | 180 registrar_.Add(this, |
181 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, | 181 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
182 content::Source<CrxInstaller>(crx_installer_.get())); | 182 content::Source<CrxInstaller>(crx_installer_.get())); |
183 | 183 |
184 crx_installer_->set_error_on_unsupported_requirements(true); | 184 crx_installer_->set_error_on_unsupported_requirements(true); |
185 | 185 |
186 if (!contents) | |
187 return; | |
188 | |
189 // Add urls from the WebApplicationInfo. | 186 // Add urls from the WebApplicationInfo. |
190 std::vector<GURL> web_app_info_icon_urls; | 187 std::vector<GURL> web_app_info_icon_urls; |
191 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it = | 188 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it = |
192 web_app_info_.icons.begin(); | 189 web_app_info_.icons.begin(); |
193 it != web_app_info_.icons.end(); | 190 it != web_app_info_.icons.end(); |
194 ++it) { | 191 ++it) { |
195 if (it->url.is_valid()) | 192 if (it->url.is_valid()) |
196 web_app_info_icon_urls.push_back(it->url); | 193 web_app_info_icon_urls.push_back(it->url); |
197 } | 194 } |
198 | 195 |
199 favicon_downloader_.reset( | 196 favicon_downloader_.reset( |
200 new FaviconDownloader(contents, | 197 new FaviconDownloader(contents, |
201 web_app_info_icon_urls, | 198 web_app_info_icon_urls, |
202 base::Bind(&BookmarkAppHelper::OnIconsDownloaded, | 199 base::Bind(&BookmarkAppHelper::OnIconsDownloaded, |
203 base::Unretained(this)))); | 200 base::Unretained(this)))); |
204 } | 201 } |
205 | 202 |
206 BookmarkAppHelper::~BookmarkAppHelper() {} | 203 BookmarkAppHelper::~BookmarkAppHelper() {} |
207 | 204 |
208 void BookmarkAppHelper::Create(const CreateBookmarkAppCallback& callback) { | 205 void BookmarkAppHelper::Create(const CreateBookmarkAppCallback& callback) { |
209 callback_ = callback; | 206 callback_ = callback; |
210 | 207 favicon_downloader_->Start(); |
211 if (favicon_downloader_.get()) | |
212 favicon_downloader_->Start(); | |
213 else | |
214 OnIconsDownloaded(true, std::map<GURL, std::vector<SkBitmap> >()); | |
215 } | 208 } |
216 | 209 |
217 void BookmarkAppHelper::OnIconsDownloaded( | 210 void BookmarkAppHelper::OnIconsDownloaded( |
218 bool success, | 211 bool success, |
219 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) { | 212 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) { |
220 // The tab has navigated away during the icon download. Cancel the bookmark | 213 // The tab has navigated away during the icon download. Cancel the bookmark |
221 // app creation. | 214 // app creation. |
222 if (!success) { | 215 if (!success) { |
223 favicon_downloader_.reset(); | 216 favicon_downloader_.reset(); |
224 callback_.Run(NULL, web_app_info_); | 217 callback_.Run(NULL, web_app_info_); |
(...skipping 14 matching lines...) Expand all Loading... |
239 map_it->second.begin(); | 232 map_it->second.begin(); |
240 bitmap_it != map_it->second.end(); | 233 bitmap_it != map_it->second.end(); |
241 ++bitmap_it) { | 234 ++bitmap_it) { |
242 if (bitmap_it->empty() || bitmap_it->width() != bitmap_it->height()) | 235 if (bitmap_it->empty() || bitmap_it->width() != bitmap_it->height()) |
243 continue; | 236 continue; |
244 | 237 |
245 downloaded_icons.push_back(*bitmap_it); | 238 downloaded_icons.push_back(*bitmap_it); |
246 } | 239 } |
247 } | 240 } |
248 | 241 |
249 // Add all existing icons from WebApplicationInfo. | |
250 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it = | |
251 web_app_info_.icons.begin(); | |
252 it != web_app_info_.icons.end(); | |
253 ++it) { | |
254 const SkBitmap& icon = it->data; | |
255 if (!icon.drawsNothing() && icon.width() == icon.height()) | |
256 downloaded_icons.push_back(icon); | |
257 } | |
258 | |
259 web_app_info_.icons.clear(); | |
260 | |
261 // If there are icons that don't match the accepted icon sizes, find the | 242 // If there are icons that don't match the accepted icon sizes, find the |
262 // closest bigger icon to the accepted sizes and resize the icon to it. An | 243 // closest bigger icon to the accepted sizes and resize the icon to it. An |
263 // icon will be resized and used for at most one size. | 244 // icon will be resized and used for at most one size. |
264 std::map<int, SkBitmap> resized_bitmaps( | 245 std::map<int, SkBitmap> resized_bitmaps( |
265 ConstrainBitmapsToSizes(downloaded_icons, allowed_sizes)); | 246 ConstrainBitmapsToSizes(downloaded_icons, allowed_sizes)); |
266 | 247 |
267 // Generate container icons from smaller icons. | 248 // Generate container icons from smaller icons. |
268 const int kIconSizesToGenerate[] = {extension_misc::EXTENSION_ICON_SMALL, | 249 const int kIconSizesToGenerate[] = {extension_misc::EXTENSION_ICON_SMALL, |
269 extension_misc::EXTENSION_ICON_MEDIUM, }; | 250 extension_misc::EXTENSION_ICON_MEDIUM, }; |
270 const std::set<int> generate_sizes( | 251 const std::set<int> generate_sizes( |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 extension, info_list, base::Bind(&OnIconsLoaded, web_app_info, callback)); | 370 extension, info_list, base::Bind(&OnIconsLoaded, web_app_info, callback)); |
390 } | 371 } |
391 | 372 |
392 bool IsValidBookmarkAppUrl(const GURL& url) { | 373 bool IsValidBookmarkAppUrl(const GURL& url) { |
393 URLPattern origin_only_pattern(Extension::kValidWebExtentSchemes); | 374 URLPattern origin_only_pattern(Extension::kValidWebExtentSchemes); |
394 origin_only_pattern.SetMatchAllURLs(true); | 375 origin_only_pattern.SetMatchAllURLs(true); |
395 return url.is_valid() && origin_only_pattern.MatchesURL(url); | 376 return url.is_valid() && origin_only_pattern.MatchesURL(url); |
396 } | 377 } |
397 | 378 |
398 } // namespace extensions | 379 } // namespace extensions |
OLD | NEW |