OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/android/webapps/manifest_upgrade_detector.h" | |
6 | |
7 #include <jni.h> | |
8 | |
9 #include "base/android/jni_android.h" | |
10 #include "base/android/jni_array.h" | |
11 #include "base/android/jni_string.h" | |
12 #include "chrome/browser/android/shortcut_info.h" | |
13 #include "chrome/browser/banners/app_banner_manager.h" | |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "content/public/browser/render_frame_host.h" | |
16 #include "content/public/browser/web_contents.h" | |
17 #include "content/public/common/manifest.h" | |
18 #include "jni/ManifestUpgradeDetector_jni.h" | |
19 #include "url/gurl.h" | |
20 | |
21 using content::Manifest; | |
22 | |
23 jlong Initialize(JNIEnv* env, | |
24 const JavaParamRef<jobject>& obj, | |
25 const JavaParamRef<jobject>& java_web_contents, | |
26 const JavaParamRef<jstring>& java_web_manifest_url) { | |
27 content::WebContents* web_contents = | |
28 content::WebContents::FromJavaWebContents(java_web_contents); | |
29 GURL web_manifest_url(base::android::ConvertJavaStringToUTF16( | |
30 env, java_web_manifest_url)); | |
31 ManifestUpgradeDetector* manifest_upgrade_detector = | |
32 new ManifestUpgradeDetector(env, obj, web_contents, web_manifest_url); | |
33 return reinterpret_cast<intptr_t>(manifest_upgrade_detector); | |
34 } | |
35 | |
36 ManifestUpgradeDetector::ManifestUpgradeDetector( | |
37 JNIEnv* env, | |
38 jobject obj, | |
39 content::WebContents* web_contents, | |
40 const GURL& web_manifest_url) | |
41 : content::WebContentsObserver(web_contents), | |
42 start_(false), | |
43 web_manifest_url_(web_manifest_url), | |
44 weak_ptr_factory_(this) { | |
45 java_ref_.Reset(env, obj); | |
46 } | |
47 | |
48 ManifestUpgradeDetector::~ManifestUpgradeDetector() { | |
49 } | |
50 | |
51 // static | |
52 bool ManifestUpgradeDetector::Register(JNIEnv* env) { | |
53 return RegisterNativesImpl(env); | |
54 } | |
55 | |
56 void ManifestUpgradeDetector::ReplaceWebContents( | |
57 JNIEnv* env, | |
58 const JavaParamRef<jobject>& obj, | |
59 const JavaParamRef<jobject>& jweb_contents) { | |
60 content::WebContents* web_contents = | |
61 content::WebContents::FromJavaWebContents(jweb_contents); | |
62 content::WebContentsObserver::Observe(web_contents); | |
63 } | |
64 | |
65 void ManifestUpgradeDetector::DidFinishLoad( | |
66 content::RenderFrameHost* render_frame_host, | |
67 const GURL& validated_url) { | |
68 if (render_frame_host->GetParent()) | |
69 return; | |
70 if (start_) { | |
71 content::WebContents* web_contents = | |
72 content::WebContents::FromRenderFrameHost(render_frame_host); | |
pkotwicz
2016/07/13 01:08:20
Nit: You can get the WebContents via WebContentsOb
Xi Han
2016/07/13 19:25:14
Thanks!
| |
73 web_contents->GetManifest( | |
74 base::Bind(&ManifestUpgradeDetector::OnDidGetManifest, | |
75 weak_ptr_factory_.GetWeakPtr())); | |
76 } | |
77 } | |
pkotwicz
2016/07/13 01:08:20
You should override WebContentsObserver::DidNaviga
Xi Han
2016/07/13 19:25:14
Add the scope to check whether the url loaded is w
| |
78 | |
79 void ManifestUpgradeDetector::OnDidGetManifest( | |
80 const GURL& manifest_url, | |
81 const content::Manifest& manifest) { | |
82 if (manifest.IsEmpty() | |
83 ||!banners::AppBannerManager::URLsAreForTheSamePage( | |
pkotwicz
2016/07/13 01:08:20
Check:
web_manifest_url_ != manifest_url
instead
Xi Han
2016/07/13 19:25:14
It makes sense, updated.
| |
84 web_manifest_url_, manifest_url)) { | |
85 return; | |
86 } | |
87 | |
88 start_ = false; | |
89 | |
90 ShortcutInfo info(GURL::EmptyGURL()); | |
91 info.UpdateFromManifest(manifest); | |
92 info.manifest_url = manifest_url; | |
93 | |
pkotwicz
2016/07/13 01:08:20
Ignore icon URLs for now too :)
Xi Han
2016/07/13 19:25:14
We could have the icon_urls, since they are obtain
pkotwicz
2016/07/13 21:40:38
I am suggesting skipping the icons for now because
Xi Han
2016/07/15 19:30:01
No matter how we treat them later, these urls are
| |
94 std::vector<std::string> icon_urls; | |
95 for (const auto& icon : manifest.icons) | |
96 icon_urls.push_back(icon.src.spec()); | |
97 | |
98 OnDataAvailable(info, icon_urls); | |
99 } | |
100 | |
101 void ManifestUpgradeDetector::OnDataAvailable( | |
102 const ShortcutInfo& info, | |
103 const std::vector<std::string>& icons) { | |
104 JNIEnv* env = base::android::AttachCurrentThread(); | |
105 | |
106 ScopedJavaLocalRef<jstring> java_url = | |
107 base::android::ConvertUTF8ToJavaString(env, info.url.spec()); | |
108 ScopedJavaLocalRef<jstring> java_user_title = | |
109 base::android::ConvertUTF16ToJavaString(env, info.user_title); | |
pkotwicz
2016/07/13 01:08:20
|java_user_title| is not used?
Xi Han
2016/07/13 19:25:14
Removed.
| |
110 ScopedJavaLocalRef<jstring> java_name = | |
111 base::android::ConvertUTF16ToJavaString(env, info.name); | |
112 ScopedJavaLocalRef<jstring> java_short_name = | |
113 base::android::ConvertUTF16ToJavaString(env, info.short_name); | |
114 ScopedJavaLocalRef<jobjectArray> java_icons = | |
115 base::android::ToJavaArrayOfStrings(env, icons); | |
116 | |
117 Java_ManifestUpgradeDetector_onDataAvailable( | |
118 env, java_ref_.obj(), | |
119 java_url.obj(), | |
120 java_name.obj(), | |
121 java_short_name.obj(), | |
122 java_icons.obj(), | |
123 info.display, | |
124 info.orientation, | |
125 info.theme_color, | |
126 info.background_color); | |
127 } | |
128 | |
129 void ManifestUpgradeDetector::Destroy(JNIEnv* env, | |
130 const JavaParamRef<jobject>& obj) { | |
131 delete this; | |
132 } | |
133 | |
134 void ManifestUpgradeDetector::Start(JNIEnv* env, | |
135 const JavaParamRef<jobject>& obj) { | |
136 start_ = true; | |
137 } | |
OLD | NEW |