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" | |
pkotwicz
2016/07/13 21:01:15
Nit: Remove include :)
Xi Han
2016/07/15 19:30:01
Done.
| |
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; | |
pkotwicz
2016/07/13 21:01:15
In my opinion (others will probably disagree), a h
Xi Han
2016/07/15 19:30:01
Sure, just forgot to remove it when some functions
| |
22 | |
23 namespace { | |
pkotwicz
2016/07/13 21:01:15
Nit: new line
Xi Han
2016/07/15 19:30:01
Done.
| |
24 // Returns whether the given |url| is within the scope of the |scope| url. | |
pkotwicz
2016/07/13 21:01:15
Super nit: I think that it is clearer if you switc
Xi Han
2016/07/15 19:30:01
Done.
| |
25 bool IsInScope(const GURL& scope, const GURL& url) { | |
26 const std::string& scope_str = scope.spec().c_str(); | |
27 const std::string& url_str = url.spec().c_str(); | |
28 auto res = std::mismatch(scope_str.begin(), scope_str.end(), url_str.begin()); | |
29 return res.first == scope_str.end(); | |
pkotwicz
2016/07/13 21:01:15
Can you use base::StartsWith() ?
Xi Han
2016/07/15 19:30:01
Have been looking for this function but haven't fo
| |
30 } | |
31 | |
32 } // namespace | |
pkotwicz
2016/07/13 21:01:15
Nit: 2 spaces before //
Xi Han
2016/07/15 19:30:01
Done.
| |
33 | |
34 jlong Initialize(JNIEnv* env, | |
35 const JavaParamRef<jobject>& obj, | |
36 const JavaParamRef<jobject>& java_web_contents, | |
37 const JavaParamRef<jstring>& java_scope, | |
38 const JavaParamRef<jstring>& java_web_manifest_url) { | |
39 content::WebContents* web_contents = | |
40 content::WebContents::FromJavaWebContents(java_web_contents); | |
41 GURL scope(base::android::ConvertJavaStringToUTF8(env, java_scope)); | |
42 GURL web_manifest_url(base::android::ConvertJavaStringToUTF8( | |
43 env, java_web_manifest_url)); | |
44 ManifestUpgradeDetector* manifest_upgrade_detector = | |
45 new ManifestUpgradeDetector(env, obj, web_contents, scope, | |
46 web_manifest_url); | |
47 return reinterpret_cast<intptr_t>(manifest_upgrade_detector); | |
48 } | |
49 | |
50 ManifestUpgradeDetector::ManifestUpgradeDetector( | |
51 JNIEnv* env, | |
52 jobject obj, | |
53 content::WebContents* web_contents, | |
54 const GURL& scope, | |
55 const GURL& web_manifest_url) | |
56 : content::WebContentsObserver(web_contents), | |
57 start_(false), | |
58 scope_(scope), | |
59 web_manifest_url_(web_manifest_url), | |
60 weak_ptr_factory_(this) { | |
61 java_ref_.Reset(env, obj); | |
62 } | |
63 | |
64 ManifestUpgradeDetector::~ManifestUpgradeDetector() { | |
65 } | |
66 | |
67 // static | |
68 bool ManifestUpgradeDetector::Register(JNIEnv* env) { | |
69 return RegisterNativesImpl(env); | |
70 } | |
71 | |
72 void ManifestUpgradeDetector::ReplaceWebContents( | |
73 JNIEnv* env, | |
74 const JavaParamRef<jobject>& obj, | |
75 const JavaParamRef<jobject>& jweb_contents) { | |
76 content::WebContents* web_contents = | |
77 content::WebContents::FromJavaWebContents(jweb_contents); | |
78 content::WebContentsObserver::Observe(web_contents); | |
79 } | |
80 | |
81 void ManifestUpgradeDetector::DidFinishLoad( | |
82 content::RenderFrameHost* render_frame_host, | |
83 const GURL& validated_url) { | |
84 if (render_frame_host->GetParent()) | |
85 return; | |
86 if (start_ && IsInScope(scope_, validated_url)) { | |
pkotwicz
2016/07/13 21:01:15
Nit: For the sake of consistency early return
if
Xi Han
2016/07/15 19:30:01
Done.
| |
87 content::WebContents* web_contents = | |
88 content::WebContentsObserver::web_contents(); | |
89 web_contents->GetManifest( | |
90 base::Bind(&ManifestUpgradeDetector::OnDidGetManifest, | |
91 weak_ptr_factory_.GetWeakPtr())); | |
92 } | |
93 } | |
94 | |
95 void ManifestUpgradeDetector::OnDidGetManifest( | |
96 const GURL& manifest_url, | |
97 const content::Manifest& manifest) { | |
pkotwicz
2016/07/13 21:01:15
Can you please add a comment why we are ignoring t
Xi Han
2016/07/15 19:30:01
Done.
| |
98 if (manifest.IsEmpty() || web_manifest_url_ != manifest_url) | |
99 return; | |
100 | |
101 start_ = false; | |
102 | |
103 ShortcutInfo info(GURL::EmptyGURL()); | |
pkotwicz
2016/07/13 21:01:15
Can you use GURL() instead of GURL::EmptyGURL() ?
Xi Han
2016/07/15 19:30:01
No, we will end up with compiler error, since the
pkotwicz
2016/07/16 00:56:45
I am willing to bet that we will not have a compil
Xi Han
2016/07/18 21:49:38
When passing a reference, the variable should be s
pkotwicz
2016/07/19 18:02:37
From offline discussion, I believe that the error
Xi Han
2016/07/20 18:54:59
Yes, an extra "()" works: ShortcutInfo info((GURL(
| |
104 info.UpdateFromManifest(manifest); | |
105 info.manifest_url = manifest_url; | |
106 | |
107 std::vector<std::string> icon_urls; | |
108 for (const auto& icon : manifest.icons) | |
109 icon_urls.push_back(icon.src.spec()); | |
110 | |
111 OnDataAvailable(info, icon_urls); | |
112 } | |
113 | |
114 void ManifestUpgradeDetector::OnDataAvailable( | |
115 const ShortcutInfo& info, | |
116 const std::vector<std::string>& icons) { | |
117 JNIEnv* env = base::android::AttachCurrentThread(); | |
118 | |
119 ScopedJavaLocalRef<jstring> java_url = | |
120 base::android::ConvertUTF8ToJavaString(env, info.url.spec()); | |
121 ScopedJavaLocalRef<jstring> java_name = | |
122 base::android::ConvertUTF16ToJavaString(env, info.name); | |
123 ScopedJavaLocalRef<jstring> java_short_name = | |
124 base::android::ConvertUTF16ToJavaString(env, info.short_name); | |
125 ScopedJavaLocalRef<jobjectArray> java_icons = | |
126 base::android::ToJavaArrayOfStrings(env, icons); | |
127 | |
128 Java_ManifestUpgradeDetector_onDataAvailable( | |
129 env, java_ref_.obj(), | |
130 java_url.obj(), | |
131 java_name.obj(), | |
132 java_short_name.obj(), | |
133 java_icons.obj(), | |
134 info.display, | |
135 info.orientation, | |
136 info.theme_color, | |
137 info.background_color); | |
138 } | |
139 | |
140 void ManifestUpgradeDetector::Destroy(JNIEnv* env, | |
141 const JavaParamRef<jobject>& obj) { | |
142 delete this; | |
143 } | |
144 | |
145 void ManifestUpgradeDetector::Start(JNIEnv* env, | |
146 const JavaParamRef<jobject>& obj) { | |
pkotwicz
2016/07/13 21:01:15
Nit: Order the functions in this file to match the
Xi Han
2016/07/15 19:30:01
Done.
| |
147 start_ = true; | |
148 } | |
OLD | NEW |