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

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

Issue 2024953005: Allow app banners to be triggered by increases in site engagement. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@site-engagement-callback
Patch Set: Created 4 years, 6 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 "chrome/browser/banners/app_banner_data_fetcher.h" 7 #include "chrome/browser/banners/app_banner_data_fetcher.h"
8 #include "chrome/browser/banners/app_banner_debug_log.h" 8 #include "chrome/browser/banners/app_banner_debug_log.h"
9 #include "chrome/browser/banners/app_banner_settings_helper.h" 9 #include "chrome/browser/banners/app_banner_settings_helper.h"
10 #include "chrome/browser/engagement/site_engagement_service.h"
11 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/browser/navigation_details.h" 12 #include "content/public/browser/navigation_details.h"
11 #include "content/public/browser/render_frame_host.h" 13 #include "content/public/browser/render_frame_host.h"
12 #include "content/public/browser/web_contents.h" 14 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/frame_navigate_params.h" 15 #include "content/public/common/frame_navigate_params.h"
14 #include "content/public/common/origin_util.h" 16 #include "content/public/common/origin_util.h"
15 #include "net/base/load_flags.h" 17 #include "net/base/load_flags.h"
16 18
17 namespace { 19 namespace {
18 bool gDisableSecureCheckForTesting = false; 20 bool gDisableSecureCheckForTesting = false;
19 } // anonymous namespace 21 } // anonymous namespace
20 22
21 namespace banners { 23 namespace banners {
22 24
23 void AppBannerManager::DisableSecureSchemeCheckForTesting() { 25 void AppBannerManager::DisableSecureSchemeCheckForTesting() {
24 gDisableSecureCheckForTesting = true; 26 gDisableSecureCheckForTesting = true;
25 } 27 }
26 28
27 void AppBannerManager::SetEngagementWeights(double direct_engagement, 29 void AppBannerManager::SetEngagementWeights(double direct_engagement,
28 double indirect_engagement) { 30 double indirect_engagement) {
29 AppBannerSettingsHelper::SetEngagementWeights(direct_engagement, 31 AppBannerSettingsHelper::SetEngagementWeights(direct_engagement,
30 indirect_engagement); 32 indirect_engagement);
31 } 33 }
32 34
33 bool AppBannerManager::URLsAreForTheSamePage(const GURL& first, 35 bool AppBannerManager::URLsAreForTheSamePage(const GURL& first,
34 const GURL& second) { 36 const GURL& second) {
35 return first.GetWithEmptyPath() == second.GetWithEmptyPath() && 37 return first.GetWithEmptyPath() == second.GetWithEmptyPath() &&
36 first.path() == second.path() && first.query() == second.query(); 38 first.path() == second.path() && first.query() == second.query();
37 } 39 }
38 40
39 AppBannerManager::AppBannerManager()
40 : data_fetcher_(nullptr),
41 weak_factory_(this) {
42 AppBannerSettingsHelper::UpdateFromFieldTrial();
43 }
44
45 AppBannerManager::AppBannerManager(content::WebContents* web_contents) 41 AppBannerManager::AppBannerManager(content::WebContents* web_contents)
46 : content::WebContentsObserver(web_contents), 42 : content::WebContentsObserver(web_contents),
43 SiteEngagementObserver(nullptr),
44 is_visible_(false),
47 data_fetcher_(nullptr), 45 data_fetcher_(nullptr),
48 weak_factory_(this) { 46 weak_factory_(this) {
49 AppBannerSettingsHelper::UpdateFromFieldTrial(); 47 AppBannerSettingsHelper::UpdateFromFieldTrial();
48 if (AppBannerSettingsHelper::ShouldUseSiteEngagementScore()) {
49 SiteEngagementObserver::Observe(SiteEngagementService::Get(
50 Profile::FromBrowserContext(web_contents->GetBrowserContext())));
51 }
50 } 52 }
51 53
52 AppBannerManager::~AppBannerManager() { 54 AppBannerManager::~AppBannerManager() {
53 CancelActiveFetcher(); 55 CancelActiveFetcher();
54 } 56 }
55 57
56 void AppBannerManager::ReplaceWebContents(content::WebContents* web_contents) { 58 void AppBannerManager::ReplaceWebContents(content::WebContents* web_contents) {
57 Observe(web_contents); 59 content::WebContentsObserver::Observe(web_contents);
58 if (data_fetcher_.get()) 60 if (data_fetcher_.get())
59 data_fetcher_.get()->ReplaceWebContents(web_contents); 61 data_fetcher_.get()->ReplaceWebContents(web_contents);
60 } 62 }
61 63
62 bool AppBannerManager::IsFetcherActive() { 64 bool AppBannerManager::IsFetcherActive() {
63 return data_fetcher_ && data_fetcher_->is_active(); 65 return data_fetcher_ && data_fetcher_->is_active();
64 } 66 }
65 67
66 void AppBannerManager::DidNavigateMainFrame( 68 void AppBannerManager::RequestAppBanner(const GURL& validated_url,
67 const content::LoadCommittedDetails& details, 69 bool is_debug_mode) {
68 const content::FrameNavigateParams& params) { 70 content::WebContents* contents = web_contents();
69 last_transition_type_ = params.transition; 71 if (contents->GetMainFrame()->GetParent()) {
70 } 72 OutputDeveloperNotShownMessage(contents, kNotLoadedInMainFrame,
71
72 void AppBannerManager::RequestAppBanner(
73 content::RenderFrameHost* render_frame_host,
74 const GURL& validated_url,
75 bool is_debug_mode) {
76 if (render_frame_host->GetParent()) {
77 OutputDeveloperNotShownMessage(web_contents(), kNotLoadedInMainFrame,
78 is_debug_mode); 73 is_debug_mode);
79 return; 74 return;
80 } 75 }
81 76
82 if (data_fetcher_.get() && data_fetcher_->is_active() && 77 if (data_fetcher_.get() && data_fetcher_->is_active() &&
83 URLsAreForTheSamePage(data_fetcher_->validated_url(), validated_url) && 78 URLsAreForTheSamePage(data_fetcher_->validated_url(), validated_url) &&
84 !is_debug_mode) { 79 !is_debug_mode) {
85 return; 80 return;
86 } 81 }
87 82
88 // A secure origin is required to show banners, so exit early if we see the 83 // A secure origin is required to show banners, so exit early if we see the
89 // URL is invalid. 84 // URL is invalid.
90 if (!content::IsOriginSecure(validated_url) && 85 if (!content::IsOriginSecure(validated_url) &&
91 !gDisableSecureCheckForTesting) { 86 !gDisableSecureCheckForTesting) {
92 OutputDeveloperNotShownMessage(web_contents(), kNotServedFromSecureOrigin, 87 OutputDeveloperNotShownMessage(contents, kNotServedFromSecureOrigin,
93 is_debug_mode); 88 is_debug_mode);
94 return; 89 return;
95 } 90 }
96 91
97 // Kick off the data retrieval pipeline. 92 // Kick off the data retrieval pipeline.
98 data_fetcher_ = 93 data_fetcher_ =
99 CreateAppBannerDataFetcher(weak_factory_.GetWeakPtr(), is_debug_mode); 94 CreateAppBannerDataFetcher(weak_factory_.GetWeakPtr(), is_debug_mode);
100 data_fetcher_->Start(validated_url, last_transition_type_); 95 data_fetcher_->Start(validated_url, last_transition_type_);
101 } 96 }
102 97
98 void AppBannerManager::DidNavigateMainFrame(
99 const content::LoadCommittedDetails& details,
100 const content::FrameNavigateParams& params) {
101 last_transition_type_ = params.transition;
102 }
103
103 void AppBannerManager::DidFinishLoad( 104 void AppBannerManager::DidFinishLoad(
104 content::RenderFrameHost* render_frame_host, 105 content::RenderFrameHost* render_frame_host,
105 const GURL& validated_url) { 106 const GURL& validated_url) {
106 // The third argument is the is_debug_mode boolean value, which is true only 107 if (AppBannerSettingsHelper::ShouldUseSiteEngagementScore()) {
107 // when it is triggered by the developer's action in DevTools. 108 validated_url_ = validated_url;
108 RequestAppBanner(render_frame_host, validated_url, false /* is_debug_mode */); 109 } else {
110 // The third argument is the is_debug_mode boolean value, which is true only
111 // when it is triggered by the developer's action in DevTools.
112 RequestAppBanner(validated_url, false /* is_debug_mode */);
113 }
114 }
115
116 void AppBannerManager::WasShown() {
117 is_visible_ = true;
118 }
119
120 void AppBannerManager::WasHidden() {
121 is_visible_ = false;
benwells 2016/06/02 04:44:36 What's this for? Is this something you found in te
dominickn 2016/06/02 04:54:37 If a user has multiple tabs open (multiple WebCont
109 } 122 }
110 123
111 bool AppBannerManager::HandleNonWebApp(const std::string& platform, 124 bool AppBannerManager::HandleNonWebApp(const std::string& platform,
112 const GURL& url, 125 const GURL& url,
113 const std::string& id, 126 const std::string& id,
114 bool is_debug_mode) { 127 bool is_debug_mode) {
115 return false; 128 return false;
116 } 129 }
117 130
131 void AppBannerManager::OnEngagementIncreased(
132 const SiteEngagementService* service,
133 const GURL& url,
134 double score) {
135 if (AppBannerSettingsHelper::HasSufficientEngagement(score) &&
136 url == validated_url_ && is_visible_) {
benwells 2016/06/02 04:44:36 I'm slightly worried there will be cases where we'
dominickn 2016/06/02 04:54:37 Yes, this is definitely a concern. We can certainl
137 // This call will perform some simple tests, create a data fetcher, and then
138 // start a series of asynchronous checks to test installability. It should
139 // be safe to start this in response to user input.
140 RequestAppBanner(url, false /* is_debug_mode */);
benwells 2016/06/02 04:44:36 IIUC this will be called a lot more now. Have you
dominickn 2016/06/02 04:54:38 I did think about this, and I believe this shouldn
141 }
142 }
143
118 void AppBannerManager::CancelActiveFetcher() { 144 void AppBannerManager::CancelActiveFetcher() {
119 if (data_fetcher_) { 145 if (data_fetcher_) {
120 data_fetcher_->Cancel(); 146 data_fetcher_->Cancel();
121 data_fetcher_ = nullptr; 147 data_fetcher_ = nullptr;
122 } 148 }
123 } 149 }
124 150
125 } // namespace banners 151 } // namespace banners
OLDNEW
« no previous file with comments | « chrome/browser/banners/app_banner_manager.h ('k') | chrome/browser/banners/app_banner_settings_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698