Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: chrome/browser/android/webapps/add_to_homescreen_helper.cc

Issue 1288903002: Refactor ShortcutHelper and merge in BookmarkUtils (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/shortcut_helper.h" 5 #include "chrome/browser/android/webapps/add_to_homescreen_helper.h"
6 6
7 #include <jni.h> 7 #include <jni.h>
8 8
9 #include "base/android/jni_android.h" 9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h" 10 #include "base/android/jni_string.h"
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/strings/string16.h" 13 #include "base/strings/string16.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/android/shortcut_helper.h"
15 #include "chrome/browser/banners/app_banner_settings_helper.h" 16 #include "chrome/browser/banners/app_banner_settings_helper.h"
16 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/manifest.h" 19 #include "content/public/common/manifest.h"
19 #include "jni/ShortcutHelper_jni.h" 20 #include "jni/AddToHomescreenHelper_jni.h"
20 #include "ui/gfx/android/java_bitmap.h" 21 #include "ui/gfx/android/java_bitmap.h"
21 #include "ui/gfx/color_analysis.h" 22 #include "ui/gfx/color_analysis.h"
22 #include "url/gurl.h" 23 #include "url/gurl.h"
23 24
24 using content::Manifest; 25 using content::Manifest;
25 26
26 jlong Initialize(JNIEnv* env, jobject obj, jobject java_web_contents) { 27 jlong Initialize(JNIEnv* env, jobject obj, jobject java_web_contents) {
27 content::WebContents* web_contents = 28 content::WebContents* web_contents =
28 content::WebContents::FromJavaWebContents(java_web_contents); 29 content::WebContents::FromJavaWebContents(java_web_contents);
29 ShortcutHelper* shortcut_helper = new ShortcutHelper(env, obj, web_contents); 30 AddToHomescreenHelper* add_to_homescreen_helper = new AddToHomescreenHelper(
30 return reinterpret_cast<intptr_t>(shortcut_helper); 31 env, obj, web_contents);
32 return reinterpret_cast<intptr_t>(add_to_homescreen_helper);
31 } 33 }
32 34
33 ShortcutHelper::ShortcutHelper(JNIEnv* env, 35 AddToHomescreenHelper::AddToHomescreenHelper(JNIEnv* env,
34 jobject obj, 36 jobject obj,
35 content::WebContents* web_contents) 37 content::WebContents* web_contents)
36 : add_shortcut_pending_(false), 38 : add_shortcut_pending_(false),
37 data_fetcher_(new ShortcutDataFetcher(web_contents, this)) { 39 data_fetcher_(new AddToHomescreenDataFetcher(web_contents, this)) {
38 java_ref_.Reset(env, obj); 40 java_ref_.Reset(env, obj);
39 } 41 }
40 42
41 ShortcutHelper::~ShortcutHelper() { 43 AddToHomescreenHelper::~AddToHomescreenHelper() {
42 data_fetcher_->set_weak_observer(nullptr); 44 data_fetcher_->set_weak_observer(nullptr);
43 data_fetcher_ = nullptr; 45 data_fetcher_ = nullptr;
44 } 46 }
45 47
46 void ShortcutHelper::OnUserTitleAvailable(const base::string16& user_title) { 48 void AddToHomescreenHelper::OnUserTitleAvailable(
49 const base::string16& user_title) {
47 JNIEnv* env = base::android::AttachCurrentThread(); 50 JNIEnv* env = base::android::AttachCurrentThread();
48 ScopedJavaLocalRef<jstring> j_user_title = 51 ScopedJavaLocalRef<jstring> j_user_title =
49 base::android::ConvertUTF16ToJavaString(env, user_title); 52 base::android::ConvertUTF16ToJavaString(env, user_title);
50 Java_ShortcutHelper_onUserTitleAvailable(env, 53 Java_AddToHomescreenHelper_onUserTitleAvailable(env,
51 java_ref_.obj(), 54 java_ref_.obj(),
52 j_user_title.obj()); 55 j_user_title.obj());
53 } 56 }
54 57
55 void ShortcutHelper::OnDataAvailable(const ShortcutInfo& info, 58 void AddToHomescreenHelper::OnDataAvailable(const ShortcutInfo& info,
56 const SkBitmap& icon) { 59 const SkBitmap& icon) {
57 JNIEnv* env = base::android::AttachCurrentThread(); 60 JNIEnv* env = base::android::AttachCurrentThread();
58 ScopedJavaLocalRef<jobject> java_bitmap; 61 ScopedJavaLocalRef<jobject> java_bitmap;
59 if (icon.getSize()) 62 if (icon.getSize())
60 java_bitmap = gfx::ConvertToJavaBitmap(&icon); 63 java_bitmap = gfx::ConvertToJavaBitmap(&icon);
61 64
62 Java_ShortcutHelper_onIconAvailable(env, 65 Java_AddToHomescreenHelper_onIconAvailable(env,
63 java_ref_.obj(), 66 java_ref_.obj(),
64 java_bitmap.obj()); 67 java_bitmap.obj());
65 68
66 if (add_shortcut_pending_) 69 if (add_shortcut_pending_)
67 AddShortcut(info, icon); 70 AddShortcut(info, icon);
68 } 71 }
69 72
70 void ShortcutHelper::Destroy(JNIEnv* env, jobject obj) { 73 void AddToHomescreenHelper::Destroy(JNIEnv* env, jobject obj) {
71 delete this; 74 delete this;
72 } 75 }
73 76
74 SkBitmap ShortcutHelper::FinalizeLauncherIcon(const SkBitmap& bitmap, 77 SkBitmap AddToHomescreenHelper::FinalizeLauncherIcon(const SkBitmap& bitmap,
75 const GURL& url) { 78 const GURL& url) {
76 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 79 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
77 80
78 // Determine a single color to use for the favicon if the favicon that is 81 // Determine a single color to use for the favicon if the favicon that is
79 // returned it is too low quality. 82 // returned it is too low quality.
80 SkColor color = color_utils::CalculateKMeanColorOfBitmap(bitmap); 83 SkColor color = color_utils::CalculateKMeanColorOfBitmap(bitmap);
81 int dominant_red = SkColorGetR(color); 84 int dominant_red = SkColorGetR(color);
82 int dominant_green = SkColorGetG(color); 85 int dominant_green = SkColorGetG(color);
83 int dominant_blue = SkColorGetB(color); 86 int dominant_blue = SkColorGetB(color);
84 87
85 // Make the icon acceptable for the Android launcher. 88 // Make the icon acceptable for the Android launcher.
86 JNIEnv* env = base::android::AttachCurrentThread(); 89 JNIEnv* env = base::android::AttachCurrentThread();
87 ScopedJavaLocalRef<jstring> java_url = 90 ScopedJavaLocalRef<jstring> java_url =
88 base::android::ConvertUTF8ToJavaString(env, url.spec()); 91 base::android::ConvertUTF8ToJavaString(env, url.spec());
89 ScopedJavaLocalRef<jobject> java_bitmap; 92 ScopedJavaLocalRef<jobject> java_bitmap;
90 if (bitmap.getSize()) 93 if (bitmap.getSize())
91 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap); 94 java_bitmap = gfx::ConvertToJavaBitmap(&bitmap);
92 95
93 base::android::ScopedJavaLocalRef<jobject> ref = 96 base::android::ScopedJavaLocalRef<jobject> ref =
94 Java_ShortcutHelper_finalizeLauncherIcon(env, 97 Java_AddToHomescreenHelper_finalizeLauncherIcon(env,
95 java_url.obj(), 98 java_url.obj(),
96 java_bitmap.obj(), 99 java_bitmap.obj(),
97 dominant_red, 100 dominant_red,
98 dominant_green, 101 dominant_green,
99 dominant_blue); 102 dominant_blue);
100 return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(ref.obj())); 103 return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(ref.obj()));
101 } 104 }
102 105
103 void ShortcutHelper::AddShortcut(JNIEnv* env, 106 void AddToHomescreenHelper::AddShortcut(JNIEnv* env,
104 jobject obj, 107 jobject obj,
105 jstring j_user_title) { 108 jstring j_user_title) {
106 add_shortcut_pending_ = true; 109 add_shortcut_pending_ = true;
107 110
108 base::string16 user_title = 111 base::string16 user_title =
109 base::android::ConvertJavaStringToUTF16(env, j_user_title); 112 base::android::ConvertJavaStringToUTF16(env, j_user_title);
110 if (!user_title.empty()) 113 if (!user_title.empty())
111 data_fetcher_->shortcut_info().user_title = user_title; 114 data_fetcher_->shortcut_info().user_title = user_title;
112 115
113 if (data_fetcher_->is_ready()) { 116 if (data_fetcher_->is_ready()) {
114 // If the fetcher isn't ready yet, the shortcut will be added when it is 117 // If the fetcher isn't ready yet, the shortcut will be added when it is
115 // via OnDataAvailable(); 118 // via OnDataAvailable();
116 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon()); 119 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon());
117 } 120 }
118 } 121 }
119 122
120 void ShortcutHelper::AddShortcut(const ShortcutInfo& info, 123 void AddToHomescreenHelper::AddShortcut(const ShortcutInfo& info,
121 const SkBitmap& icon) { 124 const SkBitmap& icon) {
122 DCHECK(add_shortcut_pending_); 125 DCHECK(add_shortcut_pending_);
123 if (!add_shortcut_pending_) 126 if (!add_shortcut_pending_)
124 return; 127 return;
125 add_shortcut_pending_ = false; 128 add_shortcut_pending_ = false;
126 129
127 RecordAddToHomescreen(); 130 RecordAddToHomescreen();
128 131
129 content::BrowserThread::PostTask( 132 content::BrowserThread::PostTask(
130 content::BrowserThread::IO, 133 content::BrowserThread::IO,
131 FROM_HERE, 134 FROM_HERE,
132 base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap, 135 base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap,
133 info, 136 info,
134 icon)); 137 icon));
135 } 138 }
136 139
137 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { 140 bool AddToHomescreenHelper::RegisterAddToHomescreenHelper(JNIEnv* env) {
138 return RegisterNativesImpl(env); 141 return RegisterNativesImpl(env);
139 } 142 }
140 143
141 // static 144 void AddToHomescreenHelper::RecordAddToHomescreen() {
142 void ShortcutHelper::AddShortcutInBackgroundWithSkBitmap(
143 const ShortcutInfo& info,
144 const SkBitmap& icon_bitmap) {
145 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
146
147 // Send the data to the Java side to create the shortcut.
148 JNIEnv* env = base::android::AttachCurrentThread();
149 ScopedJavaLocalRef<jstring> java_url =
150 base::android::ConvertUTF8ToJavaString(env, info.url.spec());
151 ScopedJavaLocalRef<jstring> java_user_title =
152 base::android::ConvertUTF16ToJavaString(env, info.user_title);
153 ScopedJavaLocalRef<jstring> java_name =
154 base::android::ConvertUTF16ToJavaString(env, info.name);
155 ScopedJavaLocalRef<jstring> java_short_name =
156 base::android::ConvertUTF16ToJavaString(env, info.short_name);
157 ScopedJavaLocalRef<jobject> java_bitmap;
158 if (icon_bitmap.getSize())
159 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap);
160
161 Java_ShortcutHelper_addShortcut(
162 env,
163 base::android::GetApplicationContext(),
164 java_url.obj(),
165 java_user_title.obj(),
166 java_name.obj(),
167 java_short_name.obj(),
168 java_bitmap.obj(),
169 info.display == blink::WebDisplayModeStandalone,
170 info.orientation,
171 info.source,
172 info.theme_color);
173 }
174
175 void ShortcutHelper::RecordAddToHomescreen() {
176 // Record that the shortcut has been added, so no banners will be shown 145 // Record that the shortcut has been added, so no banners will be shown
177 // for this app. 146 // for this app.
178 content::WebContents* web_contents = data_fetcher_->web_contents(); 147 content::WebContents* web_contents = data_fetcher_->web_contents();
179 if (!web_contents) 148 if (!web_contents)
180 return; 149 return;
181 150
182 AppBannerSettingsHelper::RecordBannerEvent( 151 AppBannerSettingsHelper::RecordBannerEvent(
183 web_contents, web_contents->GetURL(), 152 web_contents, web_contents->GetURL(),
184 data_fetcher_->shortcut_info().url.spec(), 153 data_fetcher_->shortcut_info().url.spec(),
185 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, 154 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN,
186 base::Time::Now()); 155 base::Time::Now());
187 } 156 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698