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 <utility> | |
9 | |
8 #include "base/macros.h" | 10 #include "base/macros.h" |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
11 #include "base/task/cancelable_task_tracker.h" | 12 #include "base/task/cancelable_task_tracker.h" |
12 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
13 #include "chrome/browser/android/shortcut_info.h" | 14 #include "chrome/browser/android/shortcut_info.h" |
14 #include "content/public/browser/web_contents_observer.h" | 15 #include "content/public/browser/web_contents_observer.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
16 | 17 |
17 namespace favicon_base { | 18 namespace favicon_base { |
18 struct FaviconRawBitmapResult; | 19 struct FaviconRawBitmapResult; |
19 } | 20 } |
20 | 21 |
21 class GURL; | |
22 class InstallableManager; | 22 class InstallableManager; |
23 struct InstallableData; | 23 struct InstallableData; |
24 struct WebApplicationInfo; | 24 struct WebApplicationInfo; |
25 | 25 |
26 // Aysnchronously fetches and processes data needed to create a shortcut for an | 26 // Aysnchronously fetches and processes data needed to create a shortcut for an |
27 // Android Home screen launcher. | 27 // Android Home screen launcher. |
28 // | 28 class AddToHomescreenDataFetcher : public content::WebContentsObserver { |
29 // Because of the various asynchronous calls made by this class, it is | |
30 // refcounted to prevent the class from being prematurely deleted. If the | |
31 // |weak_observer_| pointer becomes invalid, the pipeline should kill itself. | |
32 class AddToHomescreenDataFetcher | |
33 : public base::RefCounted<AddToHomescreenDataFetcher>, | |
34 public content::WebContentsObserver { | |
35 public: | 29 public: |
36 class Observer { | 30 class Observer { |
37 public: | 31 public: |
38 // Called when the installable check is complete. | 32 // Called when the installable check is complete. |
39 virtual void OnDidDetermineWebApkCompatibility( | 33 virtual void OnDidDetermineWebApkCompatibility( |
40 bool is_webapk_compatible) = 0; | 34 bool is_webapk_compatible) = 0; |
41 | 35 |
42 // Called when the title of the page is available. Will be called after | 36 // Called when the title of the page is available. Will be called after |
43 // OnDidDetermineWebApkCompatibility. | 37 // OnDidDetermineWebApkCompatibility. |
44 virtual void OnUserTitleAvailable(const base::string16& title) = 0; | 38 virtual void OnUserTitleAvailable(const base::string16& title) = 0; |
45 | 39 |
46 // Converts the icon into one that can be used on the Android Home screen. | |
47 // |is_generated| is an out-param that indicates whether the icon was | |
48 // generated by Chrome. | |
49 virtual SkBitmap FinalizeLauncherIconInBackground(const SkBitmap& icon, | |
50 const GURL& url, | |
51 bool* is_generated) = 0; | |
52 | |
53 // Called when all the data needed to create a shortcut is available. | 40 // Called when all the data needed to create a shortcut is available. |
54 virtual void OnDataAvailable(const ShortcutInfo& info, | 41 virtual void OnDataAvailable(const ShortcutInfo& info, |
55 const SkBitmap& primary_icon, | 42 const SkBitmap& primary_icon, |
56 const SkBitmap& badge_icon) = 0; | 43 const SkBitmap& badge_icon) = 0; |
57 | 44 |
58 protected: | 45 protected: |
59 virtual ~Observer() {} | 46 virtual ~Observer() {} |
60 }; | 47 }; |
61 | 48 |
62 // Initialize the fetcher by requesting the information about the page from | 49 // Initialize the fetcher by requesting the information about the page from |
63 // the renderer process. The initialization is asynchronous and | 50 // the renderer process. The initialization is asynchronous and |
64 // OnDidGetWebApplicationInfo is expected to be called when finished. | 51 // OnDidGetWebApplicationInfo is expected to be called when finished. |
dominickn
2017/07/03 00:43:16
Nit: document that |observer| must outlive this ob
| |
65 AddToHomescreenDataFetcher(content::WebContents* web_contents, | 52 AddToHomescreenDataFetcher(content::WebContents* web_contents, |
66 int ideal_icon_size_in_px, | 53 int ideal_icon_size_in_px, |
67 int minimum_icon_size_in_px, | 54 int minimum_icon_size_in_px, |
68 int ideal_splash_image_size_in_px, | 55 int ideal_splash_image_size_in_px, |
69 int minimum_splash_image_size_in_px, | 56 int minimum_splash_image_size_in_px, |
70 int badge_size_in_px, | 57 int badge_size_in_px, |
71 int data_timeout_ms, | 58 int data_timeout_ms, |
72 bool check_webapk_compatible, | 59 bool check_webapk_compatible, |
73 Observer* observer); | 60 Observer* observer); |
74 | 61 |
62 ~AddToHomescreenDataFetcher() override; | |
63 | |
75 // IPC message received when the initialization is finished. | 64 // IPC message received when the initialization is finished. |
76 void OnDidGetWebApplicationInfo(const WebApplicationInfo& web_app_info); | 65 void OnDidGetWebApplicationInfo(const WebApplicationInfo& web_app_info); |
77 | 66 |
78 // Accessors, etc. | 67 // Accessors, etc. |
79 void set_weak_observer(Observer* observer) { weak_observer_ = observer; } | |
80 const SkBitmap& badge_icon() const { return badge_icon_; } | 68 const SkBitmap& badge_icon() const { return badge_icon_; } |
81 const SkBitmap& primary_icon() const { return primary_icon_; } | 69 const SkBitmap& primary_icon() const { return primary_icon_; } |
82 ShortcutInfo& shortcut_info() { return shortcut_info_; } | 70 ShortcutInfo& shortcut_info() { return shortcut_info_; } |
83 | 71 |
84 private: | 72 private: |
85 friend class base::RefCounted<AddToHomescreenDataFetcher>; | |
86 | |
87 ~AddToHomescreenDataFetcher() override; | |
88 | |
89 // WebContentsObserver: | 73 // WebContentsObserver: |
90 bool OnMessageReceived(const IPC::Message& message, | 74 bool OnMessageReceived(const IPC::Message& message, |
91 content::RenderFrameHost* sender) override; | 75 content::RenderFrameHost* sender) override; |
92 | 76 |
93 // Called if either InstallableManager or the favicon fetch takes too long. | 77 // Called if either InstallableManager or the favicon fetch takes too long. |
94 void OnDataTimedout(); | 78 void OnDataTimedout(); |
95 | 79 |
96 // Called when InstallableManager finishes looking for a manifest and icon. | 80 // Called when InstallableManager finishes looking for a manifest and icon. |
97 void OnDidGetManifestAndIcons(const InstallableData& data); | 81 void OnDidGetManifestAndIcons(const InstallableData& data); |
98 | 82 |
99 // Called when InstallableManager finishes checking for installability. | 83 // Called when InstallableManager finishes checking for installability. |
100 void OnDidPerformInstallableCheck(const InstallableData& data); | 84 void OnDidPerformInstallableCheck(const InstallableData& data); |
101 | 85 |
102 // Grabs the favicon for the current URL. | 86 // Grabs the favicon for the current URL. |
103 void FetchFavicon(); | 87 void FetchFavicon(); |
104 void OnFaviconFetched( | 88 void OnFaviconFetched( |
105 const favicon_base::FaviconRawBitmapResult& bitmap_result); | 89 const favicon_base::FaviconRawBitmapResult& bitmap_result); |
106 | 90 |
107 // Creates the launcher icon from the given bitmap. shortcut_info_.url is | |
108 // used to generate an icon if there is no bitmap in |bitmap_result| or the | |
109 // bitmap is not large enough. | |
110 SkBitmap CreateLauncherIconFromFaviconInBackground( | |
111 const favicon_base::FaviconRawBitmapResult& bitmap_result); | |
112 | |
113 // Creates the primary launcher icon from the given |icon|. | 91 // Creates the primary launcher icon from the given |icon|. |
114 void CreateLauncherIcon(const SkBitmap& icon); | 92 void CreateLauncherIcon(const SkBitmap& icon); |
115 SkBitmap CreateLauncherIconInBackground(const SkBitmap& icon); | |
116 | 93 |
117 // Notifies the observer that the shortcut data is all available. | 94 // Notifies the observer that the shortcut data is all available. |
118 void NotifyObserver(const SkBitmap& icon); | 95 void NotifyObserver(const std::pair<SkBitmap, bool /*is_generated*/>& icon); |
119 | 96 |
120 InstallableManager* installable_manager_; | 97 InstallableManager* installable_manager_; |
121 Observer* weak_observer_; | 98 Observer* observer_; |
122 | 99 |
123 // The icons must only be set on the UI thread for thread safety. | 100 // The icons must only be set on the UI thread for thread safety. |
124 SkBitmap raw_primary_icon_; | 101 SkBitmap raw_primary_icon_; |
125 SkBitmap badge_icon_; | 102 SkBitmap badge_icon_; |
126 SkBitmap primary_icon_; | 103 SkBitmap primary_icon_; |
127 ShortcutInfo shortcut_info_; | 104 ShortcutInfo shortcut_info_; |
128 | 105 |
129 base::CancelableTaskTracker favicon_task_tracker_; | 106 base::CancelableTaskTracker favicon_task_tracker_; |
130 base::OneShotTimer data_timeout_timer_; | 107 base::OneShotTimer data_timeout_timer_; |
131 | 108 |
132 const int ideal_icon_size_in_px_; | 109 const int ideal_icon_size_in_px_; |
133 const int minimum_icon_size_in_px_; | 110 const int minimum_icon_size_in_px_; |
134 const int ideal_splash_image_size_in_px_; | 111 const int ideal_splash_image_size_in_px_; |
135 const int minimum_splash_image_size_in_px_; | 112 const int minimum_splash_image_size_in_px_; |
136 const int badge_size_in_px_; | 113 const int badge_size_in_px_; |
137 const int data_timeout_ms_; | 114 const int data_timeout_ms_; |
138 | 115 |
139 // Indicates whether to check WebAPK compatibility. | 116 // Indicates whether to check WebAPK compatibility. |
140 bool check_webapk_compatibility_; | 117 bool check_webapk_compatibility_; |
141 bool is_waiting_for_web_application_info_; | 118 bool is_waiting_for_web_application_info_; |
142 bool is_installable_check_complete_; | 119 |
120 base::WeakPtrFactory<AddToHomescreenDataFetcher> weak_ptr_factory_; | |
143 | 121 |
144 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcher); | 122 DISALLOW_COPY_AND_ASSIGN(AddToHomescreenDataFetcher); |
145 }; | 123 }; |
146 | 124 |
147 #endif // CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_ | 125 #endif // CHROME_BROWSER_ANDROID_WEBAPPS_ADD_TO_HOMESCREEN_DATA_FETCHER_H_ |
OLD | NEW |