summaryrefslogtreecommitdiffstats
path: root/chrome/browser/android/shortcut_helper.cc
blob: fc46ef7806a72f2641b340c311679f0ad4ede12b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/android/shortcut_helper.h"

#include <jni.h>

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/basictypes.h"
#include "base/location.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/banners/app_banner_settings_helper.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/manifest.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;

jlong Initialize(JNIEnv* env, jobject obj, jobject java_web_contents) {
  content::WebContents* web_contents =
      content::WebContents::FromJavaWebContents(java_web_contents);
  ShortcutHelper* shortcut_helper = new ShortcutHelper(env, obj, web_contents);
  return reinterpret_cast<intptr_t>(shortcut_helper);
}

ShortcutHelper::ShortcutHelper(JNIEnv* env,
                               jobject obj,
                               content::WebContents* web_contents)
    : add_shortcut_pending_(false),
      data_fetcher_(new ShortcutDataFetcher(web_contents, this)) {
  java_ref_.Reset(env, obj);
}

ShortcutHelper::~ShortcutHelper() {
  data_fetcher_->set_weak_observer(nullptr);
  data_fetcher_ = nullptr;
}

void ShortcutHelper::OnTitleAvailable(const base::string16& title) {
  JNIEnv* env = base::android::AttachCurrentThread();
  ScopedJavaLocalRef<jstring> j_title =
      base::android::ConvertUTF16ToJavaString(env, title);
  Java_ShortcutHelper_onTitleAvailable(env,
                                       java_ref_.obj(),
                                       j_title.obj());
}

void ShortcutHelper::OnDataAvailable(const ShortcutInfo& info,
                                     const SkBitmap& icon) {
  JNIEnv* env = base::android::AttachCurrentThread();
  ScopedJavaLocalRef<jobject> java_bitmap;
  if (icon.getSize())
    java_bitmap = gfx::ConvertToJavaBitmap(&icon);

  Java_ShortcutHelper_onIconAvailable(env,
                                      java_ref_.obj(),
                                      java_bitmap.obj());

  if (add_shortcut_pending_)
    AddShortcut(info, icon);
}

void ShortcutHelper::Destroy(JNIEnv* env, jobject obj) {
  delete this;
}

SkBitmap ShortcutHelper::FinalizeLauncherIcon(const SkBitmap& bitmap,
                                              const GURL& url) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

  // Determine a single color to use for the favicon if the favicon that is
  // returned it is too low quality.
  SkColor color = color_utils::CalculateKMeanColorOfBitmap(bitmap);
  int dominant_red = SkColorGetR(color);
  int dominant_green = SkColorGetG(color);
  int dominant_blue = SkColorGetB(color);

  // Make the icon acceptable for the Android launcher.
  JNIEnv* env = base::android::AttachCurrentThread();
  ScopedJavaLocalRef<jstring> java_url =
      base::android::ConvertUTF8ToJavaString(env, url.spec());
  ScopedJavaLocalRef<jobject> java_bitmap;
  if (bitmap.getSize())
    java_bitmap = gfx::ConvertToJavaBitmap(&bitmap);

  base::android::ScopedJavaLocalRef<jobject> ref =
      Java_ShortcutHelper_finalizeLauncherIcon(env,
                                               java_url.obj(),
                                               java_bitmap.obj(),
                                               dominant_red,
                                               dominant_green,
                                               dominant_blue);
  return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(ref.obj()));
}

void ShortcutHelper::AddShortcut(JNIEnv* env, jobject obj, jstring jtitle) {
  add_shortcut_pending_ = true;

  base::string16 title = base::android::ConvertJavaStringToUTF16(env, jtitle);
  if (!title.empty())
    data_fetcher_->shortcut_info().title = title;

  if (data_fetcher_->is_ready()) {
    // If the fetcher isn't ready yet, the shortcut will be added when it is
    // via OnDataAvailable();
    AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon());
  }
}

void ShortcutHelper::AddShortcut(const ShortcutInfo& info,
                                 const SkBitmap& icon) {
  DCHECK(add_shortcut_pending_);
  if (!add_shortcut_pending_)
    return;
  add_shortcut_pending_ = false;

  RecordAddToHomescreen();

  content::BrowserThread::PostTask(
      content::BrowserThread::IO,
      FROM_HERE,
      base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap,
                 info,
                 icon));
}

bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) {
  return RegisterNativesImpl(env);
}

// static
void ShortcutHelper::AddShortcutInBackgroundWithSkBitmap(
    const ShortcutInfo& info,
    const SkBitmap& icon_bitmap) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

  // Send the data to the Java side to create the shortcut.
  JNIEnv* env = base::android::AttachCurrentThread();
  ScopedJavaLocalRef<jstring> java_url =
      base::android::ConvertUTF8ToJavaString(env, info.url.spec());
  ScopedJavaLocalRef<jstring> java_title =
      base::android::ConvertUTF16ToJavaString(env, info.title);
  ScopedJavaLocalRef<jobject> java_bitmap;
  if (icon_bitmap.getSize())
    java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap);

  Java_ShortcutHelper_addShortcut(
      env,
      base::android::GetApplicationContext(),
      java_url.obj(),
      java_title.obj(),
      java_bitmap.obj(),
      info.display == content::Manifest::DISPLAY_MODE_STANDALONE,
      info.orientation);
}

void ShortcutHelper::RecordAddToHomescreen() {
  // Record that the shortcut has been added, so no banners will be shown
  // for this app.
  content::WebContents* web_contents = data_fetcher_->web_contents();
  if (!web_contents)
    return;

  AppBannerSettingsHelper::RecordBannerEvent(
      web_contents, web_contents->GetURL(),
      data_fetcher_->shortcut_info().url.spec(),
      AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN,
      base::Time::Now());
}