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/manifest_upgrade_detector_fetcher.h" | 5 #include "chrome/browser/android/webapk/manifest_upgrade_detector_fetcher.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
| 8 #include <vector> |
8 | 9 |
9 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 11 #include "chrome/browser/android/shortcut_helper.h" |
10 #include "chrome/browser/android/shortcut_info.h" | 12 #include "chrome/browser/android/shortcut_info.h" |
| 13 #include "chrome/browser/installable/installable_manager.h" |
11 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
12 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
13 #include "content/public/common/manifest.h" | 16 #include "content/public/common/manifest.h" |
14 #include "jni/ManifestUpgradeDetectorFetcher_jni.h" | 17 #include "jni/ManifestUpgradeDetectorFetcher_jni.h" |
| 18 #include "third_party/smhasher/src/MurmurHash2.h" |
| 19 #include "ui/gfx/android/java_bitmap.h" |
| 20 #include "ui/gfx/codec/png_codec.h" |
15 #include "url/gurl.h" | 21 #include "url/gurl.h" |
16 | 22 |
17 using base::android::JavaParamRef; | 23 using base::android::JavaParamRef; |
18 using base::android::ScopedJavaLocalRef; | 24 using base::android::ScopedJavaLocalRef; |
19 | 25 |
20 namespace { | 26 namespace { |
21 | 27 |
| 28 // The seed to use for the murmur2 hash. |
| 29 const uint64_t kMurmur2HashSeed = 0; |
| 30 |
| 31 // Computes a murmur2 hash of |bitmap|'s PNG encoded bytes. |
| 32 uint64_t ComputeBitmapHash(const SkBitmap& bitmap) { |
| 33 std::vector<unsigned char> png_bytes; |
| 34 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_bytes); |
| 35 return MurmurHash64B(&png_bytes.front(), png_bytes.size(), kMurmur2HashSeed); |
| 36 } |
| 37 |
22 // Returns whether the given |url| is within the scope of the |scope| url. | 38 // Returns whether the given |url| is within the scope of the |scope| url. |
23 bool IsInScope(const GURL& url, const GURL& scope) { | 39 bool IsInScope(const GURL& url, const GURL& scope) { |
24 return base::StartsWith(url.spec(), scope.spec(), | 40 return base::StartsWith(url.spec(), scope.spec(), |
25 base::CompareCase::SENSITIVE); | 41 base::CompareCase::SENSITIVE); |
26 } | 42 } |
27 | 43 |
28 } // anonymous namespace | 44 } // anonymous namespace |
29 | 45 |
30 jlong Initialize(JNIEnv* env, | 46 jlong Initialize(JNIEnv* env, |
31 const JavaParamRef<jobject>& obj, | 47 const JavaParamRef<jobject>& obj, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 } | 97 } |
82 | 98 |
83 void ManifestUpgradeDetectorFetcher::DidFinishLoad( | 99 void ManifestUpgradeDetectorFetcher::DidFinishLoad( |
84 content::RenderFrameHost* render_frame_host, | 100 content::RenderFrameHost* render_frame_host, |
85 const GURL& validated_url) { | 101 const GURL& validated_url) { |
86 if (render_frame_host->GetParent()) | 102 if (render_frame_host->GetParent()) |
87 return; | 103 return; |
88 if (!IsInScope(validated_url, scope_)) | 104 if (!IsInScope(validated_url, scope_)) |
89 return; | 105 return; |
90 | 106 |
91 web_contents()->GetManifest( | 107 InstallableParams params; |
92 base::Bind(&ManifestUpgradeDetectorFetcher::OnDidGetManifest, | 108 params.ideal_icon_size_in_dp = |
| 109 ShortcutHelper::GetIdealHomescreenIconSizeInDp(); |
| 110 params.minimum_icon_size_in_dp = |
| 111 ShortcutHelper::GetMinimumHomescreenIconSizeInDp(); |
| 112 params.fetch_valid_icon = true; |
| 113 InstallableManager::CreateForWebContents(web_contents()); |
| 114 InstallableManager* installable_manager = |
| 115 InstallableManager::FromWebContents(web_contents()); |
| 116 installable_manager->GetData( |
| 117 params, |
| 118 base::Bind(&ManifestUpgradeDetectorFetcher::OnDidGetInstallableData, |
93 weak_ptr_factory_.GetWeakPtr())); | 119 weak_ptr_factory_.GetWeakPtr())); |
94 } | 120 } |
95 | 121 |
96 void ManifestUpgradeDetectorFetcher::OnDidGetManifest( | 122 void ManifestUpgradeDetectorFetcher::OnDidGetInstallableData( |
97 const GURL& manifest_url, | 123 const InstallableData& data) { |
98 const content::Manifest& manifest) { | |
99 // If the manifest is empty, it means the current WebContents doesn't | 124 // If the manifest is empty, it means the current WebContents doesn't |
100 // associate with a Web Manifest. In such case, we ignore the empty manifest | 125 // associate with a Web Manifest. In such case, we ignore the empty manifest |
101 // and continue observing the WebContents's loading until we find a page that | 126 // and continue observing the WebContents's loading until we find a page that |
102 // links to the Web Manifest that we are looking for. | 127 // links to the Web Manifest that we are looking for. |
103 // If the manifest URL is different from the current one, we will continue | 128 // If the manifest URL is different from the current one, we will continue |
104 // observing too. It is based on our assumption that it is invalid for | 129 // observing too. It is based on our assumption that it is invalid for |
105 // web developers to change the Web Manifest location. When it does | 130 // web developers to change the Web Manifest location. When it does |
106 // change, we will treat the new Web Manifest as the one of another WebAPK. | 131 // change, we will treat the new Web Manifest as the one of another WebAPK. |
107 if (manifest.IsEmpty() || web_manifest_url_ != manifest_url) | 132 if (data.manifest.IsEmpty() || web_manifest_url_ != data.manifest_url) |
108 return; | 133 return; |
109 | 134 |
110 ShortcutInfo info(GURL::EmptyGURL()); | 135 ShortcutInfo info(GURL::EmptyGURL()); |
111 info.UpdateFromManifest(manifest); | 136 info.UpdateFromManifest(data.manifest); |
112 info.manifest_url = manifest_url; | 137 info.manifest_url = data.manifest_url; |
113 | 138 info.icon_url = data.icon_url; |
114 OnDataAvailable(info); | 139 OnDataAvailable(info, (data.icon ? *data.icon : SkBitmap())); |
115 } | 140 } |
116 | 141 |
117 void ManifestUpgradeDetectorFetcher::OnDataAvailable(const ShortcutInfo& info) { | 142 void ManifestUpgradeDetectorFetcher::OnDataAvailable( |
| 143 const ShortcutInfo& info, |
| 144 const SkBitmap& icon_bitmap) { |
118 JNIEnv* env = base::android::AttachCurrentThread(); | 145 JNIEnv* env = base::android::AttachCurrentThread(); |
119 | 146 |
120 ScopedJavaLocalRef<jstring> java_url = | 147 ScopedJavaLocalRef<jstring> java_url = |
121 base::android::ConvertUTF8ToJavaString(env, info.url.spec()); | 148 base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
122 ScopedJavaLocalRef<jstring> java_scope = | 149 ScopedJavaLocalRef<jstring> java_scope = |
123 base::android::ConvertUTF8ToJavaString(env, info.scope.spec()); | 150 base::android::ConvertUTF8ToJavaString(env, info.scope.spec()); |
124 ScopedJavaLocalRef<jstring> java_name = | 151 ScopedJavaLocalRef<jstring> java_name = |
125 base::android::ConvertUTF16ToJavaString(env, info.name); | 152 base::android::ConvertUTF16ToJavaString(env, info.name); |
126 ScopedJavaLocalRef<jstring> java_short_name = | 153 ScopedJavaLocalRef<jstring> java_short_name = |
127 base::android::ConvertUTF16ToJavaString(env, info.short_name); | 154 base::android::ConvertUTF16ToJavaString(env, info.short_name); |
| 155 ScopedJavaLocalRef<jstring> java_icon_url = |
| 156 base::android::ConvertUTF8ToJavaString(env, info.icon_url.spec()); |
| 157 ScopedJavaLocalRef<jobject> java_bitmap; |
| 158 uint64_t icon_murmur2_hash = 0; |
| 159 if (icon_bitmap.getSize()) { |
| 160 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
| 161 // TODO(pkotwicz): Get hash of untransformed icon's bytes (with no |
| 162 // encoding/decoding). |
| 163 icon_murmur2_hash = ComputeBitmapHash(icon_bitmap); |
| 164 } |
128 | 165 |
129 Java_ManifestUpgradeDetectorFetcher_onDataAvailable( | 166 Java_ManifestUpgradeDetectorFetcher_onDataAvailable( |
130 env, java_ref_, java_url, java_scope, java_name, java_short_name, | 167 env, java_ref_, java_url, java_scope, java_name, java_short_name, |
131 info.display, info.orientation, info.theme_color, info.background_color); | 168 java_icon_url, icon_murmur2_hash, java_bitmap, info.display, |
| 169 info.orientation, info.theme_color, info.background_color); |
132 } | 170 } |
OLD | NEW |