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::ConvertUTF8ToJavaString; | 42 using base::android::ConvertUTF8ToJavaString; |
39 using base::android::ConvertUTF16ToJavaString; | 43 using base::android::ConvertUTF16ToJavaString; |
40 | 44 |
41 namespace { | 45 namespace { |
42 const char kBannerTag[] = "google-play-id"; | 46 const char kBannerTag[] = "google-play-id"; |
43 } | 47 } |
44 | 48 |
45 namespace banners { | 49 namespace banners { |
46 | 50 |
| 51 // The AppBannerManager::IOThreadHandler class is used to hold onto the |
| 52 // weak pointer to the AppBannerManager. It catches the callbacks on the |
| 53 // IO thread, and forwards on the calls to the UI thread. This is required |
| 54 // as the weak pointer can only be dereferenced on the UI thread. |
| 55 class AppBannerManager::IOThreadHandler |
| 56 : public base::RefCounted<AppBannerManager::IOThreadHandler> { |
| 57 public: |
| 58 explicit IOThreadHandler( |
| 59 const base::WeakPtr<AppBannerManager>& banner_manager) |
| 60 : banner_manager_(banner_manager) {} |
| 61 |
| 62 void OnDidCheckHasSameServiceWorkerOnIOThread(bool has_same) { |
| 63 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 64 content::BrowserThread::PostTask( |
| 65 content::BrowserThread::UI, FROM_HERE, |
| 66 base::Bind(&AppBannerManager::OnDidCheckHasSameServiceWorker, |
| 67 banner_manager_, has_same)); |
| 68 } |
| 69 |
| 70 private: |
| 71 base::WeakPtr<AppBannerManager> banner_manager_; |
| 72 }; |
| 73 |
47 AppBannerManager::AppBannerManager(JNIEnv* env, jobject obj) | 74 AppBannerManager::AppBannerManager(JNIEnv* env, jobject obj) |
48 : weak_java_banner_view_manager_(env, obj) {} | 75 : weak_java_banner_view_manager_(env, obj), weak_factory_(this) { |
| 76 io_thread_handler_ = |
| 77 make_scoped_refptr(new IOThreadHandler(weak_factory_.GetWeakPtr())); |
| 78 } |
49 | 79 |
50 AppBannerManager::~AppBannerManager() { | 80 AppBannerManager::~AppBannerManager() { |
51 } | 81 } |
52 | 82 |
53 void AppBannerManager::Destroy(JNIEnv* env, jobject obj) { | 83 void AppBannerManager::Destroy(JNIEnv* env, jobject obj) { |
54 delete this; | 84 delete this; |
55 } | 85 } |
56 | 86 |
57 void AppBannerManager::BlockBanner(JNIEnv* env, | 87 void AppBannerManager::BlockBanner(JNIEnv* env, |
58 jobject obj, | 88 jobject obj, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 if (manifest.IsEmpty() | 163 if (manifest.IsEmpty() |
134 || !manifest.start_url.is_valid() | 164 || !manifest.start_url.is_valid() |
135 || (manifest.name.is_null() && manifest.short_name.is_null())) { | 165 || (manifest.name.is_null() && manifest.short_name.is_null())) { |
136 // No usable manifest, see if there is a play store meta tag. | 166 // No usable manifest, see if there is a play store meta tag. |
137 Send(new ChromeViewMsg_RetrieveMetaTagContent(routing_id(), | 167 Send(new ChromeViewMsg_RetrieveMetaTagContent(routing_id(), |
138 validated_url_, | 168 validated_url_, |
139 kBannerTag)); | 169 kBannerTag)); |
140 return; | 170 return; |
141 } | 171 } |
142 | 172 |
143 // TODO(benwells): Check triggering parameters here and if there is a meta | |
144 // tag. | |
145 | |
146 // Create an infobar to promote the manifest's app. | |
147 manifest_ = manifest; | 173 manifest_ = manifest; |
148 | 174 |
149 GURL icon_url = | 175 // Check to see if there is a single service worker controlling this page |
150 ManifestIconSelector::FindBestMatchingIcon( | 176 // and the manifest's start url. |
151 manifest.icons, | 177 Profile* profile = |
152 GetPreferredIconSize(), | 178 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
153 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); | 179 content::StoragePartition* storage_partition = |
154 if (icon_url.is_empty()) | 180 content::BrowserContext::GetStoragePartition( |
155 return; | 181 profile, web_contents()->GetSiteInstance()); |
| 182 DCHECK(storage_partition); |
156 | 183 |
157 FetchIcon(icon_url); | 184 storage_partition->GetServiceWorkerContext()->CheckHasSameServiceWorker( |
| 185 validated_url_, manifest.start_url, |
| 186 base::Bind(&AppBannerManager::IOThreadHandler:: |
| 187 OnDidCheckHasSameServiceWorkerOnIOThread, |
| 188 io_thread_handler_)); |
| 189 } |
| 190 |
| 191 void AppBannerManager::OnDidCheckHasSameServiceWorker(bool has_same) { |
| 192 if (has_same) { |
| 193 // TODO(benwells): Check triggering parameters. |
| 194 GURL icon_url = |
| 195 ManifestIconSelector::FindBestMatchingIcon( |
| 196 manifest_.icons, |
| 197 GetPreferredIconSize(), |
| 198 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); |
| 199 if (icon_url.is_empty()) |
| 200 return; |
| 201 |
| 202 FetchIcon(icon_url); |
| 203 } |
158 } | 204 } |
159 | 205 |
160 bool AppBannerManager::OnMessageReceived(const IPC::Message& message) { | 206 bool AppBannerManager::OnMessageReceived(const IPC::Message& message) { |
161 bool handled = true; | 207 bool handled = true; |
162 IPC_BEGIN_MESSAGE_MAP(AppBannerManager, message) | 208 IPC_BEGIN_MESSAGE_MAP(AppBannerManager, message) |
163 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidRetrieveMetaTagContent, | 209 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidRetrieveMetaTagContent, |
164 OnDidRetrieveMetaTagContent) | 210 OnDidRetrieveMetaTagContent) |
165 IPC_MESSAGE_UNHANDLED(handled = false) | 211 IPC_MESSAGE_UNHANDLED(handled = false) |
166 IPC_END_MESSAGE_MAP() | 212 IPC_END_MESSAGE_MAP() |
167 return handled; | 213 return handled; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 348 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
303 switches::kEnableAppInstallAlerts); | 349 switches::kEnableAppInstallAlerts); |
304 } | 350 } |
305 | 351 |
306 // Register native methods | 352 // Register native methods |
307 bool RegisterAppBannerManager(JNIEnv* env) { | 353 bool RegisterAppBannerManager(JNIEnv* env) { |
308 return RegisterNativesImpl(env); | 354 return RegisterNativesImpl(env); |
309 } | 355 } |
310 | 356 |
311 } // namespace banners | 357 } // namespace banners |
OLD | NEW |