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 | 11 |
12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
17 #include "chrome/browser/android/shortcut_info.h" | 17 #include "chrome/browser/android/shortcut_info.h" |
18 #include "chrome/browser/android/webapk/webapk_install_service.h" | 18 #include "chrome/browser/android/webapk/webapk_install_service.h" |
19 #include "chrome/browser/net/file_downloader.h" | |
20 #include "net/url_request/url_fetcher.h" | 19 #include "net/url_request/url_fetcher.h" |
21 #include "net/url_request/url_fetcher_delegate.h" | 20 #include "net/url_request/url_fetcher_delegate.h" |
22 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
23 | 22 |
24 namespace base { | 23 namespace base { |
25 class ElapsedTimer; | 24 class ElapsedTimer; |
26 class FilePath; | |
27 } | 25 } |
28 | 26 |
29 namespace content { | 27 namespace content { |
30 class BrowserContext; | 28 class BrowserContext; |
31 } | 29 } |
32 | 30 |
33 namespace webapk { | 31 namespace webapk { |
34 class WebApk; | 32 class WebApk; |
35 } | 33 } |
36 | 34 |
37 // Talks to Chrome WebAPK server and Google Play to generate a WebAPK on the | 35 // Talks to Chrome WebAPK server to download metadata about a WebApk and issue |
38 // server, download it, and install it. The native WebApkInstaller owns the | 36 // a request for it to be installed. The native WebApkInstaller owns the Java |
39 // Java WebApkInstaller counterpart. | 37 // WebApkInstaller counterpart. |
40 class WebApkInstaller : public net::URLFetcherDelegate { | 38 class WebApkInstaller : public net::URLFetcherDelegate { |
41 public: | 39 public: |
42 using FinishCallback = WebApkInstallService::FinishCallback; | 40 using FinishCallback = WebApkInstallService::FinishCallback; |
43 | 41 |
44 ~WebApkInstaller() override; | 42 ~WebApkInstaller() override; |
45 | 43 |
46 // Creates a self-owned WebApkInstaller instance and talks to the Chrome | 44 // Creates a self-owned WebApkInstaller instance and talks to the Chrome |
47 // WebAPK server to generate a WebAPK on the server and to Google Play to | 45 // WebAPK server to generate a WebAPK on the server and locally requests the |
48 // install the downloaded WebAPK. Calls |callback| once the install completed | 46 // APK to be installed. Calls |callback| once the install completed or failed. |
49 // or failed. | |
50 static void InstallAsync(content::BrowserContext* context, | 47 static void InstallAsync(content::BrowserContext* context, |
51 const ShortcutInfo& shortcut_info, | 48 const ShortcutInfo& shortcut_info, |
52 const SkBitmap& shortcut_icon, | 49 const SkBitmap& shortcut_icon, |
53 const FinishCallback& finish_callback); | 50 const FinishCallback& finish_callback); |
54 | 51 |
55 // Creates a self-owned WebApkInstaller instance and talks to the Chrome | 52 // Creates a self-owned WebApkInstaller instance and talks to the Chrome |
56 // WebAPK server to update a WebAPK on the server and to the Google Play | 53 // WebAPK server to update a WebAPK on the server and locally requests the |
57 // server to install the downloaded WebAPK. Calls |callback| after the request | 54 // APK to be installed. Calls |callback| once the install completed or failed. |
58 // to install the WebAPK is sent to the Google Play server. | |
59 static void UpdateAsync( | 55 static void UpdateAsync( |
60 content::BrowserContext* context, | 56 content::BrowserContext* context, |
61 const ShortcutInfo& shortcut_info, | 57 const ShortcutInfo& shortcut_info, |
62 const SkBitmap& shortcut_icon, | 58 const SkBitmap& shortcut_icon, |
63 const std::string& webapk_package, | 59 const std::string& webapk_package, |
64 int webapk_version, | 60 int webapk_version, |
65 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 61 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
66 bool is_manifest_stale, | 62 bool is_manifest_stale, |
67 const FinishCallback& callback); | 63 const FinishCallback& callback); |
68 | 64 |
(...skipping 28 matching lines...) Expand all Loading... |
97 bool is_manifest_stale); | 93 bool is_manifest_stale); |
98 | 94 |
99 // Registers JNI hooks. | 95 // Registers JNI hooks. |
100 static bool Register(JNIEnv* env); | 96 static bool Register(JNIEnv* env); |
101 | 97 |
102 protected: | 98 protected: |
103 WebApkInstaller(content::BrowserContext* browser_context, | 99 WebApkInstaller(content::BrowserContext* browser_context, |
104 const ShortcutInfo& shortcut_info, | 100 const ShortcutInfo& shortcut_info, |
105 const SkBitmap& shortcut_icon); | 101 const SkBitmap& shortcut_icon); |
106 | 102 |
107 // Starts installion of the downloaded WebAPK. | 103 // Returns whether the device supports installing WebAPKs. |
108 // |file_path| is the file path that the WebAPK was downloaded to. | 104 virtual bool CanInstallWebApks(); |
109 // |package_name| is the package name of the WebAPK. | |
110 virtual void InstallDownloadedWebApk( | |
111 JNIEnv* env, | |
112 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, | |
113 const base::android::ScopedJavaLocalRef<jstring>& java_package_name); | |
114 | |
115 // Starts update using the downloaded WebAPK. | |
116 // |file_path| is the file path that the WebAPK was downloaded to. | |
117 virtual void UpdateUsingDownloadedWebApk( | |
118 JNIEnv* env, | |
119 const base::android::ScopedJavaLocalRef<jstring>& java_file_path); | |
120 | |
121 // Returns whether Google Play supports installing WebAPKs. | |
122 virtual bool CanUseGooglePlayInstallService(); | |
123 | 105 |
124 // Called when the package name of the WebAPK is available and the install | 106 // Called when the package name of the WebAPK is available and the install |
125 // or update request is handled by Google Play. | 107 // or update request should be issued. |
126 virtual void InstallOrUpdateWebApkFromGooglePlay( | 108 virtual void InstallOrUpdateWebApk(const std::string& package_name, |
127 const std::string& package_name, | 109 int version, |
128 int version, | 110 const std::string& token); |
129 const std::string& token); | |
130 | 111 |
131 // Called when the install or update process has completed or failed. | 112 // Called when the install or update process has completed or failed. |
132 void OnResult(WebApkInstallResult result); | 113 void OnResult(WebApkInstallResult result); |
133 | 114 |
134 private: | 115 private: |
135 enum TaskType { | 116 enum TaskType { |
136 UNDEFINED, | 117 UNDEFINED, |
137 INSTALL, | 118 INSTALL, |
138 UPDATE, | 119 UPDATE, |
139 }; | 120 }; |
(...skipping 23 matching lines...) Expand all Loading... |
163 void OnGotIconMurmur2Hash(const std::string& icon_murmur2_hash); | 144 void OnGotIconMurmur2Hash(const std::string& icon_murmur2_hash); |
164 | 145 |
165 // Sends request to WebAPK server to create WebAPK. During a successful | 146 // Sends request to WebAPK server to create WebAPK. During a successful |
166 // request the WebAPK server responds with the URL of the generated WebAPK. | 147 // request the WebAPK server responds with the URL of the generated WebAPK. |
167 // |webapk| is the proto to send to the WebAPK server. | 148 // |webapk| is the proto to send to the WebAPK server. |
168 void SendCreateWebApkRequest(std::unique_ptr<webapk::WebApk> webapk_proto); | 149 void SendCreateWebApkRequest(std::unique_ptr<webapk::WebApk> webapk_proto); |
169 | 150 |
170 // Sends request to WebAPK server to update a WebAPK. During a successful | 151 // Sends request to WebAPK server to update a WebAPK. During a successful |
171 // request the WebAPK server responds with the URL of the generated WebAPK. | 152 // request the WebAPK server responds with the URL of the generated WebAPK. |
172 // |webapk| is the proto to send to the WebAPK server. | 153 // |webapk| is the proto to send to the WebAPK server. |
173 void SendUpdateWebApkRequest(std::unique_ptr<webapk::WebApk> webapk_proto); | 154 void SendUpdateWebApkRequest(int webapk_version, |
| 155 std::unique_ptr<webapk::WebApk> webapk_proto); |
174 | 156 |
175 // Sends a request to WebAPK server to create/update WebAPK. During a | 157 // Sends a request to WebAPK server to create/update WebAPK. During a |
176 // successful request the WebAPK server responds with the URL of the generated | 158 // successful request the WebAPK server responds with the URL of the generated |
177 // WebAPK. | 159 // WebAPK. |
178 void SendRequest(std::unique_ptr<webapk::WebApk> request_proto, | 160 void SendRequest(std::unique_ptr<webapk::WebApk> request_proto, |
179 const GURL& server_url); | 161 const GURL& server_url); |
180 | 162 |
181 // Called with the URL of generated WebAPK and the package name that the | |
182 // WebAPK should be installed at. | |
183 void OnGotWebApkDownloadUrl(const GURL& download_url, | |
184 const std::string& package_name); | |
185 | |
186 // Downloads the WebAPK from the given |download_url|. | |
187 void DownloadWebApk(const base::FilePath& output_path, | |
188 const GURL& download_url, | |
189 bool retry_if_fails); | |
190 | |
191 // Called once the sub directory to store the downloaded WebAPK was | |
192 // created with permissions set properly or if creation failed. | |
193 void OnCreatedSubDirAndSetPermissions(const GURL& download_url, | |
194 const base::FilePath& file_path); | |
195 | |
196 // Called once the WebAPK has been downloaded. Makes the downloaded WebAPK | |
197 // world readable and installs the WebAPK if the download was successful. | |
198 // |file_path| is the file path that the WebAPK was downloaded to. | |
199 // If |retry_if_fails| is true, will post a delayed task and retry the | |
200 // download after 2 seconds. | |
201 void OnWebApkDownloaded(const base::FilePath& file_path, | |
202 const GURL& download_url, | |
203 bool retry_if_fails, | |
204 FileDownloader::Result result); | |
205 | |
206 // Called once the downloaded WebAPK has been made world readable. Installs | |
207 // the WebAPK. | |
208 // |file_path| is the file path that the WebAPK was downloaded to. | |
209 // |change_permission_success| is whether the WebAPK could be made world | |
210 // readable. | |
211 void OnWebApkMadeWorldReadable(const base::FilePath& file_path, | |
212 bool change_permission_success); | |
213 | |
214 net::URLRequestContextGetter* request_context_getter_; | 163 net::URLRequestContextGetter* request_context_getter_; |
215 | 164 |
216 // Sends HTTP request to WebAPK server. | 165 // Sends HTTP request to WebAPK server. |
217 std::unique_ptr<net::URLFetcher> url_fetcher_; | 166 std::unique_ptr<net::URLFetcher> url_fetcher_; |
218 | 167 |
219 // Downloads WebAPK. | |
220 std::unique_ptr<FileDownloader> downloader_; | |
221 | |
222 // Fails WebApkInstaller if WebAPK server takes too long to respond or if the | 168 // Fails WebApkInstaller if WebAPK server takes too long to respond or if the |
223 // download takes too long. | 169 // download takes too long. |
224 base::OneShotTimer timer_; | 170 base::OneShotTimer timer_; |
225 | 171 |
226 // Tracks how long it takes to install a WebAPK. | 172 // Tracks how long it takes to install a WebAPK. |
227 std::unique_ptr<base::ElapsedTimer> install_duration_timer_; | 173 std::unique_ptr<base::ElapsedTimer> install_duration_timer_; |
228 | 174 |
229 // Callback to call once WebApkInstaller succeeds or fails. | 175 // Callback to call once WebApkInstaller succeeds or fails. |
230 FinishCallback finish_callback_; | 176 FinishCallback finish_callback_; |
231 | 177 |
232 // Web Manifest info. | 178 // Web Manifest info. |
233 const ShortcutInfo shortcut_info_; | 179 const ShortcutInfo shortcut_info_; |
234 | 180 |
235 // WebAPK app icon. | 181 // WebAPK app icon. |
236 const SkBitmap shortcut_icon_; | 182 const SkBitmap shortcut_icon_; |
237 | 183 |
238 // WebAPK server URL. | 184 // WebAPK server URL. |
239 GURL server_url_; | 185 GURL server_url_; |
240 | 186 |
241 // The number of milliseconds to wait for the WebAPK download URL from the | 187 // The number of milliseconds to wait for the WebAPK server to respond. |
242 // WebAPK server. | 188 int webapk_server_timeout_ms_; |
243 int webapk_download_url_timeout_ms_; | |
244 | |
245 // The number of milliseconds to wait for the WebAPK download to complete. | |
246 int download_timeout_ms_; | |
247 | 189 |
248 // WebAPK package name. | 190 // WebAPK package name. |
249 std::string webapk_package_; | 191 std::string webapk_package_; |
250 | 192 |
251 // Whether the server wants the WebAPK to request updates less frequently. | 193 // Whether the server wants the WebAPK to request updates less frequently. |
252 bool relax_updates_; | 194 bool relax_updates_; |
253 | 195 |
254 // WebAPK version code. | |
255 int webapk_version_; | |
256 | 196 |
257 // Indicates whether the installer is for installing or updating a WebAPK. | 197 // Indicates whether the installer is for installing or updating a WebAPK. |
258 TaskType task_type_; | 198 TaskType task_type_; |
259 | 199 |
260 // Points to the Java Object. | 200 // Points to the Java Object. |
261 base::android::ScopedJavaGlobalRef<jobject> java_ref_; | 201 base::android::ScopedJavaGlobalRef<jobject> java_ref_; |
262 | 202 |
263 // Used to get |weak_ptr_|. | 203 // Used to get |weak_ptr_|. |
264 base::WeakPtrFactory<WebApkInstaller> weak_ptr_factory_; | 204 base::WeakPtrFactory<WebApkInstaller> weak_ptr_factory_; |
265 | 205 |
266 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); | 206 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); |
267 }; | 207 }; |
268 | 208 |
269 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ | 209 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ |
OLD | NEW |