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) | |
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 bool fetch_icons_immediately) { | |
47 delegate_ = delegate; | |
48 fetch_icons_immediately_ = fetch_icons_immediately; | |
49 } | |
50 | |
51 void HostedAppTabHelper::FetchIcons() { | |
52 for (std::set<GURL>::const_iterator it = favicon_url_candidates_->begin(); | |
53 it != favicon_url_candidates_->end(); ++it) | |
54 in_progress_requests_.insert(DownloadImage(*it)); | |
tapted
2013/11/13 07:44:53
would it make sense for the insert to happen in Do
calamity
2013/11/15 04:25:50
That's where it was, but it made testing difficult
| |
55 | |
56 } | |
57 | |
58 void HostedAppTabHelper::FetchIconsIfNecessaryForCreateHostedApp() { | |
59 // If the delegate doesn't need the window icon, we wouldn't have fetched the | |
60 // icons yet. | |
61 if (!fetch_icons_immediately_) | |
62 FetchIcons(); | |
63 | |
64 // If there are extra icon URLs in the WebApplicationInfo, download them. | |
65 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it = | |
66 web_app_info_->icons.begin(); it != web_app_info_->icons.end(); | |
67 ++it) { | |
68 if (it->url.is_valid() && favicon_url_candidates_->count(it->url) == 0) | |
69 in_progress_requests_.insert(DownloadImage(it->url)); | |
70 } | |
71 | |
72 // If no downloads are pending, we can proceed directly to creating the hosted | |
73 // app. | |
74 if (in_progress_requests_.empty()) | |
75 FinishCreateHostedApp(); | |
76 } | |
77 | |
78 void HostedAppTabHelper::FinishCreateHostedApp() { | |
79 if (web_app_info_->app_url.is_empty()) | |
80 web_app_info_->app_url = web_contents()->GetURL(); | |
81 | |
82 if (web_app_info_->title.empty()) | |
83 web_app_info_->title = web_contents()->GetTitle(); | |
84 if (web_app_info_->title.empty()) | |
85 web_app_info_->title = UTF8ToUTF16(web_app_info_->app_url.spec()); | |
86 | |
87 web_app_info_->urls.push_back(web_app_info_->app_url); | |
88 web_app_info_->is_bookmark_app = true; | |
89 | |
90 // Add the downloaded icons. | |
91 for (gfx::ImageFamily::const_iterator it = image_family_.begin(); | |
92 it != image_family_.end(); ++it) { | |
93 if (!it->IsEmpty()) { | |
94 WebApplicationInfo::IconInfo icon_info; | |
95 icon_info.data = it->AsBitmap(); | |
96 icon_info.width = icon_info.data.width(); | |
97 icon_info.height = icon_info.data.height(); | |
98 web_app_info_->icons.push_back(icon_info); | |
99 } | |
100 } | |
101 | |
102 Profile* profile = | |
103 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
104 scoped_refptr<extensions::CrxInstaller> installer( | |
105 extensions::CrxInstaller::CreateSilent(profile->GetExtensionService())); | |
106 installer->set_error_on_unsupported_requirements(true); | |
107 installer->InstallWebApp(*web_app_info_); | |
108 | |
109 web_app_info_.reset(); | |
110 } | |
111 | |
112 void HostedAppTabHelper::DidDownloadFavicon( | |
113 int id, | |
114 int http_status_code, | |
115 const GURL& image_url, | |
116 const std::vector<SkBitmap>& bitmaps, | |
117 const std::vector<gfx::Size>& original_bitmap_sizes) { | |
118 // Request canceled by DidUpdateFaviconURL() or DidNavigateMainFrame(). | |
119 if (in_progress_requests_.erase(id) == 0) | |
120 return; | |
121 for (std::vector<SkBitmap>::const_iterator it = bitmaps.begin(); | |
122 it != bitmaps.end(); ++it) { | |
123 image_family_.Add(gfx::Image::CreateFrom1xBitmap(*it)); | |
124 } | |
125 | |
126 // Once all requests have been resolved, perform post-download tasks. | |
127 if (!in_progress_requests_.empty()) | |
128 return; | |
129 | |
130 if (delegate_) | |
131 delegate_->OnWindowIconLoaded(web_contents()); | |
132 | |
133 if (web_app_info_) | |
134 FinishCreateHostedApp(); | |
135 } | |
136 | |
137 int HostedAppTabHelper::DownloadImage(const GURL& url) { | |
138 return web_contents()->DownloadImage( | |
139 url, | |
140 true, // is_favicon | |
141 0, // no max size | |
142 base::Bind(&HostedAppTabHelper::DidDownloadFavicon, | |
143 base::Unretained(this))); | |
144 } | |
145 | |
146 // content::WebContentsObserver overrides: | |
147 void HostedAppTabHelper::DidNavigateMainFrame( | |
148 const content::LoadCommittedDetails& details, | |
149 const content::FrameNavigateParams& params) { | |
150 // Clear all pending requests. | |
151 favicon_url_candidates_.reset(); | |
152 in_progress_requests_.clear(); | |
153 web_app_info_.reset(); | |
154 image_family_.clear(); | |
155 } | |
156 | |
157 void HostedAppTabHelper::DidUpdateFaviconURL( | |
158 int32 page_id, | |
159 const std::vector<content::FaviconURL>& candidates) { | |
160 in_progress_requests_.clear(); | |
161 image_family_.clear(); | |
162 favicon_url_candidates_.reset(new std::set<GURL>()); | |
163 for (std::vector<content::FaviconURL>::const_iterator it = candidates.begin(); | |
164 it != candidates.end(); ++it) { | |
165 favicon_url_candidates_->insert(it->icon_url); | |
166 } | |
167 | |
168 if (fetch_icons_immediately_) | |
169 FetchIcons(); | |
170 | |
171 // If |web_app_info_| is populated, we are in the middle of creating a hosted | |
172 // app. Resume this process. | |
173 if (web_app_info_) | |
174 FetchIconsIfNecessaryForCreateHostedApp(); | |
175 } | |
OLD | NEW |