Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: chrome/browser/android/shortcut_helper.cc

Issue 568823003: Merge Android RetrieveWebappInformation and Extensions GetApplicationInfo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@manifest_manager_content
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/shortcut_helper.h" 5 #include "chrome/browser/android/shortcut_helper.h"
6 6
7 #include <jni.h> 7 #include <jni.h>
8 8
9 #include "base/android/jni_android.h" 9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h" 10 #include "base/android/jni_string.h"
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/strings/string16.h" 13 #include "base/strings/string16.h"
14 #include "base/task/cancelable_task_tracker.h" 14 #include "base/task/cancelable_task_tracker.h"
15 #include "base/threading/worker_pool.h" 15 #include "base/threading/worker_pool.h"
16 #include "chrome/browser/android/tab_android.h" 16 #include "chrome/browser/android/tab_android.h"
17 #include "chrome/browser/favicon/favicon_service.h" 17 #include "chrome/browser/favicon/favicon_service.h"
18 #include "chrome/browser/favicon/favicon_service_factory.h" 18 #include "chrome/browser/favicon/favicon_service_factory.h"
19 #include "chrome/common/chrome_constants.h"
19 #include "chrome/common/render_messages.h" 20 #include "chrome/common/render_messages.h"
21 #include "chrome/common/web_application_info.h"
20 #include "content/public/browser/user_metrics.h" 22 #include "content/public/browser/user_metrics.h"
21 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_contents_observer.h" 24 #include "content/public/browser/web_contents_observer.h"
23 #include "content/public/common/frame_navigate_params.h" 25 #include "content/public/common/frame_navigate_params.h"
24 #include "jni/ShortcutHelper_jni.h" 26 #include "jni/ShortcutHelper_jni.h"
25 #include "ui/gfx/android/java_bitmap.h" 27 #include "ui/gfx/android/java_bitmap.h"
26 #include "ui/gfx/codec/png_codec.h" 28 #include "ui/gfx/codec/png_codec.h"
27 #include "ui/gfx/color_analysis.h" 29 #include "ui/gfx/color_analysis.h"
28 #include "ui/gfx/favicon_size.h" 30 #include "ui/gfx/favicon_size.h"
29 #include "url/gurl.h" 31 #include "url/gurl.h"
30 32
31 jlong Initialize(JNIEnv* env, jobject obj, jlong tab_android_ptr) { 33 jlong Initialize(JNIEnv* env, jobject obj, jlong tab_android_ptr) {
32 TabAndroid* tab = reinterpret_cast<TabAndroid*>(tab_android_ptr); 34 TabAndroid* tab = reinterpret_cast<TabAndroid*>(tab_android_ptr);
33 35
34 ShortcutHelper* shortcut_helper = 36 ShortcutHelper* shortcut_helper =
35 new ShortcutHelper(env, obj, tab->web_contents()); 37 new ShortcutHelper(env, obj, tab->web_contents());
36 shortcut_helper->Initialize(); 38 shortcut_helper->Initialize();
37 39
38 return reinterpret_cast<intptr_t>(shortcut_helper); 40 return reinterpret_cast<intptr_t>(shortcut_helper);
39 } 41 }
40 42
41 ShortcutHelper::ShortcutHelper(JNIEnv* env, 43 ShortcutHelper::ShortcutHelper(JNIEnv* env,
42 jobject obj, 44 jobject obj,
43 content::WebContents* web_contents) 45 content::WebContents* web_contents)
44 : WebContentsObserver(web_contents), 46 : WebContentsObserver(web_contents),
45 java_ref_(env, obj), 47 java_ref_(env, obj),
46 url_(web_contents->GetURL()), 48 url_(web_contents->GetURL()),
47 shortcut_type_(BOOKMARK) { 49 web_app_capable_(WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED) {
48 } 50 }
49 51
50 void ShortcutHelper::Initialize() { 52 void ShortcutHelper::Initialize() {
51 // Send a message to the renderer to retrieve information about the page. 53 // Send a message to the renderer to retrieve information about the page.
52 Send(new ChromeViewMsg_RetrieveWebappInformation(routing_id(), url_)); 54 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id()));
53 } 55 }
54 56
55 ShortcutHelper::~ShortcutHelper() { 57 ShortcutHelper::~ShortcutHelper() {
56 } 58 }
57 59
58 void ShortcutHelper::OnDidRetrieveWebappInformation( 60 void ShortcutHelper::OnDidGetWebApplicationInfo(
59 bool success, 61 const WebApplicationInfo& received_web_app_info) {
palmer 2014/09/15 21:02:44 You should also check the sanity of the GURL (leng
mlamouri (slow - plz ping) 2014/09/16 14:38:22 Hmm, I do not use those. I'm not sure how useful i
60 bool is_mobile_webapp_capable, 62 // Sanitize received_web_app_info.
61 bool is_apple_mobile_webapp_capable, 63 WebApplicationInfo web_app_info = received_web_app_info;
62 const GURL& expected_url) { 64 web_app_info.title =
63 // This should silently fail. 65 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength);
64 if (!success) { 66 web_app_info.description =
65 LOG(ERROR) << "Failed to parse webpage."; 67 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength);
66 } else if (expected_url != url_) {
67 LOG(ERROR) << "Unexpected URL returned.";
68 }
69 68
70 if (is_apple_mobile_webapp_capable && !is_mobile_webapp_capable) { 69 web_app_capable_ = web_app_info.mobile_capable;
71 shortcut_type_ = APP_SHORTCUT_APPLE;
72 } else if (is_apple_mobile_webapp_capable || is_mobile_webapp_capable) {
73 shortcut_type_ = APP_SHORTCUT;
74 } else {
75 shortcut_type_ = BOOKMARK;
76 }
77 70
78 title_ = web_contents()->GetTitle(); 71 title_ = web_app_info.title.empty() ? web_contents()->GetTitle()
72 : web_app_info.title;
79 73
80 JNIEnv* env = base::android::AttachCurrentThread(); 74 JNIEnv* env = base::android::AttachCurrentThread();
81 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); 75 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env);
82 ScopedJavaLocalRef<jstring> j_title = 76 ScopedJavaLocalRef<jstring> j_title =
83 base::android::ConvertUTF16ToJavaString(env, title_); 77 base::android::ConvertUTF16ToJavaString(env, title_);
84 78
85 Java_ShortcutHelper_onInitialized(env, j_obj.obj(), j_title.obj()); 79 Java_ShortcutHelper_onInitialized(env, j_obj.obj(), j_title.obj());
86 } 80 }
87 81
88 void ShortcutHelper::TearDown(JNIEnv*, jobject) { 82 void ShortcutHelper::TearDown(JNIEnv*, jobject) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 icon_ = bitmap_result; 124 icon_ = bitmap_result;
131 125
132 // Stop observing so we don't get destroyed while doing the last steps. 126 // Stop observing so we don't get destroyed while doing the last steps.
133 Observe(NULL); 127 Observe(NULL);
134 128
135 base::WorkerPool::PostTask( 129 base::WorkerPool::PostTask(
136 FROM_HERE, 130 FROM_HERE,
137 base::Bind(&ShortcutHelper::AddShortcutInBackground, 131 base::Bind(&ShortcutHelper::AddShortcutInBackground,
138 url_, 132 url_,
139 title_, 133 title_,
140 shortcut_type_, 134 web_app_capable_,
141 icon_), 135 icon_),
142 true); 136 true);
143 137
144 Destroy(); 138 Destroy();
145 } 139 }
146 140
147 bool ShortcutHelper::OnMessageReceived(const IPC::Message& message) { 141 bool ShortcutHelper::OnMessageReceived(const IPC::Message& message) {
148 bool handled = true; 142 bool handled = true;
149 143
150 IPC_BEGIN_MESSAGE_MAP(ShortcutHelper, message) 144 IPC_BEGIN_MESSAGE_MAP(ShortcutHelper, message)
151 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidRetrieveWebappInformation, 145 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo,
152 OnDidRetrieveWebappInformation) 146 OnDidGetWebApplicationInfo)
153 IPC_MESSAGE_UNHANDLED(handled = false) 147 IPC_MESSAGE_UNHANDLED(handled = false)
154 IPC_END_MESSAGE_MAP() 148 IPC_END_MESSAGE_MAP()
155 149
156 return handled; 150 return handled;
157 } 151 }
158 152
159 void ShortcutHelper::WebContentsDestroyed() { 153 void ShortcutHelper::WebContentsDestroyed() {
160 Destroy(); 154 Destroy();
161 } 155 }
162 156
163 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { 157 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) {
164 return RegisterNativesImpl(env); 158 return RegisterNativesImpl(env);
165 } 159 }
166 160
167 void ShortcutHelper::AddShortcutInBackground( 161 void ShortcutHelper::AddShortcutInBackground(
168 const GURL& url, 162 const GURL& url,
169 const base::string16& title, 163 const base::string16& title,
170 ShortcutType shortcut_type, 164 WebApplicationInfo::MobileCapable web_app_capable,
171 const favicon_base::FaviconRawBitmapResult& bitmap_result) { 165 const favicon_base::FaviconRawBitmapResult& bitmap_result) {
172 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread()); 166 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread());
173 167
174 // Grab the average color from the bitmap. 168 // Grab the average color from the bitmap.
175 SkColor color = SK_ColorWHITE; 169 SkColor color = SK_ColorWHITE;
176 SkBitmap favicon_bitmap; 170 SkBitmap favicon_bitmap;
177 if (bitmap_result.is_valid()) { 171 if (bitmap_result.is_valid()) {
178 if (gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), 172 if (gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(),
179 bitmap_result.bitmap_data->size(), 173 bitmap_result.bitmap_data->size(),
180 &favicon_bitmap)) 174 &favicon_bitmap))
181 color = color_utils::CalculateKMeanColorOfBitmap(favicon_bitmap); 175 color = color_utils::CalculateKMeanColorOfBitmap(favicon_bitmap);
182 } 176 }
183 177
184 int r_value = SkColorGetR(color); 178 int r_value = SkColorGetR(color);
185 int g_value = SkColorGetG(color); 179 int g_value = SkColorGetG(color);
186 int b_value = SkColorGetB(color); 180 int b_value = SkColorGetB(color);
187 181
188 // Send the data to the Java side to create the shortcut. 182 // Send the data to the Java side to create the shortcut.
189 JNIEnv* env = base::android::AttachCurrentThread(); 183 JNIEnv* env = base::android::AttachCurrentThread();
190 ScopedJavaLocalRef<jstring> java_url = 184 ScopedJavaLocalRef<jstring> java_url =
191 base::android::ConvertUTF8ToJavaString(env, url.spec()); 185 base::android::ConvertUTF8ToJavaString(env, url.spec());
192 ScopedJavaLocalRef<jstring> java_title = 186 ScopedJavaLocalRef<jstring> java_title =
193 base::android::ConvertUTF16ToJavaString(env, title); 187 base::android::ConvertUTF16ToJavaString(env, title);
194 ScopedJavaLocalRef<jobject> java_bitmap; 188 ScopedJavaLocalRef<jobject> java_bitmap;
195 if (favicon_bitmap.getSize()) 189 if (favicon_bitmap.getSize())
196 java_bitmap = gfx::ConvertToJavaBitmap(&favicon_bitmap); 190 java_bitmap = gfx::ConvertToJavaBitmap(&favicon_bitmap);
197 191
198 Java_ShortcutHelper_addShortcut(env, 192 Java_ShortcutHelper_addShortcut(
199 base::android::GetApplicationContext(), 193 env,
200 java_url.obj(), 194 base::android::GetApplicationContext(),
201 java_title.obj(), 195 java_url.obj(),
202 java_bitmap.obj(), 196 java_title.obj(),
203 r_value, 197 java_bitmap.obj(),
204 g_value, 198 r_value,
205 b_value, 199 g_value,
206 shortcut_type != BOOKMARK); 200 b_value,
201 web_app_capable != WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED);
207 202
208 // Record what type of shortcut was added by the user. 203 // Record what type of shortcut was added by the user.
209 switch (shortcut_type) { 204 switch (web_app_capable) {
210 case APP_SHORTCUT: 205 case WebApplicationInfo::MOBILE_CAPABLE:
211 content::RecordAction( 206 content::RecordAction(
212 base::UserMetricsAction("webapps.AddShortcut.AppShortcut")); 207 base::UserMetricsAction("webapps.AddShortcut.AppShortcut"));
213 break; 208 break;
214 case APP_SHORTCUT_APPLE: 209 case WebApplicationInfo::MOBILE_CAPABLE_APPLE:
215 content::RecordAction( 210 content::RecordAction(
216 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); 211 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple"));
217 break; 212 break;
218 case BOOKMARK: 213 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED:
219 content::RecordAction( 214 content::RecordAction(
220 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); 215 base::UserMetricsAction("webapps.AddShortcut.Bookmark"));
221 break; 216 break;
222 default: 217 default:
223 NOTREACHED(); 218 NOTREACHED();
224 } 219 }
225 } 220 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698