| 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 #ifndef CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_ |
| 6 #define CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_ | 6 #define CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_ |
| 7 | 7 |
| 8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ref_counted.h" |
| 10 #include "base/task/cancelable_task_tracker.h" | 11 #include "base/task/cancelable_task_tracker.h" |
| 11 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
| 12 #include "chrome/browser/android/shortcut_info.h" | 13 #include "chrome/browser/android/shortcut_info.h" |
| 13 #include "chrome/common/web_application_info.h" | |
| 14 #include "components/favicon_base/favicon_types.h" | |
| 15 #include "content/public/browser/web_contents_observer.h" | 14 #include "content/public/browser/web_contents_observer.h" |
| 16 #include "content/public/common/manifest.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 | 16 |
| 18 namespace content { | 17 namespace content { |
| 19 class WebContents; | 18 class WebContents; |
| 20 } // namespace content | 19 } |
| 20 |
| 21 namespace favicon_base { |
| 22 struct FaviconRawBitmapResult; |
| 23 } |
| 21 | 24 |
| 22 namespace IPC { | 25 namespace IPC { |
| 23 class Message; | 26 class Message; |
| 24 } | 27 } |
| 25 | 28 |
| 26 class GURL; | 29 class GURL; |
| 30 struct InstallableData; |
| 31 struct WebApplicationInfo; |
| 27 | 32 |
| 28 // Aysnchronously fetches and processes data needed to create a shortcut for an | 33 // Aysnchronously fetches and processes data needed to create a shortcut for an |
| 29 // Android Home screen launcher. | 34 // Android Home screen launcher. |
| 30 // | 35 // |
| 31 // Because of the various asynchronous calls made by this class, it is | 36 // Because of the various asynchronous calls made by this class, it is |
| 32 // refcounted to prevent the class from being prematurely deleted. If the | 37 // refcounted to prevent the class from being prematurely deleted. If the |
| 33 // pointer to the ShortcutHelper becomes invalid, the pipeline should kill | 38 // pointer to the ShortcutHelper becomes invalid, the pipeline should kill |
| 34 // itself. | 39 // itself. |
| 35 class AddToHomescreenDataFetcher | 40 class AddToHomescreenDataFetcher |
| 36 : public base::RefCounted<AddToHomescreenDataFetcher>, | 41 : public base::RefCounted<AddToHomescreenDataFetcher>, |
| 37 public content::WebContentsObserver { | 42 public content::WebContentsObserver { |
| 38 public: | 43 public: |
| 39 class Observer { | 44 class Observer { |
| 40 public: | 45 public: |
| 41 // Called when the title of the page is available. | 46 // Called when the title of the page is available. |
| 42 virtual void OnUserTitleAvailable(const base::string16& title) = 0; | 47 virtual void OnUserTitleAvailable(const base::string16& title) = 0; |
| 43 | 48 |
| 44 // Converts the icon into one that can be used on the Android Home screen. | 49 // Converts the icon into one that can be used on the Android Home screen. |
| 45 // |is_generated| is an out-param that indicates whether the icon was | 50 // |is_generated| is an out-param that indicates whether the icon was |
| 46 // generated by Chrome. | 51 // generated by Chrome. |
| 47 virtual SkBitmap FinalizeLauncherIconInBackground(const SkBitmap& icon, | 52 virtual SkBitmap FinalizeLauncherIconInBackground(const SkBitmap& icon, |
| 48 const GURL& url, | 53 const GURL& url, |
| 49 bool* is_generated) = 0; | 54 bool* is_generated) = 0; |
| 50 | 55 |
| 51 // Called when all the data needed to create a shortcut is available. | 56 // Called when all the data needed to create a shortcut is available. |
| 52 virtual void OnDataAvailable(const ShortcutInfo& info, | 57 virtual void OnDataAvailable(const ShortcutInfo& info, |
| 53 const SkBitmap& icon) = 0; | 58 const SkBitmap& icon) = 0; |
| 54 }; | 59 }; |
| 55 | 60 |
| 56 // Initialize the fetcher by requesting the information about the page to the | 61 // Initialize the fetcher by requesting the information about the page from |
| 57 // renderer process. The initialization is asynchronous and | 62 // the renderer process. The initialization is asynchronous and |
| 58 // OnDidGetWebApplicationInfo is expected to be called when finished. | 63 // OnDidGetWebApplicationInfo is expected to be called when finished. |
| 59 AddToHomescreenDataFetcher(content::WebContents* web_contents, | 64 AddToHomescreenDataFetcher(content::WebContents* web_contents, |
| 60 int ideal_icon_size_in_dp, | 65 int ideal_icon_size_in_dp, |
| 61 int minimum_icon_size_in_dp, | 66 int minimum_icon_size_in_dp, |
| 62 int ideal_splash_image_size_in_dp, | 67 int ideal_splash_image_size_in_dp, |
| 63 int minimum_splash_image_size_in_dp, | 68 int minimum_splash_image_size_in_dp, |
| 64 Observer* observer); | 69 Observer* observer); |
| 65 | 70 |
| 66 // Returns a callback which fetches the splash screen image to be stored for | 71 // Returns a callback which fetches the splash screen image to be stored for |
| 67 // the webapp with the specified |id|. | 72 // the webapp with the specified |id|. |
| 68 base::Closure FetchSplashScreenImageCallback(const std::string& id); | 73 base::Closure FetchSplashScreenImageCallback(const std::string& id); |
| 69 | 74 |
| 70 // IPC message received when the initialization is finished. | 75 // IPC message received when the initialization is finished. |
| 71 void OnDidGetWebApplicationInfo(const WebApplicationInfo& web_app_info); | 76 void OnDidGetWebApplicationInfo(const WebApplicationInfo& web_app_info); |
| 72 | 77 |
| 73 // Called when the Manifest has been parsed, or if no Manifest was found. | |
| 74 void OnDidGetManifest(const GURL& manifest_url, | |
| 75 const content::Manifest& manifest); | |
| 76 | |
| 77 // Accessors, etc. | 78 // Accessors, etc. |
| 78 void set_weak_observer(Observer* observer) { weak_observer_ = observer; } | 79 void set_weak_observer(Observer* observer) { weak_observer_ = observer; } |
| 79 bool is_ready() { return is_ready_; } | 80 bool is_ready() const { return is_ready_; } |
| 80 ShortcutInfo& shortcut_info() { return shortcut_info_; } | 81 ShortcutInfo& shortcut_info() { return shortcut_info_; } |
| 81 const SkBitmap& shortcut_icon() { return shortcut_icon_; } | 82 const SkBitmap& shortcut_icon() const { return shortcut_icon_; } |
| 82 | 83 |
| 83 // WebContentsObserver | 84 // WebContentsObserver |
| 84 bool OnMessageReceived(const IPC::Message& message) override; | 85 bool OnMessageReceived(const IPC::Message& message) override; |
| 85 | 86 |
| 86 private: | 87 private: |
| 88 friend class base::RefCounted<AddToHomescreenDataFetcher>; |
| 89 |
| 87 ~AddToHomescreenDataFetcher() override; | 90 ~AddToHomescreenDataFetcher() override; |
| 88 | 91 |
| 89 // Grabs the favicon for the current URL. | 92 // Grabs the favicon for the current URL. |
| 90 void FetchFavicon(); | 93 void FetchFavicon(); |
| 91 void OnFaviconFetched( | 94 void OnFaviconFetched( |
| 92 const favicon_base::FaviconRawBitmapResult& bitmap_result); | 95 const favicon_base::FaviconRawBitmapResult& bitmap_result); |
| 93 | 96 |
| 94 // Creates the launcher icon from the given bitmap and page URL. The page URL | 97 // Creates the launcher icon from the given bitmap and page URL. The page URL |
| 95 // is used to generate an icon if there is no bitmap in |bitmap_result| or the | 98 // is used to generate an icon if there is no bitmap in |bitmap_result| or the |
| 96 // bitmap is not large enough. | 99 // bitmap is not large enough. |
| 97 void CreateLauncherIconInBackground( | 100 void CreateLauncherIconInBackground( |
| 98 const GURL& page_url, | 101 const GURL& page_url, |
| 99 const favicon_base::FaviconRawBitmapResult& bitmap_result); | 102 const favicon_base::FaviconRawBitmapResult& bitmap_result); |
| 100 | 103 |
| 101 // Callback run after an attempt to download manifest icon has been made. May | 104 // Called when InstallableManager finishes looking for a manifest and icon. |
| 102 // kick off the download of a favicon if it failed (i.e. the bitmap is empty). | 105 void OnDidPerformInstallableCheck(const InstallableData& data); |
| 103 void OnManifestIconFetched(const GURL& icon_url, const SkBitmap& icon); | |
| 104 | 106 |
| 105 // Notifies the observer that the shortcut data is all available. | 107 // Notifies the observer that the shortcut data is all available. |
| 106 void NotifyObserver(const GURL& icon_url, const SkBitmap& icon); | 108 void NotifyObserver(const GURL& icon_url, const SkBitmap& icon); |
| 107 | 109 |
| 108 // Looks up the original, online URL of the site requested. The URL from the | |
| 109 // WebContents may be an offline page or a distilled article which is not | |
| 110 // appropriate for a home screen shortcut. | |
| 111 GURL GetShortcutUrl(const GURL& original_url); | |
| 112 | |
| 113 Observer* weak_observer_; | 110 Observer* weak_observer_; |
| 114 | 111 |
| 115 bool is_waiting_for_web_application_info_; | 112 // The icon must only be set on the UI thread for thread safety. |
| 116 bool is_icon_saved_; | 113 SkBitmap shortcut_icon_; |
| 117 bool is_ready_; | |
| 118 base::Timer icon_timeout_timer_; | |
| 119 ShortcutInfo shortcut_info_; | 114 ShortcutInfo shortcut_info_; |
| 120 GURL splash_screen_url_; | 115 GURL splash_screen_url_; |
| 121 | 116 |
| 122 // The icon must only be set on the UI thread for thread safety. | |
| 123 SkBitmap shortcut_icon_; | |
| 124 base::CancelableTaskTracker favicon_task_tracker_; | 117 base::CancelableTaskTracker favicon_task_tracker_; |
| 118 base::Timer data_timeout_timer_; |
| 125 | 119 |
| 126 const int ideal_icon_size_in_dp_; | 120 const int ideal_icon_size_in_dp_; |
| 127 const int minimum_icon_size_in_dp_; | 121 const int minimum_icon_size_in_dp_; |
| 128 const int ideal_splash_image_size_in_dp_; | 122 const int ideal_splash_image_size_in_dp_; |
| 129 const int minimum_splash_image_size_in_dp_; | 123 const int minimum_splash_image_size_in_dp_; |
| 124 bool is_waiting_for_web_application_info_; |
| 125 bool is_icon_saved_; |
| 126 bool is_ready_; |
| 130 | 127 |
| 131 friend class base::RefCounted<AddToHomescreenDataFetcher>; | |
| 132 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcher); | 128 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcher); |
| 133 }; | 129 }; |
| 134 | 130 |
| 135 #endif // CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_ | 131 #endif // CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_ |
| OLD | NEW |