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/web_applications/hosted_app_tab_helper_delegate.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) | |
|
tapted
2013/11/11 08:52:57
initialize delegate_ to NULL
calamity
2013/11/13 06:37:14
Done.
| |
| 19 : content::WebContentsObserver(web_contents), | |
| 20 fetch_icons_immediately_(false) { | |
| 21 } | |
| 22 | |
| 23 HostedAppTabHelper::~HostedAppTabHelper() { | |
| 24 } | |
| 25 | |
| 26 void HostedAppTabHelper::CreateHostedApp( | |
| 27 const WebApplicationInfo& web_app_info) { | |
| 28 // If a hosted app creation request is already pending, ignore subsequent | |
| 29 // requests. | |
| 30 if (web_app_info_) | |
| 31 return; | |
| 32 | |
| 33 web_app_info_.reset(new WebApplicationInfo()); | |
|
tapted
2013/11/11 08:52:57
nit: pretty sure you can still call the copy const
calamity
2013/11/13 06:37:14
Done.
| |
| 34 *web_app_info_ = web_app_info; | |
| 35 // It's possible for |favicon_url_candidates_| to be populated after this is | |
| 36 // called. DidUpdateFaviconURL() will resume the app creation process in this | |
| 37 // case. | |
| 38 if (!favicon_url_candidates_) | |
|
tapted
2013/11/11 08:52:57
Could this just be `if (favicon_url_candidates.emp
calamity
2013/11/13 06:37:14
I wanted it as a scoped_ptr so that its existence
| |
| 39 return; | |
| 40 | |
| 41 FetchIconsIfNecessaryForCreateHostedApp(); | |
| 42 } | |
| 43 | |
| 44 void HostedAppTabHelper::FetchIcons() { | |
| 45 for (std::set<GURL>::const_iterator iter = favicon_url_candidates_->begin(); | |
| 46 iter != favicon_url_candidates_->end(); ++iter) { | |
| 47 in_progress_requests_.insert( | |
|
tapted
2013/11/11 08:52:57
Maybe make these 7 lines a helper function `Downlo
calamity
2013/11/13 06:37:14
Done.
tapted
2013/11/13 07:44:53
Did you check up on the weakptr vs unretained goo?
| |
| 48 web_contents()->DownloadImage( | |
| 49 *iter, | |
| 50 true, // is_favicon | |
| 51 0, // no max size | |
| 52 base::Bind(&HostedAppTabHelper::DidDownloadFavicon, | |
| 53 base::Unretained(this)))); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 void HostedAppTabHelper::FetchIconsIfNecessaryForCreateHostedApp() { | |
| 58 // If there are extra icon URLs in the WebApplicationInfo, we should download | |
|
tapted
2013/11/11 08:52:57
nit: remove `we should` from comment
calamity
2013/11/13 06:37:14
Done.
| |
| 59 // them. | |
| 60 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it = | |
| 61 web_app_info_->icons.begin(); it != web_app_info_->icons.end(); | |
| 62 ++it) { | |
| 63 if (it->url.is_valid()) { | |
| 64 std::set<GURL>::iterator url_it = | |
| 65 favicon_url_candidates_->lower_bound(it->url); | |
|
tapted
2013/11/11 08:52:57
I don't think lower_bound is what's needed here..
calamity
2013/11/13 06:37:14
Done.
| |
| 66 if (url_it == favicon_url_candidates_->end()) { | |
| 67 favicon_url_candidates_->insert(url_it, it->url); | |
| 68 in_progress_requests_.insert( | |
| 69 web_contents()->DownloadImage(it->url, true, 0, | |
| 70 base::Bind(&HostedAppTabHelper::DidDownloadFavicon, | |
| 71 base::Unretained(this)))); | |
| 72 } | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 // If the delegate doesn't need the window icon, we wouldn't have fetched the | |
| 77 // icons yet. | |
| 78 // If no more downloads are pending, we can proceed directly to creating the | |
| 79 // hosted app. | |
| 80 if (!fetch_icons_immediately_) | |
| 81 FetchIcons(); | |
| 82 else if (in_progress_requests_.empty()) | |
| 83 FinishCreateHostedApp(); | |
| 84 } | |
| 85 | |
| 86 void HostedAppTabHelper::FinishCreateHostedApp() { | |
| 87 web_app_info_->is_bookmark_app = true; | |
| 88 | |
| 89 web_app_info_->app_url = web_app_info_->app_url.is_empty() ? | |
| 90 web_contents()->GetURL() : web_app_info_->app_url; | |
| 91 web_app_info_->title = web_app_info_->title.empty() | |
|
tapted
2013/11/11 08:52:57
nit: the nested ternaries are a bit hard to read,
calamity
2013/11/13 06:37:14
Done.
| |
| 92 ? (web_contents()->GetTitle().empty() | |
| 93 ? UTF8ToUTF16(web_app_info_->app_url.spec()) | |
| 94 : web_contents()->GetTitle()) | |
| 95 : web_app_info_->title; | |
| 96 web_app_info_->urls.push_back(web_app_info_->app_url); | |
| 97 | |
| 98 // Add the downloaded icons. | |
| 99 for (gfx::ImageFamily::const_iterator it = icon_family_.begin(); | |
| 100 it != icon_family_.end(); ++it) { | |
| 101 if (!it->IsEmpty()) { | |
| 102 WebApplicationInfo::IconInfo icon_info; | |
| 103 icon_info.data = it->AsBitmap(); | |
| 104 icon_info.width = icon_info.data.width(); | |
| 105 icon_info.height = icon_info.data.height(); | |
| 106 web_app_info_->icons.push_back(icon_info); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 Profile* profile = | |
| 111 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 112 scoped_refptr<extensions::CrxInstaller> installer( | |
| 113 extensions::CrxInstaller::CreateSilent(profile->GetExtensionService())); | |
| 114 installer->set_error_on_unsupported_requirements(true); | |
| 115 installer->InstallWebApp(*web_app_info_); | |
| 116 | |
| 117 web_app_info_.reset(); | |
| 118 } | |
| 119 | |
| 120 void HostedAppTabHelper::DidDownloadFavicon( | |
| 121 int id, | |
| 122 int http_status_code, | |
| 123 const GURL& image_url, | |
| 124 const std::vector<SkBitmap>& bitmaps, | |
| 125 const std::vector<gfx::Size>& original_bitmap_sizes) { | |
| 126 | |
| 127 std::set<int>::iterator iter = in_progress_requests_.find(id); | |
| 128 // Check that this request is one of ours. | |
|
tapted
2013/11/11 08:52:57
should this always be true? (DCHECK?)
calamity
2013/11/13 06:37:14
in_progress_requests could have been cleared by a
tapted
2013/11/13 07:44:53
oops - I realised that after writing that comment.
| |
| 129 if (iter == in_progress_requests_.end()) | |
| 130 return; | |
| 131 | |
| 132 in_progress_requests_.erase(iter); | |
|
tapted
2013/11/11 08:52:57
erase(val) returns the number of things erased. So
calamity
2013/11/13 06:37:14
Done.
| |
| 133 | |
| 134 for (std::vector<SkBitmap>::const_iterator it = bitmaps.begin(); | |
| 135 it != bitmaps.end(); ++it) { | |
| 136 icon_family_.Add(gfx::Image::CreateFrom1xBitmap(*it)); | |
| 137 } | |
| 138 | |
| 139 // Once all requests have been resolved, perform post-download tasks. | |
| 140 if (!in_progress_requests_.empty()) | |
| 141 return; | |
| 142 | |
| 143 if (delegate_) | |
| 144 delegate_->OnWindowIconLoaded(web_contents()); | |
| 145 | |
| 146 if (web_app_info_) | |
| 147 FinishCreateHostedApp(); | |
| 148 } | |
| 149 | |
| 150 // content::WebContentsObserver overrides: | |
| 151 void HostedAppTabHelper::DidNavigateMainFrame( | |
| 152 const content::LoadCommittedDetails& details, | |
| 153 const content::FrameNavigateParams& params) { | |
| 154 // Clear all pending requests. | |
| 155 favicon_url_candidates_.reset(); | |
| 156 in_progress_requests_.clear(); | |
| 157 web_app_info_.reset(); | |
| 158 icon_family_.clear(); | |
| 159 } | |
| 160 | |
| 161 void HostedAppTabHelper::DidUpdateFaviconURL( | |
| 162 int32 page_id, | |
| 163 const std::vector<content::FaviconURL>& candidates) { | |
| 164 in_progress_requests_.clear(); | |
| 165 icon_family_.clear(); | |
| 166 favicon_url_candidates_.reset(new std::set<GURL>()); | |
|
tapted
2013/11/11 08:52:57
std::set constructor can take two iterators i.e.
calamity
2013/11/13 06:37:14
This needs to grab the icon_url from each element.
tapted
2013/11/13 07:44:53
Ah, so it does.
Does this needs a comment that (f
| |
| 167 for (std::vector<content::FaviconURL>::const_iterator it = candidates.begin(); | |
| 168 it != candidates.end(); ++it) { | |
| 169 favicon_url_candidates_->insert(it->icon_url); | |
| 170 } | |
| 171 | |
| 172 if (fetch_icons_immediately_) | |
| 173 FetchIcons(); | |
| 174 | |
| 175 // If |web_app_info_| is populated, we are in the middle of creating a hosted | |
| 176 // app. Resume this process. | |
| 177 if (web_app_info_) | |
| 178 FetchIconsIfNecessaryForCreateHostedApp(); | |
| 179 } | |
| OLD | NEW |