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 <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
16 #include "chrome/browser/android/shortcut_info.h" | 16 #include "chrome/browser/android/shortcut_info.h" |
17 #include "chrome/browser/net/file_downloader.h" | 17 #include "chrome/browser/net/file_downloader.h" |
18 #include "net/url_request/url_fetcher.h" | |
18 #include "net/url_request/url_fetcher_delegate.h" | 19 #include "net/url_request/url_fetcher_delegate.h" |
19 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
20 | 21 |
21 namespace base { | 22 namespace base { |
22 class FilePath; | 23 class FilePath; |
23 } | 24 } |
24 | 25 |
25 namespace content { | 26 namespace content { |
26 class BrowserContext; | 27 class BrowserContext; |
27 } | 28 } |
28 | 29 |
29 namespace net { | |
30 class URLFetcher; | |
31 class URLRequestContextGetter; | |
32 } | |
33 | |
34 namespace webapk { | 30 namespace webapk { |
35 class WebApk; | 31 class WebApk; |
36 } | 32 } |
37 | 33 |
38 // Talks to Chrome WebAPK server and Google Play to generate a WebAPK on the | 34 // Talks to Chrome WebAPK server and Google Play to generate a WebAPK on the |
39 // server, download it, and install it. | 35 // server, download it, and install it. |
40 class WebApkInstaller : public net::URLFetcherDelegate { | 36 class WebApkInstaller : public net::URLFetcherDelegate { |
41 public: | 37 public: |
38 // Called when either a request of creating or updating WebAPK has been sent | |
39 // or the creation process of the WebAPK on the server side fails. | |
40 // Parameters: | |
41 // - whether the request succeeds. | |
pkotwicz
2016/08/09 14:35:43
How about:
"Called when either a request for creat
Xi Han
2016/08/11 19:01:15
Done.
| |
42 using FinishCallback = base::Callback<void(bool)>; | 42 using FinishCallback = base::Callback<void(bool)>; |
43 | 43 |
44 WebApkInstaller(const ShortcutInfo& shortcut_info, | 44 WebApkInstaller(const ShortcutInfo& shortcut_info, |
45 const SkBitmap& shorcut_icon); | 45 const SkBitmap& shorcut_icon); |
46 | |
46 ~WebApkInstaller() override; | 47 ~WebApkInstaller() override; |
47 | 48 |
48 // Talks to the Chrome WebAPK server to generate a WebAPK on the server and to | 49 // Talks to the Chrome WebAPK server to generate a WebAPK on the server and to |
49 // Google Play to install the generated WebAPK. Calls |callback| after the | 50 // Google Play to install the generated WebAPK. Calls |callback| after the |
50 // request to install the WebAPK is sent to Google Play. | 51 // request to install the WebAPK is sent to Google Play. |
51 void InstallAsync(content::BrowserContext* browser_context, | 52 void InstallAsync(content::BrowserContext* browser_context, |
52 const FinishCallback& callback); | 53 const FinishCallback& callback); |
53 | 54 |
54 // Same as InstallAsync() but uses the passed in |request_context_getter|. | 55 // Same as InstallAsync() but uses the passed in |request_context_getter|. |
55 void InstallAsyncWithURLRequestContextGetter( | 56 void InstallAsyncWithURLRequestContextGetter( |
56 net::URLRequestContextGetter* request_context_getter, | 57 net::URLRequestContextGetter* request_context_getter, |
57 const FinishCallback& callback); | 58 const FinishCallback& callback); |
58 | 59 |
60 // Talks to the Chrome WebAPK server to update a WebAPK on the server and to | |
61 // the Google Play server to download and install the generated WebAPK. Calls | |
62 // |callback| after the request to download and install the WebAPK is sent to | |
63 // the Google Play server. | |
64 void UpdateAsync(content::BrowserContext* browser_context, | |
65 const FinishCallback& callback, | |
66 const std::string& webapk_package, | |
67 int version); | |
68 | |
59 // Sets the timeout for the server requests. | 69 // Sets the timeout for the server requests. |
60 void SetTimeoutMs(int timeout_ms); | 70 void SetTimeoutMs(int timeout_ms); |
61 | 71 |
62 protected: | 72 protected: |
63 // Starts installation of the downloaded WebAPK. Returns whether the install | 73 // Starts installation of the downloaded WebAPK. Returns whether the install |
64 // could be started. The installation may still fail if true is returned. | 74 // could be started. The installation may still fail if true is returned. |
65 // |file_path| is the file path that the WebAPK was downloaded to. | 75 // |file_path| is the file path that the WebAPK was downloaded to. |
66 // |package_name| is the package name that the WebAPK should be installed at. | 76 // |package_name| is the package name that the WebAPK should be installed at. |
67 virtual bool StartDownloadedWebApkInstall( | 77 virtual bool StartDownloadedWebApkInstall( |
68 JNIEnv* env, | 78 JNIEnv* env, |
69 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, | 79 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, |
70 const base::android::ScopedJavaLocalRef<jstring>& java_package_name); | 80 const base::android::ScopedJavaLocalRef<jstring>& java_package_name); |
71 | 81 |
72 private: | 82 private: |
83 enum TaskType { | |
84 UNDEFINED, | |
85 INSTALL, | |
86 UPDATE, | |
87 }; | |
88 | |
73 // net::URLFetcherDelegate: | 89 // net::URLFetcherDelegate: |
74 void OnURLFetchComplete(const net::URLFetcher* source) override; | 90 void OnURLFetchComplete(const net::URLFetcher* source) override; |
75 | 91 |
76 // Initializes |request_context_getter_| on UI thread. | 92 // Initializes |request_context_getter_| on UI thread. |
77 void InitializeRequestContextGetterOnUIThread( | 93 void InitializeRequestContextGetterOnUIThread( |
78 content::BrowserContext* browser_context); | 94 content::BrowserContext* browser_context); |
79 | 95 |
80 // Sends request to WebAPK server to create WebAPK. During a successful | 96 // Sends request to WebAPK server to create WebAPK. |
81 // request the WebAPK server responds with the URL of the generated WebAPK. | |
82 void SendCreateWebApkRequest(); | 97 void SendCreateWebApkRequest(); |
83 | 98 |
99 // Sends request to WebAPK server to update a WebAPK. | |
100 void SendUpdateWebApkRequest(); | |
101 | |
102 // Sends a request to WebAPK server. During a successful request the | |
103 // WebAPK server responds with a URL to send to Google Play to | |
104 // download and install the generated WebAPK. | |
pkotwicz
2016/08/09 14:35:43
Nit: The comment is incorrect. WebApkInstaller doe
Xi Han
2016/08/11 19:01:15
Done.
| |
105 void SendRequest( | |
106 std::unique_ptr<webapk::WebApk> request_proto, | |
107 net::URLFetcher::RequestType request_type, | |
108 const GURL& server_url); | |
109 | |
84 // Called with the URL of generated WebAPK and the package name that the | 110 // Called with the URL of generated WebAPK and the package name that the |
85 // WebAPK should be installed at. | 111 // WebAPK should be installed at. |
86 void OnGotWebApkDownloadUrl(const GURL& download_url, | 112 void OnGotWebApkDownloadUrl(const GURL& download_url, |
87 const std::string& package_name); | 113 const std::string& package_name); |
88 | 114 |
89 // Called once the WebAPK has been downloaded. Installs the WebAPK if the | 115 // Called once the WebAPK has been downloaded. Installs the WebAPK if the |
90 // download was successful. | 116 // download was successful. |
91 // |file_path| is the file path that the WebAPK was downloaded to. | 117 // |file_path| is the file path that the WebAPK was downloaded to. |
92 // |package_name| is the package name that the WebAPK should be installed at. | 118 // |package_name| is the package name that the WebAPK should be installed at. |
93 void OnWebApkDownloaded(const base::FilePath& file_path, | 119 void OnWebApkDownloaded(const base::FilePath& file_path, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 // WebAPK server URL. | 160 // WebAPK server URL. |
135 GURL server_url_; | 161 GURL server_url_; |
136 | 162 |
137 // The number of milliseconds to wait for the WebAPK download URL from the | 163 // The number of milliseconds to wait for the WebAPK download URL from the |
138 // WebAPK server. | 164 // WebAPK server. |
139 int webapk_download_url_timeout_ms_; | 165 int webapk_download_url_timeout_ms_; |
140 | 166 |
141 // The number of milliseconds to wait for the WebAPK download to complete. | 167 // The number of milliseconds to wait for the WebAPK download to complete. |
142 int download_timeout_ms_; | 168 int download_timeout_ms_; |
143 | 169 |
170 // WebAPK package name. | |
171 std::string webapk_package_; | |
172 | |
173 // WebAPK version code. | |
174 int webapk_version_; | |
175 | |
176 // A flag to indicate whether the installer is for installing or updating a | |
177 // WebAPK. | |
pkotwicz
2016/08/09 14:35:43
How about: "Indicates whether the installer ..."
Xi Han
2016/08/11 19:01:15
Done.
| |
178 TaskType task_type_; | |
179 | |
144 // Used to get |weak_ptr_| on the IO thread. | 180 // Used to get |weak_ptr_| on the IO thread. |
145 base::WeakPtrFactory<WebApkInstaller> io_weak_ptr_factory_; | 181 base::WeakPtrFactory<WebApkInstaller> io_weak_ptr_factory_; |
146 | 182 |
147 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); | 183 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); |
148 }; | 184 }; |
149 | 185 |
150 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ | 186 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ |
OLD | NEW |