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