Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/web_applications/hosted_app_tab_helper.h" | |
| 6 | |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "chrome/browser/extensions/crx_installer.h" | |
| 9 #include "chrome/browser/favicon/favicon_tab_helper.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/ui/browser.h" | |
| 12 #include "chrome/common/web_application_info.h" | |
| 13 #include "content/public/browser/web_contents.h" | |
| 14 #include "content/public/common/favicon_url.h" | |
| 15 | |
| 16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(HostedAppTabHelper); | |
| 17 | |
| 18 HostedAppTabHelper::HostedAppTabHelper(content::WebContents* web_contents) | |
| 19 : content::WebContentsObserver(web_contents) { | |
| 20 } | |
| 21 | |
| 22 HostedAppTabHelper::~HostedAppTabHelper() { | |
| 23 } | |
| 24 | |
| 25 void HostedAppTabHelper::CreateHostedApp( | |
| 26 const WebApplicationInfo& web_app_info) { | |
| 27 // If a hosted app creation request is already pending, ignore subsequent | |
| 28 // requests. | |
| 29 if (web_app_info_) | |
| 30 return; | |
| 31 | |
| 32 web_app_info_.reset(new WebApplicationInfo(web_app_info)); | |
| 33 | |
| 34 // It's possible for |favicon_url_candidates_| to be populated after this is | |
| 35 // called. DidUpdateFaviconURL() will resume the app creation process in this | |
| 36 // case. | |
| 37 if (!favicon_url_candidates_) | |
| 38 return; | |
| 39 | |
| 40 FetchIcons(); | |
|
pkotwicz
2013/11/25 01:21:28
Can we add a method to FaviconTabHelper to get the
| |
| 41 } | |
| 42 | |
| 43 void HostedAppTabHelper::FetchIcons() { | |
| 44 // Download icons from the favicon url candidates | |
| 45 for (std::set<GURL>::const_iterator it = favicon_url_candidates_->begin(); | |
| 46 it != favicon_url_candidates_->end(); ++it) | |
| 47 in_progress_requests_.insert(DownloadImage(*it)); | |
| 48 | |
| 49 // If there are extra icon URLs in the WebApplicationInfo, download them. | |
| 50 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it = | |
| 51 web_app_info_->icons.begin(); it != web_app_info_->icons.end(); | |
| 52 ++it) { | |
| 53 if (it->url.is_valid() && favicon_url_candidates_->count(it->url) == 0) | |
| 54 in_progress_requests_.insert(DownloadImage(it->url)); | |
| 55 } | |
| 56 | |
| 57 // If no downloads are pending, we can proceed directly to creating the hosted | |
| 58 // app. | |
| 59 if (in_progress_requests_.empty()) | |
| 60 FinishCreateHostedApp(); | |
| 61 } | |
| 62 | |
| 63 void HostedAppTabHelper::FinishCreateHostedApp() { | |
| 64 if (web_app_info_->app_url.is_empty()) | |
| 65 web_app_info_->app_url = web_contents()->GetURL(); | |
| 66 | |
| 67 if (web_app_info_->title.empty()) | |
| 68 web_app_info_->title = web_contents()->GetTitle(); | |
| 69 if (web_app_info_->title.empty()) | |
| 70 web_app_info_->title = UTF8ToUTF16(web_app_info_->app_url.spec()); | |
| 71 | |
| 72 web_app_info_->urls.push_back(web_app_info_->app_url); | |
| 73 web_app_info_->is_bookmark_app = true; | |
| 74 | |
| 75 // Add the downloaded icons. | |
| 76 for (gfx::ImageFamily::const_iterator it = image_family_.begin(); | |
| 77 it != image_family_.end(); ++it) { | |
| 78 if (!it->IsEmpty()) { | |
| 79 WebApplicationInfo::IconInfo icon_info; | |
| 80 icon_info.data = it->AsBitmap(); | |
| 81 icon_info.width = icon_info.data.width(); | |
| 82 icon_info.height = icon_info.data.height(); | |
| 83 web_app_info_->icons.push_back(icon_info); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 Profile* profile = | |
| 88 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 89 scoped_refptr<extensions::CrxInstaller> installer( | |
| 90 extensions::CrxInstaller::CreateSilent(profile->GetExtensionService())); | |
| 91 installer->set_error_on_unsupported_requirements(true); | |
| 92 installer->InstallWebApp(*web_app_info_); | |
| 93 | |
| 94 web_app_info_.reset(); | |
| 95 } | |
| 96 | |
| 97 void HostedAppTabHelper::DidDownloadFavicon( | |
| 98 int id, | |
| 99 int http_status_code, | |
| 100 const GURL& image_url, | |
| 101 const std::vector<SkBitmap>& bitmaps, | |
| 102 const std::vector<gfx::Size>& original_bitmap_sizes) { | |
| 103 // Request canceled by DidUpdateFaviconURL() or DidNavigateMainFrame(). | |
| 104 if (in_progress_requests_.erase(id) == 0) | |
| 105 return; | |
| 106 for (std::vector<SkBitmap>::const_iterator it = bitmaps.begin(); | |
| 107 it != bitmaps.end(); ++it) { | |
| 108 image_family_.Add(gfx::Image::CreateFrom1xBitmap(*it)); | |
| 109 } | |
| 110 | |
| 111 // Once all requests have been resolved, perform post-download tasks. | |
| 112 if (!in_progress_requests_.empty()) | |
| 113 return; | |
| 114 | |
| 115 if (web_app_info_) | |
| 116 FinishCreateHostedApp(); | |
| 117 } | |
| 118 | |
| 119 // content::WebContentsObserver overrides: | |
| 120 void HostedAppTabHelper::DidNavigateMainFrame( | |
| 121 const content::LoadCommittedDetails& details, | |
| 122 const content::FrameNavigateParams& params) { | |
| 123 // Clear all pending requests. | |
| 124 favicon_url_candidates_.reset(); | |
| 125 in_progress_requests_.clear(); | |
| 126 web_app_info_.reset(); | |
| 127 image_family_.clear(); | |
| 128 } | |
| 129 | |
| 130 void HostedAppTabHelper::DidUpdateFaviconURL( | |
| 131 int32 page_id, | |
| 132 const std::vector<content::FaviconURL>& candidates) { | |
| 133 in_progress_requests_.clear(); | |
| 134 image_family_.clear(); | |
| 135 favicon_url_candidates_.reset(new std::set<GURL>()); | |
| 136 for (std::vector<content::FaviconURL>::const_iterator it = candidates.begin(); | |
| 137 it != candidates.end(); ++it) { | |
| 138 if (it->icon_type != content::FaviconURL::INVALID_ICON) | |
| 139 favicon_url_candidates_->insert(it->icon_url); | |
| 140 } | |
| 141 | |
| 142 // If |web_app_info_| is populated, we are in the middle of creating a hosted | |
| 143 // app. Resume this process. | |
| 144 if (web_app_info_) | |
| 145 FetchIcons(); | |
| 146 } | |
| 147 | |
| 148 int HostedAppTabHelper::DownloadImage(const GURL& url) { | |
| 149 return web_contents()->DownloadImage( | |
| 150 url, | |
| 151 true, // is_favicon | |
| 152 0, // no max size | |
| 153 base::Bind(&HostedAppTabHelper::DidDownloadFavicon, | |
| 154 base::Unretained(this))); | |
| 155 } | |
| OLD | NEW |