Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: chrome/browser/ui/web_applications/hosted_app_tab_helper.cc

Issue 64853004: Use high resolution icons where possible for streamlined hosted app icons. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@browser_experiment_create_app_from_page
Patch Set: rework Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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)
19 : content::WebContentsObserver(web_contents),
20 fetch_icons_immediately_(false),
21 delegate_(NULL) {
22 }
23
24 HostedAppTabHelper::~HostedAppTabHelper() {
25 }
26
27 void HostedAppTabHelper::CreateHostedApp(
28 const WebApplicationInfo& web_app_info) {
29 // If a hosted app creation request is already pending, ignore subsequent
30 // requests.
31 if (web_app_info_)
32 return;
33
34 web_app_info_.reset(new WebApplicationInfo(web_app_info));
35
36 // It's possible for |favicon_url_candidates_| to be populated after this is
37 // called. DidUpdateFaviconURL() will resume the app creation process in this
38 // case.
39 if (!favicon_url_candidates_)
40 return;
41
42 FetchIconsIfNecessaryForCreateHostedApp();
43 }
44
45 void HostedAppTabHelper::SetDelegate(HostedAppTabHelperDelegate* delegate) {
46 delegate_ = delegate;
47 #if defined(OS_WIN)
48 // On Windows the icons are needed for the window icon which should be fetched
49 // as soon as we have the favicon URLs.
50 fetch_icons_immediately_ = true;
51 #endif
52 }
53
54 void HostedAppTabHelper::FetchIcons() {
55 for (std::set<GURL>::const_iterator it = favicon_url_candidates_->begin();
56 it != favicon_url_candidates_->end(); ++it)
57 in_progress_requests_.insert(DownloadImage(*it));
58
59 }
60
61 void HostedAppTabHelper::FetchIconsIfNecessaryForCreateHostedApp() {
62 // If the delegate doesn't need the window icon, we wouldn't have fetched the
63 // icons yet.
64 if (!fetch_icons_immediately_)
65 FetchIcons();
66
67 // If there are extra icon URLs in the WebApplicationInfo, download them.
68 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it =
69 web_app_info_->icons.begin(); it != web_app_info_->icons.end();
70 ++it) {
71 if (it->url.is_valid() && favicon_url_candidates_->count(it->url) == 0)
72 in_progress_requests_.insert(DownloadImage(it->url));
73 }
74
75 // If no downloads are pending, we can proceed directly to creating the hosted
76 // app.
77 if (in_progress_requests_.empty())
78 FinishCreateHostedApp();
79 }
80
81 void HostedAppTabHelper::FinishCreateHostedApp() {
82 if (web_app_info_->app_url.is_empty())
83 web_app_info_->app_url = web_contents()->GetURL();
84
85 if (web_app_info_->title.empty())
86 web_app_info_->title = web_contents()->GetTitle();
87 if (web_app_info_->title.empty())
88 web_app_info_->title = UTF8ToUTF16(web_app_info_->app_url.spec());
89
90 web_app_info_->urls.push_back(web_app_info_->app_url);
91 web_app_info_->is_bookmark_app = true;
92
93 // Add the downloaded icons.
94 for (gfx::ImageFamily::const_iterator it = image_family_.begin();
95 it != image_family_.end(); ++it) {
96 if (!it->IsEmpty()) {
97 WebApplicationInfo::IconInfo icon_info;
98 icon_info.data = it->AsBitmap();
99 icon_info.width = icon_info.data.width();
100 icon_info.height = icon_info.data.height();
101 web_app_info_->icons.push_back(icon_info);
102 }
103 }
104
105 Profile* profile =
106 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
107 scoped_refptr<extensions::CrxInstaller> installer(
108 extensions::CrxInstaller::CreateSilent(profile->GetExtensionService()));
109 installer->set_error_on_unsupported_requirements(true);
110 installer->InstallWebApp(*web_app_info_);
111
112 web_app_info_.reset();
113 }
114
115 void HostedAppTabHelper::DidDownloadFavicon(
116 int id,
117 int http_status_code,
118 const GURL& image_url,
119 const std::vector<SkBitmap>& bitmaps,
120 const std::vector<gfx::Size>& original_bitmap_sizes) {
121 // Request canceled by DidUpdateFaviconURL() or DidNavigateMainFrame().
122 if (in_progress_requests_.erase(id) == 0)
123 return;
124 for (std::vector<SkBitmap>::const_iterator it = bitmaps.begin();
125 it != bitmaps.end(); ++it) {
126 image_family_.Add(gfx::Image::CreateFrom1xBitmap(*it));
127 }
128
129 // Once all requests have been resolved, perform post-download tasks.
130 if (!in_progress_requests_.empty())
131 return;
132
133 if (delegate_)
134 delegate_->OnWindowIconLoaded(web_contents());
135
136 if (web_app_info_)
137 FinishCreateHostedApp();
138 }
139
140 int HostedAppTabHelper::DownloadImage(const GURL& url) {
141 return web_contents()->DownloadImage(
142 url,
143 true, // is_favicon
144 0, // no max size
145 base::Bind(&HostedAppTabHelper::DidDownloadFavicon,
146 base::Unretained(this)));
147 }
148
149 // content::WebContentsObserver overrides:
150 void HostedAppTabHelper::DidNavigateMainFrame(
151 const content::LoadCommittedDetails& details,
152 const content::FrameNavigateParams& params) {
153 // Clear all pending requests.
154 favicon_url_candidates_.reset();
155 in_progress_requests_.clear();
156 web_app_info_.reset();
157 image_family_.clear();
158 }
159
160 void HostedAppTabHelper::DidUpdateFaviconURL(
161 int32 page_id,
162 const std::vector<content::FaviconURL>& candidates) {
163 in_progress_requests_.clear();
164 image_family_.clear();
165 favicon_url_candidates_.reset(new std::set<GURL>());
166 for (std::vector<content::FaviconURL>::const_iterator it = candidates.begin();
167 it != candidates.end(); ++it) {
168 if (it->icon_type != content::FaviconURL::INVALID_ICON)
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698