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_delegate.h" | 18 #include "net/url_request/url_fetcher_delegate.h" |
19 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
20 | 20 |
21 namespace base { | 21 namespace base { |
22 class FilePath; | 22 class FilePath; |
23 } | 23 } |
24 | 24 |
25 namespace content { | 25 namespace content { |
26 class BrowserContext; | 26 class BrowserContext; |
27 } | 27 } |
28 | 28 |
| 29 namespace google { |
| 30 namespace protobuf { |
| 31 class MessageLite; |
| 32 } |
| 33 } |
| 34 |
29 namespace net { | 35 namespace net { |
30 class URLFetcher; | 36 class URLFetcher; |
31 class URLRequestContextGetter; | 37 class URLRequestContextGetter; |
32 } | 38 } |
33 | 39 |
34 namespace webapk { | 40 namespace webapk { |
35 class CreateWebApkRequest; | 41 class WebApk; |
36 } | 42 } |
37 | 43 |
38 // Talks to Chrome WebAPK server and Google Play to generate a WebAPK on the | 44 // Talks to Chrome WebAPK server and Google Play to generate a WebAPK on the |
39 // server, download it, and install it. | 45 // server, download it, and install it. |
40 class WebApkInstaller : public net::URLFetcherDelegate { | 46 class WebApkInstaller : public net::URLFetcherDelegate { |
41 public: | 47 public: |
| 48 // Called when either a request of creating or updating WebAPK has been sent |
| 49 // or the creation process of the WebAPK on the server side fails. |
| 50 // Parameters: |
| 51 // - whether the request succeeds. |
42 using FinishCallback = base::Callback<void(bool)>; | 52 using FinishCallback = base::Callback<void(bool)>; |
43 | 53 |
44 WebApkInstaller(const ShortcutInfo& shortcut_info, | 54 WebApkInstaller(const ShortcutInfo& shortcut_info, |
45 const SkBitmap& shorcut_icon); | 55 const SkBitmap& shorcut_icon); |
| 56 |
46 ~WebApkInstaller() override; | 57 ~WebApkInstaller() override; |
47 | 58 |
48 // Register JNI methods. | 59 // Register JNI methods. |
49 static bool Register(JNIEnv* env); | 60 static bool Register(JNIEnv* env); |
50 | 61 |
51 // Talks to the Chrome WebAPK server to generate a WebAPK on the server and to | 62 // Talks to the Chrome WebAPK server to generate a WebAPK on the server and to |
52 // Google Play to install the generated WebAPK. Calls |callback| after the | 63 // Google Play to install the generated WebAPK. Calls |callback| after the |
53 // request to install the WebAPK is sent to Google Play. | 64 // request to install the WebAPK is sent to Google Play. |
54 void InstallAsync(content::BrowserContext* browser_context, | 65 void InstallAsync(content::BrowserContext* browser_context, |
55 const FinishCallback& callback); | 66 const FinishCallback& callback); |
56 | 67 |
57 // Same as InstallAsync() but uses the passed in |request_context_getter|. | 68 // Same as InstallAsync() but uses the passed in |request_context_getter|. |
58 void InstallAsyncWithURLRequestContextGetter( | 69 void InstallAsyncWithURLRequestContextGetter( |
59 net::URLRequestContextGetter* request_context_getter, | 70 net::URLRequestContextGetter* request_context_getter, |
60 const FinishCallback& callback); | 71 const FinishCallback& callback); |
61 | 72 |
| 73 // Talks to the Chrome WebAPK server to update a WebAPK on the server and to |
| 74 // the Google Play server to download and install the generated WebAPK. Calls |
| 75 // |callback| after the request to download and install the WebAPK is sent to |
| 76 // the Google Play server. |
| 77 void UpdateAsync(content::BrowserContext* browser_context, |
| 78 const FinishCallback& callback, |
| 79 const std::string& webapk_package, |
| 80 int version); |
| 81 |
62 protected: | 82 protected: |
63 // Starts installation of the downloaded WebAPK. Returns whether the install | 83 // Starts installation of the downloaded WebAPK. Returns whether the install |
64 // could be started. The installation may still fail if true is returned. | 84 // 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. | 85 // |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. | 86 // |package_name| is the package name that the WebAPK should be installed at. |
67 // The Method is virtual for the sake of testing. | 87 // The Method is virtual for the sake of testing. |
68 virtual bool StartDownloadedWebApkInstall( | 88 virtual bool StartDownloadedWebApkInstall( |
69 JNIEnv* env, | 89 JNIEnv* env, |
70 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, | 90 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, |
71 const base::android::ScopedJavaLocalRef<jstring>& java_package_name); | 91 const base::android::ScopedJavaLocalRef<jstring>& java_package_name); |
72 | 92 |
73 private: | 93 private: |
74 // net::URLFetcherDelegate: | 94 // net::URLFetcherDelegate: |
75 void OnURLFetchComplete(const net::URLFetcher* source) override; | 95 void OnURLFetchComplete(const net::URLFetcher* source) override; |
76 | 96 |
77 // Initializes |request_context_getter_| on UI thread. | 97 // Initializes |request_context_getter_| on UI thread. |
78 void InitializeRequestContextGetterOnUIThread( | 98 void InitializeRequestContextGetterOnUIThread( |
79 content::BrowserContext* browser_context); | 99 content::BrowserContext* browser_context); |
80 | 100 |
81 // Sends request to WebAPK server to create WebAPK. During a successful | 101 // Sends request to WebAPK server to create WebAPK. |
82 // request the WebAPK server responds with the URL of the generated WebAPK. | |
83 void SendCreateWebApkRequest(); | 102 void SendCreateWebApkRequest(); |
84 | 103 |
| 104 // Sends request to WebAPK server to update a WebAPK. |
| 105 void SendUpdateWebApkRequest(); |
| 106 |
| 107 // Sends a request to WebAPK server. During a successful request the |
| 108 // WebAPK server responds with a URL to send to Google Play to |
| 109 // download and install the generated WebAPK. |
| 110 void SendRequest( |
| 111 std::unique_ptr<::google::protobuf::MessageLite> request_proto); |
| 112 |
85 // Called with the URL of generated WebAPK and the package name that the | 113 // Called with the URL of generated WebAPK and the package name that the |
86 // WebAPK should be installed at. | 114 // WebAPK should be installed at. |
87 void OnGotWebApkDownloadUrl(const std::string& download_url, | 115 void OnGotWebApkDownloadUrl(const std::string& download_url, |
88 const std::string& package_name); | 116 const std::string& package_name); |
89 | 117 |
90 // Called once the WebAPK has been downloaded. Installs the WebAPK if the | 118 // Called once the WebAPK has been downloaded. Installs the WebAPK if the |
91 // download was successful. | 119 // download was successful. |
92 // |file_path| is the file path that the WebAPK was downloaded to. | 120 // |file_path| is the file path that the WebAPK was downloaded to. |
93 // |package_name| is the package name that the WebAPK should be installed at. | 121 // |package_name| is the package name that the WebAPK should be installed at. |
94 void OnWebApkDownloaded(const base::FilePath& file_path, | 122 void OnWebApkDownloaded(const base::FilePath& file_path, |
95 const std::string& package_name, | 123 const std::string& package_name, |
96 FileDownloader::Result result); | 124 FileDownloader::Result result); |
97 | 125 |
98 // Populates webapk::CreateWebApkRequest and returns it. | 126 // Populate the |webapk| field of a request. |
99 std::unique_ptr<webapk::CreateWebApkRequest> BuildCreateWebApkRequest(); | 127 void PopulateWebApkProto(webapk::WebApk* webapk); |
100 | 128 |
101 // Called when the request to the WebAPK server times out or when the WebAPK | 129 // Called when the request to the WebAPK server times out or when the WebAPK |
102 // download times out. | 130 // download times out. |
103 void OnTimeout(); | 131 void OnTimeout(); |
104 | 132 |
105 // Called when the request to install the WebAPK is sent to Google Play. | 133 // Called when the request to install the WebAPK is sent to Google Play. |
106 void OnSuccess(); | 134 void OnSuccess(); |
107 | 135 |
108 // Called if a WebAPK could not be created. WebApkInstaller only tracks the | 136 // Called if a WebAPK could not be created. WebApkInstaller only tracks the |
109 // WebAPK creation and the WebAPK download. It does not track the | 137 // WebAPK creation and the WebAPK download. It does not track the |
(...skipping 18 matching lines...) Expand all Loading... |
128 | 156 |
129 // Web Manifest info. | 157 // Web Manifest info. |
130 const ShortcutInfo shortcut_info_; | 158 const ShortcutInfo shortcut_info_; |
131 | 159 |
132 // WebAPK app icon. | 160 // WebAPK app icon. |
133 const SkBitmap shortcut_icon_; | 161 const SkBitmap shortcut_icon_; |
134 | 162 |
135 // WebAPK server URL. | 163 // WebAPK server URL. |
136 GURL server_url_; | 164 GURL server_url_; |
137 | 165 |
| 166 // WebAPK package name. |
| 167 std::string webapk_package_; |
| 168 |
| 169 // WebAPK version code. |
| 170 int version_; |
| 171 |
138 // Used to get |weak_ptr_| on the IO thread. | 172 // Used to get |weak_ptr_| on the IO thread. |
139 base::WeakPtrFactory<WebApkInstaller> io_weak_ptr_factory_; | 173 base::WeakPtrFactory<WebApkInstaller> io_weak_ptr_factory_; |
140 | 174 |
141 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); | 175 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); |
142 }; | 176 }; |
143 | 177 |
144 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ | 178 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_INSTALLER_H_ |
OLD | NEW |