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 |
186 // Add urls from the WebApplicationInfo. | 189 // Add urls from the WebApplicationInfo. |
187 std::vector<GURL> web_app_info_icon_urls; | 190 std::vector<GURL> web_app_info_icon_urls; |
188 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it = | 191 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it = |
189 web_app_info_.icons.begin(); | 192 web_app_info_.icons.begin(); |
190 it != web_app_info_.icons.end(); | 193 it != web_app_info_.icons.end(); |
191 ++it) { | 194 ++it) { |
192 if (it->url.is_valid()) | 195 if (it->url.is_valid()) |
193 web_app_info_icon_urls.push_back(it->url); | 196 web_app_info_icon_urls.push_back(it->url); |
194 } | 197 } |
195 | 198 |
196 favicon_downloader_.reset( | 199 favicon_downloader_.reset( |
197 new FaviconDownloader(contents, | 200 new FaviconDownloader(contents, |
198 web_app_info_icon_urls, | 201 web_app_info_icon_urls, |
199 base::Bind(&BookmarkAppHelper::OnIconsDownloaded, | 202 base::Bind(&BookmarkAppHelper::OnIconsDownloaded, |
200 base::Unretained(this)))); | 203 base::Unretained(this)))); |
201 } | 204 } |
202 | 205 |
203 BookmarkAppHelper::~BookmarkAppHelper() {} | 206 BookmarkAppHelper::~BookmarkAppHelper() {} |
204 | 207 |
205 void BookmarkAppHelper::Create(const CreateBookmarkAppCallback& callback) { | 208 void BookmarkAppHelper::Create(const CreateBookmarkAppCallback& callback) { |
206 callback_ = callback; | 209 callback_ = callback; |
207 favicon_downloader_->Start(); | 210 |
| 211 if (favicon_downloader_.get()) |
| 212 favicon_downloader_->Start(); |
| 213 else |
| 214 OnIconsDownloaded(true, std::map<GURL, std::vector<SkBitmap> >()); |
208 } | 215 } |
209 | 216 |
210 void BookmarkAppHelper::OnIconsDownloaded( | 217 void BookmarkAppHelper::OnIconsDownloaded( |
211 bool success, | 218 bool success, |
212 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) { | 219 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) { |
213 // The tab has navigated away during the icon download. Cancel the bookmark | 220 // The tab has navigated away during the icon download. Cancel the bookmark |
214 // app creation. | 221 // app creation. |
215 if (!success) { | 222 if (!success) { |
216 favicon_downloader_.reset(); | 223 favicon_downloader_.reset(); |
217 callback_.Run(NULL, web_app_info_); | 224 callback_.Run(NULL, web_app_info_); |
(...skipping 14 matching lines...) Expand all Loading... |
232 map_it->second.begin(); | 239 map_it->second.begin(); |
233 bitmap_it != map_it->second.end(); | 240 bitmap_it != map_it->second.end(); |
234 ++bitmap_it) { | 241 ++bitmap_it) { |
235 if (bitmap_it->empty() || bitmap_it->width() != bitmap_it->height()) | 242 if (bitmap_it->empty() || bitmap_it->width() != bitmap_it->height()) |
236 continue; | 243 continue; |
237 | 244 |
238 downloaded_icons.push_back(*bitmap_it); | 245 downloaded_icons.push_back(*bitmap_it); |
239 } | 246 } |
240 } | 247 } |
241 | 248 |
| 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 |
242 // If there are icons that don't match the accepted icon sizes, find the | 261 // If there are icons that don't match the accepted icon sizes, find the |
243 // closest bigger icon to the accepted sizes and resize the icon to it. An | 262 // closest bigger icon to the accepted sizes and resize the icon to it. An |
244 // icon will be resized and used for at most one size. | 263 // icon will be resized and used for at most one size. |
245 std::map<int, SkBitmap> resized_bitmaps( | 264 std::map<int, SkBitmap> resized_bitmaps( |
246 ConstrainBitmapsToSizes(downloaded_icons, allowed_sizes)); | 265 ConstrainBitmapsToSizes(downloaded_icons, allowed_sizes)); |
247 | 266 |
248 // Generate container icons from smaller icons. | 267 // Generate container icons from smaller icons. |
249 const int kIconSizesToGenerate[] = {extension_misc::EXTENSION_ICON_SMALL, | 268 const int kIconSizesToGenerate[] = {extension_misc::EXTENSION_ICON_SMALL, |
250 extension_misc::EXTENSION_ICON_MEDIUM, }; | 269 extension_misc::EXTENSION_ICON_MEDIUM, }; |
251 const std::set<int> generate_sizes( | 270 const std::set<int> generate_sizes( |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 extension, info_list, base::Bind(&OnIconsLoaded, web_app_info, callback)); | 393 extension, info_list, base::Bind(&OnIconsLoaded, web_app_info, callback)); |
375 } | 394 } |
376 | 395 |
377 bool IsValidBookmarkAppUrl(const GURL& url) { | 396 bool IsValidBookmarkAppUrl(const GURL& url) { |
378 URLPattern origin_only_pattern(Extension::kValidWebExtentSchemes); | 397 URLPattern origin_only_pattern(Extension::kValidWebExtentSchemes); |
379 origin_only_pattern.SetMatchAllURLs(true); | 398 origin_only_pattern.SetMatchAllURLs(true); |
380 return url.is_valid() && origin_only_pattern.MatchesURL(url); | 399 return url.is_valid() && origin_only_pattern.MatchesURL(url); |
381 } | 400 } |
382 | 401 |
383 } // namespace extensions | 402 } // namespace extensions |
OLD | NEW |