OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/banners/app_banner_manager.h" | 5 #include "chrome/browser/android/banners/app_banner_manager.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/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/threading/worker_pool.h" | 12 #include "base/threading/worker_pool.h" |
13 #include "chrome/browser/android/banners/app_banner_infobar_delegate.h" | 13 #include "chrome/browser/android/banners/app_banner_infobar_delegate.h" |
14 #include "chrome/browser/android/banners/app_banner_metrics_ids.h" | 14 #include "chrome/browser/android/banners/app_banner_metrics_ids.h" |
15 #include "chrome/browser/android/banners/app_banner_utilities.h" | 15 #include "chrome/browser/android/banners/app_banner_utilities.h" |
16 #include "chrome/browser/android/manifest_icon_selector.h" | 16 #include "chrome/browser/android/manifest_icon_selector.h" |
17 #include "chrome/browser/android/shortcut_helper.h" | 17 #include "chrome/browser/android/shortcut_helper.h" |
18 #include "chrome/browser/android/shortcut_info.h" | 18 #include "chrome/browser/android/shortcut_info.h" |
19 #include "chrome/browser/banners/app_banner_settings_helper.h" | 19 #include "chrome/browser/banners/app_banner_settings_helper.h" |
20 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" | 20 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" |
21 #include "chrome/browser/infobars/infobar_service.h" | 21 #include "chrome/browser/infobars/infobar_service.h" |
22 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/common/chrome_constants.h" | 23 #include "chrome/common/chrome_constants.h" |
24 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
25 #include "chrome/common/render_messages.h" | 25 #include "chrome/common/render_messages.h" |
26 #include "content/public/browser/android/content_view_core.h" | 26 #include "content/public/browser/android/content_view_core.h" |
| 27 #include "content/public/browser/browser_context.h" |
| 28 #include "content/public/browser/browser_thread.h" |
27 #include "content/public/browser/navigation_details.h" | 29 #include "content/public/browser/navigation_details.h" |
28 #include "content/public/browser/render_frame_host.h" | 30 #include "content/public/browser/render_frame_host.h" |
| 31 #include "content/public/browser/service_worker_context.h" |
| 32 #include "content/public/browser/storage_partition.h" |
29 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
30 #include "content/public/common/frame_navigate_params.h" | 34 #include "content/public/common/frame_navigate_params.h" |
31 #include "content/public/common/manifest.h" | 35 #include "content/public/common/manifest.h" |
32 #include "jni/AppBannerManager_jni.h" | 36 #include "jni/AppBannerManager_jni.h" |
33 #include "net/base/load_flags.h" | 37 #include "net/base/load_flags.h" |
34 #include "ui/gfx/android/java_bitmap.h" | 38 #include "ui/gfx/android/java_bitmap.h" |
35 #include "ui/gfx/screen.h" | 39 #include "ui/gfx/screen.h" |
36 | 40 |
37 using base::android::ConvertJavaStringToUTF8; | 41 using base::android::ConvertJavaStringToUTF8; |
38 using base::android::ConvertJavaStringToUTF16; | 42 using base::android::ConvertJavaStringToUTF16; |
39 using base::android::ConvertUTF8ToJavaString; | 43 using base::android::ConvertUTF8ToJavaString; |
40 using base::android::ConvertUTF16ToJavaString; | 44 using base::android::ConvertUTF16ToJavaString; |
41 | 45 |
42 namespace { | 46 namespace { |
43 const char kBannerTag[] = "google-play-id"; | 47 const char kBannerTag[] = "google-play-id"; |
44 } | 48 } |
45 | 49 |
46 namespace banners { | 50 namespace banners { |
47 | 51 |
48 AppBannerManager::AppBannerManager(JNIEnv* env, jobject obj) | 52 AppBannerManager::AppBannerManager(JNIEnv* env, jobject obj) |
49 : weak_java_banner_view_manager_(env, obj) {} | 53 : weak_java_banner_view_manager_(env, obj), weak_factory_(this) { |
| 54 } |
50 | 55 |
51 AppBannerManager::~AppBannerManager() { | 56 AppBannerManager::~AppBannerManager() { |
52 } | 57 } |
53 | 58 |
54 void AppBannerManager::Destroy(JNIEnv* env, jobject obj) { | 59 void AppBannerManager::Destroy(JNIEnv* env, jobject obj) { |
55 delete this; | 60 delete this; |
56 } | 61 } |
57 | 62 |
58 void AppBannerManager::BlockBanner(JNIEnv* env, | 63 void AppBannerManager::BlockBanner(JNIEnv* env, |
59 jobject obj, | 64 jobject obj, |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 if (manifest.IsEmpty() | 146 if (manifest.IsEmpty() |
142 || !manifest.start_url.is_valid() | 147 || !manifest.start_url.is_valid() |
143 || (manifest.name.is_null() && manifest.short_name.is_null())) { | 148 || (manifest.name.is_null() && manifest.short_name.is_null())) { |
144 // No usable manifest, see if there is a play store meta tag. | 149 // No usable manifest, see if there is a play store meta tag. |
145 Send(new ChromeViewMsg_RetrieveMetaTagContent(routing_id(), | 150 Send(new ChromeViewMsg_RetrieveMetaTagContent(routing_id(), |
146 validated_url_, | 151 validated_url_, |
147 kBannerTag)); | 152 kBannerTag)); |
148 return; | 153 return; |
149 } | 154 } |
150 | 155 |
151 // TODO(benwells): Check triggering parameters here and if there is a meta | |
152 // tag. | |
153 | |
154 // Create an infobar to promote the manifest's app. | |
155 web_app_data_ = manifest; | 156 web_app_data_ = manifest; |
156 app_title_ = web_app_data_.name.string(); | 157 app_title_ = web_app_data_.name.string(); |
157 GURL icon_url = | |
158 ManifestIconSelector::FindBestMatchingIcon( | |
159 manifest.icons, | |
160 GetPreferredIconSize(), | |
161 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); | |
162 if (icon_url.is_empty()) | |
163 return; | |
164 | 158 |
165 FetchIcon(icon_url); | 159 // Check to see if there is a single service worker controlling this page |
| 160 // and the manifest's start url. |
| 161 Profile* profile = |
| 162 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 163 content::StoragePartition* storage_partition = |
| 164 content::BrowserContext::GetStoragePartition( |
| 165 profile, web_contents()->GetSiteInstance()); |
| 166 DCHECK(storage_partition); |
| 167 |
| 168 storage_partition->GetServiceWorkerContext()->CheckHasServiceWorker( |
| 169 validated_url_, manifest.start_url, |
| 170 base::Bind(&AppBannerManager::OnDidCheckHasServiceWorker, |
| 171 weak_factory_.GetWeakPtr())); |
| 172 } |
| 173 |
| 174 void AppBannerManager::OnDidCheckHasServiceWorker(bool has_service_worker) { |
| 175 if (has_service_worker) { |
| 176 // TODO(benwells): Check triggering parameters. |
| 177 // Create an infobar to promote the manifest's app. |
| 178 GURL icon_url = |
| 179 ManifestIconSelector::FindBestMatchingIcon( |
| 180 web_app_data_.icons, |
| 181 GetPreferredIconSize(), |
| 182 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); |
| 183 if (icon_url.is_empty()) |
| 184 return; |
| 185 |
| 186 FetchIcon(icon_url); |
| 187 } |
166 } | 188 } |
167 | 189 |
168 bool AppBannerManager::OnMessageReceived(const IPC::Message& message) { | 190 bool AppBannerManager::OnMessageReceived(const IPC::Message& message) { |
169 bool handled = true; | 191 bool handled = true; |
170 IPC_BEGIN_MESSAGE_MAP(AppBannerManager, message) | 192 IPC_BEGIN_MESSAGE_MAP(AppBannerManager, message) |
171 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidRetrieveMetaTagContent, | 193 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidRetrieveMetaTagContent, |
172 OnDidRetrieveMetaTagContent) | 194 OnDidRetrieveMetaTagContent) |
173 IPC_MESSAGE_UNHANDLED(handled = false) | 195 IPC_MESSAGE_UNHANDLED(handled = false) |
174 IPC_END_MESSAGE_MAP() | 196 IPC_END_MESSAGE_MAP() |
175 return handled; | 197 return handled; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 323 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
302 switches::kEnableAppInstallAlerts); | 324 switches::kEnableAppInstallAlerts); |
303 } | 325 } |
304 | 326 |
305 // Register native methods | 327 // Register native methods |
306 bool RegisterAppBannerManager(JNIEnv* env) { | 328 bool RegisterAppBannerManager(JNIEnv* env) { |
307 return RegisterNativesImpl(env); | 329 return RegisterNativesImpl(env); |
308 } | 330 } |
309 | 331 |
310 } // namespace banners | 332 } // namespace banners |
OLD | NEW |