Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/render_messages.h" | 19 #include "chrome/common/render_messages.h" |
| 20 #include "chrome/common/web_application_info.h" | |
| 20 #include "content/public/browser/user_metrics.h" | 21 #include "content/public/browser/user_metrics.h" |
| 21 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/browser/web_contents_observer.h" | 23 #include "content/public/browser/web_contents_observer.h" |
| 23 #include "content/public/common/frame_navigate_params.h" | 24 #include "content/public/common/frame_navigate_params.h" |
| 24 #include "jni/ShortcutHelper_jni.h" | 25 #include "jni/ShortcutHelper_jni.h" |
| 25 #include "ui/gfx/android/java_bitmap.h" | 26 #include "ui/gfx/android/java_bitmap.h" |
| 26 #include "ui/gfx/codec/png_codec.h" | 27 #include "ui/gfx/codec/png_codec.h" |
| 27 #include "ui/gfx/color_analysis.h" | 28 #include "ui/gfx/color_analysis.h" |
| 28 #include "ui/gfx/favicon_size.h" | 29 #include "ui/gfx/favicon_size.h" |
| 29 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 30 | 31 |
| 31 jlong Initialize(JNIEnv* env, jobject obj, jlong tab_android_ptr) { | 32 jlong Initialize(JNIEnv* env, jobject obj, jlong tab_android_ptr) { |
| 32 TabAndroid* tab = reinterpret_cast<TabAndroid*>(tab_android_ptr); | 33 TabAndroid* tab = reinterpret_cast<TabAndroid*>(tab_android_ptr); |
| 33 | 34 |
| 34 ShortcutHelper* shortcut_helper = | 35 ShortcutHelper* shortcut_helper = |
| 35 new ShortcutHelper(env, obj, tab->web_contents()); | 36 new ShortcutHelper(env, obj, tab->web_contents()); |
| 36 shortcut_helper->Initialize(); | 37 shortcut_helper->Initialize(); |
| 37 | 38 |
| 38 return reinterpret_cast<intptr_t>(shortcut_helper); | 39 return reinterpret_cast<intptr_t>(shortcut_helper); |
| 39 } | 40 } |
| 40 | 41 |
| 41 ShortcutHelper::ShortcutHelper(JNIEnv* env, | 42 ShortcutHelper::ShortcutHelper(JNIEnv* env, |
| 42 jobject obj, | 43 jobject obj, |
| 43 content::WebContents* web_contents) | 44 content::WebContents* web_contents) |
| 44 : WebContentsObserver(web_contents), | 45 : WebContentsObserver(web_contents), |
| 45 java_ref_(env, obj), | 46 java_ref_(env, obj), |
| 46 url_(web_contents->GetURL()), | 47 url_(web_contents->GetURL()), |
| 47 shortcut_type_(BOOKMARK) { | 48 mobile_capable_(WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED) { |
| 48 } | 49 } |
| 49 | 50 |
| 50 void ShortcutHelper::Initialize() { | 51 void ShortcutHelper::Initialize() { |
| 51 // Send a message to the renderer to retrieve information about the page. | 52 // Send a message to the renderer to retrieve information about the page. |
| 52 Send(new ChromeViewMsg_RetrieveWebappInformation(routing_id(), url_)); | 53 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); |
| 53 } | 54 } |
| 54 | 55 |
| 55 ShortcutHelper::~ShortcutHelper() { | 56 ShortcutHelper::~ShortcutHelper() { |
| 56 } | 57 } |
| 57 | 58 |
| 58 void ShortcutHelper::OnDidRetrieveWebappInformation( | 59 void ShortcutHelper::OnDidGetWebApplicationInfo( |
| 59 bool success, | 60 const WebApplicationInfo& web_app_info) { |
| 60 bool is_mobile_webapp_capable, | 61 mobile_capable_ = web_app_info.mobile_capable; |
|
gone
2014/09/15 16:55:17
mobile_capable_ is a really strange name for this.
mlamouri (slow - plz ping)
2014/09/15 20:30:53
Renamed web_app_capable_.
| |
| 61 bool is_apple_mobile_webapp_capable, | |
| 62 const GURL& expected_url) { | |
| 63 // This should silently fail. | |
| 64 if (!success) { | |
| 65 LOG(ERROR) << "Failed to parse webpage."; | |
| 66 } else if (expected_url != url_) { | |
| 67 LOG(ERROR) << "Unexpected URL returned."; | |
| 68 } | |
| 69 | 62 |
| 70 if (is_apple_mobile_webapp_capable && !is_mobile_webapp_capable) { | 63 title_ = !web_app_info.title.empty() ? web_app_info.title |
|
gone
2014/09/15 16:55:17
nit: ! seems redundant when you can just flip the
mlamouri (slow - plz ping)
2014/09/15 20:30:53
Done.
| |
| 71 shortcut_type_ = APP_SHORTCUT_APPLE; | 64 : web_contents()->GetTitle(); |
| 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 | |
| 78 title_ = web_contents()->GetTitle(); | |
| 79 | 65 |
| 80 JNIEnv* env = base::android::AttachCurrentThread(); | 66 JNIEnv* env = base::android::AttachCurrentThread(); |
| 81 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); | 67 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); |
| 82 ScopedJavaLocalRef<jstring> j_title = | 68 ScopedJavaLocalRef<jstring> j_title = |
| 83 base::android::ConvertUTF16ToJavaString(env, title_); | 69 base::android::ConvertUTF16ToJavaString(env, title_); |
| 84 | 70 |
| 85 Java_ShortcutHelper_onInitialized(env, j_obj.obj(), j_title.obj()); | 71 Java_ShortcutHelper_onInitialized(env, j_obj.obj(), j_title.obj()); |
| 86 } | 72 } |
| 87 | 73 |
| 88 void ShortcutHelper::TearDown(JNIEnv*, jobject) { | 74 void ShortcutHelper::TearDown(JNIEnv*, jobject) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 icon_ = bitmap_result; | 116 icon_ = bitmap_result; |
| 131 | 117 |
| 132 // Stop observing so we don't get destroyed while doing the last steps. | 118 // Stop observing so we don't get destroyed while doing the last steps. |
| 133 Observe(NULL); | 119 Observe(NULL); |
| 134 | 120 |
| 135 base::WorkerPool::PostTask( | 121 base::WorkerPool::PostTask( |
| 136 FROM_HERE, | 122 FROM_HERE, |
| 137 base::Bind(&ShortcutHelper::AddShortcutInBackground, | 123 base::Bind(&ShortcutHelper::AddShortcutInBackground, |
| 138 url_, | 124 url_, |
| 139 title_, | 125 title_, |
| 140 shortcut_type_, | 126 mobile_capable_, |
| 141 icon_), | 127 icon_), |
| 142 true); | 128 true); |
| 143 | 129 |
| 144 Destroy(); | 130 Destroy(); |
| 145 } | 131 } |
| 146 | 132 |
| 147 bool ShortcutHelper::OnMessageReceived(const IPC::Message& message) { | 133 bool ShortcutHelper::OnMessageReceived(const IPC::Message& message) { |
| 148 bool handled = true; | 134 bool handled = true; |
| 149 | 135 |
| 150 IPC_BEGIN_MESSAGE_MAP(ShortcutHelper, message) | 136 IPC_BEGIN_MESSAGE_MAP(ShortcutHelper, message) |
| 151 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidRetrieveWebappInformation, | 137 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo, |
| 152 OnDidRetrieveWebappInformation) | 138 OnDidGetWebApplicationInfo) |
| 153 IPC_MESSAGE_UNHANDLED(handled = false) | 139 IPC_MESSAGE_UNHANDLED(handled = false) |
| 154 IPC_END_MESSAGE_MAP() | 140 IPC_END_MESSAGE_MAP() |
| 155 | 141 |
| 156 return handled; | 142 return handled; |
| 157 } | 143 } |
| 158 | 144 |
| 159 void ShortcutHelper::WebContentsDestroyed() { | 145 void ShortcutHelper::WebContentsDestroyed() { |
| 160 Destroy(); | 146 Destroy(); |
| 161 } | 147 } |
| 162 | 148 |
| 163 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { | 149 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
| 164 return RegisterNativesImpl(env); | 150 return RegisterNativesImpl(env); |
| 165 } | 151 } |
| 166 | 152 |
| 167 void ShortcutHelper::AddShortcutInBackground( | 153 void ShortcutHelper::AddShortcutInBackground( |
| 168 const GURL& url, | 154 const GURL& url, |
| 169 const base::string16& title, | 155 const base::string16& title, |
| 170 ShortcutType shortcut_type, | 156 WebApplicationInfo::MobileCapable mobile_capable, |
| 171 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 157 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 172 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread()); | 158 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread()); |
| 173 | 159 |
| 174 // Grab the average color from the bitmap. | 160 // Grab the average color from the bitmap. |
| 175 SkColor color = SK_ColorWHITE; | 161 SkColor color = SK_ColorWHITE; |
| 176 SkBitmap favicon_bitmap; | 162 SkBitmap favicon_bitmap; |
| 177 if (bitmap_result.is_valid()) { | 163 if (bitmap_result.is_valid()) { |
| 178 if (gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), | 164 if (gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), |
| 179 bitmap_result.bitmap_data->size(), | 165 bitmap_result.bitmap_data->size(), |
| 180 &favicon_bitmap)) | 166 &favicon_bitmap)) |
| 181 color = color_utils::CalculateKMeanColorOfBitmap(favicon_bitmap); | 167 color = color_utils::CalculateKMeanColorOfBitmap(favicon_bitmap); |
| 182 } | 168 } |
| 183 | 169 |
| 184 int r_value = SkColorGetR(color); | 170 int r_value = SkColorGetR(color); |
| 185 int g_value = SkColorGetG(color); | 171 int g_value = SkColorGetG(color); |
| 186 int b_value = SkColorGetB(color); | 172 int b_value = SkColorGetB(color); |
| 187 | 173 |
| 188 // Send the data to the Java side to create the shortcut. | 174 // Send the data to the Java side to create the shortcut. |
| 189 JNIEnv* env = base::android::AttachCurrentThread(); | 175 JNIEnv* env = base::android::AttachCurrentThread(); |
| 190 ScopedJavaLocalRef<jstring> java_url = | 176 ScopedJavaLocalRef<jstring> java_url = |
| 191 base::android::ConvertUTF8ToJavaString(env, url.spec()); | 177 base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 192 ScopedJavaLocalRef<jstring> java_title = | 178 ScopedJavaLocalRef<jstring> java_title = |
| 193 base::android::ConvertUTF16ToJavaString(env, title); | 179 base::android::ConvertUTF16ToJavaString(env, title); |
| 194 ScopedJavaLocalRef<jobject> java_bitmap; | 180 ScopedJavaLocalRef<jobject> java_bitmap; |
| 195 if (favicon_bitmap.getSize()) | 181 if (favicon_bitmap.getSize()) |
| 196 java_bitmap = gfx::ConvertToJavaBitmap(&favicon_bitmap); | 182 java_bitmap = gfx::ConvertToJavaBitmap(&favicon_bitmap); |
| 197 | 183 |
| 198 Java_ShortcutHelper_addShortcut(env, | 184 Java_ShortcutHelper_addShortcut( |
| 199 base::android::GetApplicationContext(), | 185 env, |
| 200 java_url.obj(), | 186 base::android::GetApplicationContext(), |
| 201 java_title.obj(), | 187 java_url.obj(), |
| 202 java_bitmap.obj(), | 188 java_title.obj(), |
| 203 r_value, | 189 java_bitmap.obj(), |
| 204 g_value, | 190 r_value, |
| 205 b_value, | 191 g_value, |
| 206 shortcut_type != BOOKMARK); | 192 b_value, |
| 193 mobile_capable != WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED); | |
| 207 | 194 |
| 208 // Record what type of shortcut was added by the user. | 195 // Record what type of shortcut was added by the user. |
| 209 switch (shortcut_type) { | 196 switch (mobile_capable) { |
| 210 case APP_SHORTCUT: | 197 case WebApplicationInfo::MOBILE_CAPABLE: |
| 211 content::RecordAction( | 198 content::RecordAction( |
| 212 base::UserMetricsAction("webapps.AddShortcut.AppShortcut")); | 199 base::UserMetricsAction("webapps.AddShortcut.AppShortcut")); |
| 213 break; | 200 break; |
| 214 case APP_SHORTCUT_APPLE: | 201 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: |
| 215 content::RecordAction( | 202 content::RecordAction( |
| 216 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); | 203 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); |
| 217 break; | 204 break; |
| 218 case BOOKMARK: | 205 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: |
| 219 content::RecordAction( | 206 content::RecordAction( |
| 220 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); | 207 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); |
| 221 break; | 208 break; |
| 222 default: | 209 default: |
| 223 NOTREACHED(); | 210 NOTREACHED(); |
| 224 } | 211 } |
| 225 } | 212 } |
| OLD | NEW |