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/chrome_constants.h" | 19 #include "chrome/common/chrome_constants.h" |
20 #include "chrome/common/render_messages.h" | 20 #include "chrome/common/render_messages.h" |
21 #include "chrome/common/web_application_info.h" | 21 #include "chrome/common/web_application_info.h" |
22 #include "content/public/browser/user_metrics.h" | 22 #include "content/public/browser/user_metrics.h" |
23 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
24 #include "content/public/browser/web_contents_observer.h" | 24 #include "content/public/browser/web_contents_observer.h" |
25 #include "content/public/common/frame_navigate_params.h" | 25 #include "content/public/common/frame_navigate_params.h" |
| 26 #include "content/public/common/manifest.h" |
26 #include "jni/ShortcutHelper_jni.h" | 27 #include "jni/ShortcutHelper_jni.h" |
27 #include "ui/gfx/android/java_bitmap.h" | 28 #include "ui/gfx/android/java_bitmap.h" |
28 #include "ui/gfx/codec/png_codec.h" | 29 #include "ui/gfx/codec/png_codec.h" |
29 #include "ui/gfx/color_analysis.h" | 30 #include "ui/gfx/color_analysis.h" |
30 #include "ui/gfx/favicon_size.h" | 31 #include "ui/gfx/favicon_size.h" |
31 #include "url/gurl.h" | 32 #include "url/gurl.h" |
32 | 33 |
33 jlong Initialize(JNIEnv* env, jobject obj, jlong tab_android_ptr) { | 34 jlong Initialize(JNIEnv* env, jobject obj, jlong tab_android_ptr) { |
34 TabAndroid* tab = reinterpret_cast<TabAndroid*>(tab_android_ptr); | 35 TabAndroid* tab = reinterpret_cast<TabAndroid*>(tab_android_ptr); |
35 | 36 |
36 ShortcutHelper* shortcut_helper = | 37 ShortcutHelper* shortcut_helper = |
37 new ShortcutHelper(env, obj, tab->web_contents()); | 38 new ShortcutHelper(env, obj, tab->web_contents()); |
38 shortcut_helper->Initialize(); | 39 shortcut_helper->Initialize(); |
39 | 40 |
40 return reinterpret_cast<intptr_t>(shortcut_helper); | 41 return reinterpret_cast<intptr_t>(shortcut_helper); |
41 } | 42 } |
42 | 43 |
43 ShortcutHelper::ShortcutHelper(JNIEnv* env, | 44 ShortcutHelper::ShortcutHelper(JNIEnv* env, |
44 jobject obj, | 45 jobject obj, |
45 content::WebContents* web_contents) | 46 content::WebContents* web_contents) |
46 : WebContentsObserver(web_contents), | 47 : WebContentsObserver(web_contents), |
47 java_ref_(env, obj), | 48 java_ref_(env, obj), |
48 url_(web_contents->GetURL()), | 49 url_(web_contents->GetURL()), |
49 web_app_capable_(WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED) { | 50 web_app_capable_(WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED), |
| 51 weak_ptr_factory_(this) { |
50 } | 52 } |
51 | 53 |
52 void ShortcutHelper::Initialize() { | 54 void ShortcutHelper::Initialize() { |
53 // 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. |
54 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); | 56 Send(new ChromeViewMsg_GetWebApplicationInfo(routing_id())); |
55 } | 57 } |
56 | 58 |
57 ShortcutHelper::~ShortcutHelper() { | 59 ShortcutHelper::~ShortcutHelper() { |
58 } | 60 } |
59 | 61 |
60 void ShortcutHelper::OnDidGetWebApplicationInfo( | 62 void ShortcutHelper::OnDidGetWebApplicationInfo( |
61 const WebApplicationInfo& received_web_app_info) { | 63 const WebApplicationInfo& received_web_app_info) { |
62 // Sanitize received_web_app_info. | 64 // Sanitize received_web_app_info. |
63 WebApplicationInfo web_app_info = received_web_app_info; | 65 WebApplicationInfo web_app_info = received_web_app_info; |
64 web_app_info.title = | 66 web_app_info.title = |
65 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength); | 67 web_app_info.title.substr(0, chrome::kMaxMetaTagAttributeLength); |
66 web_app_info.description = | 68 web_app_info.description = |
67 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength); | 69 web_app_info.description.substr(0, chrome::kMaxMetaTagAttributeLength); |
68 | 70 |
69 web_app_capable_ = web_app_info.mobile_capable; | 71 web_app_capable_ = web_app_info.mobile_capable; |
70 | 72 |
71 title_ = web_app_info.title.empty() ? web_contents()->GetTitle() | 73 title_ = web_app_info.title.empty() ? web_contents()->GetTitle() |
72 : web_app_info.title; | 74 : web_app_info.title; |
73 | 75 |
| 76 web_contents()->GetManifest(base::Bind(&ShortcutHelper::OnDidGetManifest, |
| 77 weak_ptr_factory_.GetWeakPtr())); |
| 78 } |
| 79 |
| 80 void ShortcutHelper::OnDidGetManifest(const content::Manifest& manifest) { |
| 81 // Set the title based on the manifest value, if any. |
| 82 if (!manifest.short_name.is_null()) |
| 83 title_ = manifest.short_name.string(); |
| 84 else if (!manifest.name.is_null()) |
| 85 title_ = manifest.name.string(); |
| 86 |
| 87 // Set the url based on the manifest value, if any. |
| 88 if (manifest.start_url.is_valid()) |
| 89 url_ = manifest.start_url; |
| 90 |
| 91 // The ShortcutHelper is now able to notify its Java counterpart that it is |
| 92 // initialized. OnInitialized method is not conceptually part of getting the |
| 93 // manifest data but it happens that the initialization is finalized when |
| 94 // these data are available. |
74 JNIEnv* env = base::android::AttachCurrentThread(); | 95 JNIEnv* env = base::android::AttachCurrentThread(); |
75 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); | 96 ScopedJavaLocalRef<jobject> j_obj = java_ref_.get(env); |
76 ScopedJavaLocalRef<jstring> j_title = | 97 ScopedJavaLocalRef<jstring> j_title = |
77 base::android::ConvertUTF16ToJavaString(env, title_); | 98 base::android::ConvertUTF16ToJavaString(env, title_); |
78 | 99 |
79 Java_ShortcutHelper_onInitialized(env, j_obj.obj(), j_title.obj()); | 100 Java_ShortcutHelper_onInitialized(env, j_obj.obj(), j_title.obj()); |
80 } | 101 } |
81 | 102 |
82 void ShortcutHelper::TearDown(JNIEnv*, jobject) { | 103 void ShortcutHelper::TearDown(JNIEnv*, jobject) { |
83 Destroy(); | 104 Destroy(); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); | 232 base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple")); |
212 break; | 233 break; |
213 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: | 234 case WebApplicationInfo::MOBILE_CAPABLE_UNSPECIFIED: |
214 content::RecordAction( | 235 content::RecordAction( |
215 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); | 236 base::UserMetricsAction("webapps.AddShortcut.Bookmark")); |
216 break; | 237 break; |
217 default: | 238 default: |
218 NOTREACHED(); | 239 NOTREACHED(); |
219 } | 240 } |
220 } | 241 } |
OLD | NEW |