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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_
6 #define CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_
8 #include "base/android/jni_android.h"
9 #include "base/android/jni_weak_ref.h"
10 #include "base/basictypes.h"
11 #include "base/task/cancelable_task_tracker.h"
12 #include "chrome/browser/android/shortcut_info.h"
13 #include "chrome/common/web_application_info.h"
14 #include "components/favicon_base/favicon_types.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "content/public/common/manifest.h"
20 } // namespace content
28 // ShortcutHelper is the C++ counterpart of org.chromium.chrome.browser's
29 // ShortcutHelper in Java. The object is owned by the Java object. It is created
30 // from there via a JNI (Initialize) call and can be destroyed from Java too
31 // using TearDown. When the Java implementations calls AddShortcut, it gives up
32 // the ownership of the object. The instance will then destroy itself when done.
33 class ShortcutHelper
: public content::WebContentsObserver
{
35 ShortcutHelper(JNIEnv
* env
,
37 content::WebContents
* web_contents
);
39 // Initialize the helper by requesting the information about the page to the
40 // renderer process. The initialization is asynchronous and
41 // OnDidRetrieveWebappInformation is expected to be called when finished.
44 // Called by the Java counter part to let the object knows that it can destroy
46 void TearDown(JNIEnv
* env
, jobject obj
);
48 // IPC message received when the initialization is finished.
49 void OnDidGetWebApplicationInfo(const WebApplicationInfo
& web_app_info
);
51 // Callback run when the Manifest is ready to be used.
52 void OnDidGetManifest(const content::Manifest
& manifest
);
54 // Adds a shortcut to the current URL to the Android home screen.
55 void AddShortcut(JNIEnv
* env
,
58 jint launcher_large_icon_size
);
60 // Callback run when the requested Manifest icon is ready to be used.
61 void OnDidDownloadIcon(int id
,
64 const std::vector
<SkBitmap
>& bitmaps
,
65 const std::vector
<gfx::Size
>& sizes
);
67 // Called after AddShortcut() and OnDidDownloadIcon() are run if
68 // OnDidDownloadIcon has a valid icon.
69 void AddShortcutUsingManifestIcon();
71 // Use FaviconService to get the best available favicon and create the
72 // shortcut using it. This is used when no Manifest icons are available or
74 void AddShortcutUsingFavicon();
76 // Callback run when a favicon is received from GetFavicon() call.
78 const favicon_base::FaviconRawBitmapResult
& bitmap_result
);
80 // WebContentsObserver
81 bool OnMessageReceived(const IPC::Message
& message
) override
;
82 void WebContentsDestroyed() override
;
84 // Adds a shortcut to the launcher using a FaviconRawBitmapResult.
85 // Must be called from a WorkerPool task.
86 static void AddShortcutInBackgroundWithRawBitmap(
87 const ShortcutInfo
& info
,
88 const favicon_base::FaviconRawBitmapResult
& bitmap_result
);
90 // Adds a shortcut to the launcher using a SkBitmap.
91 // Must be called from a WorkerPool task.
92 static void AddShortcutInBackgroundWithSkBitmap(
93 const ShortcutInfo
& info
,
94 const SkBitmap
& icon_bitmap
,
95 const bool return_to_homescreen
);
97 // Registers JNI hooks.
98 static bool RegisterShortcutHelper(JNIEnv
* env
);
101 enum ManifestIconStatus
{
102 MANIFEST_ICON_STATUS_NONE
,
103 MANIFEST_ICON_STATUS_FETCHING
,
104 MANIFEST_ICON_STATUS_DONE
107 ~ShortcutHelper() override
;
111 void RecordAddToHomescreen();
113 JavaObjectWeakGlobalRef java_ref_
;
115 ShortcutInfo shortcut_info_
;
116 SkBitmap manifest_icon_
;
117 base::CancelableTaskTracker cancelable_task_tracker_
;
119 bool add_shortcut_requested_
;
121 ManifestIconStatus manifest_icon_status_
;
122 const int preferred_icon_size_in_px_
;
123 static const int kPreferredIconSizeInDp
;
125 base::WeakPtrFactory
<ShortcutHelper
> weak_ptr_factory_
;
127 DISALLOW_COPY_AND_ASSIGN(ShortcutHelper
);
130 #endif // CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_