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

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

Issue 2244223002: Determine whether to show "Add to Homescreen" dialog or WebAPK infobar (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge branch 'master' into webapk_dialog_detector Created 4 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 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_process.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h" 8 #include "base/android/jni_string.h"
9 #include "base/command_line.h"
9 #include "base/guid.h" 10 #include "base/guid.h"
10 #include "base/location.h" 11 #include "base/location.h"
11 #include "base/strings/string16.h" 12 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/android/shortcut_helper.h" 14 #include "chrome/browser/android/shortcut_helper.h"
14 #include "chrome/browser/banners/app_banner_settings_helper.h" 15 #include "chrome/browser/banners/app_banner_settings_helper.h"
16 #include "chrome/browser/installable/installable_manager.h"
17 #include "chrome/common/chrome_switches.h"
15 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/web_contents.h" 19 #include "content/public/browser/web_contents.h"
17 #include "jni/AddToHomescreenDialogHelper_jni.h" 20 #include "jni/AddToHomescreenProcess_jni.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/gfx/android/java_bitmap.h" 22 #include "ui/gfx/android/java_bitmap.h"
19 23
20 using base::android::JavaParamRef; 24 using base::android::JavaParamRef;
21 using base::android::ScopedJavaLocalRef; 25 using base::android::ScopedJavaLocalRef;
22 26
23 jlong Initialize(JNIEnv* env, 27 jlong InitializeAndStart(JNIEnv* env,
24 const JavaParamRef<jobject>& obj, 28 const JavaParamRef<jobject>& obj,
25 const JavaParamRef<jobject>& java_web_contents) { 29 const JavaParamRef<jobject>& java_web_contents) {
26 content::WebContents* web_contents = 30 content::WebContents* web_contents =
27 content::WebContents::FromJavaWebContents(java_web_contents); 31 content::WebContents::FromJavaWebContents(java_web_contents);
28 AddToHomescreenDialogHelper* add_to_homescreen_helper = 32 AddToHomescreenProcess* process = new AddToHomescreenProcess(env, obj);
29 new AddToHomescreenDialogHelper(env, obj, web_contents); 33 process->Start(web_contents);
30 return reinterpret_cast<intptr_t>(add_to_homescreen_helper); 34 return reinterpret_cast<intptr_t>(process);
31 } 35 }
32 36
33 AddToHomescreenDialogHelper::AddToHomescreenDialogHelper( 37 AddToHomescreenProcess::AddToHomescreenProcess(JNIEnv* env, jobject obj)
34 JNIEnv* env, 38 : add_shortcut_pending_(false), weak_ptr_factory_(this) {
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); 39 java_ref_.Reset(env, obj);
45 } 40 }
46 41
47 AddToHomescreenDialogHelper::~AddToHomescreenDialogHelper() { 42 // static
48 data_fetcher_->set_weak_observer(nullptr); 43 bool AddToHomescreenProcess::Register(JNIEnv* env) {
49 data_fetcher_ = nullptr; 44 return RegisterNativesImpl(env);
50 } 45 }
51 46
52 void AddToHomescreenDialogHelper::OnUserTitleAvailable( 47 void AddToHomescreenProcess::Destroy(JNIEnv* env,
53 const base::string16& user_title) { 48 const JavaParamRef<jobject>& obj) {
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; 49 delete this;
80 } 50 }
81 51
82 SkBitmap AddToHomescreenDialogHelper::FinalizeLauncherIconInBackground( 52 void AddToHomescreenProcess::AddShortcut(
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, 53 JNIEnv* env,
94 const JavaParamRef<jobject>& obj, 54 const JavaParamRef<jobject>& obj,
95 const JavaParamRef<jstring>& j_user_title) { 55 const JavaParamRef<jstring>& j_user_title) {
96 add_shortcut_pending_ = true; 56 add_shortcut_pending_ = true;
97 57
98 base::string16 user_title = 58 base::string16 user_title =
99 base::android::ConvertJavaStringToUTF16(env, j_user_title); 59 base::android::ConvertJavaStringToUTF16(env, j_user_title);
100 if (!user_title.empty()) 60 if (!user_title.empty())
101 data_fetcher_->shortcut_info().user_title = user_title; 61 data_fetcher_->shortcut_info().user_title = user_title;
102 62
103 if (data_fetcher_->is_ready()) { 63 if (data_fetcher_->is_ready()) {
104 // If the fetcher isn't ready yet, the shortcut will be added when it is 64 // If the fetcher isn't ready yet, the shortcut will be added when it is
105 // via OnDataAvailable(); 65 // via OnDataAvailable();
106 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon()); 66 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon());
107 } 67 }
108 } 68 }
109 69
110 void AddToHomescreenDialogHelper::AddShortcut(const ShortcutInfo& info, 70 void AddToHomescreenProcess::Start(content::WebContents* web_contents) {
111 const SkBitmap& icon) { 71 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
72 switches::kEnableWebApk)) {
73 CheckWebApkCompatible(web_contents);
74 return;
75 }
76 ShowDialog();
77 StartFetchingInfoForShortcut(web_contents);
78 }
79
80 AddToHomescreenProcess::~AddToHomescreenProcess() {
81 if (data_fetcher_) {
82 data_fetcher_->set_weak_observer(nullptr);
83 data_fetcher_ = nullptr;
84 }
85 }
86
87 void AddToHomescreenProcess::CheckWebApkCompatible(
88 content::WebContents* web_contents) {
89 InstallableManager::CreateForWebContents(web_contents);
90 InstallableManager* installable_manager =
91 InstallableManager::FromWebContents(web_contents);
92 InstallableParams params;
93 params.check_installable = true;
94 params.fetch_valid_icon = false;
95 installable_manager->GetData(
96 params, base::Bind(&AddToHomescreenProcess::OnGotWebApkCompatibilityData,
97 weak_ptr_factory_.GetWeakPtr(), web_contents));
98 }
99
100 void AddToHomescreenProcess::OnGotWebApkCompatibilityData(
101 content::WebContents* web_contents,
102 const InstallableData& installable_data) {
103 // TODO(pkotwicz): Select whether to use the dialog or not based on
104 // |installable_data.is_installable|.
105 ShowDialog();
106 StartFetchingInfoForShortcut(web_contents);
107 }
108
109 void AddToHomescreenProcess::ShowDialog() {
110 JNIEnv* env = base::android::AttachCurrentThread();
111 Java_AddToHomescreenProcess_showDialog(env, java_ref_.obj());
112 }
113
114 void AddToHomescreenProcess::StartFetchingInfoForShortcut(
115 content::WebContents* web_contents) {
116 data_fetcher_ = new AddToHomescreenDataFetcher(
117 web_contents, ShortcutHelper::GetIdealHomescreenIconSizeInDp(),
118 ShortcutHelper::GetMinimumHomescreenIconSizeInDp(),
119 ShortcutHelper::GetIdealSplashImageSizeInDp(),
120 ShortcutHelper::GetMinimumSplashImageSizeInDp(), this);
121 }
122
123 void AddToHomescreenProcess::AddShortcut(const ShortcutInfo& info,
124 const SkBitmap& icon) {
112 DCHECK(add_shortcut_pending_); 125 DCHECK(add_shortcut_pending_);
113 if (!add_shortcut_pending_) 126 if (!add_shortcut_pending_)
114 return; 127 return;
115 add_shortcut_pending_ = false; 128 add_shortcut_pending_ = false;
116 129
117 content::WebContents* web_contents = data_fetcher_->web_contents(); 130 content::WebContents* web_contents = data_fetcher_->web_contents();
118 if (!web_contents) 131 if (!web_contents)
119 return; 132 return;
120 133
121 RecordAddToHomescreen(); 134 RecordAddToHomescreen();
122 135
123 const std::string& uid = base::GenerateGUID(); 136 const std::string& uid = base::GenerateGUID();
124 ShortcutHelper::AddToLauncherWithSkBitmap( 137 ShortcutHelper::AddToLauncherWithSkBitmap(
125 web_contents->GetBrowserContext(), info, uid, icon, 138 web_contents->GetBrowserContext(), info, uid, icon,
126 data_fetcher_->FetchSplashScreenImageCallback(uid)); 139 data_fetcher_->FetchSplashScreenImageCallback(uid));
127 } 140 }
128 141
129 bool AddToHomescreenDialogHelper::RegisterAddToHomescreenDialogHelper( 142 void AddToHomescreenProcess::RecordAddToHomescreen() {
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 143 // Record that the shortcut has been added, so no banners will be shown
136 // for this app. 144 // for this app.
137 content::WebContents* web_contents = data_fetcher_->web_contents(); 145 content::WebContents* web_contents = data_fetcher_->web_contents();
138 if (!web_contents) 146 if (!web_contents)
139 return; 147 return;
140 148
141 AppBannerSettingsHelper::RecordBannerEvent( 149 AppBannerSettingsHelper::RecordBannerEvent(
142 web_contents, web_contents->GetURL(), 150 web_contents, web_contents->GetURL(),
143 data_fetcher_->shortcut_info().url.spec(), 151 data_fetcher_->shortcut_info().url.spec(),
144 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, 152 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN,
145 base::Time::Now()); 153 base::Time::Now());
146 } 154 }
155
156 void AddToHomescreenProcess::OnUserTitleAvailable(
157 const base::string16& user_title) {
158 JNIEnv* env = base::android::AttachCurrentThread();
159 ScopedJavaLocalRef<jstring> j_user_title =
160 base::android::ConvertUTF16ToJavaString(env, user_title);
161 Java_AddToHomescreenProcess_onUserTitleAvailable(env,
162 java_ref_.obj(),
Xi Han 2016/08/19 14:11:06 It seems ".obj()" has been removed in my recently
163 j_user_title.obj());
164 }
165
166 void AddToHomescreenProcess::OnDataAvailable(const ShortcutInfo& info,
167 const SkBitmap& icon) {
Xi Han 2016/08/19 14:11:06 The indent of "const SkBitmap& icon" is wrong, ple
168 JNIEnv* env = base::android::AttachCurrentThread();
169 ScopedJavaLocalRef<jobject> java_bitmap;
170 if (icon.getSize())
171 java_bitmap = gfx::ConvertToJavaBitmap(&icon);
172
173 Java_AddToHomescreenProcess_onReadyToAdd(env, java_ref_.obj(),
174 java_bitmap.obj());
175
176 if (add_shortcut_pending_)
177 AddShortcut(info, icon);
178 }
179
180 SkBitmap AddToHomescreenProcess::FinalizeLauncherIconInBackground(
181 const SkBitmap& bitmap,
182 const GURL& url,
183 bool* is_generated) {
184 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
185
186 return ShortcutHelper::FinalizeLauncherIconInBackground(bitmap, url,
187 is_generated);
188 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698