Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / android / shortcut_helper.cc
blobde75b8a9bbe9c38311932b53b6fff658e2de592d
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 #include "chrome/browser/android/shortcut_helper.h"
7 #include <jni.h>
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_array.h"
11 #include "base/android/jni_string.h"
12 #include "base/basictypes.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/manifest/manifest_icon_downloader.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/web_contents.h"
18 #include "jni/ShortcutHelper_jni.h"
19 #include "ui/gfx/android/java_bitmap.h"
20 #include "url/gurl.h"
22 using content::Manifest;
24 namespace {
26 static int kIdealHomescreenIconSize = -1;
27 static int kMinimumHomescreenIconSize = -1;
28 static int kIdealSplashImageSize = -1;
29 static int kMinimumSplashImageSize = -1;
31 // Retrieves and caches the ideal and minimum sizes of the Home screen icon
32 // and the splash screen image.
33 void GetHomescreenIconAndSplashImageSizes() {
34 JNIEnv* env = base::android::AttachCurrentThread();
35 ScopedJavaLocalRef<jintArray> java_size_array =
36 Java_ShortcutHelper_getHomescreenIconAndSplashImageSizes(env,
37 base::android::GetApplicationContext());
38 std::vector<int> sizes;
39 base::android::JavaIntArrayToIntVector(
40 env, java_size_array.obj(), &sizes);
42 // Check that the size returned is what is expected.
43 DCHECK(sizes.size() == 4);
45 // This ordering must be kept up to date with the Java ShortcutHelper.
46 kIdealHomescreenIconSize = sizes[0];
47 kMinimumHomescreenIconSize = sizes[1];
48 kIdealSplashImageSize = sizes[2];
49 kMinimumSplashImageSize = sizes[3];
51 // Try to ensure that the data returned is sane.
52 DCHECK(kMinimumHomescreenIconSize <= kIdealHomescreenIconSize);
53 DCHECK(kMinimumSplashImageSize <= kIdealSplashImageSize);
56 } // anonymous namespace
58 // static
59 void ShortcutHelper::AddShortcutInBackgroundWithSkBitmap(
60 const ShortcutInfo& info,
61 const std::string& webapp_id,
62 const SkBitmap& icon_bitmap) {
63 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
65 // Send the data to the Java side to create the shortcut.
66 JNIEnv* env = base::android::AttachCurrentThread();
67 ScopedJavaLocalRef<jstring> java_webapp_id =
68 base::android::ConvertUTF8ToJavaString(env, webapp_id);
69 ScopedJavaLocalRef<jstring> java_url =
70 base::android::ConvertUTF8ToJavaString(env, info.url.spec());
71 ScopedJavaLocalRef<jstring> java_user_title =
72 base::android::ConvertUTF16ToJavaString(env, info.user_title);
73 ScopedJavaLocalRef<jstring> java_name =
74 base::android::ConvertUTF16ToJavaString(env, info.name);
75 ScopedJavaLocalRef<jstring> java_short_name =
76 base::android::ConvertUTF16ToJavaString(env, info.short_name);
77 ScopedJavaLocalRef<jobject> java_bitmap;
78 if (icon_bitmap.getSize())
79 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap);
81 Java_ShortcutHelper_addShortcut(
82 env,
83 base::android::GetApplicationContext(),
84 java_webapp_id.obj(),
85 java_url.obj(),
86 java_user_title.obj(),
87 java_name.obj(),
88 java_short_name.obj(),
89 java_bitmap.obj(),
90 info.display == blink::WebDisplayModeStandalone,
91 info.orientation,
92 info.source,
93 info.theme_color,
94 info.background_color);
97 int ShortcutHelper::GetIdealHomescreenIconSizeInDp() {
98 if (kIdealHomescreenIconSize == -1)
99 GetHomescreenIconAndSplashImageSizes();
100 return kIdealHomescreenIconSize;
103 int ShortcutHelper::GetMinimumHomescreenIconSizeInDp() {
104 if (kMinimumHomescreenIconSize == -1)
105 GetHomescreenIconAndSplashImageSizes();
106 return kMinimumHomescreenIconSize;
109 int ShortcutHelper::GetIdealSplashImageSizeInDp() {
110 if (kIdealSplashImageSize == -1)
111 GetHomescreenIconAndSplashImageSizes();
112 return kIdealSplashImageSize;
115 int ShortcutHelper::GetMinimumSplashImageSizeInDp() {
116 if (kMinimumSplashImageSize == -1)
117 GetHomescreenIconAndSplashImageSizes();
118 return kMinimumSplashImageSize;
121 // static
122 void ShortcutHelper::FetchSplashScreenImage(
123 content::WebContents* web_contents,
124 const GURL& image_url,
125 const int ideal_splash_image_size_in_dp,
126 const int minimum_splash_image_size_in_dp,
127 const std::string& webapp_id) {
128 // This is a fire and forget task. It is not vital for the splash screen image
129 // to be downloaded so if the downloader returns false there is no fallback.
130 ManifestIconDownloader::Download(
131 web_contents,
132 image_url,
133 ideal_splash_image_size_in_dp,
134 minimum_splash_image_size_in_dp,
135 base::Bind(&ShortcutHelper::StoreWebappData, webapp_id));
138 // static
139 void ShortcutHelper::StoreWebappData(
140 const std::string& webapp_id,
141 const SkBitmap& splash_image) {
142 if (splash_image.drawsNothing())
143 return;
145 JNIEnv* env = base::android::AttachCurrentThread();
146 ScopedJavaLocalRef<jstring> java_webapp_id =
147 base::android::ConvertUTF8ToJavaString(env, webapp_id);
148 ScopedJavaLocalRef<jobject> java_splash_image =
149 gfx::ConvertToJavaBitmap(&splash_image);
151 Java_ShortcutHelper_storeWebappData(
152 env,
153 base::android::GetApplicationContext(),
154 java_webapp_id.obj(),
155 java_splash_image.obj());
158 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) {
159 return RegisterNativesImpl(env);