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

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

Issue 2393513004: Convert app banners to use Mojo. (Closed)
Patch Set: Rebase. init() can cause frames to detach 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 <algorithm>
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"
10 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
11 #include "base/time/time.h" 13 #include "base/time/time.h"
12 #include "chrome/browser/banners/app_banner_metrics.h" 14 #include "chrome/browser/banners/app_banner_metrics.h"
13 #include "chrome/browser/banners/app_banner_settings_helper.h" 15 #include "chrome/browser/banners/app_banner_settings_helper.h"
14 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/engagement/site_engagement_service.h" 17 #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" 18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/chrome_switches.h" 19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/render_messages.h"
21 #include "components/rappor/rappor_utils.h" 20 #include "components/rappor/rappor_utils.h"
22 #include "content/public/browser/navigation_handle.h" 21 #include "content/public/browser/navigation_handle.h"
23 #include "content/public/browser/render_frame_host.h" 22 #include "content/public/browser/render_frame_host.h"
24 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/origin_util.h" 24 #include "content/public/common/origin_util.h"
26 #include "third_party/WebKit/public/platform/modules/app_banner/WebAppBannerProm ptReply.h" 25 #include "services/service_manager/public/cpp/interface_provider.h"
27 #include "third_party/skia/include/core/SkBitmap.h" 26 #include "third_party/skia/include/core/SkBitmap.h"
28 #include "ui/display/display.h" 27 #include "ui/display/display.h"
29 #include "ui/display/screen.h" 28 #include "ui/display/screen.h"
30 29
31 namespace { 30 namespace {
32 31
33 bool gDisableSecureCheckForTesting = false; 32 bool gDisableSecureCheckForTesting = false;
34 int gCurrentRequestID = -1; 33 int gCurrentRequestID = -1;
35 base::LazyInstance<base::TimeDelta> gTimeDeltaForTesting = 34 int gTimeDeltaInDaysForTesting = 0;
36 LAZY_INSTANCE_INITIALIZER;
37 35
38 // Returns |size_in_px| in dp, i.e. divided by the current device scale factor. 36 // Returns |size_in_px| in dp, i.e. divided by the current device scale factor.
39 int ConvertIconSizeFromPxToDp(int size_in_px) { 37 int ConvertIconSizeFromPxToDp(int size_in_px) {
40 return size_in_px / 38 return size_in_px /
41 display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor(); 39 display::Screen::GetScreen()->GetPrimaryDisplay().device_scale_factor();
42 } 40 }
43 41
44 InstallableParams ParamsToGetManifest() { 42 InstallableParams ParamsToGetManifest() {
45 return InstallableParams(); 43 return InstallableParams();
46 } 44 }
(...skipping 15 matching lines...) Expand all
62 60
63 namespace banners { 61 namespace banners {
64 62
65 // static 63 // static
66 void AppBannerManager::DisableSecureSchemeCheckForTesting() { 64 void AppBannerManager::DisableSecureSchemeCheckForTesting() {
67 gDisableSecureCheckForTesting = true; 65 gDisableSecureCheckForTesting = true;
68 } 66 }
69 67
70 // static 68 // static
71 base::Time AppBannerManager::GetCurrentTime() { 69 base::Time AppBannerManager::GetCurrentTime() {
72 return base::Time::Now() + gTimeDeltaForTesting.Get(); 70 return base::Time::Now() +
71 base::TimeDelta::FromDays(gTimeDeltaInDaysForTesting);
73 } 72 }
74 73
75 // static 74 // static
76 void AppBannerManager::SetTimeDeltaForTesting(int days) { 75 void AppBannerManager::SetTimeDeltaForTesting(int days) {
77 gTimeDeltaForTesting.Get() = base::TimeDelta::FromDays(days); 76 gTimeDeltaInDaysForTesting = days;
78 } 77 }
79 78
80 // static 79 // static
81 void AppBannerManager::SetEngagementWeights(double direct_engagement, 80 void AppBannerManager::SetEngagementWeights(double direct_engagement,
82 double indirect_engagement) { 81 double indirect_engagement) {
83 AppBannerSettingsHelper::SetEngagementWeights(direct_engagement, 82 AppBannerSettingsHelper::SetEngagementWeights(direct_engagement,
84 indirect_engagement); 83 indirect_engagement);
85 } 84 }
86 85
87 // static 86 // static
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 if (!content::IsOriginSecure(validated_url) && 121 if (!content::IsOriginSecure(validated_url) &&
123 !gDisableSecureCheckForTesting) { 122 !gDisableSecureCheckForTesting) {
124 ReportStatus(contents, NOT_FROM_SECURE_ORIGIN); 123 ReportStatus(contents, NOT_FROM_SECURE_ORIGIN);
125 Stop(); 124 Stop();
126 return; 125 return;
127 } 126 }
128 127
129 if (validated_url_.is_empty()) 128 if (validated_url_.is_empty())
130 validated_url_ = validated_url; 129 validated_url_ = validated_url;
131 130
131 // Any existing binding is invalid when we request a new banner.
132 if (binding_.is_bound())
133 binding_.Close();
134
132 manager_->GetData( 135 manager_->GetData(
133 ParamsToGetManifest(), 136 ParamsToGetManifest(),
134 base::Bind(&AppBannerManager::OnDidGetManifest, GetWeakPtr())); 137 base::Bind(&AppBannerManager::OnDidGetManifest, GetWeakPtr()));
135 } 138 }
136 139
140 void AppBannerManager::SendBannerAccepted(int request_id) {
141 if (request_id != gCurrentRequestID)
142 return;
143
144 DCHECK(event_.is_bound());
145 event_->BannerAccepted(GetBannerType());
146 }
147
148 void AppBannerManager::SendBannerDismissed(int request_id) {
149 if (request_id != gCurrentRequestID)
150 return;
151
152 DCHECK(event_.is_bound());
153 event_->BannerDismissed();
154 }
155
137 base::Closure AppBannerManager::FetchWebappSplashScreenImageCallback( 156 base::Closure AppBannerManager::FetchWebappSplashScreenImageCallback(
138 const std::string& webapp_id) { 157 const std::string& webapp_id) {
139 return base::Closure(); 158 return base::Closure();
140 } 159 }
141 160
142 AppBannerManager::AppBannerManager(content::WebContents* web_contents) 161 AppBannerManager::AppBannerManager(content::WebContents* web_contents)
143 : content::WebContentsObserver(web_contents), 162 : content::WebContentsObserver(web_contents),
144 SiteEngagementObserver(nullptr), 163 SiteEngagementObserver(nullptr),
145 manager_(nullptr), 164 manager_(nullptr),
146 event_request_id_(-1), 165 event_request_id_(-1),
166 binding_(this),
147 is_active_(false), 167 is_active_(false),
148 banner_request_queued_(false), 168 banner_request_queued_(false),
149 load_finished_(false), 169 load_finished_(false),
150 was_canceled_by_page_(false), 170 was_canceled_by_page_(false),
151 page_requested_prompt_(false), 171 page_requested_prompt_(false),
152 is_debug_mode_(false), 172 is_debug_mode_(false),
153 need_to_log_status_(false), 173 need_to_log_status_(false),
154 weak_factory_(this) { 174 weak_factory_(this) {
155 // Ensure the InstallableManager exists since we have a hard dependency on it. 175 // Ensure the InstallableManager exists since we have a hard dependency on it.
156 InstallableManager::CreateForWebContents(web_contents); 176 InstallableManager::CreateForWebContents(web_contents);
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 342
323 // Given all of the other checks that have been made, the only possible reason 343 // Given all of the other checks that have been made, the only possible reason
324 // for stopping now is that the app has been added to the homescreen. 344 // for stopping now is that the app has been added to the homescreen.
325 if (!IsDebugMode() && !CheckIfShouldShowBanner()) { 345 if (!IsDebugMode() && !CheckIfShouldShowBanner()) {
326 Stop(); 346 Stop();
327 return; 347 return;
328 } 348 }
329 349
330 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_CREATED); 350 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_CREATED);
331 event_request_id_ = ++gCurrentRequestID; 351 event_request_id_ = ++gCurrentRequestID;
332 content::RenderFrameHost* frame = web_contents()->GetMainFrame(); 352
333 frame->Send(new ChromeViewMsg_AppBannerPromptRequest( 353 web_contents()->GetMainFrame()->GetRemoteInterfaces()->GetInterface(
334 frame->GetRoutingID(), event_request_id_, GetBannerType())); 354 mojo::GetProxy(&controller_));
355
356 controller_->BannerPromptRequest(
357 binding_.CreateInterfacePtrAndBind(), mojo::GetProxy(&event_),
358 {GetBannerType()},
359 base::Bind(&AppBannerManager::OnBannerPromptReply, GetWeakPtr()));
335 } 360 }
336 361
337 void AppBannerManager::DidStartNavigation(content::NavigationHandle* handle) { 362 void AppBannerManager::DidStartNavigation(content::NavigationHandle* handle) {
338 if (!handle->IsInMainFrame()) 363 if (!handle->IsInMainFrame())
339 return; 364 return;
340 365
341 load_finished_ = false; 366 load_finished_ = false;
342 if (AppBannerSettingsHelper::ShouldUseSiteEngagementScore() && 367 if (AppBannerSettingsHelper::ShouldUseSiteEngagementScore() &&
343 GetSiteEngagementService() == nullptr) { 368 GetSiteEngagementService() == nullptr) {
344 // Ensure that we are observing the site engagement service on navigation 369 // Ensure that we are observing the site engagement service on navigation
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 } 478 }
454 479
455 // If we are in debug mode, AppBannerSettingsHelper::ShouldShowBanner must 480 // If we are in debug mode, AppBannerSettingsHelper::ShouldShowBanner must
456 // return NO_ERROR_DETECTED (bypass flag is set) or we must not have entered 481 // return NO_ERROR_DETECTED (bypass flag is set) or we must not have entered
457 // this method. 482 // this method.
458 DCHECK(!IsDebugMode()); 483 DCHECK(!IsDebugMode());
459 ReportStatus(web_contents(), code); 484 ReportStatus(web_contents(), code);
460 return false; 485 return false;
461 } 486 }
462 487
463 bool AppBannerManager::OnMessageReceived(
464 const IPC::Message& message,
465 content::RenderFrameHost* render_frame_host) {
466 bool handled = true;
467
468 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(AppBannerManager, message, render_frame_host)
469 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_AppBannerPromptReply,
470 OnBannerPromptReply)
471 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_RequestShowAppBanner,
472 OnRequestShowAppBanner)
473 IPC_MESSAGE_UNHANDLED(handled = false)
474 IPC_END_MESSAGE_MAP()
475
476 return handled;
477 }
478
479 void AppBannerManager::OnBannerPromptReply( 488 void AppBannerManager::OnBannerPromptReply(
480 content::RenderFrameHost* render_frame_host, 489 blink::mojom::AppBannerPromptReply reply,
481 int request_id, 490 const std::string& referrer) {
482 blink::WebAppBannerPromptReply reply,
483 std::string referrer) {
484 content::WebContents* contents = web_contents(); 491 content::WebContents* contents = web_contents();
485 if (request_id != event_request_id_)
486 return;
487
488 // The renderer might have requested the prompt to be canceled. 492 // The renderer might have requested the prompt to be canceled.
489 // They may request that it is redisplayed later, so don't Stop() here. 493 // They may request that it is redisplayed later, so don't Stop() here.
490 // However, log that the cancelation was requested, so Stop() can be 494 // However, log that the cancelation was requested, so Stop() can be
491 // called if a redisplay isn't asked for. 495 // called if a redisplay isn't asked for.
492 // 496 //
493 // We use the additional page_requested_prompt_ variable because the redisplay 497 // We use the additional page_requested_prompt_ variable because the redisplay
494 // request may be received *before* the Cancel prompt reply (e.g. if redisplay 498 // request may be received *before* the Cancel prompt reply (e.g. if redisplay
495 // is requested in the beforeinstallprompt event handler). 499 // is requested in the beforeinstallprompt event handler).
496 referrer_ = referrer; 500 referrer_ = referrer;
497 if (reply == blink::WebAppBannerPromptReply::Cancel && 501 if (reply == blink::mojom::AppBannerPromptReply::CANCEL &&
498 !page_requested_prompt_) { 502 !page_requested_prompt_) {
499 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_PREVENT_DEFAULT_CALLED); 503 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_PREVENT_DEFAULT_CALLED);
500 was_canceled_by_page_ = true; 504 was_canceled_by_page_ = true;
501 return; 505 return;
502 } 506 }
503 507
504 // If we haven't yet returned, but either of |was_canceled_by_page_| or 508 // If we haven't yet returned, but either of |was_canceled_by_page_| or
505 // |page_requested_prompt_| is true, the page has requested a delayed showing 509 // |page_requested_prompt_| is true, the page has requested a delayed showing
506 // of the prompt. Otherwise, the prompt was never canceled by the page. 510 // of the prompt. Otherwise, the prompt was never canceled by the page.
507 if (was_canceled_by_page_ || page_requested_prompt_) { 511 if (was_canceled_by_page_ || page_requested_prompt_) {
(...skipping 10 matching lines...) Expand all
518 DCHECK(!manifest_url_.is_empty()); 522 DCHECK(!manifest_url_.is_empty());
519 DCHECK(!manifest_.IsEmpty()); 523 DCHECK(!manifest_.IsEmpty());
520 DCHECK(!icon_url_.is_empty()); 524 DCHECK(!icon_url_.is_empty());
521 DCHECK(icon_.get()); 525 DCHECK(icon_.get());
522 526
523 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_COMPLETE); 527 TrackBeforeInstallEvent(BEFORE_INSTALL_EVENT_COMPLETE);
524 ShowBanner(); 528 ShowBanner();
525 is_active_ = false; 529 is_active_ = false;
526 } 530 }
527 531
528 void AppBannerManager::OnRequestShowAppBanner( 532 void AppBannerManager::DisplayAppBanner() {
529 content::RenderFrameHost* render_frame_host,
530 int request_id) {
531 if (was_canceled_by_page_) { 533 if (was_canceled_by_page_) {
532 // Simulate a non-canceled OnBannerPromptReply to show the delayed banner. 534 // Simulate a non-canceled OnBannerPromptReply to show the delayed banner.
533 // Don't reset |was_canceled_by_page_| yet for metrics purposes. 535 // Don't reset |was_canceled_by_page_| yet for metrics purposes.
534 OnBannerPromptReply(render_frame_host, request_id, 536 OnBannerPromptReply(blink::mojom::AppBannerPromptReply::NONE, referrer_);
535 blink::WebAppBannerPromptReply::None, referrer_);
536 } else { 537 } else {
537 // Log that the prompt request was made for when we get the prompt reply. 538 // Log that the prompt request was made for when we get the prompt reply.
538 page_requested_prompt_ = true; 539 page_requested_prompt_ = true;
539 } 540 }
540 } 541 }
541 542
542 } // namespace banners 543 } // namespace banners
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698