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

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

Issue 2393513004: Convert app banners to use Mojo. (Closed)
Patch Set: Fix Win clang compile Created 4 years, 2 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/banners/app_banner_manager.h" 5 #include "chrome/browser/banners/app_banner_manager.h"
6 6
7 #include <utility>
8
7 #include "base/bind.h" 9 #include "base/bind.h"
8 #include "base/callback.h" 10 #include "base/callback.h"
9 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/lazy_instance.h"
13 #include "base/memory/ptr_util.h"
10 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
11 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "build/build_config.h"
12 #include "chrome/browser/banners/app_banner_metrics.h" 17 #include "chrome/browser/banners/app_banner_metrics.h"
13 #include "chrome/browser/banners/app_banner_settings_helper.h" 18 #include "chrome/browser/banners/app_banner_settings_helper.h"
14 #include "chrome/browser/browser_process.h" 19 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/engagement/site_engagement_service.h" 20 #include "chrome/browser/engagement/site_engagement_service.h"
16 #include "chrome/browser/installable/installable_logging.h"
17 #include "chrome/browser/installable/installable_manager.h"
18 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/chrome_switches.h" 22 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/render_messages.h"
21 #include "components/rappor/rappor_utils.h" 23 #include "components/rappor/rappor_utils.h"
22 #include "content/public/browser/navigation_handle.h" 24 #include "content/public/browser/navigation_handle.h"
23 #include "content/public/browser/render_frame_host.h" 25 #include "content/public/browser/render_frame_host.h"
24 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/origin_util.h" 27 #include "content/public/common/origin_util.h"
26 #include "third_party/WebKit/public/platform/modules/app_banner/WebAppBannerProm ptReply.h" 28 #include "mojo/public/cpp/bindings/interface_request.h"
29 #include "services/shell/public/cpp/interface_provider.h"
27 #include "third_party/skia/include/core/SkBitmap.h" 30 #include "third_party/skia/include/core/SkBitmap.h"
28 #include "ui/display/display.h" 31 #include "ui/display/display.h"
29 #include "ui/display/screen.h" 32 #include "ui/display/screen.h"
30 33
34 #if defined(OS_ANDROID)
35 #include "chrome/browser/android/banners/app_banner_manager_android.h"
36 #else
37 #include "chrome/browser/banners/app_banner_manager_desktop.h"
38 #endif
39
31 namespace { 40 namespace {
32 41
33 bool gDisableSecureCheckForTesting = false; 42 bool gDisableSecureCheckForTesting = false;
34 int gCurrentRequestID = -1; 43 int gCurrentRequestID = -1;
35 base::LazyInstance<base::TimeDelta> gTimeDeltaForTesting = 44 base::LazyInstance<base::TimeDelta> gTimeDeltaForTesting =
36 LAZY_INSTANCE_INITIALIZER; 45 LAZY_INSTANCE_INITIALIZER;
37 46
38 // Returns |size_in_px| in dp, i.e. divided by the current device scale factor. 47 // Returns |size_in_px| in dp, i.e. divided by the current device scale factor.
39 int ConvertIconSizeFromPxToDp(int size_in_px) { 48 int ConvertIconSizeFromPxToDp(int size_in_px) {
40 return size_in_px / 49 return size_in_px /
(...skipping 15 matching lines...) Expand all
56 params.fetch_valid_icon = true; 65 params.fetch_valid_icon = true;
57 66
58 return params; 67 return params;
59 } 68 }
60 69
61 } // anonymous namespace 70 } // anonymous namespace
62 71
63 namespace banners { 72 namespace banners {
64 73
65 // static 74 // static
75 void AppBannerManager::Create(content::WebContents* web_contents) {
76 #if defined(OS_ANDROID)
77 return AppBannerManagerAndroid::CreateForWebContents(web_contents);
78 #else
79 return AppBannerManagerDesktop::CreateForWebContents(web_contents);
80 #endif
81 }
82
83 // static
84 AppBannerManager* AppBannerManager::Get(content::WebContents* web_contents) {
85 #if defined(OS_ANDROID)
86 return AppBannerManagerAndroid::FromWebContents(web_contents);
87 #else
88 return AppBannerManagerDesktop::FromWebContents(web_contents);
89 #endif
90 }
91
92 // static
93 void AppBannerManager::BindToMojoRequest(
94 content::RenderFrameHost* render_frame_host,
95 blink::mojom::AppBannerServiceRequest request) {
96 content::WebContents* web_contents =
97 content::WebContents::FromRenderFrameHost(render_frame_host);
98 DCHECK(web_contents);
99
100 AppBannerManager* manager = AppBannerManager::Get(web_contents);
101 DCHECK(manager);
102
103 manager->binding_.reset(new mojo::Binding<blink::mojom::AppBannerService>(
104 manager, std::move(request)));
105 }
106
107 // static
66 void AppBannerManager::DisableSecureSchemeCheckForTesting() { 108 void AppBannerManager::DisableSecureSchemeCheckForTesting() {
67 gDisableSecureCheckForTesting = true; 109 gDisableSecureCheckForTesting = true;
68 } 110 }
69 111
70 // static 112 // static
71 base::Time AppBannerManager::GetCurrentTime() { 113 base::Time AppBannerManager::GetCurrentTime() {
72 return base::Time::Now() + gTimeDeltaForTesting.Get(); 114 return base::Time::Now() + gTimeDeltaForTesting.Get();
73 } 115 }
74 116
75 // static 117 // static
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 ReportStatus(contents, NOT_FROM_SECURE_ORIGIN); 166 ReportStatus(contents, NOT_FROM_SECURE_ORIGIN);
125 Stop(); 167 Stop();
126 return; 168 return;
127 } 169 }
128 170
129 manager_->GetData( 171 manager_->GetData(
130 ParamsToGetManifest(), 172 ParamsToGetManifest(),
131 base::Bind(&AppBannerManager::OnDidGetManifest, GetWeakPtr())); 173 base::Bind(&AppBannerManager::OnDidGetManifest, GetWeakPtr()));
132 } 174 }
133 175
176 void AppBannerManager::SendBannerAccepted(int request_id) {
177 if (request_id != gCurrentRequestID)
178 return;
179
180 DCHECK(event_->is_bound());
181 (*event_)->BannerAccepted(request_id, GetBannerType());
182 }
183
184 void AppBannerManager::SendBannerDismissed(int request_id) {
185 if (request_id != gCurrentRequestID)
186 return;
187
188 DCHECK(event_->is_bound());
189 (*event_)->BannerDismissed(request_id);
190 }
191
192 void AppBannerManager::SetEvent(blink::mojom::AppBannerEventPtr event) {
193 event_.reset(new blink::mojom::AppBannerEventPtr(std::move(event)));
194 }
195
134 base::Closure AppBannerManager::FetchWebappSplashScreenImageCallback( 196 base::Closure AppBannerManager::FetchWebappSplashScreenImageCallback(
135 const std::string& webapp_id) { 197 const std::string& webapp_id) {
136 return base::Closure(); 198 return base::Closure();
137 } 199 }
138 200
139 AppBannerManager::AppBannerManager(content::WebContents* web_contents) 201 AppBannerManager::AppBannerManager(content::WebContents* web_contents)
140 : content::WebContentsObserver(web_contents), 202 : content::WebContentsObserver(web_contents),
141 SiteEngagementObserver(nullptr), 203 SiteEngagementObserver(nullptr),
142 manager_(nullptr), 204 manager_(nullptr),
143 event_request_id_(-1), 205 event_request_id_(-1),
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 380
319 // Given all of the other checks that have been made, the only possible reason 381 // Given all of the other checks that have been made, the only possible reason
320 // for stopping now is that the app has been added to the homescreen. 382 // for stopping now is that the app has been added to the homescreen.
321 if (!IsDebugMode() && !CheckIfShouldShowBanner()) { 383 if (!IsDebugMode() && !CheckIfShouldShowBanner()) {
322 Stop(); 384 Stop();
323 return; 385 return;
324 } 386 }
325 387
326 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_CREATED); 388 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_CREATED);
327 event_request_id_ = ++gCurrentRequestID; 389 event_request_id_ = ++gCurrentRequestID;
328 content::RenderFrameHost* frame = web_contents()->GetMainFrame(); 390
329 frame->Send(new ChromeViewMsg_AppBannerPromptRequest( 391 web_contents()->GetMainFrame()->GetRemoteInterfaces()->GetInterface(
330 frame->GetRoutingID(), event_request_id_, GetBannerType())); 392 mojo::GetProxy(&banner_client_));
393
394 banner_client_->BannerPromptRequest(
395 event_request_id_, GetBannerType(),
396 base::Bind(&AppBannerManager::OnBannerPromptReply, GetWeakPtr()));
331 } 397 }
332 398
333 void AppBannerManager::DidStartNavigation(content::NavigationHandle* handle) { 399 void AppBannerManager::DidStartNavigation(content::NavigationHandle* handle) {
334 if (!handle->IsInMainFrame()) 400 if (!handle->IsInMainFrame())
335 return; 401 return;
336 402
337 load_finished_ = false; 403 load_finished_ = false;
338 if (AppBannerSettingsHelper::ShouldUseSiteEngagementScore() && 404 if (AppBannerSettingsHelper::ShouldUseSiteEngagementScore() &&
339 GetSiteEngagementService() == nullptr) { 405 GetSiteEngagementService() == nullptr) {
340 // Ensure that we are observing the site engagement service on navigation 406 // Ensure that we are observing the site engagement service on navigation
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 } 515 }
450 516
451 // If we are in debug mode, AppBannerSettingsHelper::ShouldShowBanner must 517 // If we are in debug mode, AppBannerSettingsHelper::ShouldShowBanner must
452 // return NO_ERROR_DETECTED (bypass flag is set) or we must not have entered 518 // return NO_ERROR_DETECTED (bypass flag is set) or we must not have entered
453 // this method. 519 // this method.
454 DCHECK(!IsDebugMode()); 520 DCHECK(!IsDebugMode());
455 ReportStatus(web_contents(), code); 521 ReportStatus(web_contents(), code);
456 return false; 522 return false;
457 } 523 }
458 524
459 bool AppBannerManager::OnMessageReceived(
460 const IPC::Message& message,
461 content::RenderFrameHost* render_frame_host) {
462 bool handled = true;
463
464 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(AppBannerManager, message, render_frame_host)
465 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_AppBannerPromptReply,
466 OnBannerPromptReply)
467 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_RequestShowAppBanner,
468 OnRequestShowAppBanner)
469 IPC_MESSAGE_UNHANDLED(handled = false)
470 IPC_END_MESSAGE_MAP()
471
472 return handled;
473 }
474
475 void AppBannerManager::OnBannerPromptReply( 525 void AppBannerManager::OnBannerPromptReply(
476 content::RenderFrameHost* render_frame_host,
477 int request_id, 526 int request_id,
478 blink::WebAppBannerPromptReply reply, 527 blink::mojom::AppBannerPromptReply reply,
479 std::string referrer) { 528 mojo::String referrer) {
Sam McNally 2016/10/06 07:17:43 const std::string&
dominickn 2016/10/06 22:58:12 Done.
480 content::WebContents* contents = web_contents(); 529 content::WebContents* contents = web_contents();
481 if (request_id != event_request_id_) 530 if (request_id != event_request_id_)
482 return; 531 return;
483 532
484 // The renderer might have requested the prompt to be canceled. 533 // The renderer might have requested the prompt to be canceled.
485 // They may request that it is redisplayed later, so don't Stop() here. 534 // They may request that it is redisplayed later, so don't Stop() here.
486 // However, log that the cancelation was requested, so Stop() can be 535 // However, log that the cancelation was requested, so Stop() can be
487 // called if a redisplay isn't asked for. 536 // called if a redisplay isn't asked for.
488 // 537 //
489 // We use the additional page_requested_prompt_ variable because the redisplay 538 // We use the additional page_requested_prompt_ variable because the redisplay
490 // request may be received *before* the Cancel prompt reply (e.g. if redisplay 539 // request may be received *before* the Cancel prompt reply (e.g. if redisplay
491 // is requested in the beforeinstallprompt event handler). 540 // is requested in the beforeinstallprompt event handler).
492 referrer_ = referrer; 541 referrer_ = referrer.get();
493 if (reply == blink::WebAppBannerPromptReply::Cancel && 542 if (reply == blink::mojom::AppBannerPromptReply::CANCEL &&
494 !page_requested_prompt_) { 543 !page_requested_prompt_) {
495 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_PREVENT_DEFAULT_CALLED); 544 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_PREVENT_DEFAULT_CALLED);
496 was_canceled_by_page_ = true; 545 was_canceled_by_page_ = true;
497 return; 546 return;
498 } 547 }
499 548
500 // If we haven't yet returned, but either of |was_canceled_by_page_| or 549 // If we haven't yet returned, but either of |was_canceled_by_page_| or
501 // |page_requested_prompt_| is true, the page has requested a delayed showing 550 // |page_requested_prompt_| is true, the page has requested a delayed showing
502 // of the prompt. Otherwise, the prompt was never canceled by the page. 551 // of the prompt. Otherwise, the prompt was never canceled by the page.
503 if (was_canceled_by_page_ || page_requested_prompt_) { 552 if (was_canceled_by_page_ || page_requested_prompt_) {
(...skipping 10 matching lines...) Expand all
514 DCHECK(!manifest_url_.is_empty()); 563 DCHECK(!manifest_url_.is_empty());
515 DCHECK(!manifest_.IsEmpty()); 564 DCHECK(!manifest_.IsEmpty());
516 DCHECK(!icon_url_.is_empty()); 565 DCHECK(!icon_url_.is_empty());
517 DCHECK(icon_.get()); 566 DCHECK(icon_.get());
518 567
519 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_COMPLETE); 568 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_COMPLETE);
520 ShowBanner(); 569 ShowBanner();
521 is_active_ = false; 570 is_active_ = false;
522 } 571 }
523 572
524 void AppBannerManager::OnRequestShowAppBanner( 573 void AppBannerManager::DisplayAppBanner(int request_id) {
525 content::RenderFrameHost* render_frame_host,
526 int request_id) {
527 if (was_canceled_by_page_) { 574 if (was_canceled_by_page_) {
528 // Simulate a non-canceled OnBannerPromptReply to show the delayed banner. 575 // Simulate a non-canceled OnBannerPromptReply to show the delayed banner.
529 // Don't reset |was_canceled_by_page_| yet for metrics purposes. 576 // Don't reset |was_canceled_by_page_| yet for metrics purposes.
530 OnBannerPromptReply(render_frame_host, request_id, 577 OnBannerPromptReply(request_id, blink::mojom::AppBannerPromptReply::NONE,
531 blink::WebAppBannerPromptReply::None, referrer_); 578 referrer_);
532 } else { 579 } else {
533 // Log that the prompt request was made for when we get the prompt reply. 580 // Log that the prompt request was made for when we get the prompt reply.
534 page_requested_prompt_ = true; 581 page_requested_prompt_ = true;
535 } 582 }
536 } 583 }
537 584
538 } // namespace banners 585 } // namespace banners
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698