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/callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/timer/timer.h" |
| 15 #include "chrome/browser/android/shortcut_info.h" |
| 16 #include "net/url_request/url_fetcher_delegate.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 18 |
| 19 namespace content { |
| 20 class BrowserContext; |
| 21 } |
| 22 |
| 23 namespace net { |
| 24 class URLFetcher; |
| 25 class URLRequestContextGetter; |
| 26 } |
| 27 |
| 28 namespace webapk { |
| 29 class CreateWebApkRequest; |
| 30 } |
| 31 |
| 32 // Talks to Chrome WebAPK server and Google Play servers to generate a WebAPK on |
| 33 // the server, download it, and install it. |
| 34 class WebApkBuilder : public net::URLFetcherDelegate { |
| 35 public: |
| 36 typedef base::Callback<void(bool)> FinishCallback; |
| 37 |
| 38 WebApkBuilder(content::BrowserContext* browser_context, |
| 39 const ShortcutInfo& shortcut_info, |
| 40 const SkBitmap& shorcut_icon); |
| 41 ~WebApkBuilder() override; |
| 42 |
| 43 // Register JNI methods. |
| 44 static bool Register(JNIEnv* env); |
| 45 |
| 46 // Converts a blink::WebDisplayMode to a string. Returns one of |
| 47 // https://www.w3.org/TR/appmanifest/#dfn-fallback-display-mode |
| 48 static std::string DisplayToString(blink::WebDisplayMode display); |
| 49 |
| 50 // Converts a blink::WebScreenOrientationLockType to a string. Returns one of |
| 51 // https://www.w3.org/TR/screen-orientation/#orientationlocktype-enum |
| 52 static std::string OrientationToString( |
| 53 blink::WebScreenOrientationLockType orientation); |
| 54 |
| 55 // Converts a color from the format specified in content::Manifest to a CSS |
| 56 // string. |
| 57 static std::string ColorToString(int64_t color); |
| 58 |
| 59 // Talks to the Chrome WebAPK server to generate a WebAPK on the server and to |
| 60 // the Google Play server to download and install the generated WebAPK. Calls |
| 61 // |callback| after the request to download and install the WebAPK is sent to |
| 62 // the Google Play server. |
| 63 void BuildAsync(const FinishCallback& callback); |
| 64 |
| 65 private: |
| 66 // net::URLFetcherDelegate: |
| 67 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 68 |
| 69 // Initializes |request_context_getter_| on UI thread. |
| 70 void InitializeRequestContextGetterOnUIThread(); |
| 71 |
| 72 // Sends request to WebAPK server to create WebAPK. During a successful |
| 73 // request the WebAPK server responds with a URL to send to Google Play to |
| 74 // download and install the generated WebAPK. |
| 75 void SendCreateWebApkRequest(); |
| 76 |
| 77 // Called with the URL to send to the Google Play server to download and |
| 78 // install the WebAPK. |
| 79 void OnGotMarketUrl(const std::string& signed_market_url); |
| 80 |
| 81 // Populates webapk::CreateWebApkRequest and returns it. |
| 82 std::unique_ptr<webapk::CreateWebApkRequest> BuildCreateWebApkRequest(); |
| 83 |
| 84 // Called when the request to the server times out. |
| 85 void OnTimeout(); |
| 86 |
| 87 // Called when the request to download and install the WebAPK is sent to the |
| 88 // Google Play server. |
| 89 void OnSuccess(); |
| 90 |
| 91 // Called if a WebAPK could not be created. WebApkBuilder only tracks the |
| 92 // generation of the WebAPK on the WebAPK server. It does not track the WebAPK |
| 93 // download and installation. OnFailure() is not called if the WebAPK could |
| 94 // not be downloaded or installed. |
| 95 void OnFailure(); |
| 96 |
| 97 content::BrowserContext* browser_context_; |
| 98 net::URLRequestContextGetter* request_context_getter_; |
| 99 |
| 100 // Sends HTTP request to WebAPK server. |
| 101 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 102 |
| 103 // Fails WebApkBuilder if WebAPK server takes too long to respond. |
| 104 base::OneShotTimer timer_; |
| 105 |
| 106 // Callback to call once WebApkBuilder succeeds or fails. |
| 107 FinishCallback finish_callback_; |
| 108 |
| 109 // Web Manifest info. |
| 110 const ShortcutInfo shortcut_info_; |
| 111 |
| 112 // WebAPK app icon. |
| 113 const SkBitmap shortcut_icon_; |
| 114 |
| 115 // WebAPK server URL. |
| 116 GURL server_url_; |
| 117 |
| 118 base::WeakPtrFactory<WebApkBuilder> weak_ptr_factory_; |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(WebApkBuilder); |
| 121 }; |
| 122 |
| 123 #endif // CHROME_BROWSER_ANDROID_WEBAPK_WEBAPK_BUILDER_H_ |
OLD | NEW |