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 #ifndef CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_ |
6 #define CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_ | 6 #define CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_ |
7 | 7 |
| 8 #include "base/android/jni_android.h" |
8 #include "base/android/jni_weak_ref.h" | 9 #include "base/android/jni_weak_ref.h" |
9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
10 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
11 #include "base/task/cancelable_task_tracker.h" | 12 #include "base/task/cancelable_task_tracker.h" |
12 #include "chrome/browser/android/tab_android.h" | 13 #include "components/favicon_base/favicon_types.h" |
13 #include "content/public/browser/web_contents_observer.h" | 14 #include "content/public/browser/web_contents_observer.h" |
14 | 15 |
15 namespace favicon_base { | |
16 struct FaviconRawBitmapResult; | |
17 } // namespace favicon_base | |
18 | |
19 namespace content { | 16 namespace content { |
20 class WebContents; | 17 class WebContents; |
21 } // namespace content | 18 } // namespace content |
22 | 19 |
23 namespace IPC { | 20 namespace IPC { |
24 class Message; | 21 class Message; |
25 } | 22 } |
26 | 23 |
27 class GURL; | 24 class GURL; |
28 | 25 |
29 // Adds a shortcut to the current URL to the Android home screen. | 26 // ShortcutHelper is the C++ counterpart of org.chromium.chrome.browser's |
30 // This proceeds over three phases: | 27 // ShortcutHelper in Java. The object is owned by the Java object. It is created |
31 // 1) The renderer is asked to parse out webapp related meta tags with an async | 28 // from there via a JNI (Initialize) call and can be destroyed from Java too |
32 // IPC message. | 29 // using TearDown. When the Java implementations calls AddShortcut, it gives up |
33 // 2) The highest-resolution favicon available is retrieved for use as the | 30 // the ownership of the object. The instance will then destroy itself when done. |
34 // icon on the home screen. | 31 class ShortcutHelper : public content::WebContentsObserver { |
35 // 3) A JNI call is made to fire an Intent at the Android launcher, which adds | |
36 // the shortcut. | |
37 class ShortcutBuilder : public content::WebContentsObserver { | |
38 public: | 32 public: |
39 enum ShortcutType { | 33 enum ShortcutType { |
40 APP_SHORTCUT, | 34 APP_SHORTCUT, |
41 APP_SHORTCUT_APPLE, | 35 APP_SHORTCUT_APPLE, |
42 BOOKMARK | 36 BOOKMARK |
43 }; | 37 }; |
44 | 38 |
45 explicit ShortcutBuilder(content::WebContents* web_contents, | 39 explicit ShortcutHelper(JNIEnv* env, |
46 const base::string16& title, | 40 jobject obj, |
47 int launcher_large_icon_size); | 41 content::WebContents* web_contents); |
48 virtual ~ShortcutBuilder() {} | |
49 | 42 |
| 43 // Initialize the helper by requesting the information about the page to the |
| 44 // renderer process. The initialization is asynchronous and |
| 45 // OnDidRetrieveWebappInformation is expected to be called when finished. |
| 46 void Initialize(); |
| 47 |
| 48 // Called by the Java counter part to let the object knows that it can destroy |
| 49 // itself. |
| 50 void TearDown(JNIEnv* env, jobject obj); |
| 51 |
| 52 // IPC message received when the initialization is finished. |
50 void OnDidRetrieveWebappInformation(bool success, | 53 void OnDidRetrieveWebappInformation(bool success, |
51 bool is_mobile_webapp_capable, | 54 bool is_mobile_webapp_capable, |
52 bool is_apple_mobile_webapp_capable, | 55 bool is_apple_mobile_webapp_capable, |
53 const GURL& expected_url); | 56 const GURL& expected_url); |
54 | 57 |
| 58 // Adds a shortcut to the current URL to the Android home screen. |
| 59 void AddShortcut(JNIEnv* env, |
| 60 jobject obj, |
| 61 jstring title, |
| 62 jint launcher_large_icon_size); |
| 63 |
55 void FinishAddingShortcut( | 64 void FinishAddingShortcut( |
56 const favicon_base::FaviconRawBitmapResult& bitmap_result); | 65 const favicon_base::FaviconRawBitmapResult& bitmap_result); |
57 | 66 |
58 // WebContentsObserver | 67 // WebContentsObserver |
59 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 68 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
60 virtual void WebContentsDestroyed() OVERRIDE; | 69 virtual void WebContentsDestroyed() OVERRIDE; |
61 | 70 |
62 private: | |
63 void Destroy(); | |
64 | |
65 GURL url_; | |
66 base::string16 title_; | |
67 int launcher_large_icon_size_; | |
68 ShortcutType shortcut_type_; | |
69 base::CancelableTaskTracker cancelable_task_tracker_; | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(ShortcutBuilder); | |
72 }; | |
73 | |
74 class ShortcutHelper { | |
75 public: | |
76 // Adds a shortcut to the current URL to the Android home screen, firing | |
77 // background tasks to pull all the data required. | |
78 static void AddShortcut(content::WebContents* web_contents, | |
79 const base::string16& title, | |
80 int launcher_larger_icon_size); | |
81 | |
82 // Adds a shortcut to the launcher. Must be called from a WorkerPool task. | 71 // Adds a shortcut to the launcher. Must be called from a WorkerPool task. |
83 static void AddShortcutInBackground( | 72 static void AddShortcutInBackground( |
84 const GURL& url, | 73 const GURL& url, |
85 const base::string16& title, | 74 const base::string16& title, |
86 ShortcutBuilder::ShortcutType shortcut_type, | 75 ShortcutType shortcut_type, |
87 const favicon_base::FaviconRawBitmapResult& bitmap_result); | 76 const favicon_base::FaviconRawBitmapResult& bitmap_result); |
88 | 77 |
89 // Registers JNI hooks. | 78 // Registers JNI hooks. |
90 static bool RegisterShortcutHelper(JNIEnv* env); | 79 static bool RegisterShortcutHelper(JNIEnv* env); |
91 | 80 |
92 private: | 81 private: |
| 82 virtual ~ShortcutHelper(); |
| 83 |
| 84 void Destroy(); |
| 85 |
| 86 JavaObjectWeakGlobalRef java_ref_; |
| 87 |
| 88 GURL url_; |
| 89 base::string16 title_; |
| 90 int launcher_large_icon_size_; |
| 91 ShortcutType shortcut_type_; |
| 92 favicon_base::FaviconRawBitmapResult icon_; |
| 93 base::CancelableTaskTracker cancelable_task_tracker_; |
| 94 |
93 DISALLOW_COPY_AND_ASSIGN(ShortcutHelper); | 95 DISALLOW_COPY_AND_ASSIGN(ShortcutHelper); |
94 }; | 96 }; |
95 | 97 |
96 #endif // CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_ | 98 #endif // CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_ |
OLD | NEW |