Commit c679bbfd authored by mlamouri's avatar mlamouri Committed by Commit bot

Use Manifest.icons instead of favicon in ShortcutHelper when possible.

The algorithm is first trying to find an image to fit exactly the
required size in the device scale factor and default scale factor.
If it can't it will try to find the closest but preferrable largest
image. Note that the algorithm completely ignore an entry with no 'sizes'.

Fetching the image happens as soon as the Manifest is loaded and can be
done before or after the call to ShortcutHelper::AddShortcut().

BUG=366145
TEST=ShortcutHelperTest

Review URL: https://codereview.chromium.org/601433002

Cr-Commit-Position: refs/heads/master@{#296522}
parent 7169b5a9
......@@ -122,17 +122,17 @@ public class ShortcutHelper {
*/
@SuppressWarnings("unused")
@CalledByNative
private static void addShortcut(Context context, String url, String title, Bitmap favicon,
private static void addShortcut(Context context, String url, String title, Bitmap icon,
int red, int green, int blue, boolean isWebappCapable, int orientation) {
assert sFullScreenAction != null;
Intent shortcutIntent;
if (isWebappCapable) {
// Encode the favicon as a base64 string (Launcher drops Bitmaps in the Intent).
// Encode the icon as a base64 string (Launcher drops Bitmaps in the Intent).
String encodedIcon = "";
if (favicon != null) {
if (icon != null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
favicon.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
icon.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
encodedIcon = Base64.encodeToString(byteArray, Base64.DEFAULT);
}
......@@ -159,7 +159,7 @@ public class ShortcutHelper {
shortcutIntent.setPackage(context.getPackageName());
context.sendBroadcast(BookmarkUtils.createAddToHomeIntent(context, shortcutIntent, title,
favicon, red, green, blue));
icon, red, green, blue));
// User is sent to the homescreen as soon as the shortcut is created.
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
......
This diff is collapsed.
......@@ -58,41 +58,105 @@ class ShortcutHelper : public content::WebContentsObserver {
jstring title,
jint launcher_large_icon_size);
void FinishAddingShortcut(
// Callback run when the requested Manifest icon is ready to be used.
void OnDidDownloadIcon(int id,
int http_status_code,
const GURL& url,
const std::vector<SkBitmap>& bitmaps,
const std::vector<gfx::Size>& sizes);
// Called after AddShortcut() and OnDidDownloadIcon() are run if
// OnDidDownloadIcon has a valid icon.
void AddShortcutUsingManifestIcon();
// Use FaviconService to get the best available favicon and create the
// shortcut using it. This is used when no Manifest icons are available or
// appropriate.
void AddShortcutUsingFavicon();
// Callback run when a favicon is received from GetFavicon() call.
void OnDidGetFavicon(
const favicon_base::FaviconRawBitmapResult& bitmap_result);
// WebContentsObserver
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
virtual void WebContentsDestroyed() OVERRIDE;
// Adds a shortcut to the launcher. Must be called from a WorkerPool task.
static void AddShortcutInBackground(
// Adds a shortcut to the launcher using a FaviconRawBitmapResult.
// Must be called from a WorkerPool task.
static void AddShortcutInBackgroundWithRawBitmap(
const GURL& url,
const base::string16& title,
content::Manifest::DisplayMode display,
const favicon_base::FaviconRawBitmapResult& bitmap_result,
blink::WebScreenOrientationLockType orientation);
// Adds a shortcut to the launcher using a SkBitmap.
// Must be called from a WorkerPool task.
static void AddShortcutInBackgroundWithSkBitmap(
const GURL& url,
const base::string16& title,
content::Manifest::DisplayMode display,
const SkBitmap& icon_bitmap,
blink::WebScreenOrientationLockType orientation);
// Registers JNI hooks.
static bool RegisterShortcutHelper(JNIEnv* env);
private:
enum ManifestIconStatus {
MANIFEST_ICON_STATUS_NONE,
MANIFEST_ICON_STATUS_FETCHING,
MANIFEST_ICON_STATUS_DONE
};
virtual ~ShortcutHelper();
void Destroy();
// Runs the algorithm to find the best matching icon in the icons listed in
// the Manifest.
// Returns the icon url if a suitable icon is found. An empty URL otherwise.
GURL FindBestMatchingIcon(
const std::vector<content::Manifest::Icon>& icons) const;
// Runs an algorithm only based on icon declared sizes. It will try to find
// size that is the closest to preferred_icon_size_in_px_ but bigger than
// preferred_icon_size_in_px_ if possible.
// Returns the icon url if a suitable icon is found. An empty URL otherwise.
GURL FindBestMatchingIcon(const std::vector<content::Manifest::Icon>& icons,
float density) const;
// Returns an array containing the items in |icons| without the unsupported
// image MIME types.
static std::vector<content::Manifest::Icon> FilterIconsByType(
const std::vector<content::Manifest::Icon>& icons);
// Returns whether the preferred_icon_size_in_px_ is in the given |sizes|.
bool IconSizesContainsPreferredSize(
const std::vector<gfx::Size>& sizes) const;
// Returns whether the 'any' (ie. gfx::Size(0,0)) is in the given |sizes|.
bool IconSizesContainsAny(const std::vector<gfx::Size>& sizes) const;
JavaObjectWeakGlobalRef java_ref_;
GURL url_;
base::string16 title_;
int launcher_large_icon_size_;
content::Manifest::DisplayMode display_;
favicon_base::FaviconRawBitmapResult icon_;
SkBitmap manifest_icon_;
base::CancelableTaskTracker cancelable_task_tracker_;
blink::WebScreenOrientationLockType orientation_;
bool add_shortcut_requested_;
ManifestIconStatus manifest_icon_status_;
const int preferred_icon_size_in_px_;
static const int kPreferredIconSizeInDp;
base::WeakPtrFactory<ShortcutHelper> weak_ptr_factory_;
friend class ShortcutHelperTest;
DISALLOW_COPY_AND_ASSIGN(ShortcutHelper);
};
......
This diff is collapsed.
......@@ -27,6 +27,7 @@
# includes the relevant version directly.
'browser/android/mock_google_location_settings_helper.cc',
'browser/android/mock_google_location_settings_helper.h',
'browser/android/shortcut_helper_unittest.cc',
'browser/app_controller_mac_unittest.mm',
'browser/apps/drive/drive_app_mapping_unittest.cc',
'browser/apps/ephemeral_app_service_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment