| 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" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 return reinterpret_cast<intptr_t>(shortcut_helper); | 41 return reinterpret_cast<intptr_t>(shortcut_helper); |
| 42 } | 42 } |
| 43 | 43 |
| 44 ShortcutHelper::ShortcutHelper(JNIEnv* env, | 44 ShortcutHelper::ShortcutHelper(JNIEnv* env, |
| 45 jobject obj, | 45 jobject obj, |
| 46 content::WebContents* web_contents) | 46 content::WebContents* web_contents) |
| 47 : WebContentsObserver(web_contents), | 47 : WebContentsObserver(web_contents), |
| 48 java_ref_(env, obj), | 48 java_ref_(env, obj), |
| 49 url_(web_contents->GetURL()), | 49 url_(web_contents->GetURL()), |
| 50 web_app_capable_(WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED), | 50 display_(content::Manifest::DISPLAY_MODE_BROWSER), |
| 51 weak_ptr_factory_(this) { | 51 weak_ptr_factory_(this) { |
| 52 } | 52 } |
| 53 | 53 |
| 54 void ShortcutHelper::Initialize() { | 54 void ShortcutHelper::Initialize() { |
| 55 // Send a message to the renderer to retrieve information about the page. | 55 // Send a message to the renderer to retrieve information about the page. |
| 56 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); | 56 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); |
| 57 } | 57 } |
| 58 | 58 |
| 59 ShortcutHelper::~ShortcutHelper() { | 59 ShortcutHelper::~ShortcutHelper() { |
| 60 } | 60 } |
| 61 | 61 |
| 62 void ShortcutHelper::OnDidGetWebApplicationInfo( | 62 void ShortcutHelper::OnDidGetWebApplicationInfo( |
| 63 const WebApplicationInfo& received_web_app_info) { | 63 const WebApplicationInfo& received_web_app_info) { |
| 64 // Sanitize received_web_app_info. | 64 // Sanitize received_web_app_info. |
| 65 WebApplicationInfo web_app_info = received_web_app_info; | 65 WebApplicationInfo web_app_info = received_web_app_info; |
| 66 web_app_info.title = | 66 web_app_info.title = |
| 67 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength); | 67 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength); |
| 68 web_app_info.description = | 68 web_app_info.description = |
| 69 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength); | 69 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength); |
| 70 | 70 |
| 71 web_app_capable_ = web_app_info.mobile_capable; | |
| 72 | |
| 73 title_ = web_app_info.title.empty() ? web_contents()->GetTitle() | 71 title_ = web_app_info.title.empty() ? web_contents()->GetTitle() |
| 74 : web_app_info.title; | 72 : web_app_info.title; |
| 75 | 73 |
| 74 if (web_app_info.mobile_capable == WebApplicationInfo::MOBILE_CAPABLE || |
| 75 web_app_info.mobile_capable == WebApplicationInfo::MOBILE_CAPABLE_APPLE) { |
| 76 display_ = content::Manifest::DISPLAY_MODE_STANDALONE; |
| 77 } |
| 78 |
| 79 // Record what type of shortcut was added by the user. |
| 80 switch (web_app_info.mobile_capable) { |
| 81 case WebApplicationInfo::MOBILE_CAPABLE: |
| 82 content::RecordAction( |
| 83 base::UserMetricsAction("webapps.AddShortcut.AppShortcut")); |
| 84 break; |
| 85 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: |
| 86 content::RecordAction( |
| 87 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); |
| 88 break; |
| 89 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: |
| 90 content::RecordAction( |
| 91 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); |
| 92 break; |
| 93 } |
| 94 |
| 76 web_contents()->GetManifest(base::Bind(&ShortcutHelper::OnDidGetManifest, | 95 web_contents()->GetManifest(base::Bind(&ShortcutHelper::OnDidGetManifest, |
| 77 weak_ptr_factory_.GetWeakPtr())); | 96 weak_ptr_factory_.GetWeakPtr())); |
| 78 } | 97 } |
| 79 | 98 |
| 80 void ShortcutHelper::OnDidGetManifest(const content::Manifest& manifest) { | 99 void ShortcutHelper::OnDidGetManifest(const content::Manifest& manifest) { |
| 81 // Set the title based on the manifest value, if any. | 100 // Set the title based on the manifest value, if any. |
| 82 if (!manifest.short_name.is_null()) | 101 if (!manifest.short_name.is_null()) |
| 83 title_ = manifest.short_name.string(); | 102 title_ = manifest.short_name.string(); |
| 84 else if (!manifest.name.is_null()) | 103 else if (!manifest.name.is_null()) |
| 85 title_ = manifest.name.string(); | 104 title_ = manifest.name.string(); |
| 86 | 105 |
| 87 // Set the url based on the manifest value, if any. | 106 // Set the url based on the manifest value, if any. |
| 88 if (manifest.start_url.is_valid()) | 107 if (manifest.start_url.is_valid()) |
| 89 url_ = manifest.start_url; | 108 url_ = manifest.start_url; |
| 90 | 109 |
| 110 // Set the display based on the manifest value, if any. |
| 111 if (manifest.display != content::Manifest::DISPLAY_MODE_UNSPECIFIED) |
| 112 display_ = manifest.display; |
| 113 |
| 114 // 'fullscreen' and 'minimal-ui' are not yet supported, fallback to the right |
| 115 // mode in those cases. |
| 116 if (manifest.display == content::Manifest::DISPLAY_MODE_FULLSCREEN) |
| 117 display_ = content::Manifest::DISPLAY_MODE_STANDALONE; |
| 118 if (manifest.display == content::Manifest::DISPLAY_MODE_MINIMAL_UI) |
| 119 display_ = content::Manifest::DISPLAY_MODE_BROWSER; |
| 120 |
| 91 // The ShortcutHelper is now able to notify its Java counterpart that it is | 121 // The ShortcutHelper is now able to notify its Java counterpart that it is |
| 92 // initialized. OnInitialized method is not conceptually part of getting the | 122 // initialized. OnInitialized method is not conceptually part of getting the |
| 93 // manifest data but it happens that the initialization is finalized when | 123 // manifest data but it happens that the initialization is finalized when |
| 94 // these data are available. | 124 // these data are available. |
| 95 JNIEnv* env = base::android::AttachCurrentThread(); | 125 JNIEnv* env = base::android::AttachCurrentThread(); |
| 96 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); | 126 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); |
| 97 ScopedJavaLocalRef<jstring> j_title = | 127 ScopedJavaLocalRef<jstring> j_title = |
| 98 base::android::ConvertUTF16ToJavaString(env, title_); | 128 base::android::ConvertUTF16ToJavaString(env, title_); |
| 99 | 129 |
| 100 Java_ShortcutHelper_onInitialized(env, j_obj.obj(), j_title.obj()); | 130 Java_ShortcutHelper_onInitialized(env, j_obj.obj(), j_title.obj()); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 icon_ = bitmap_result; | 175 icon_ = bitmap_result; |
| 146 | 176 |
| 147 // Stop observing so we don't get destroyed while doing the last steps. | 177 // Stop observing so we don't get destroyed while doing the last steps. |
| 148 Observe(NULL); | 178 Observe(NULL); |
| 149 | 179 |
| 150 base::WorkerPool::PostTask( | 180 base::WorkerPool::PostTask( |
| 151 FROM_HERE, | 181 FROM_HERE, |
| 152 base::Bind(&ShortcutHelper::AddShortcutInBackground, | 182 base::Bind(&ShortcutHelper::AddShortcutInBackground, |
| 153 url_, | 183 url_, |
| 154 title_, | 184 title_, |
| 155 web_app_capable_, | 185 display_, |
| 156 icon_), | 186 icon_), |
| 157 true); | 187 true); |
| 158 | 188 |
| 159 Destroy(); | 189 Destroy(); |
| 160 } | 190 } |
| 161 | 191 |
| 162 bool ShortcutHelper::OnMessageReceived(const IPC::Message& message) { | 192 bool ShortcutHelper::OnMessageReceived(const IPC::Message& message) { |
| 163 bool handled = true; | 193 bool handled = true; |
| 164 | 194 |
| 165 IPC_BEGIN_MESSAGE_MAP(ShortcutHelper, message) | 195 IPC_BEGIN_MESSAGE_MAP(ShortcutHelper, message) |
| 166 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo, | 196 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidGetWebApplicationInfo, |
| 167 OnDidGetWebApplicationInfo) | 197 OnDidGetWebApplicationInfo) |
| 168 IPC_MESSAGE_UNHANDLED(handled = false) | 198 IPC_MESSAGE_UNHANDLED(handled = false) |
| 169 IPC_END_MESSAGE_MAP() | 199 IPC_END_MESSAGE_MAP() |
| 170 | 200 |
| 171 return handled; | 201 return handled; |
| 172 } | 202 } |
| 173 | 203 |
| 174 void ShortcutHelper::WebContentsDestroyed() { | 204 void ShortcutHelper::WebContentsDestroyed() { |
| 175 Destroy(); | 205 Destroy(); |
| 176 } | 206 } |
| 177 | 207 |
| 178 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { | 208 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
| 179 return RegisterNativesImpl(env); | 209 return RegisterNativesImpl(env); |
| 180 } | 210 } |
| 181 | 211 |
| 182 void ShortcutHelper::AddShortcutInBackground( | 212 void ShortcutHelper::AddShortcutInBackground( |
| 183 const GURL& url, | 213 const GURL& url, |
| 184 const base::string16& title, | 214 const base::string16& title, |
| 185 WebApplicationInfo::MobileCapable web_app_capable, | 215 content::Manifest::DisplayMode display, |
| 186 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 216 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 187 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread()); | 217 DCHECK(base::WorkerPool::RunsTasksOnCurrentThread()); |
| 188 | 218 |
| 189 // Grab the average color from the bitmap. | 219 // Grab the average color from the bitmap. |
| 190 SkColor color = SK_ColorWHITE; | 220 SkColor color = SK_ColorWHITE; |
| 191 SkBitmap favicon_bitmap; | 221 SkBitmap favicon_bitmap; |
| 192 if (bitmap_result.is_valid()) { | 222 if (bitmap_result.is_valid()) { |
| 193 if (gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), | 223 if (gfx::PNGCodec::Decode(bitmap_result.bitmap_data->front(), |
| 194 bitmap_result.bitmap_data->size(), | 224 bitmap_result.bitmap_data->size(), |
| 195 &favicon_bitmap)) | 225 &favicon_bitmap)) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 212 | 242 |
| 213 Java_ShortcutHelper_addShortcut( | 243 Java_ShortcutHelper_addShortcut( |
| 214 env, | 244 env, |
| 215 base::android::GetApplicationContext(), | 245 base::android::GetApplicationContext(), |
| 216 java_url.obj(), | 246 java_url.obj(), |
| 217 java_title.obj(), | 247 java_title.obj(), |
| 218 java_bitmap.obj(), | 248 java_bitmap.obj(), |
| 219 r_value, | 249 r_value, |
| 220 g_value, | 250 g_value, |
| 221 b_value, | 251 b_value, |
| 222 web_app_capable != WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED); | 252 display == content::Manifest::DISPLAY_MODE_STANDALONE); |
| 223 | |
| 224 // Record what type of shortcut was added by the user. | |
| 225 switch (web_app_capable) { | |
| 226 case WebApplicationInfo::MOBILE_CAPABLE: | |
| 227 content::RecordAction( | |
| 228 base::UserMetricsAction("webapps.AddShortcut.AppShortcut")); | |
| 229 break; | |
| 230 case WebApplicationInfo::MOBILE_CAPABLE_APPLE: | |
| 231 content::RecordAction( | |
| 232 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); | |
| 233 break; | |
| 234 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: | |
| 235 content::RecordAction( | |
| 236 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); | |
| 237 break; | |
| 238 default: | |
| 239 NOTREACHED(); | |
| 240 } | |
| 241 } | 253 } |
| OLD | NEW |