OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_BUILDER_H_ |
| 6 #define CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_BUILDER_H_ |
| 7 |
| 8 #include <jni.h> |
| 9 #include <memory> |
| 10 |
| 11 #include "base/android/scoped_java_ref.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/timer/timer.h" |
| 16 #include "chrome/browser/android/shortcut_info.h" |
| 17 #include "chrome/browser/net/file_downloader.h" |
| 18 #include "net/url_request/url_fetcher_delegate.h" |
| 19 #include "third_party/skia/include/core/SkBitmap.h" |
| 20 |
| 21 namespace base { |
| 22 class FilePath; |
| 23 } |
| 24 |
| 25 namespace content { |
| 26 class BrowserContext; |
| 27 } |
| 28 |
| 29 namespace net { |
| 30 class URLFetcher; |
| 31 class URLRequestContextGetter; |
| 32 } |
| 33 |
| 34 namespace webapk { |
| 35 class CreateWebApkRequest; |
| 36 } |
| 37 |
| 38 // Talks to Chrome WebAPK server and Google Play to generate a WebAPK on the |
| 39 // server, download it, and install it. |
| 40 class WebApkInstaller : public net::URLFetcherDelegate { |
| 41 public: |
| 42 using FinishCallback = base::Callback<void(bool)>; |
| 43 |
| 44 WebApkInstaller(const ShortcutInfo& shortcut_info, |
| 45 const SkBitmap& shorcut_icon); |
| 46 ~WebApkInstaller() override; |
| 47 |
| 48 // Register JNI methods. |
| 49 static bool Register(JNIEnv* env); |
| 50 |
| 51 // 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 |
| 53 // request to install the WebAPK is sent to Google Play. |
| 54 void InstallAsync(content::BrowserContext* browser_context, |
| 55 const FinishCallback& callback); |
| 56 |
| 57 // Same as InstallAsync() but uses the passed in |request_context_getter|. |
| 58 void InstallAsyncWithURLRequestContextGetter( |
| 59 net::URLRequestContextGetter* request_context_getter, |
| 60 const FinishCallback& callback); |
| 61 |
| 62 protected: |
| 63 // Starts installation of the downloaded WebAPK. Returns whether the install |
| 64 // 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. |
| 66 // |package_name| is the package name that the WebAPK should be installed at. |
| 67 // The Method is virtual for the sake of testing. |
| 68 virtual bool StartDownloadedWebApkInstall( |
| 69 JNIEnv* env, |
| 70 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, |
| 71 const base::android::ScopedJavaLocalRef<jstring>& java_package_name); |
| 72 |
| 73 private: |
| 74 // net::URLFetcherDelegate: |
| 75 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 76 |
| 77 // Initializes |request_context_getter_| on UI thread. |
| 78 void InitializeRequestContextGetterOnUIThread( |
| 79 content::BrowserContext* browser_context); |
| 80 |
| 81 // Sends request to WebAPK server to create WebAPK. During a successful |
| 82 // request the WebAPK server responds with the URL of the generated WebAPK. |
| 83 void SendCreateWebApkRequest(); |
| 84 |
| 85 // Called with the URL of generated WebAPK and the package name that the |
| 86 // WebAPK should be installed at. |
| 87 void OnGotWebApkDownloadUrl(const std::string& download_url, |
| 88 const std::string& package_name); |
| 89 |
| 90 // Called once the WebAPK has been downloaded. Installs the WebAPK if the |
| 91 // download was successful. |
| 92 // |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. |
| 94 void OnWebApkDownloaded(const base::FilePath& file_path, |
| 95 const std::string& package_name, |
| 96 FileDownloader::Result result); |
| 97 |
| 98 // Populates webapk::CreateWebApkRequest and returns it. |
| 99 std::unique_ptr<webapk::CreateWebApkRequest> BuildCreateWebApkRequest(); |
| 100 |
| 101 // Called when the request to the WebAPK server times out or when the WebAPK |
| 102 // download times out. |
| 103 void OnTimeout(); |
| 104 |
| 105 // Called when the request to install the WebAPK is sent to Google Play. |
| 106 void OnSuccess(); |
| 107 |
| 108 // Called if a WebAPK could not be created. WebApkInstaller only tracks the |
| 109 // WebAPK creation and the WebAPK download. It does not track the |
| 110 // WebAPK installation. OnFailure() is not called if the WebAPK could not be |
| 111 // installed. |
| 112 void OnFailure(); |
| 113 |
| 114 net::URLRequestContextGetter* request_context_getter_; |
| 115 |
| 116 // Sends HTTP request to WebAPK server. |
| 117 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 118 |
| 119 // Downloads WebAPK. |
| 120 std::unique_ptr<FileDownloader> downloader_; |
| 121 |
| 122 // Fails WebApkInstaller if WebAPK server takes too long to respond or if the |
| 123 // download takes too long. |
| 124 base::OneShotTimer timer_; |
| 125 |
| 126 // Callback to call once WebApkInstaller succeeds or fails. |
| 127 FinishCallback finish_callback_; |
| 128 |
| 129 // Web Manifest info. |
| 130 const ShortcutInfo shortcut_info_; |
| 131 |
| 132 // WebAPK app icon. |
| 133 const SkBitmap shortcut_icon_; |
| 134 |
| 135 // WebAPK server URL. |
| 136 GURL server_url_; |
| 137 |
| 138 // Used to get |weak_ptr_| on the IO thread. |
| 139 base::WeakPtrFactory<WebApkInstaller> io_weak_ptr_factory_; |
| 140 |
| 141 DISALLOW_COPY_AND_ASSIGN(WebApkInstaller); |
| 142 }; |
| 143 |
| 144 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_BUILDER_H_ |
OLD | NEW |