OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_WEBAPK_WEBAPK_INSTALLER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ |
6 #define CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ | 6 #define CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
| 11 #include <vector> |
11 | 12 |
12 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
13 #include "base/callback.h" | 14 #include "base/callback.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/strings/string16.h" |
16 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
17 #include "chrome/browser/android/shortcut_info.h" | 19 #include "chrome/browser/android/shortcut_info.h" |
18 #include "chrome/browser/android/webapk/webapk_install_service.h" | 20 #include "chrome/browser/android/webapk/webapk_install_service.h" |
19 #include "net/url_request/url_fetcher.h" | 21 #include "net/url_request/url_fetcher.h" |
20 #include "net/url_request/url_fetcher_delegate.h" | 22 #include "net/url_request/url_fetcher_delegate.h" |
21 #include "third_party/skia/include/core/SkBitmap.h" | 23 #include "third_party/skia/include/core/SkBitmap.h" |
22 | 24 |
23 namespace base { | 25 namespace base { |
24 class ElapsedTimer; | 26 class ElapsedTimer; |
25 } | 27 } |
26 | 28 |
27 namespace content { | 29 namespace content { |
28 class BrowserContext; | 30 class BrowserContext; |
29 } | 31 } |
30 | 32 |
31 namespace webapk { | 33 // Talks to Chrome WebAPK server to download metadata about a WebAPK and issue |
32 class WebApk; | |
33 } | |
34 | |
35 // Talks to Chrome WebAPK server to download metadata about a WebApk and issue | |
36 // a request for it to be installed. The native WebApkInstaller owns the Java | 34 // a request for it to be installed. The native WebApkInstaller owns the Java |
37 // WebApkInstaller counterpart. | 35 // WebApkInstaller counterpart. |
38 class WebApkInstaller : public net::URLFetcherDelegate { | 36 class WebApkInstaller : public net::URLFetcherDelegate { |
39 public: | 37 public: |
40 using FinishCallback = WebApkInstallService::FinishCallback; | 38 using FinishCallback = WebApkInstallService::FinishCallback; |
41 | 39 |
42 ~WebApkInstaller() override; | 40 ~WebApkInstaller() override; |
43 | 41 |
44 // Creates a self-owned WebApkInstaller instance and talks to the Chrome | 42 // Creates a self-owned WebApkInstaller instance and talks to the Chrome |
45 // WebAPK server to generate a WebAPK on the server and locally requests the | 43 // WebAPK server to generate a WebAPK on the server and locally requests the |
46 // APK to be installed. Calls |callback| once the install completed or failed. | 44 // APK to be installed. Calls |callback| once the install completed or failed. |
47 static void InstallAsync(content::BrowserContext* context, | 45 static void InstallAsync(content::BrowserContext* context, |
48 const ShortcutInfo& shortcut_info, | 46 const ShortcutInfo& shortcut_info, |
49 const SkBitmap& primary_icon, | 47 const SkBitmap& primary_icon, |
50 const SkBitmap& badge_icon, | 48 const SkBitmap& badge_icon, |
51 const FinishCallback& finish_callback); | 49 const FinishCallback& finish_callback); |
52 | 50 |
53 // TODO(zpeng): Add badge icon to WebAPK update route. | 51 // TODO(zpeng): Add badge icon to WebAPK update route. |
54 // Creates a self-owned WebApkInstaller instance and talks to the Chrome | 52 // Creates a self-owned WebApkInstaller instance and talks to the Chrome |
55 // WebAPK server to update a WebAPK on the server and locally requests the | 53 // WebAPK server to update a WebAPK on the server and locally requests the |
56 // APK to be installed. Calls |callback| once the install completed or failed. | 54 // APK to be installed. Calls |callback| once the install completed or failed. |
57 static void UpdateAsync( | 55 static void UpdateAsync( |
58 content::BrowserContext* context, | 56 content::BrowserContext* context, |
59 const ShortcutInfo& shortcut_info, | |
60 const SkBitmap& primary_icon, | |
61 const std::string& webapk_package, | 57 const std::string& webapk_package, |
62 int webapk_version, | 58 const GURL& start_url, |
63 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 59 const base::string16& short_name, |
64 bool is_manifest_stale, | 60 std::unique_ptr<std::vector<uint8_t>> serialized_proto, |
65 const FinishCallback& callback); | 61 const FinishCallback& callback); |
66 | 62 |
67 // Calls the private function |InstallAsync| for testing. | 63 // Calls the private function |InstallAsync| for testing. |
68 // Should be used only for testing. | 64 // Should be used only for testing. |
69 static void InstallAsyncForTesting(WebApkInstaller* installer, | 65 static void InstallAsyncForTesting(WebApkInstaller* installer, |
| 66 const ShortcutInfo& shortcut_info, |
| 67 const SkBitmap& primary_icon, |
| 68 const SkBitmap& badge_icon, |
70 const FinishCallback& finish_callback); | 69 const FinishCallback& finish_callback); |
71 | 70 |
72 // Calls the private function |UpdateAsync| for testing. | 71 // Calls the private function |UpdateAsync| for testing. |
73 // Should be used only for testing. | 72 // Should be used only for testing. |
74 static void UpdateAsyncForTesting( | 73 static void UpdateAsyncForTesting( |
75 WebApkInstaller* installer, | 74 WebApkInstaller* installer, |
76 const std::string& webapk_package, | 75 const std::string& webapk_package, |
77 int webapk_version, | 76 const GURL& start_url, |
78 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 77 const base::string16& short_name, |
79 bool is_manifest_stale, | 78 std::unique_ptr<std::vector<uint8_t>> serialized_proto, |
80 const FinishCallback& callback); | 79 const FinishCallback& callback); |
81 | 80 |
82 // Sets the timeout for the server requests. | 81 // Sets the timeout for the server requests. |
83 void SetTimeoutMs(int timeout_ms); | 82 void SetTimeoutMs(int timeout_ms); |
84 | 83 |
85 // Called once the installation is complete or failed. | 84 // Called once the installation is complete or failed. |
86 void OnInstallFinished(JNIEnv* env, | 85 void OnInstallFinished(JNIEnv* env, |
87 const base::android::JavaParamRef<jobject>& obj, | 86 const base::android::JavaParamRef<jobject>& obj, |
88 jint result); | 87 jint result); |
89 | 88 |
90 // Builds proto in the background for install or update request. | 89 // Builds proto in the background for install or update request. |
91 static void BuildProto( | 90 static void BuildProto( |
92 const ShortcutInfo& shortcut_info, | 91 const ShortcutInfo& shortcut_info, |
93 const SkBitmap& primary_icon, | 92 const SkBitmap& primary_icon, |
94 const SkBitmap& badge_icon, | 93 const SkBitmap& badge_icon, |
95 const std::string& package_name, | 94 const std::string& package_name, |
96 const std::string& version, | 95 const std::string& version, |
97 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 96 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
98 bool is_manifest_stale, | 97 bool is_manifest_stale, |
99 const base::Callback<void(std::unique_ptr<webapk::WebApk>)>& callback); | 98 const base::Callback<void(std::unique_ptr<std::vector<uint8_t>>)>& |
| 99 callback); |
100 | 100 |
101 // Registers JNI hooks. | 101 // Registers JNI hooks. |
102 static bool Register(JNIEnv* env); | 102 static bool Register(JNIEnv* env); |
103 | 103 |
104 protected: | 104 protected: |
105 WebApkInstaller(content::BrowserContext* browser_context, | 105 explicit WebApkInstaller(content::BrowserContext* browser_context); |
106 const ShortcutInfo& shortcut_info, | |
107 const SkBitmap& primary_icon, | |
108 const SkBitmap& badge_icon); | |
109 | 106 |
110 // Called when the package name of the WebAPK is available and the install | 107 // Called when the package name of the WebAPK is available and the install |
111 // or update request should be issued. | 108 // or update request should be issued. |
112 virtual void InstallOrUpdateWebApk(const std::string& package_name, | 109 virtual void InstallOrUpdateWebApk(const std::string& package_name, |
113 int version, | 110 int version, |
114 const std::string& token); | 111 const std::string& token); |
115 | 112 |
116 // Called when the install or update process has completed or failed. | 113 // Called when the install or update process has completed or failed. |
117 void OnResult(WebApkInstallResult result); | 114 void OnResult(WebApkInstallResult result); |
118 | 115 |
119 private: | 116 private: |
120 enum TaskType { | 117 enum TaskType { |
121 UNDEFINED, | 118 UNDEFINED, |
122 INSTALL, | 119 INSTALL, |
123 UPDATE, | 120 UPDATE, |
124 }; | 121 }; |
125 | 122 |
126 // Create the Java object. | 123 // Create the Java object. |
127 void CreateJavaRef(); | 124 void CreateJavaRef(); |
128 | 125 |
129 // Talks to the Chrome WebAPK server to generate a WebAPK on the server and to | 126 // Talks to the Chrome WebAPK server to generate a WebAPK on the server and to |
130 // Google Play to install the downloaded WebAPK. Calls |callback| once the | 127 // Google Play to install the downloaded WebAPK. Calls |callback| once the |
131 // install completed or failed. | 128 // install completed or failed. |
132 void InstallAsync(const FinishCallback& finish_callback); | 129 void InstallAsync(const ShortcutInfo& shortcut_info, |
| 130 const SkBitmap& primary_icon, |
| 131 const SkBitmap& badge_icon, |
| 132 const FinishCallback& finish_callback); |
133 | 133 |
134 // Talks to the Chrome WebAPK server to update a WebAPK on the server and to | 134 // Talks to the Chrome WebAPK server to update a WebAPK on the server and to |
135 // the Google Play server to install the downloaded WebAPK. Calls |callback| | 135 // the Google Play server to install the downloaded WebAPK. Calls |callback| |
136 // after the request to install the WebAPK is sent to the Google Play server. | 136 // once the update completed or failed. |
137 void UpdateAsync( | 137 void UpdateAsync(const std::string& webapk_package, |
138 const std::string& webapk_package, | 138 const GURL& start_url, |
139 int webapk_version, | 139 const base::string16& short_name, |
140 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 140 const std::unique_ptr<std::vector<uint8_t>> serialized_proto, |
141 bool is_manifest_stale, | 141 const FinishCallback& callback); |
142 const FinishCallback& callback); | |
143 | 142 |
144 // net::URLFetcherDelegate: | 143 // net::URLFetcherDelegate: |
145 void OnURLFetchComplete(const net::URLFetcher* source) override; | 144 void OnURLFetchComplete(const net::URLFetcher* source) override; |
146 | 145 |
147 // Called with the computed Murmur2 hash for the primary icon. | 146 // Called with the computed Murmur2 hash for the primary icon. |
148 void OnGotPrimaryIconMurmur2Hash(const std::string& primary_icon_hash); | 147 void OnGotPrimaryIconMurmur2Hash(const std::string& primary_icon_hash); |
149 | 148 |
150 // Called with the computed Murmur2 hash for the badge icon, and | 149 // Called with the computed Murmur2 hash for the badge icon, and |
151 // |did_fetch_badge_icon| to indicate whether there was an attempt to fetch | 150 // |did_fetch_badge_icon| to indicate whether there was an attempt to fetch |
152 // badge icon. | 151 // badge icon. |
153 void OnGotBadgeIconMurmur2Hash(bool did_fetch_badge_icon, | 152 void OnGotBadgeIconMurmur2Hash(bool did_fetch_badge_icon, |
154 const std::string& primary_icon_hash, | 153 const std::string& primary_icon_hash, |
155 const std::string& badge_icon_hash); | 154 const std::string& badge_icon_hash); |
156 | 155 |
157 // Sends a request to WebAPK server to create/update WebAPK. During a | 156 // Sends a request to WebAPK server to create/update WebAPK. During a |
158 // successful request the WebAPK server responds with the URL of the generated | 157 // successful request the WebAPK server responds with a token to send to |
159 // WebAPK. | 158 // Google Play. |
160 void SendRequest(std::unique_ptr<webapk::WebApk> request_proto); | 159 void SendRequest(std::unique_ptr<std::vector<uint8_t>> serialized_proto); |
161 | 160 |
162 net::URLRequestContextGetter* request_context_getter_; | 161 net::URLRequestContextGetter* request_context_getter_; |
163 | 162 |
164 // Sends HTTP request to WebAPK server. | 163 // Sends HTTP request to WebAPK server. |
165 std::unique_ptr<net::URLFetcher> url_fetcher_; | 164 std::unique_ptr<net::URLFetcher> url_fetcher_; |
166 | 165 |
167 // Fails WebApkInstaller if WebAPK server takes too long to respond or if the | 166 // Fails WebApkInstaller if WebAPK server takes too long to respond or if the |
168 // download takes too long. | 167 // download takes too long. |
169 base::OneShotTimer timer_; | 168 base::OneShotTimer timer_; |
170 | 169 |
171 // Tracks how long it takes to install a WebAPK. | 170 // Tracks how long it takes to install a WebAPK. |
172 std::unique_ptr<base::ElapsedTimer> install_duration_timer_; | 171 std::unique_ptr<base::ElapsedTimer> install_duration_timer_; |
173 | 172 |
174 // Callback to call once WebApkInstaller succeeds or fails. | 173 // Callback to call once WebApkInstaller succeeds or fails. |
175 FinishCallback finish_callback_; | 174 FinishCallback finish_callback_; |
176 | 175 |
177 // Web Manifest info. | 176 // Data for installs. |
178 const ShortcutInfo shortcut_info_; | 177 std::unique_ptr<ShortcutInfo> install_shortcut_info_; |
| 178 SkBitmap install_primary_icon_; |
| 179 SkBitmap install_badge_icon_; |
179 | 180 |
180 // WebAPK primary icon. | 181 GURL start_url_; |
181 const SkBitmap primary_icon_; | 182 base::string16 short_name_; |
182 | |
183 // WebAPK badge icon. | |
184 const SkBitmap badge_icon_; | |
185 | 183 |
186 // WebAPK server URL. | 184 // WebAPK server URL. |
187 GURL server_url_; | 185 GURL server_url_; |
188 | 186 |
189 // The number of milliseconds to wait for the WebAPK server to respond. | 187 // The number of milliseconds to wait for the WebAPK server to respond. |
190 int webapk_server_timeout_ms_; | 188 int webapk_server_timeout_ms_; |
191 | 189 |
192 // WebAPK package name. | 190 // WebAPK package name. |
193 std::string webapk_package_; | 191 std::string webapk_package_; |
194 | 192 |
195 // Whether the server wants the WebAPK to request updates less frequently. | 193 // Whether the server wants the WebAPK to request updates less frequently. |
196 bool relax_updates_; | 194 bool relax_updates_; |
197 | 195 |
198 // Indicates whether the installer is for installing or updating a WebAPK. | 196 // Indicates whether the installer is for installing or updating a WebAPK. |
199 TaskType task_type_; | 197 TaskType task_type_; |
200 | 198 |
201 // Points to the Java Object. | 199 // Points to the Java Object. |
202 base::android::ScopedJavaGlobalRef<jobject> java_ref_; | 200 base::android::ScopedJavaGlobalRef<jobject> java_ref_; |
203 | 201 |
204 // Used to get |weak_ptr_|. | 202 // Used to get |weak_ptr_|. |
205 base::WeakPtrFactory<WebApkInstaller> weak_ptr_factory_; | 203 base::WeakPtrFactory<WebApkInstaller> weak_ptr_factory_; |
206 | 204 |
207 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); | 205 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); |
208 }; | 206 }; |
209 | 207 |
210 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ | 208 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ |
OLD | NEW |