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

Side by Side Diff: chrome/browser/android/banners/app_banner_manager.cc

Issue 893793005: Check if there is a SW controlling the start page for app banner. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Android compile Created 5 years, 10 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 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 DISALLOW_COPY_AND_ASSIGN(IOThreadHandler);
74 };
75
47 AppBannerManager::AppBannerManager(JNIEnv* env, jobject obj) 76 AppBannerManager::AppBannerManager(JNIEnv* env, jobject obj)
48 : weak_java_banner_view_manager_(env, obj) {} 77 : weak_java_banner_view_manager_(env, obj), weak_factory_(this) {
78 io_thread_handler_ =
79 make_scoped_refptr(new IOThreadHandler(weak_factory_.GetWeakPtr()));
80 }
49 81
50 AppBannerManager::~AppBannerManager() { 82 AppBannerManager::~AppBannerManager() {
51 } 83 }
52 84
53 void AppBannerManager::Destroy(JNIEnv* env, jobject obj) { 85 void AppBannerManager::Destroy(JNIEnv* env, jobject obj) {
54 delete this; 86 delete this;
55 } 87 }
56 88
57 void AppBannerManager::BlockBanner(JNIEnv* env, 89 void AppBannerManager::BlockBanner(JNIEnv* env,
58 jobject obj, 90 jobject obj,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 if (manifest.IsEmpty() 165 if (manifest.IsEmpty()
134 || !manifest.start_url.is_valid() 166 || !manifest.start_url.is_valid()
135 || (manifest.name.is_null() && manifest.short_name.is_null())) { 167 || (manifest.name.is_null() && manifest.short_name.is_null())) {
136 // No usable manifest, see if there is a play store meta tag. 168 // No usable manifest, see if there is a play store meta tag.
137 Send(new ChromeViewMsg_RetrieveMetaTagContent(routing_id(), 169 Send(new ChromeViewMsg_RetrieveMetaTagContent(routing_id(),
138 validated_url_, 170 validated_url_,
139 kBannerTag)); 171 kBannerTag));
140 return; 172 return;
141 } 173 }
142 174
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; 175 manifest_ = manifest;
148 176
149 GURL icon_url = 177 // Check to see if there is a single service worker controlling this page
150 ManifestIconSelector::FindBestMatchingIcon( 178 // and the manifest's start url.
151 manifest.icons, 179 Profile* profile =
152 GetPreferredIconSize(), 180 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
153 gfx::Screen::GetScreenFor(web_contents()->GetNativeView())); 181 content::StoragePartition* storage_partition =
154 if (icon_url.is_empty()) 182 content::BrowserContext::GetStoragePartition(
155 return; 183 profile, web_contents()->GetSiteInstance());
184 DCHECK(storage_partition);
156 185
157 FetchIcon(icon_url); 186 storage_partition->GetServiceWorkerContext()->CheckHasSameServiceWorker(
187 validated_url_, manifest.start_url,
188 base::Bind(&AppBannerManager::IOThreadHandler::
189 OnDidCheckHasSameServiceWorkerOnIOThread,
190 io_thread_handler_));
191 }
192
193 void AppBannerManager::OnDidCheckHasSameServiceWorker(bool has_same) {
194 if (has_same) {
195 // TODO(benwells): Check triggering parameters.
196 GURL icon_url =
197 ManifestIconSelector::FindBestMatchingIcon(
198 manifest_.icons,
199 GetPreferredIconSize(),
200 gfx::Screen::GetScreenFor(web_contents()->GetNativeView()));
201 if (icon_url.is_empty())
202 return;
203
204 FetchIcon(icon_url);
205 }
158 } 206 }
159 207
160 bool AppBannerManager::OnMessageReceived(const IPC::Message& message) { 208 bool AppBannerManager::OnMessageReceived(const IPC::Message& message) {
161 bool handled = true; 209 bool handled = true;
162 IPC_BEGIN_MESSAGE_MAP(AppBannerManager, message) 210 IPC_BEGIN_MESSAGE_MAP(AppBannerManager, message)
163 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidRetrieveMetaTagContent, 211 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidRetrieveMetaTagContent,
164 OnDidRetrieveMetaTagContent) 212 OnDidRetrieveMetaTagContent)
165 IPC_MESSAGE_UNHANDLED(handled = false) 213 IPC_MESSAGE_UNHANDLED(handled = false)
166 IPC_END_MESSAGE_MAP() 214 IPC_END_MESSAGE_MAP()
167 return handled; 215 return handled;
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 return base::CommandLine::ForCurrentProcess()->HasSwitch( 350 return base::CommandLine::ForCurrentProcess()->HasSwitch(
303 switches::kEnableAppInstallAlerts); 351 switches::kEnableAppInstallAlerts);
304 } 352 }
305 353
306 // Register native methods 354 // Register native methods
307 bool RegisterAppBannerManager(JNIEnv* env) { 355 bool RegisterAppBannerManager(JNIEnv* env) {
308 return RegisterNativesImpl(env); 356 return RegisterNativesImpl(env);
309 } 357 }
310 358
311 } // namespace banners 359 } // namespace banners
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698