Chromium Code Reviews| 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 // If the manifest is empty, it means the current WebContents doesn't |
| 129 // associate with a Web Manifest. In such case, we ignore the empty manifest | 133 // associate with a Web Manifest. In such case, we ignore the empty manifest |
| 130 // and continue observing the WebContents's loading until we find a page that | 134 // and continue observing the WebContents's loading until we find a page that |
| 131 // links to the Web Manifest that we are looking for. | 135 // links to the Web Manifest that we are looking for. |
| 132 // If the manifest URL is different from the current one, we will continue | 136 // 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 | 137 // 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 | 138 // 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. | 139 // 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) | 140 if (data.manifest.IsEmpty() || web_manifest_url_ != data.manifest_url) { |
| 137 return; | 141 OnWebManifestNotWebApkCompatible(); |
| 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 || | |
| 142 !AreWebManifestUrlsWebApkCompatible(data.manifest)) { | |
| 143 return; | 142 return; |
| 144 } | 143 } |
| 145 | 144 |
| 145 if (data.error_code != NO_ERROR_DETECTED || | |
|
Xi Han
2016/12/20 20:53:23
I believe Peter splited them as two checks because
dominickn
2016/12/21 02:14:32
The comment can be very easily changed to incorpor
Xi Han
2016/12/21 22:25:11
All right, merge them together. I re-order these c
| |
| 146 !AreWebManifestUrlsWebApkCompatible(data.manifest)) { | |
| 147 OnWebManifestNotWebApkCompatible(); | |
| 148 return; | |
| 149 } | |
| 150 | |
| 146 info_.UpdateFromManifest(data.manifest); | 151 info_.UpdateFromManifest(data.manifest); |
| 147 info_.manifest_url = data.manifest_url; | 152 info_.manifest_url = data.manifest_url; |
| 148 info_.best_icon_url = data.icon_url; | 153 info_.best_icon_url = data.icon_url; |
| 149 best_icon_ = *data.icon; | 154 best_icon_ = *data.icon; |
| 150 | 155 |
| 151 icon_hasher_.reset(new WebApkIconHasher()); | 156 icon_hasher_.reset(new WebApkIconHasher()); |
| 152 Profile* profile = | 157 Profile* profile = |
| 153 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 158 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 154 icon_hasher_->DownloadAndComputeMurmur2Hash( | 159 icon_hasher_->DownloadAndComputeMurmur2Hash( |
| 155 profile->GetRequestContext(), | 160 profile->GetRequestContext(), |
| 156 data.icon_url, | 161 data.icon_url, |
| 157 base::Bind(&WebApkUpdateDataFetcher::OnGotIconMurmur2Hash, | 162 base::Bind(&WebApkUpdateDataFetcher::OnGotIconMurmur2Hash, |
| 158 weak_ptr_factory_.GetWeakPtr())); | 163 weak_ptr_factory_.GetWeakPtr())); |
| 159 } | 164 } |
| 160 | 165 |
| 161 void WebApkUpdateDataFetcher::OnGotIconMurmur2Hash( | 166 void WebApkUpdateDataFetcher::OnGotIconMurmur2Hash( |
| 162 const std::string& best_icon_murmur2_hash) { | 167 const std::string& best_icon_murmur2_hash) { |
| 163 icon_hasher_.reset(); | 168 icon_hasher_.reset(); |
| 164 | 169 |
| 165 if (best_icon_murmur2_hash.empty()) { | 170 if (best_icon_murmur2_hash.empty()) { |
| 166 // TODO(pkotwicz): Tell Java side that the Web Manifest was fetched but the | 171 OnWebManifestNotWebApkCompatible(); |
| 167 // Web Manifest is not WebAPK-compatible. (http://crbug.com/639536) | |
| 168 return; | 172 return; |
| 169 } | 173 } |
| 170 | 174 |
| 171 OnDataAvailable(info_, best_icon_murmur2_hash, best_icon_); | 175 OnDataAvailable(info_, best_icon_murmur2_hash, best_icon_); |
| 172 } | 176 } |
| 173 | 177 |
| 174 void WebApkUpdateDataFetcher::OnDataAvailable( | 178 void WebApkUpdateDataFetcher::OnDataAvailable( |
| 175 const ShortcutInfo& info, | 179 const ShortcutInfo& info, |
| 176 const std::string& best_icon_murmur2_hash, | 180 const std::string& best_icon_murmur2_hash, |
| 177 const SkBitmap& best_icon_bitmap) { | 181 const SkBitmap& best_icon_bitmap) { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 194 | 198 |
| 195 ScopedJavaLocalRef<jobjectArray> java_icon_urls = | 199 ScopedJavaLocalRef<jobjectArray> java_icon_urls = |
| 196 base::android::ToJavaArrayOfStrings(env, info.icon_urls); | 200 base::android::ToJavaArrayOfStrings(env, info.icon_urls); |
| 197 | 201 |
| 198 Java_WebApkUpdateDataFetcher_onDataAvailable( | 202 Java_WebApkUpdateDataFetcher_onDataAvailable( |
| 199 env, java_ref_, java_url, java_scope, java_name, java_short_name, | 203 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, | 204 java_best_icon_url, java_best_icon_murmur2_hash, java_best_bitmap, |
| 201 java_icon_urls, info.display, info.orientation, info.theme_color, | 205 java_icon_urls, info.display, info.orientation, info.theme_color, |
| 202 info.background_color); | 206 info.background_color); |
| 203 } | 207 } |
| 208 | |
| 209 void WebApkUpdateDataFetcher::OnWebManifestNotWebApkCompatible() { | |
| 210 if (!is_initial_fetch_) | |
| 211 return; | |
| 212 | |
| 213 Java_WebApkUpdateDataFetcher_onWebManifestForInitialUrlNotWebApkCompatible( | |
| 214 base::android::AttachCurrentThread(), java_ref_); | |
| 215 } | |
| OLD | NEW |