OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/android/webapps/add_to_homescreen_dialog_helper.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/jni_string.h" | |
9 #include "base/guid.h" | |
10 #include "base/location.h" | |
11 #include "base/strings/string16.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "chrome/browser/android/shortcut_helper.h" | |
14 #include "chrome/browser/banners/app_banner_settings_helper.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "content/public/browser/web_contents.h" | |
17 #include "jni/AddToHomescreenDialogHelper_jni.h" | |
18 #include "ui/gfx/android/java_bitmap.h" | |
19 | |
20 using base::android::JavaParamRef; | |
21 using base::android::ScopedJavaLocalRef; | |
22 | |
23 jlong Initialize(JNIEnv* env, | |
24 const JavaParamRef<jobject>& obj, | |
25 const JavaParamRef<jobject>& java_web_contents) { | |
26 content::WebContents* web_contents = | |
27 content::WebContents::FromJavaWebContents(java_web_contents); | |
28 AddToHomescreenDialogHelper* add_to_homescreen_helper = | |
29 new AddToHomescreenDialogHelper(env, obj, web_contents); | |
30 return reinterpret_cast<intptr_t>(add_to_homescreen_helper); | |
31 } | |
32 | |
33 AddToHomescreenDialogHelper::AddToHomescreenDialogHelper( | |
34 JNIEnv* env, | |
35 jobject obj, | |
36 content::WebContents* web_contents) | |
37 : add_shortcut_pending_(false), | |
38 data_fetcher_(new AddToHomescreenDataFetcher(web_contents, | |
39 ShortcutHelper::GetIdealHomescreenIconSizeInDp(), | |
40 ShortcutHelper::GetMinimumHomescreenIconSizeInDp(), | |
41 ShortcutHelper::GetIdealSplashImageSizeInDp(), | |
42 ShortcutHelper::GetMinimumSplashImageSizeInDp(), | |
43 this)) { | |
44 java_ref_.Reset(env, obj); | |
45 } | |
46 | |
47 AddToHomescreenDialogHelper::~AddToHomescreenDialogHelper() { | |
48 data_fetcher_->set_weak_observer(nullptr); | |
49 data_fetcher_ = nullptr; | |
50 } | |
51 | |
52 void AddToHomescreenDialogHelper::OnUserTitleAvailable( | |
53 const base::string16& user_title) { | |
54 JNIEnv* env = base::android::AttachCurrentThread(); | |
55 ScopedJavaLocalRef<jstring> j_user_title = | |
56 base::android::ConvertUTF16ToJavaString(env, user_title); | |
57 Java_AddToHomescreenDialogHelper_onUserTitleAvailable(env, | |
58 java_ref_.obj(), | |
59 j_user_title.obj()); | |
60 } | |
61 | |
62 void AddToHomescreenDialogHelper::OnDataAvailable(const ShortcutInfo& info, | |
63 const SkBitmap& icon) { | |
64 JNIEnv* env = base::android::AttachCurrentThread(); | |
65 ScopedJavaLocalRef<jobject> java_bitmap; | |
66 if (icon.getSize()) | |
67 java_bitmap = gfx::ConvertToJavaBitmap(&icon); | |
68 | |
69 Java_AddToHomescreenDialogHelper_onIconAvailable(env, | |
70 java_ref_.obj(), | |
71 java_bitmap.obj()); | |
72 | |
73 if (add_shortcut_pending_) | |
74 AddShortcut(info, icon); | |
75 } | |
76 | |
77 void AddToHomescreenDialogHelper::Destroy(JNIEnv* env, | |
78 const JavaParamRef<jobject>& obj) { | |
79 delete this; | |
80 } | |
81 | |
82 SkBitmap AddToHomescreenDialogHelper::FinalizeLauncherIconInBackground( | |
83 const SkBitmap& bitmap, | |
84 const GURL& url, | |
85 bool* is_generated) { | |
86 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
87 | |
88 return ShortcutHelper::FinalizeLauncherIconInBackground(bitmap, url, | |
89 is_generated); | |
90 } | |
91 | |
92 void AddToHomescreenDialogHelper::AddShortcut( | |
93 JNIEnv* env, | |
94 const JavaParamRef<jobject>& obj, | |
95 const JavaParamRef<jstring>& j_user_title) { | |
96 add_shortcut_pending_ = true; | |
97 | |
98 base::string16 user_title = | |
99 base::android::ConvertJavaStringToUTF16(env, j_user_title); | |
100 if (!user_title.empty()) | |
101 data_fetcher_->shortcut_info().user_title = user_title; | |
102 | |
103 if (data_fetcher_->is_ready()) { | |
104 // If the fetcher isn't ready yet, the shortcut will be added when it is | |
105 // via OnDataAvailable(); | |
106 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon()); | |
107 } | |
108 } | |
109 | |
110 void AddToHomescreenDialogHelper::AddShortcut(const ShortcutInfo& info, | |
111 const SkBitmap& icon) { | |
112 DCHECK(add_shortcut_pending_); | |
113 if (!add_shortcut_pending_) | |
114 return; | |
115 add_shortcut_pending_ = false; | |
116 | |
117 content::WebContents* web_contents = data_fetcher_->web_contents(); | |
118 if (!web_contents) | |
119 return; | |
120 | |
121 RecordAddToHomescreen(); | |
122 | |
123 const std::string& uid = base::GenerateGUID(); | |
124 ShortcutHelper::AddToLauncherWithSkBitmap( | |
125 web_contents->GetBrowserContext(), info, uid, icon, | |
126 data_fetcher_->FetchSplashScreenImageCallback(uid)); | |
127 } | |
128 | |
129 bool AddToHomescreenDialogHelper::RegisterAddToHomescreenDialogHelper( | |
130 JNIEnv* env) { | |
131 return RegisterNativesImpl(env); | |
132 } | |
133 | |
134 void AddToHomescreenDialogHelper::RecordAddToHomescreen() { | |
135 // Record that the shortcut has been added, so no banners will be shown | |
136 // for this app. | |
137 content::WebContents* web_contents = data_fetcher_->web_contents(); | |
138 if (!web_contents) | |
139 return; | |
140 | |
141 AppBannerSettingsHelper::RecordBannerEvent( | |
142 web_contents, web_contents->GetURL(), | |
143 data_fetcher_->shortcut_info().url.spec(), | |
144 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, | |
145 base::Time::Now()); | |
146 } | |
OLD | NEW |