| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/android/shortcut_data_fetcher.h" | 5 #include "chrome/browser/android/webapps/add_to_homescreen_data_fetcher.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
| 10 #include "base/task/cancelable_task_tracker.h" | 10 #include "base/task/cancelable_task_tracker.h" |
| 11 #include "chrome/browser/favicon/favicon_service_factory.h" | 11 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 12 #include "chrome/browser/manifest/manifest_icon_selector.h" | 12 #include "chrome/browser/manifest/manifest_icon_selector.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/chrome_constants.h" | 14 #include "chrome/common/chrome_constants.h" |
| 15 #include "chrome/common/render_messages.h" | 15 #include "chrome/common/render_messages.h" |
| 16 #include "chrome/common/web_application_info.h" | 16 #include "chrome/common/web_application_info.h" |
| 17 #include "components/dom_distiller/core/url_utils.h" | 17 #include "components/dom_distiller/core/url_utils.h" |
| 18 #include "components/favicon/core/favicon_service.h" | 18 #include "components/favicon/core/favicon_service.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/user_metrics.h" | 20 #include "content/public/browser/user_metrics.h" |
| 21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/browser/web_contents_observer.h" | 22 #include "content/public/browser/web_contents_observer.h" |
| 23 #include "content/public/common/frame_navigate_params.h" | 23 #include "content/public/common/frame_navigate_params.h" |
| 24 #include "content/public/common/manifest.h" | 24 #include "content/public/common/manifest.h" |
| 25 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree
nOrientationLockType.h" | 25 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree
nOrientationLockType.h" |
| 26 #include "ui/gfx/codec/png_codec.h" | 26 #include "ui/gfx/codec/png_codec.h" |
| 27 #include "ui/gfx/favicon_size.h" | 27 #include "ui/gfx/favicon_size.h" |
| 28 #include "ui/gfx/screen.h" | 28 #include "ui/gfx/screen.h" |
| 29 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 30 | 30 |
| 31 using content::Manifest; | 31 using content::Manifest; |
| 32 | 32 |
| 33 // Android's preferred icon size in DP is 48, as defined in | 33 // Android's preferred icon size in DP is 48, as defined in |
| 34 // http://developer.android.com/design/style/iconography.html | 34 // http://developer.android.com/design/style/iconography.html |
| 35 const int ShortcutDataFetcher::kPreferredIconSizeInDp = 48; | 35 const int AddToHomescreenDataFetcher::kPreferredIconSizeInDp = 48; |
| 36 | 36 |
| 37 ShortcutDataFetcher::ShortcutDataFetcher( | 37 AddToHomescreenDataFetcher::AddToHomescreenDataFetcher( |
| 38 content::WebContents* web_contents, | 38 content::WebContents* web_contents, |
| 39 Observer* observer) | 39 Observer* observer) |
| 40 : WebContentsObserver(web_contents), | 40 : WebContentsObserver(web_contents), |
| 41 weak_observer_(observer), | 41 weak_observer_(observer), |
| 42 is_waiting_for_web_application_info_(false), | 42 is_waiting_for_web_application_info_(false), |
| 43 is_icon_saved_(false), | 43 is_icon_saved_(false), |
| 44 is_ready_(false), | 44 is_ready_(false), |
| 45 icon_timeout_timer_(false, false), | 45 icon_timeout_timer_(false, false), |
| 46 shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( | 46 shortcut_info_(dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl( |
| 47 web_contents->GetURL())), | 47 web_contents->GetURL())), |
| 48 preferred_icon_size_in_px_(kPreferredIconSizeInDp * | 48 preferred_icon_size_in_px_(kPreferredIconSizeInDp * |
| 49 gfx::Screen::GetScreenFor(web_contents->GetNativeView())-> | 49 gfx::Screen::GetScreenFor(web_contents->GetNativeView())-> |
| 50 GetPrimaryDisplay().device_scale_factor()) { | 50 GetPrimaryDisplay().device_scale_factor()) { |
| 51 // Send a message to the renderer to retrieve information about the page. | 51 // Send a message to the renderer to retrieve information about the page. |
| 52 is_waiting_for_web_application_info_ = true; | 52 is_waiting_for_web_application_info_ = true; |
| 53 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); | 53 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void ShortcutDataFetcher::OnDidGetWebApplicationInfo( | 56 void AddToHomescreenDataFetcher::OnDidGetWebApplicationInfo( |
| 57 const WebApplicationInfo& received_web_app_info) { | 57 const WebApplicationInfo& received_web_app_info) { |
| 58 is_waiting_for_web_application_info_ = false; | 58 is_waiting_for_web_application_info_ = false; |
| 59 if (!web_contents() || !weak_observer_) return; | 59 if (!web_contents() || !weak_observer_) return; |
| 60 | 60 |
| 61 // Sanitize received_web_app_info. | 61 // Sanitize received_web_app_info. |
| 62 WebApplicationInfo web_app_info = received_web_app_info; | 62 WebApplicationInfo web_app_info = received_web_app_info; |
| 63 web_app_info.title = | 63 web_app_info.title = |
| 64 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength); | 64 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength); |
| 65 web_app_info.description = | 65 web_app_info.description = |
| 66 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength); | 66 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 86 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: | 86 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: |
| 87 content::RecordAction( | 87 content::RecordAction( |
| 88 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); | 88 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); |
| 89 break; | 89 break; |
| 90 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: | 90 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: |
| 91 content::RecordAction( | 91 content::RecordAction( |
| 92 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); | 92 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); |
| 93 break; | 93 break; |
| 94 } | 94 } |
| 95 | 95 |
| 96 web_contents()->GetManifest(base::Bind(&ShortcutDataFetcher::OnDidGetManifest, | 96 web_contents()->GetManifest( |
| 97 this)); | 97 base::Bind(&AddToHomescreenDataFetcher::OnDidGetManifest, this)); |
| 98 } | 98 } |
| 99 | 99 |
| 100 void ShortcutDataFetcher::OnDidGetManifest(const content::Manifest& manifest) { | 100 void AddToHomescreenDataFetcher::OnDidGetManifest( |
| 101 const content::Manifest& manifest) { |
| 101 if (!web_contents() || !weak_observer_) return; | 102 if (!web_contents() || !weak_observer_) return; |
| 102 | 103 |
| 103 if (!manifest.IsEmpty()) { | 104 if (!manifest.IsEmpty()) { |
| 104 content::RecordAction( | 105 content::RecordAction( |
| 105 base::UserMetricsAction("webapps.AddShortcut.Manifest")); | 106 base::UserMetricsAction("webapps.AddShortcut.Manifest")); |
| 106 shortcut_info_.UpdateFromManifest(manifest); | 107 shortcut_info_.UpdateFromManifest(manifest); |
| 107 } | 108 } |
| 108 | 109 |
| 109 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( | 110 GURL icon_src = ManifestIconSelector::FindBestMatchingIcon( |
| 110 manifest.icons, | 111 manifest.icons, |
| 111 kPreferredIconSizeInDp, | 112 kPreferredIconSizeInDp, |
| 112 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); | 113 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); |
| 113 if (icon_src.is_valid()) { | 114 if (icon_src.is_valid()) { |
| 114 // Grab the best icon from the manifest. | 115 // Grab the best icon from the manifest. |
| 115 web_contents()->DownloadImage( | 116 web_contents()->DownloadImage( |
| 116 icon_src, | 117 icon_src, |
| 117 false, | 118 false, |
| 118 preferred_icon_size_in_px_, | 119 preferred_icon_size_in_px_, |
| 119 false, | 120 false, |
| 120 base::Bind(&ShortcutDataFetcher::OnManifestIconFetched, | 121 base::Bind(&AddToHomescreenDataFetcher::OnManifestIconFetched, |
| 121 this)); | 122 this)); |
| 122 } else { | 123 } else { |
| 123 // Grab the best favicon for the page. | 124 // Grab the best favicon for the page. |
| 124 FetchFavicon(); | 125 FetchFavicon(); |
| 125 } | 126 } |
| 126 | 127 |
| 127 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); | 128 weak_observer_->OnUserTitleAvailable(shortcut_info_.user_title); |
| 128 | 129 |
| 129 // Kick off a timeout for downloading the icon. If an icon isn't set within | 130 // Kick off a timeout for downloading the icon. If an icon isn't set within |
| 130 // the timeout, fall back to using a dynamically-generated launcher icon. | 131 // the timeout, fall back to using a dynamically-generated launcher icon. |
| 131 icon_timeout_timer_.Start(FROM_HERE, | 132 icon_timeout_timer_.Start(FROM_HERE, |
| 132 base::TimeDelta::FromMilliseconds(3000), | 133 base::TimeDelta::FromMilliseconds(3000), |
| 133 base::Bind(&ShortcutDataFetcher::OnFaviconFetched, | 134 base::Bind( |
| 134 this, | 135 &AddToHomescreenDataFetcher::OnFaviconFetched, |
| 135 favicon_base::FaviconRawBitmapResult())); | 136 this, |
| 137 favicon_base::FaviconRawBitmapResult())); |
| 136 } | 138 } |
| 137 | 139 |
| 138 bool ShortcutDataFetcher::OnMessageReceived(const IPC::Message& message) { | 140 bool AddToHomescreenDataFetcher::OnMessageReceived( |
| 141 const IPC::Message& message) { |
| 139 if (!is_waiting_for_web_application_info_) return false; | 142 if (!is_waiting_for_web_application_info_) return false; |
| 140 | 143 |
| 141 bool handled = true; | 144 bool handled = true; |
| 142 | 145 |
| 143 IPC_BEGIN_MESSAGE_MAP(ShortcutDataFetcher, message) | 146 IPC_BEGIN_MESSAGE_MAP(AddToHomescreenDataFetcher, message) |
| 144 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo, | 147 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo, |
| 145 OnDidGetWebApplicationInfo) | 148 OnDidGetWebApplicationInfo) |
| 146 IPC_MESSAGE_UNHANDLED(handled = false) | 149 IPC_MESSAGE_UNHANDLED(handled = false) |
| 147 IPC_END_MESSAGE_MAP() | 150 IPC_END_MESSAGE_MAP() |
| 148 | 151 |
| 149 return handled; | 152 return handled; |
| 150 } | 153 } |
| 151 | 154 |
| 152 ShortcutDataFetcher::~ShortcutDataFetcher() { | 155 AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() { |
| 153 DCHECK(!weak_observer_); | 156 DCHECK(!weak_observer_); |
| 154 } | 157 } |
| 155 | 158 |
| 156 void ShortcutDataFetcher::FetchFavicon() { | 159 void AddToHomescreenDataFetcher::FetchFavicon() { |
| 157 if (!web_contents() || !weak_observer_) return; | 160 if (!web_contents() || !weak_observer_) return; |
| 158 | 161 |
| 159 Profile* profile = | 162 Profile* profile = |
| 160 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 163 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 161 | 164 |
| 162 // Grab the best, largest icon we can find to represent this bookmark. | 165 // Grab the best, largest icon we can find to represent this bookmark. |
| 163 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its | 166 // TODO(dfalcantara): Try combining with the new BookmarksHandler once its |
| 164 // rewrite is further along. | 167 // rewrite is further along. |
| 165 std::vector<int> icon_types; | 168 std::vector<int> icon_types; |
| 166 icon_types.push_back(favicon_base::FAVICON); | 169 icon_types.push_back(favicon_base::FAVICON); |
| 167 icon_types.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON | | 170 icon_types.push_back(favicon_base::TOUCH_PRECOMPOSED_ICON | |
| 168 favicon_base::TOUCH_ICON); | 171 favicon_base::TOUCH_ICON); |
| 169 favicon::FaviconService* favicon_service = | 172 favicon::FaviconService* favicon_service = |
| 170 FaviconServiceFactory::GetForProfile(profile, | 173 FaviconServiceFactory::GetForProfile(profile, |
| 171 ServiceAccessType::EXPLICIT_ACCESS); | 174 ServiceAccessType::EXPLICIT_ACCESS); |
| 172 | 175 |
| 173 // Using favicon if its size is not smaller than platform required size, | 176 // Using favicon if its size is not smaller than platform required size, |
| 174 // otherwise using the largest icon among all avaliable icons. | 177 // otherwise using the largest icon among all avaliable icons. |
| 175 int threshold_to_get_any_largest_icon = preferred_icon_size_in_px_ - 1; | 178 int threshold_to_get_any_largest_icon = preferred_icon_size_in_px_ - 1; |
| 176 favicon_service->GetLargestRawFaviconForPageURL( | 179 favicon_service->GetLargestRawFaviconForPageURL( |
| 177 shortcut_info_.url, | 180 shortcut_info_.url, |
| 178 icon_types, | 181 icon_types, |
| 179 threshold_to_get_any_largest_icon, | 182 threshold_to_get_any_largest_icon, |
| 180 base::Bind(&ShortcutDataFetcher::OnFaviconFetched, this), | 183 base::Bind(&AddToHomescreenDataFetcher::OnFaviconFetched, this), |
| 181 &favicon_task_tracker_); | 184 &favicon_task_tracker_); |
| 182 } | 185 } |
| 183 | 186 |
| 184 void ShortcutDataFetcher::OnFaviconFetched( | 187 void AddToHomescreenDataFetcher::OnFaviconFetched( |
| 185 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 188 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 186 if (!web_contents() || !weak_observer_ || is_icon_saved_) { | 189 if (!web_contents() || !weak_observer_ || is_icon_saved_) { |
| 187 return; | 190 return; |
| 188 } | 191 } |
| 189 | 192 |
| 190 content::BrowserThread::PostTask( | 193 content::BrowserThread::PostTask( |
| 191 content::BrowserThread::IO, | 194 content::BrowserThread::IO, |
| 192 FROM_HERE, | 195 FROM_HERE, |
| 193 base::Bind(&ShortcutDataFetcher::CreateLauncherIcon, | 196 base::Bind(&AddToHomescreenDataFetcher::CreateLauncherIcon, |
| 194 this, | 197 this, |
| 195 bitmap_result)); | 198 bitmap_result)); |
| 196 } | 199 } |
| 197 | 200 |
| 198 void ShortcutDataFetcher::CreateLauncherIcon( | 201 void AddToHomescreenDataFetcher::CreateLauncherIcon( |
| 199 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 202 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 200 if (!web_contents() || !weak_observer_) return; | 203 if (!web_contents() || !weak_observer_) return; |
| 201 | 204 |
| 202 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 205 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 203 SkBitmap icon_bitmap; | 206 SkBitmap icon_bitmap; |
| 204 if (bitmap_result.is_valid()) { | 207 if (bitmap_result.is_valid()) { |
| 205 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), | 208 gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), |
| 206 bitmap_result.bitmap_data->size(), | 209 bitmap_result.bitmap_data->size(), |
| 207 &icon_bitmap); | 210 &icon_bitmap); |
| 208 } | 211 } |
| 209 | 212 |
| 210 if (weak_observer_) { | 213 if (weak_observer_) { |
| 211 icon_bitmap = weak_observer_->FinalizeLauncherIcon(icon_bitmap, | 214 icon_bitmap = weak_observer_->FinalizeLauncherIcon(icon_bitmap, |
| 212 shortcut_info_.url); | 215 shortcut_info_.url); |
| 213 } | 216 } |
| 214 | 217 |
| 215 content::BrowserThread::PostTask( | 218 content::BrowserThread::PostTask( |
| 216 content::BrowserThread::UI, | 219 content::BrowserThread::UI, |
| 217 FROM_HERE, | 220 FROM_HERE, |
| 218 base::Bind(&ShortcutDataFetcher::NotifyObserver, this, icon_bitmap)); | 221 base::Bind(&AddToHomescreenDataFetcher::NotifyObserver, |
| 222 this, |
| 223 icon_bitmap)); |
| 219 } | 224 } |
| 220 | 225 |
| 221 void ShortcutDataFetcher::OnManifestIconFetched( | 226 void AddToHomescreenDataFetcher::OnManifestIconFetched( |
| 222 int id, | 227 int id, |
| 223 int http_status_code, | 228 int http_status_code, |
| 224 const GURL& url, | 229 const GURL& url, |
| 225 const std::vector<SkBitmap>& bitmaps, | 230 const std::vector<SkBitmap>& bitmaps, |
| 226 const std::vector<gfx::Size>& sizes) { | 231 const std::vector<gfx::Size>& sizes) { |
| 227 if (!web_contents() || !weak_observer_) return; | 232 if (!web_contents() || !weak_observer_) return; |
| 228 | 233 |
| 229 // If getting the candidate manifest icon failed, the ShortcutHelper should | 234 // If getting the candidate manifest icon failed, the ShortcutHelper should |
| 230 // fallback to the favicon. | 235 // fallback to the favicon. |
| 231 // Otherwise, it sets the state as if there was no manifest icon pending. | 236 // Otherwise, it sets the state as if there was no manifest icon pending. |
| 232 if (bitmaps.empty()) { | 237 if (bitmaps.empty()) { |
| 233 FetchFavicon(); | 238 FetchFavicon(); |
| 234 return; | 239 return; |
| 235 } | 240 } |
| 236 | 241 |
| 237 // There might be multiple bitmaps returned. The one to pick is bigger or | 242 // There might be multiple bitmaps returned. The one to pick is bigger or |
| 238 // equal to the preferred size. |bitmaps| is ordered from bigger to smaller. | 243 // equal to the preferred size. |bitmaps| is ordered from bigger to smaller. |
| 239 int preferred_bitmap_index = 0; | 244 int preferred_bitmap_index = 0; |
| 240 for (size_t i = 0; i < bitmaps.size(); ++i) { | 245 for (size_t i = 0; i < bitmaps.size(); ++i) { |
| 241 if (bitmaps[i].height() < preferred_icon_size_in_px_) | 246 if (bitmaps[i].height() < preferred_icon_size_in_px_) |
| 242 break; | 247 break; |
| 243 preferred_bitmap_index = i; | 248 preferred_bitmap_index = i; |
| 244 } | 249 } |
| 245 | 250 |
| 246 NotifyObserver(bitmaps[preferred_bitmap_index]); | 251 NotifyObserver(bitmaps[preferred_bitmap_index]); |
| 247 } | 252 } |
| 248 | 253 |
| 249 void ShortcutDataFetcher::NotifyObserver(const SkBitmap& bitmap) { | 254 void AddToHomescreenDataFetcher::NotifyObserver(const SkBitmap& bitmap) { |
| 250 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 255 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 251 if (!web_contents() || !weak_observer_ || is_icon_saved_) | 256 if (!web_contents() || !weak_observer_ || is_icon_saved_) |
| 252 return; | 257 return; |
| 253 | 258 |
| 254 is_icon_saved_ = true; | 259 is_icon_saved_ = true; |
| 255 shortcut_icon_ = bitmap; | 260 shortcut_icon_ = bitmap; |
| 256 is_ready_ = true; | 261 is_ready_ = true; |
| 257 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); | 262 weak_observer_->OnDataAvailable(shortcut_info_, shortcut_icon_); |
| 258 } | 263 } |
| OLD | NEW |