1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/webapk/webapk_update_data_fetcher.h"
#include <jni.h>
#include <optional>
#include <set>
#include <string>
#include <vector>
#include "base/android/build_info.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "components/webapps/browser/android/webapp_icon.h"
#include "components/webapps/browser/android/webapps_icon_utils.h"
#include "components/webapps/browser/android/webapps_utils.h"
#include "components/webapps/browser/features.h"
#include "components/webapps/browser/installable/installable_manager.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "third_party/blink/public/common/manifest/manifest_util.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"
#include "third_party/smhasher/src/src/MurmurHash2.h"
#include "ui/android/color_utils_android.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "url/gurl.h"
#include "url/origin.h"
// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/android/chrome_jni_headers/WebApkUpdateDataFetcher_jni.h"
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;
namespace {
// Returns whether the given |url| is within the scope of the |scope| url.
bool IsInScope(const GURL& url, const GURL& scope) {
return base::StartsWith(url.spec(), scope.spec(),
base::CompareCase::SENSITIVE);
}
} // anonymous namespace
jlong JNI_WebApkUpdateDataFetcher_Initialize(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
std::string& java_start_url,
std::string& java_scope_url,
std::string& java_web_manifest_url,
const JavaParamRef<jstring>& java_web_manifest_id) {
GURL start_url(java_start_url);
GURL scope(java_scope_url);
GURL web_manifest_url(java_web_manifest_url);
GURL web_manifest_id;
if (!java_web_manifest_id.is_null()) {
web_manifest_id =
GURL(base::android::ConvertJavaStringToUTF8(env, java_web_manifest_id));
}
WebApkUpdateDataFetcher* fetcher = new WebApkUpdateDataFetcher(
env, obj, start_url, scope, web_manifest_url, web_manifest_id);
return reinterpret_cast<intptr_t>(fetcher);
}
WebApkUpdateDataFetcher::WebApkUpdateDataFetcher(JNIEnv* env,
jobject obj,
const GURL& start_url,
const GURL& scope,
const GURL& web_manifest_url,
const GURL& web_manifest_id)
: content::WebContentsObserver(nullptr),
start_url_(start_url),
scope_(scope),
web_manifest_url_(web_manifest_url),
web_manifest_id_(web_manifest_id),
info_(GURL()) {
java_ref_.Reset(env, obj);
}
WebApkUpdateDataFetcher::~WebApkUpdateDataFetcher() = default;
void WebApkUpdateDataFetcher::ReplaceWebContents(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& java_web_contents) {
content::WebContents* web_contents =
content::WebContents::FromJavaWebContents(java_web_contents);
content::WebContentsObserver::Observe(web_contents);
}
void WebApkUpdateDataFetcher::Destroy(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
delete this;
}
void WebApkUpdateDataFetcher::Start(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& java_web_contents) {
ReplaceWebContents(env, obj, java_web_contents);
if (!web_contents()->IsLoading())
FetchInstallableData();
}
void WebApkUpdateDataFetcher::DidStopLoading() {
FetchInstallableData();
}
void WebApkUpdateDataFetcher::FetchInstallableData() {
GURL url = web_contents()->GetLastCommittedURL();
// DidStopLoading() can be called multiple times for a single URL. Only fetch
// installable data the first time.
if (url == last_fetched_url_)
return;
last_fetched_url_ = url;
if (!IsInScope(url, scope_))
return;
webapps::InstallableParams params;
params.installable_criteria =
webapps::InstallableCriteria::kValidManifestWithIcons;
params.prefer_maskable_icon = true;
params.valid_primary_icon = true;
webapps::InstallableManager* installable_manager =
webapps::InstallableManager::FromWebContents(web_contents());
installable_manager->GetData(
params, base::BindOnce(&WebApkUpdateDataFetcher::OnDidGetInstallableData,
weak_ptr_factory_.GetWeakPtr()));
}
void WebApkUpdateDataFetcher::OnDidGetInstallableData(
const webapps::InstallableData& data) {
// Determine whether or not the manifest is WebAPK-compatible. There are 3
// cases:
// 1. the site isn't installable.
// 2. the URLs in the manifest expose passwords.
// 3. there is no manifest or the manifest is different to the one we're
// expecting.
// For case 3, if the manifest is empty, it means the current WebContents
// doesn't associate with a Web Manifest. In such case, we ignore the empty
// manifest and continue observing the WebContents's loading until we find a
// page that links to the Web Manifest that we are looking for.
// If the manifest URL is different from the current one, we will continue
// observing too. It is based on our assumption that it is invalid for
// web developers to change the Web Manifest location. When it does
// change, we will treat the new Web Manifest as the one of another WebAPK.
if (!data.errors.empty() || blink::IsEmptyManifest(*data.manifest) ||
!webapps::WebappsUtils::AreWebManifestUrlsWebApkCompatible(
*data.manifest)) {
return;
}
CHECK(!data.manifest->id.is_empty());
// If there isn't an existing manifest ID, check if either manifest URL or
// start URL are the same. If neither of them are the same, we treat the
// manifest as one of another WebAPK.
if (web_manifest_id_.is_empty() && web_manifest_url_ != *data.manifest_url &&
start_url_ != data.manifest->start_url) {
return;
}
// If there is an existing manifest ID, but the fetched manifest id is
// different from the current one, continue observing as the id is the
// identity for the application. We will treat the manifest with different id
// as the one of another WebAPK.
if (!web_manifest_id_.is_empty() && web_manifest_id_ != data.manifest->id) {
return;
}
info_.UpdateFromManifest(*data.manifest);
info_.manifest_url = *data.manifest_url;
info_.best_primary_icon_url = *data.primary_icon_url;
info_.is_primary_icon_maskable = data.has_maskable_primary_icon;
primary_icon_ = *data.primary_icon;
info_.UpdateBestSplashIcon(*data.manifest);
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
icon_hasher_ = std::make_unique<webapps::WebApkIconsHasher>();
icon_hasher_->DownloadAndComputeMurmur2Hash(
profile->GetDefaultStoragePartition()
->GetURLLoaderFactoryForBrowserProcess()
.get(),
web_contents()->GetWeakPtr(), url::Origin::Create(last_fetched_url_),
info_, primary_icon_,
base::BindOnce(&WebApkUpdateDataFetcher::OnGotIconMurmur2Hashes,
weak_ptr_factory_.GetWeakPtr()));
}
void WebApkUpdateDataFetcher::OnGotIconMurmur2Hashes(
std::map<GURL, std::unique_ptr<webapps::WebappIcon>> icons) {
if (icons.empty()) {
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jstring> java_manifest_id =
base::android::ConvertUTF8ToJavaString(env, info_.manifest_id.spec());
std::string primary_icon_murmur2_hash =
icons[info_.best_primary_icon_url]->hash();
ScopedJavaLocalRef<jobject> java_primary_icon =
gfx::ConvertToJavaBitmap(primary_icon_);
jboolean java_is_primary_icon_maskable = info_.is_primary_icon_maskable;
jboolean java_is_splash_icon_maskable = info_.is_splash_image_maskable;
std::string splash_icon_hash = "";
std::string splash_icon_data = "";
{
auto it = icons.find(info_.splash_image_url);
if (it != icons.end()) {
splash_icon_hash = it->second->hash();
splash_icon_data = it->second->ExtractData();
}
}
base::android::ScopedJavaLocalRef<jbyteArray> java_splash_icon_data =
base::android::ToJavaByteArray(env, splash_icon_data);
ScopedJavaLocalRef<jobjectArray> java_icon_urls =
base::android::ToJavaArrayOfStrings(env, info_.icon_urls);
std::string share_action;
std::u16string share_params_title;
std::u16string share_params_text;
jboolean java_share_params_is_method_post = false;
jboolean java_share_params_is_enctype_multipart = false;
ScopedJavaLocalRef<jobjectArray> java_share_params_file_names;
ScopedJavaLocalRef<jobjectArray> java_share_params_accepts;
if (info_.share_target.has_value() && info_.share_target->action.is_valid()) {
share_action = info_.share_target->action.spec();
share_params_title = info_.share_target->params.title;
share_params_text = info_.share_target->params.text;
java_share_params_is_method_post =
(info_.share_target->method ==
blink::mojom::ManifestShareTarget_Method::kPost);
java_share_params_is_enctype_multipart =
(info_.share_target->enctype ==
blink::mojom::ManifestShareTarget_Enctype::kMultipartFormData);
std::vector<std::u16string> file_names;
std::vector<std::vector<std::u16string>> accepts;
for (auto& f : info_.share_target->params.files) {
file_names.push_back(f.name);
accepts.push_back(f.accept);
}
java_share_params_file_names =
base::android::ToJavaArrayOfStrings(env, file_names);
java_share_params_accepts =
base::android::ToJavaArrayOfStringArray(env, accepts);
}
// Wraps the shortcut info in a 2D vector for convenience.
// The inner vector represents a shortcut items, with the following fields:
// <name>, <short name>, <launch url>, <icon url>, <icon hash>.
std::vector<std::vector<std::u16string>> shortcuts;
// Each entry contains the icon data for the corresponding entry in
// |shortcuts|.
std::vector<std::string> shortcut_icon_data;
DCHECK_EQ(info_.shortcut_items.size(), info_.best_shortcut_icon_urls.size());
for (size_t i = 0; i < info_.shortcut_items.size(); i++) {
const auto& shortcut = info_.shortcut_items[i];
const GURL& chosen_icon_url = info_.best_shortcut_icon_urls[i];
auto it = icons.find(chosen_icon_url);
std::string chosen_icon_hash;
std::string chosen_icon_data;
if (it != icons.end()) {
chosen_icon_hash = it->second->hash();
chosen_icon_data = it->second->ExtractData();
}
shortcuts.push_back({shortcut.name,
shortcut.short_name.value_or(std::u16string()),
base::UTF8ToUTF16(shortcut.url.spec()),
base::UTF8ToUTF16(chosen_icon_url.spec()),
base::UTF8ToUTF16(chosen_icon_hash)});
shortcut_icon_data.push_back(std::move(chosen_icon_data));
}
Java_WebApkUpdateDataFetcher_onDataAvailable(
env, java_ref_, info_.url.spec(), info_.scope.spec(), info_.name,
info_.short_name, info_.manifest_url.spec(), java_manifest_id,
info_.best_primary_icon_url.spec(), primary_icon_murmur2_hash,
java_primary_icon, java_is_primary_icon_maskable,
info_.splash_image_url.spec(), splash_icon_hash, java_splash_icon_data,
java_is_splash_icon_maskable, java_icon_urls,
static_cast<int>(info_.display), static_cast<int>(info_.orientation),
ui::OptionalSkColorToJavaColor(info_.theme_color),
ui::OptionalSkColorToJavaColor(info_.background_color),
ui::OptionalSkColorToJavaColor(info_.dark_theme_color),
ui::OptionalSkColorToJavaColor(info_.dark_background_color), share_action,
share_params_title, share_params_text, java_share_params_is_method_post,
java_share_params_is_enctype_multipart, java_share_params_file_names,
java_share_params_accepts,
base::android::ToJavaArrayOfStringArray(env, shortcuts),
base::android::ToJavaArrayOfByteArray(env, shortcut_icon_data));
}
|