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/strings/string16.h"
12 #include "base/task/cancelable_task_tracker.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"
17 #include "third_party/WebKit/public/platform/WebScreenOrientationLockType.h"
21 } // namespace content
29 // ShortcutHelper is the C++ counterpart of org.chromium.chrome.browser's
30 // ShortcutHelper in Java. The object is owned by the Java object. It is created
31 // from there via a JNI (Initialize) call and can be destroyed from Java too
32 // using TearDown. When the Java implementations calls AddShortcut, it gives up
33 // the ownership of the object. The instance will then destroy itself when done.
34 class ShortcutHelper
: public content::WebContentsObserver
{
36 ShortcutHelper(JNIEnv
* env
,
38 content::WebContents
* web_contents
);
40 // Initialize the helper by requesting the information about the page to the
41 // renderer process. The initialization is asynchronous and
42 // OnDidRetrieveWebappInformation is expected to be called when finished.
45 // Called by the Java counter part to let the object knows that it can destroy
47 void TearDown(JNIEnv
* env
, jobject obj
);
49 // IPC message received when the initialization is finished.
50 void OnDidGetWebApplicationInfo(const WebApplicationInfo
& web_app_info
);
52 // Callback run when the Manifest is ready to be used.
53 void OnDidGetManifest(const content::Manifest
& manifest
);
55 // Adds a shortcut to the current URL to the Android home screen.
56 void AddShortcut(JNIEnv
* env
,
59 jint launcher_large_icon_size
);
61 // Callback run when the requested Manifest icon is ready to be used.
62 void OnDidDownloadIcon(int id
,
65 const std::vector
<SkBitmap
>& bitmaps
,
66 const std::vector
<gfx::Size
>& sizes
);
68 // Called after AddShortcut() and OnDidDownloadIcon() are run if
69 // OnDidDownloadIcon has a valid icon.
70 void AddShortcutUsingManifestIcon();
72 // Use FaviconService to get the best available favicon and create the
73 // shortcut using it. This is used when no Manifest icons are available or
75 void AddShortcutUsingFavicon();
77 // Callback run when a favicon is received from GetFavicon() call.
79 const favicon_base::FaviconRawBitmapResult
& bitmap_result
);
81 // WebContentsObserver
82 virtual bool OnMessageReceived(const IPC::Message
& message
) override
;
83 virtual void WebContentsDestroyed() override
;
85 // Adds a shortcut to the launcher using a FaviconRawBitmapResult.
86 // Must be called from a WorkerPool task.
87 static void AddShortcutInBackgroundWithRawBitmap(
89 const base::string16
& title
,
90 content::Manifest::DisplayMode display
,
91 const favicon_base::FaviconRawBitmapResult
& bitmap_result
,
92 blink::WebScreenOrientationLockType orientation
);
94 // Adds a shortcut to the launcher using a SkBitmap.
95 // Must be called from a WorkerPool task.
96 static void AddShortcutInBackgroundWithSkBitmap(
98 const base::string16
& title
,
99 content::Manifest::DisplayMode display
,
100 const SkBitmap
& icon_bitmap
,
101 blink::WebScreenOrientationLockType orientation
);
103 // Registers JNI hooks.
104 static bool RegisterShortcutHelper(JNIEnv
* env
);
107 enum ManifestIconStatus
{
108 MANIFEST_ICON_STATUS_NONE
,
109 MANIFEST_ICON_STATUS_FETCHING
,
110 MANIFEST_ICON_STATUS_DONE
113 virtual ~ShortcutHelper();
117 // Runs the algorithm to find the best matching icon in the icons listed in
119 // Returns the icon url if a suitable icon is found. An empty URL otherwise.
120 GURL
FindBestMatchingIcon(
121 const std::vector
<content::Manifest::Icon
>& icons
) const;
123 // Runs an algorithm only based on icon declared sizes. It will try to find
124 // size that is the closest to preferred_icon_size_in_px_ but bigger than
125 // preferred_icon_size_in_px_ if possible.
126 // Returns the icon url if a suitable icon is found. An empty URL otherwise.
127 GURL
FindBestMatchingIcon(const std::vector
<content::Manifest::Icon
>& icons
,
128 float density
) const;
130 // Returns an array containing the items in |icons| without the unsupported
132 static std::vector
<content::Manifest::Icon
> FilterIconsByType(
133 const std::vector
<content::Manifest::Icon
>& icons
);
135 // Returns whether the preferred_icon_size_in_px_ is in the given |sizes|.
136 bool IconSizesContainsPreferredSize(
137 const std::vector
<gfx::Size
>& sizes
) const;
139 // Returns whether the 'any' (ie. gfx::Size(0,0)) is in the given |sizes|.
140 bool IconSizesContainsAny(const std::vector
<gfx::Size
>& sizes
) const;
142 JavaObjectWeakGlobalRef java_ref_
;
145 base::string16 title_
;
146 content::Manifest::DisplayMode display_
;
147 SkBitmap manifest_icon_
;
148 base::CancelableTaskTracker cancelable_task_tracker_
;
149 blink::WebScreenOrientationLockType orientation_
;
151 bool add_shortcut_requested_
;
153 ManifestIconStatus manifest_icon_status_
;
154 const int preferred_icon_size_in_px_
;
155 static const int kPreferredIconSizeInDp
;
157 base::WeakPtrFactory
<ShortcutHelper
> weak_ptr_factory_
;
159 friend class ShortcutHelperTest
;
160 DISALLOW_COPY_AND_ASSIGN(ShortcutHelper
);
163 #endif // CHROME_BROWSER_ANDROID_SHORTCUT_HELPER_H_