Web app: set a flag on the Intent if the icon was generated by Chromium.

This is in order to prevent Chromium to then re-use that icon for
anything related to web app branding.

BUG=546212

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

Cr-Commit-Position: refs/heads/master@{#355634}
diff --git a/chrome/browser/android/shortcut_helper.cc b/chrome/browser/android/shortcut_helper.cc
index de75b8a..c39462a3 100644
--- a/chrome/browser/android/shortcut_helper.cc
+++ b/chrome/browser/android/shortcut_helper.cc
@@ -17,6 +17,7 @@
 #include "content/public/browser/web_contents.h"
 #include "jni/ShortcutHelper_jni.h"
 #include "ui/gfx/android/java_bitmap.h"
+#include "ui/gfx/color_analysis.h"
 #include "url/gurl.h"
 
 using content::Manifest;
@@ -28,6 +29,8 @@
 static int kIdealSplashImageSize = -1;
 static int kMinimumSplashImageSize = -1;
 
+static int kDefaultRGBIconValue = 145;
+
 // Retrieves and caches the ideal and minimum sizes of the Home screen icon
 // and the splash screen image.
 void GetHomescreenIconAndSplashImageSizes() {
@@ -91,7 +94,8 @@
       info.orientation,
       info.source,
       info.theme_color,
-      info.background_color);
+      info.background_color,
+      info.is_icon_generated);
 }
 
 int ShortcutHelper::GetIdealHomescreenIconSizeInDp() {
@@ -155,6 +159,44 @@
       java_splash_image.obj());
 }
 
+// static
+SkBitmap ShortcutHelper::FinalizeLauncherIcon(const SkBitmap& bitmap,
+                                              const GURL& url,
+                                              bool* is_generated) {
+  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
+
+  JNIEnv* env = base::android::AttachCurrentThread();
+  ScopedJavaLocalRef<jobject> result;
+  *is_generated = false;
+
+  if (!bitmap.isNull()) {
+    ScopedJavaLocalRef<jobject> java_bitmap = gfx::ConvertToJavaBitmap(&bitmap);
+    if (Java_ShortcutHelper_isIconLargeEnoughForLauncher(
+        env, base::android::GetApplicationContext(), java_bitmap.obj())) {
+      result = Java_ShortcutHelper_modifyIconForLauncher(
+          env, base::android::GetApplicationContext(), java_bitmap.obj());
+    }
+  }
+
+  if (result.is_null()) {
+    ScopedJavaLocalRef<jstring> java_url =
+        base::android::ConvertUTF8ToJavaString(env, url.spec());
+    SkColor mean_color = SkColorSetRGB(
+        kDefaultRGBIconValue, kDefaultRGBIconValue, kDefaultRGBIconValue);
+
+    if (!bitmap.isNull())
+      mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap);
+
+    *is_generated = true;
+    result = Java_ShortcutHelper_generateLauncherIcon(
+        env, base::android::GetApplicationContext(), java_url.obj(),
+        SkColorGetR(mean_color), SkColorGetG(mean_color),
+        SkColorGetB(mean_color));
+  }
+
+  return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result.obj()));
+}
+
 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) {
   return RegisterNativesImpl(env);
 }