| 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 #include "chrome/browser/android/webapk/webapk_update_data_fetcher.h" | 5 #include "chrome/browser/android/webapk/webapk_update_data_fetcher.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 } | 48 } |
| 49 | 49 |
| 50 WebApkUpdateDataFetcher::WebApkUpdateDataFetcher( | 50 WebApkUpdateDataFetcher::WebApkUpdateDataFetcher( |
| 51 JNIEnv* env, | 51 JNIEnv* env, |
| 52 jobject obj, | 52 jobject obj, |
| 53 const GURL& scope, | 53 const GURL& scope, |
| 54 const GURL& web_manifest_url) | 54 const GURL& web_manifest_url) |
| 55 : content::WebContentsObserver(nullptr), | 55 : content::WebContentsObserver(nullptr), |
| 56 scope_(scope), | 56 scope_(scope), |
| 57 web_manifest_url_(web_manifest_url), | 57 web_manifest_url_(web_manifest_url), |
| 58 is_initial_fetch_(false), |
| 58 info_(GURL()), | 59 info_(GURL()), |
| 59 weak_ptr_factory_(this) { | 60 weak_ptr_factory_(this) { |
| 60 java_ref_.Reset(env, obj); | 61 java_ref_.Reset(env, obj); |
| 61 } | 62 } |
| 62 | 63 |
| 63 WebApkUpdateDataFetcher::~WebApkUpdateDataFetcher() { | 64 WebApkUpdateDataFetcher::~WebApkUpdateDataFetcher() { |
| 64 } | 65 } |
| 65 | 66 |
| 66 // static | 67 // static |
| 67 bool WebApkUpdateDataFetcher::Register(JNIEnv* env) { | 68 bool WebApkUpdateDataFetcher::Register(JNIEnv* env) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 95 FetchInstallableData(); | 96 FetchInstallableData(); |
| 96 } | 97 } |
| 97 | 98 |
| 98 void WebApkUpdateDataFetcher::FetchInstallableData() { | 99 void WebApkUpdateDataFetcher::FetchInstallableData() { |
| 99 GURL url = web_contents()->GetLastCommittedURL(); | 100 GURL url = web_contents()->GetLastCommittedURL(); |
| 100 | 101 |
| 101 // DidStopLoading() can be called multiple times for a single URL. Only fetch | 102 // DidStopLoading() can be called multiple times for a single URL. Only fetch |
| 102 // installable data the first time. | 103 // installable data the first time. |
| 103 if (url == last_fetched_url_) | 104 if (url == last_fetched_url_) |
| 104 return; | 105 return; |
| 106 is_initial_fetch_ = last_fetched_url_.is_empty(); |
| 105 last_fetched_url_ = url; | 107 last_fetched_url_ = url; |
| 106 | 108 |
| 107 if (!IsInScope(url, scope_)) | 109 if (!IsInScope(url, scope_)) { |
| 110 OnWebManifestNotWebApkCompatible(); |
| 108 return; | 111 return; |
| 112 } |
| 109 | 113 |
| 110 InstallableParams params; | 114 InstallableParams params; |
| 111 params.ideal_icon_size_in_dp = | 115 params.ideal_icon_size_in_dp = |
| 112 ShortcutHelper::GetIdealHomescreenIconSizeInDp(); | 116 ShortcutHelper::GetIdealHomescreenIconSizeInDp(); |
| 113 params.minimum_icon_size_in_dp = | 117 params.minimum_icon_size_in_dp = |
| 114 ShortcutHelper::GetMinimumHomescreenIconSizeInDp(); | 118 ShortcutHelper::GetMinimumHomescreenIconSizeInDp(); |
| 115 params.check_installable = true; | 119 params.check_installable = true; |
| 116 params.fetch_valid_icon = true; | 120 params.fetch_valid_icon = true; |
| 117 InstallableManager::CreateForWebContents(web_contents()); | 121 InstallableManager::CreateForWebContents(web_contents()); |
| 118 InstallableManager* installable_manager = | 122 InstallableManager* installable_manager = |
| 119 InstallableManager::FromWebContents(web_contents()); | 123 InstallableManager::FromWebContents(web_contents()); |
| 120 installable_manager->GetData( | 124 installable_manager->GetData( |
| 121 params, | 125 params, |
| 122 base::Bind(&WebApkUpdateDataFetcher::OnDidGetInstallableData, | 126 base::Bind(&WebApkUpdateDataFetcher::OnDidGetInstallableData, |
| 123 weak_ptr_factory_.GetWeakPtr())); | 127 weak_ptr_factory_.GetWeakPtr())); |
| 124 } | 128 } |
| 125 | 129 |
| 126 void WebApkUpdateDataFetcher::OnDidGetInstallableData( | 130 void WebApkUpdateDataFetcher::OnDidGetInstallableData( |
| 127 const InstallableData& data) { | 131 const InstallableData& data) { |
| 128 // If the manifest is empty, it means the current WebContents doesn't | 132 // Determine whether or not the manifest is WebAPK-compatible. There are 3 |
| 129 // associate with a Web Manifest. In such case, we ignore the empty manifest | 133 // cases: |
| 130 // and continue observing the WebContents's loading until we find a page that | 134 // 1. the site isn't installable. |
| 131 // links to the Web Manifest that we are looking for. | 135 // 2. the URLs in the manifest expose passwords. |
| 136 // 3. there is no manifest or the manifest is different to the one we're |
| 137 // expecting. |
| 138 // For case 3, if the manifest is empty, it means the current WebContents |
| 139 // doesn't associate with a Web Manifest. In such case, we ignore the empty |
| 140 // manifest and continue observing the WebContents's loading until we find a |
| 141 // page that links to the Web Manifest that we are looking for. |
| 132 // If the manifest URL is different from the current one, we will continue | 142 // If the manifest URL is different from the current one, we will continue |
| 133 // observing too. It is based on our assumption that it is invalid for | 143 // observing too. It is based on our assumption that it is invalid for |
| 134 // web developers to change the Web Manifest location. When it does | 144 // web developers to change the Web Manifest location. When it does |
| 135 // change, we will treat the new Web Manifest as the one of another WebAPK. | 145 // change, we will treat the new Web Manifest as the one of another WebAPK. |
| 136 if (data.manifest.IsEmpty() || web_manifest_url_ != data.manifest_url) | |
| 137 return; | |
| 138 | |
| 139 // TODO(pkotwicz): Tell Java side that the Web Manifest was fetched but the | |
| 140 // Web Manifest is not WebAPK-compatible. (http://crbug.com/639536) | |
| 141 if (data.error_code != NO_ERROR_DETECTED || | 146 if (data.error_code != NO_ERROR_DETECTED || |
| 147 data.manifest.IsEmpty() || web_manifest_url_ != data.manifest_url || |
| 142 !AreWebManifestUrlsWebApkCompatible(data.manifest)) { | 148 !AreWebManifestUrlsWebApkCompatible(data.manifest)) { |
| 149 OnWebManifestNotWebApkCompatible(); |
| 143 return; | 150 return; |
| 144 } | 151 } |
| 145 | 152 |
| 146 info_.UpdateFromManifest(data.manifest); | 153 info_.UpdateFromManifest(data.manifest); |
| 147 info_.manifest_url = data.manifest_url; | 154 info_.manifest_url = data.manifest_url; |
| 148 info_.best_icon_url = data.icon_url; | 155 info_.best_icon_url = data.icon_url; |
| 149 best_icon_ = *data.icon; | 156 best_icon_ = *data.icon; |
| 150 | 157 |
| 151 icon_hasher_.reset(new WebApkIconHasher()); | 158 icon_hasher_.reset(new WebApkIconHasher()); |
| 152 Profile* profile = | 159 Profile* profile = |
| 153 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 160 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 154 icon_hasher_->DownloadAndComputeMurmur2Hash( | 161 icon_hasher_->DownloadAndComputeMurmur2Hash( |
| 155 profile->GetRequestContext(), | 162 profile->GetRequestContext(), |
| 156 data.icon_url, | 163 data.icon_url, |
| 157 base::Bind(&WebApkUpdateDataFetcher::OnGotIconMurmur2Hash, | 164 base::Bind(&WebApkUpdateDataFetcher::OnGotIconMurmur2Hash, |
| 158 weak_ptr_factory_.GetWeakPtr())); | 165 weak_ptr_factory_.GetWeakPtr())); |
| 159 } | 166 } |
| 160 | 167 |
| 161 void WebApkUpdateDataFetcher::OnGotIconMurmur2Hash( | 168 void WebApkUpdateDataFetcher::OnGotIconMurmur2Hash( |
| 162 const std::string& best_icon_murmur2_hash) { | 169 const std::string& best_icon_murmur2_hash) { |
| 163 icon_hasher_.reset(); | 170 icon_hasher_.reset(); |
| 164 | 171 |
| 165 if (best_icon_murmur2_hash.empty()) { | 172 if (best_icon_murmur2_hash.empty()) { |
| 166 // TODO(pkotwicz): Tell Java side that the Web Manifest was fetched but the | 173 OnWebManifestNotWebApkCompatible(); |
| 167 // Web Manifest is not WebAPK-compatible. (http://crbug.com/639536) | |
| 168 return; | 174 return; |
| 169 } | 175 } |
| 170 | 176 |
| 171 OnDataAvailable(info_, best_icon_murmur2_hash, best_icon_); | 177 OnDataAvailable(info_, best_icon_murmur2_hash, best_icon_); |
| 172 } | 178 } |
| 173 | 179 |
| 174 void WebApkUpdateDataFetcher::OnDataAvailable( | 180 void WebApkUpdateDataFetcher::OnDataAvailable( |
| 175 const ShortcutInfo& info, | 181 const ShortcutInfo& info, |
| 176 const std::string& best_icon_murmur2_hash, | 182 const std::string& best_icon_murmur2_hash, |
| 177 const SkBitmap& best_icon_bitmap) { | 183 const SkBitmap& best_icon_bitmap) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 194 | 200 |
| 195 ScopedJavaLocalRef<jobjectArray> java_icon_urls = | 201 ScopedJavaLocalRef<jobjectArray> java_icon_urls = |
| 196 base::android::ToJavaArrayOfStrings(env, info.icon_urls); | 202 base::android::ToJavaArrayOfStrings(env, info.icon_urls); |
| 197 | 203 |
| 198 Java_WebApkUpdateDataFetcher_onDataAvailable( | 204 Java_WebApkUpdateDataFetcher_onDataAvailable( |
| 199 env, java_ref_, java_url, java_scope, java_name, java_short_name, | 205 env, java_ref_, java_url, java_scope, java_name, java_short_name, |
| 200 java_best_icon_url, java_best_icon_murmur2_hash, java_best_bitmap, | 206 java_best_icon_url, java_best_icon_murmur2_hash, java_best_bitmap, |
| 201 java_icon_urls, info.display, info.orientation, info.theme_color, | 207 java_icon_urls, info.display, info.orientation, info.theme_color, |
| 202 info.background_color); | 208 info.background_color); |
| 203 } | 209 } |
| 210 |
| 211 void WebApkUpdateDataFetcher::OnWebManifestNotWebApkCompatible() { |
| 212 if (!is_initial_fetch_) |
| 213 return; |
| 214 |
| 215 Java_WebApkUpdateDataFetcher_onWebManifestForInitialUrlNotWebApkCompatible( |
| 216 base::android::AttachCurrentThread(), java_ref_); |
| 217 } |
| OLD | NEW |