OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/webapps/add_to_homescreen_dialog_helper.h" | 5 #include "chrome/browser/android/webapps/add_to_homescreen_dialog_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/guid.h" |
12 #include "base/location.h" | 13 #include "base/location.h" |
13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
15 #include "chrome/browser/android/shortcut_helper.h" | 16 #include "chrome/browser/android/shortcut_helper.h" |
16 #include "chrome/browser/banners/app_banner_settings_helper.h" | 17 #include "chrome/browser/banners/app_banner_settings_helper.h" |
17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
19 #include "content/public/common/manifest.h" | 20 #include "content/public/common/manifest.h" |
20 #include "jni/AddToHomescreenDialogHelper_jni.h" | 21 #include "jni/AddToHomescreenDialogHelper_jni.h" |
21 #include "ui/gfx/android/java_bitmap.h" | 22 #include "ui/gfx/android/java_bitmap.h" |
22 #include "ui/gfx/color_analysis.h" | 23 #include "ui/gfx/color_analysis.h" |
23 #include "url/gurl.h" | 24 #include "url/gurl.h" |
24 | 25 |
25 using content::Manifest; | 26 using content::Manifest; |
26 | 27 |
27 jlong Initialize(JNIEnv* env, | 28 jlong Initialize(JNIEnv* env, |
28 const JavaParamRef<jobject>& obj, | 29 const JavaParamRef<jobject>& obj, |
29 const JavaParamRef<jobject>& java_web_contents) { | 30 const JavaParamRef<jobject>& java_web_contents, |
| 31 jint ideal_splash_image_size_in_dp, |
| 32 jint ideal_icon_size_in_dp) { |
30 content::WebContents* web_contents = | 33 content::WebContents* web_contents = |
31 content::WebContents::FromJavaWebContents(java_web_contents); | 34 content::WebContents::FromJavaWebContents(java_web_contents); |
32 AddToHomescreenDialogHelper* add_to_homescreen_helper = | 35 AddToHomescreenDialogHelper* add_to_homescreen_helper = |
33 new AddToHomescreenDialogHelper(env, obj, web_contents); | 36 new AddToHomescreenDialogHelper(env, obj, web_contents, |
| 37 ideal_splash_image_size_in_dp, ideal_icon_size_in_dp); |
34 return reinterpret_cast<intptr_t>(add_to_homescreen_helper); | 38 return reinterpret_cast<intptr_t>(add_to_homescreen_helper); |
35 } | 39 } |
36 | 40 |
37 AddToHomescreenDialogHelper::AddToHomescreenDialogHelper(JNIEnv* env, | 41 AddToHomescreenDialogHelper::AddToHomescreenDialogHelper( |
38 jobject obj, | 42 JNIEnv* env, |
39 content::WebContents* web_contents) | 43 jobject obj, |
| 44 content::WebContents* web_contents, |
| 45 int ideal_splash_image_size_in_dp, |
| 46 int ideal_icon_size_in_dp) |
40 : add_shortcut_pending_(false), | 47 : add_shortcut_pending_(false), |
41 data_fetcher_(new AddToHomescreenDataFetcher(web_contents, this)) { | 48 data_fetcher_(new AddToHomescreenDataFetcher(web_contents, |
| 49 ideal_splash_image_size_in_dp, |
| 50 ideal_icon_size_in_dp, |
| 51 this)) { |
42 java_ref_.Reset(env, obj); | 52 java_ref_.Reset(env, obj); |
43 } | 53 } |
44 | 54 |
45 AddToHomescreenDialogHelper::~AddToHomescreenDialogHelper() { | 55 AddToHomescreenDialogHelper::~AddToHomescreenDialogHelper() { |
46 data_fetcher_->set_weak_observer(nullptr); | 56 data_fetcher_->set_weak_observer(nullptr); |
47 data_fetcher_ = nullptr; | 57 data_fetcher_ = nullptr; |
48 } | 58 } |
49 | 59 |
50 void AddToHomescreenDialogHelper::OnUserTitleAvailable( | 60 void AddToHomescreenDialogHelper::OnUserTitleAvailable( |
51 const base::string16& user_title) { | 61 const base::string16& user_title) { |
52 JNIEnv* env = base::android::AttachCurrentThread(); | 62 JNIEnv* env = base::android::AttachCurrentThread(); |
53 ScopedJavaLocalRef<jstring> j_user_title = | 63 ScopedJavaLocalRef<jstring> j_user_title = |
54 base::android::ConvertUTF16ToJavaString(env, user_title); | 64 base::android::ConvertUTF16ToJavaString(env, user_title); |
55 Java_AddToHomescreenDialogHelper_onUserTitleAvailable(env, | 65 Java_AddToHomescreenDialogHelper_onUserTitleAvailable(env, |
56 java_ref_.obj(), | 66 java_ref_.obj(), |
57 j_user_title.obj()); | 67 j_user_title.obj()); |
58 } | 68 } |
59 | 69 |
60 void AddToHomescreenDialogHelper::OnDataAvailable(const ShortcutInfo& info, | 70 void AddToHomescreenDialogHelper::OnDataAvailable(const ShortcutInfo& info, |
61 const SkBitmap& icon) { | 71 const SkBitmap& icon) { |
62 JNIEnv* env = base::android::AttachCurrentThread(); | 72 JNIEnv* env = base::android::AttachCurrentThread(); |
63 ScopedJavaLocalRef<jobject> java_bitmap; | 73 ScopedJavaLocalRef<jobject> java_bitmap; |
64 if (icon.getSize()) | 74 if (icon.getSize()) |
65 java_bitmap = gfx::ConvertToJavaBitmap(&icon); | 75 java_bitmap = gfx::ConvertToJavaBitmap(&icon); |
66 | 76 |
67 Java_AddToHomescreenDialogHelper_onIconAvailable(env, | 77 Java_AddToHomescreenDialogHelper_onIconAvailable(env, |
68 java_ref_.obj(), | 78 java_ref_.obj(), |
69 java_bitmap.obj()); | 79 java_bitmap.obj()); |
70 | 80 |
71 if (add_shortcut_pending_) | 81 if (add_shortcut_pending_) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 data_fetcher_->shortcut_info().user_title = user_title; | 127 data_fetcher_->shortcut_info().user_title = user_title; |
118 | 128 |
119 if (data_fetcher_->is_ready()) { | 129 if (data_fetcher_->is_ready()) { |
120 // If the fetcher isn't ready yet, the shortcut will be added when it is | 130 // If the fetcher isn't ready yet, the shortcut will be added when it is |
121 // via OnDataAvailable(); | 131 // via OnDataAvailable(); |
122 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon()); | 132 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon()); |
123 } | 133 } |
124 } | 134 } |
125 | 135 |
126 void AddToHomescreenDialogHelper::AddShortcut(const ShortcutInfo& info, | 136 void AddToHomescreenDialogHelper::AddShortcut(const ShortcutInfo& info, |
127 const SkBitmap& icon) { | 137 const SkBitmap& icon) { |
128 DCHECK(add_shortcut_pending_); | 138 DCHECK(add_shortcut_pending_); |
129 if (!add_shortcut_pending_) | 139 if (!add_shortcut_pending_) |
130 return; | 140 return; |
131 add_shortcut_pending_ = false; | 141 add_shortcut_pending_ = false; |
132 | 142 |
133 RecordAddToHomescreen(); | 143 RecordAddToHomescreen(); |
134 | 144 |
| 145 const std::string& uid = base::GenerateGUID(); |
135 content::BrowserThread::PostTask( | 146 content::BrowserThread::PostTask( |
136 content::BrowserThread::IO, | 147 content::BrowserThread::IO, |
137 FROM_HERE, | 148 FROM_HERE, |
138 base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap, | 149 base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap, |
139 info, | 150 info, |
| 151 uid, |
140 icon)); | 152 icon)); |
| 153 |
| 154 data_fetcher_->FetchSplashScreenImage(uid); |
141 } | 155 } |
142 | 156 |
143 bool AddToHomescreenDialogHelper::RegisterAddToHomescreenDialogHelper( | 157 bool AddToHomescreenDialogHelper::RegisterAddToHomescreenDialogHelper( |
144 JNIEnv* env) { | 158 JNIEnv* env) { |
145 return RegisterNativesImpl(env); | 159 return RegisterNativesImpl(env); |
146 } | 160 } |
147 | 161 |
148 void AddToHomescreenDialogHelper::RecordAddToHomescreen() { | 162 void AddToHomescreenDialogHelper::RecordAddToHomescreen() { |
149 // Record that the shortcut has been added, so no banners will be shown | 163 // Record that the shortcut has been added, so no banners will be shown |
150 // for this app. | 164 // for this app. |
151 content::WebContents* web_contents = data_fetcher_->web_contents(); | 165 content::WebContents* web_contents = data_fetcher_->web_contents(); |
152 if (!web_contents) | 166 if (!web_contents) |
153 return; | 167 return; |
154 | 168 |
155 AppBannerSettingsHelper::RecordBannerEvent( | 169 AppBannerSettingsHelper::RecordBannerEvent( |
156 web_contents, web_contents->GetURL(), | 170 web_contents, web_contents->GetURL(), |
157 data_fetcher_->shortcut_info().url.spec(), | 171 data_fetcher_->shortcut_info().url.spec(), |
158 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, | 172 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, |
159 base::Time::Now()); | 173 base::Time::Now()); |
160 } | 174 } |
OLD | NEW |