Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "content/public/browser/navigation_details.h" | 10 #include "chrome/browser/engagement/site_engagement_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "content/public/browser/navigation_handle.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" | |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 bool gDisableSecureCheckForTesting = false; | 19 bool gDisableSecureCheckForTesting = false; |
| 19 } // anonymous namespace | 20 } // anonymous namespace |
| 20 | 21 |
| 21 namespace banners { | 22 namespace banners { |
| 22 | 23 |
| 23 void AppBannerManager::DisableSecureSchemeCheckForTesting() { | 24 void AppBannerManager::DisableSecureSchemeCheckForTesting() { |
| 24 gDisableSecureCheckForTesting = true; | 25 gDisableSecureCheckForTesting = true; |
| 25 } | 26 } |
| 26 | 27 |
| 27 void AppBannerManager::SetEngagementWeights(double direct_engagement, | 28 void AppBannerManager::SetEngagementWeights(double direct_engagement, |
| 28 double indirect_engagement) { | 29 double indirect_engagement) { |
| 29 AppBannerSettingsHelper::SetEngagementWeights(direct_engagement, | 30 AppBannerSettingsHelper::SetEngagementWeights(direct_engagement, |
| 30 indirect_engagement); | 31 indirect_engagement); |
| 31 } | 32 } |
| 32 | 33 |
| 33 bool AppBannerManager::URLsAreForTheSamePage(const GURL& first, | 34 bool AppBannerManager::URLsAreForTheSamePage(const GURL& first, |
| 34 const GURL& second) { | 35 const GURL& second) { |
| 35 return first.GetWithEmptyPath() == second.GetWithEmptyPath() && | 36 return first.GetWithEmptyPath() == second.GetWithEmptyPath() && |
| 36 first.path() == second.path() && first.query() == second.query(); | 37 first.path() == second.path() && first.query() == second.query(); |
| 37 } | 38 } |
| 38 | 39 |
| 39 AppBannerManager::AppBannerManager() | |
| 40 : data_fetcher_(nullptr), | |
| 41 weak_factory_(this) { | |
| 42 AppBannerSettingsHelper::UpdateFromFieldTrial(); | |
| 43 } | |
| 44 | |
| 45 AppBannerManager::AppBannerManager(content::WebContents* web_contents) | 40 AppBannerManager::AppBannerManager(content::WebContents* web_contents) |
| 46 : content::WebContentsObserver(web_contents), | 41 : content::WebContentsObserver(web_contents), |
| 42 SiteEngagementObserver(nullptr), | |
| 47 data_fetcher_(nullptr), | 43 data_fetcher_(nullptr), |
| 44 is_hidden_(false), | |
| 45 load_finished_(false), | |
| 46 banner_requested_(false), | |
| 48 weak_factory_(this) { | 47 weak_factory_(this) { |
| 49 AppBannerSettingsHelper::UpdateFromFieldTrial(); | 48 AppBannerSettingsHelper::UpdateFromFieldTrial(); |
| 50 } | 49 } |
| 51 | 50 |
| 52 AppBannerManager::~AppBannerManager() { | 51 AppBannerManager::~AppBannerManager() { |
| 53 CancelActiveFetcher(); | 52 CancelActiveFetcher(); |
| 54 } | 53 } |
| 55 | 54 |
| 56 void AppBannerManager::ReplaceWebContents(content::WebContents* web_contents) { | 55 void AppBannerManager::ReplaceWebContents(content::WebContents* web_contents) { |
| 57 Observe(web_contents); | 56 content::WebContentsObserver::Observe(web_contents); |
| 58 if (data_fetcher_.get()) | 57 if (data_fetcher_.get()) |
| 59 data_fetcher_.get()->ReplaceWebContents(web_contents); | 58 data_fetcher_.get()->ReplaceWebContents(web_contents); |
| 60 } | 59 } |
| 61 | 60 |
| 62 bool AppBannerManager::IsFetcherActive() { | 61 bool AppBannerManager::IsFetcherActive() { |
| 63 return data_fetcher_ && data_fetcher_->is_active(); | 62 return data_fetcher_ && data_fetcher_->is_active(); |
| 64 } | 63 } |
| 65 | 64 |
| 66 void AppBannerManager::DidNavigateMainFrame( | 65 void AppBannerManager::RequestAppBanner(const GURL& validated_url, |
|
benwells
2016/06/28 00:57:50
Why did you change this from DidNavigateMainFrame
dominickn
2016/06/28 03:52:29
DidNavigateMainFrame is deprecated and is being re
| |
| 67 const content::LoadCommittedDetails& details, | 66 bool is_debug_mode) { |
| 68 const content::FrameNavigateParams& params) { | 67 content::WebContents* contents = web_contents(); |
| 69 last_transition_type_ = params.transition; | 68 if (contents->GetMainFrame()->GetParent()) { |
| 70 } | 69 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); | 70 is_debug_mode); |
| 79 return; | 71 return; |
| 80 } | 72 } |
| 81 | 73 |
| 82 if (data_fetcher_.get() && data_fetcher_->is_active() && | 74 if (data_fetcher_.get() && data_fetcher_->is_active() && |
| 83 URLsAreForTheSamePage(data_fetcher_->validated_url(), validated_url) && | 75 URLsAreForTheSamePage(data_fetcher_->validated_url(), validated_url) && |
| 84 !is_debug_mode) { | 76 !is_debug_mode) { |
| 85 return; | 77 return; |
| 86 } | 78 } |
| 87 | 79 |
| 88 // A secure origin is required to show banners, so exit early if we see the | 80 // A secure origin is required to show banners, so exit early if we see the |
| 89 // URL is invalid. | 81 // URL is invalid. |
| 90 if (!content::IsOriginSecure(validated_url) && | 82 if (!content::IsOriginSecure(validated_url) && |
| 91 !gDisableSecureCheckForTesting) { | 83 !gDisableSecureCheckForTesting) { |
| 92 OutputDeveloperNotShownMessage(web_contents(), kNotServedFromSecureOrigin, | 84 OutputDeveloperNotShownMessage(contents, kNotServedFromSecureOrigin, |
| 93 is_debug_mode); | 85 is_debug_mode); |
| 94 return; | 86 return; |
| 95 } | 87 } |
| 96 | 88 |
| 97 // Kick off the data retrieval pipeline. | 89 // Kick off the data retrieval pipeline. |
| 98 data_fetcher_ = | 90 data_fetcher_ = |
| 99 CreateAppBannerDataFetcher(weak_factory_.GetWeakPtr(), is_debug_mode); | 91 CreateAppBannerDataFetcher(weak_factory_.GetWeakPtr(), is_debug_mode); |
| 100 data_fetcher_->Start(validated_url, last_transition_type_); | 92 data_fetcher_->Start(validated_url, last_transition_type_); |
| 101 } | 93 } |
| 102 | 94 |
| 95 void AppBannerManager::DidStartNavigation( | |
| 96 content::NavigationHandle* navigation_handle) { | |
| 97 if (AppBannerSettingsHelper::ShouldUseSiteEngagementScore() && | |
| 98 navigation_handle->IsInMainFrame()) { | |
| 99 load_finished_ = false; | |
|
benwells
2016/06/28 00:57:50
Seems strange that this is set false only if we're
dominickn
2016/06/28 03:52:29
The non site engagement triggered banners don't us
| |
| 100 | |
| 101 // Ensure that we are observing the site engagement service on navigation | |
| 102 // start. This may be the first navigation, or we may have stopped observing | |
| 103 // as the banner flow was triggered on the previous page. | |
| 104 if (GetSiteEngagementService() == nullptr) { | |
| 105 SiteEngagementObserver::Observe(SiteEngagementService::Get( | |
| 106 Profile::FromBrowserContext(web_contents()->GetBrowserContext()))); | |
| 107 } | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 void AppBannerManager::DidFinishNavigation( | |
| 112 content::NavigationHandle* navigation_handle) { | |
| 113 if (navigation_handle->HasCommitted()) | |
| 114 last_transition_type_ = navigation_handle->GetPageTransition(); | |
| 115 } | |
| 116 | |
| 103 void AppBannerManager::DidFinishLoad( | 117 void AppBannerManager::DidFinishLoad( |
| 104 content::RenderFrameHost* render_frame_host, | 118 content::RenderFrameHost* render_frame_host, |
| 105 const GURL& validated_url) { | 119 const GURL& validated_url) { |
| 106 // The third argument is the is_debug_mode boolean value, which is true only | 120 // Don't start the banner flow unless the main frame has finished loading. |
| 107 // when it is triggered by the developer's action in DevTools. | 121 if (render_frame_host->GetParent()) |
| 108 RequestAppBanner(render_frame_host, validated_url, false /* is_debug_mode */); | 122 return; |
| 123 | |
| 124 load_finished_ = true; | |
| 125 if (!AppBannerSettingsHelper::ShouldUseSiteEngagementScore() || | |
| 126 banner_requested_) { | |
|
benwells
2016/06/28 00:57:50
This logic reads weirdly. If banner_requested is t
dominickn
2016/06/28 03:52:29
Changed to banner eligible.
benwells
2016/06/29 03:38:24
Hmmm thanks.
Nit: maybe banner_request_queued_ or
dominickn
2016/06/29 04:18:58
Done.
| |
| 127 banner_requested_ = false; | |
| 128 | |
| 129 // The third argument is the is_debug_mode boolean value, which is true only | |
| 130 // when it is triggered by the developer's action in DevTools. | |
| 131 RequestAppBanner(validated_url, false /* is_debug_mode */); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 void AppBannerManager::MediaStartedPlaying(const MediaPlayerId& id) { | |
| 136 active_media_players_.push_back(id); | |
| 137 } | |
| 138 | |
| 139 void AppBannerManager::MediaStoppedPlaying(const MediaPlayerId& id) { | |
| 140 active_media_players_.erase(std::remove(active_media_players_.begin(), | |
| 141 active_media_players_.end(), id), | |
| 142 active_media_players_.end()); | |
| 143 } | |
| 144 | |
| 145 void AppBannerManager::WasShown() { | |
| 146 is_hidden_ = false; | |
| 147 } | |
| 148 | |
| 149 void AppBannerManager::WasHidden() { | |
| 150 is_hidden_ = true; | |
| 109 } | 151 } |
| 110 | 152 |
| 111 bool AppBannerManager::HandleNonWebApp(const std::string& platform, | 153 bool AppBannerManager::HandleNonWebApp(const std::string& platform, |
| 112 const GURL& url, | 154 const GURL& url, |
| 113 const std::string& id, | 155 const std::string& id, |
| 114 bool is_debug_mode) { | 156 bool is_debug_mode) { |
| 115 return false; | 157 return false; |
| 116 } | 158 } |
| 117 | 159 |
| 160 void AppBannerManager::OnEngagementIncreased(content::WebContents* contents, | |
| 161 const GURL& url, | |
| 162 double score) { | |
| 163 // Only trigger a banner using site engagement if: | |
| 164 // 1. engagement increased for the web contents which we are attached to; and | |
| 165 // 2. there are no currently active media players; and | |
| 166 // 3. we have accumulated sufficient engagement. | |
| 167 if (web_contents() == contents && !is_hidden_ && | |
| 168 active_media_players_.empty() && | |
| 169 AppBannerSettingsHelper::HasSufficientEngagement(score)) { | |
| 170 // Stop observing so we don't double-trigger the banner. | |
|
benwells
2016/06/28 00:57:50
What will happen if you have two visible web conte
dominickn
2016/06/28 03:52:29
The content setting should prevent that, since a b
benwells
2016/06/29 03:38:24
Acknowledged.
| |
| 171 SiteEngagementObserver::Observe(nullptr); | |
| 172 | |
| 173 if (!load_finished_ || banner_requested_) { | |
| 174 // Wait until the main frame finishes loading before requesting a banner. | |
| 175 banner_requested_ = true; | |
| 176 } else { | |
| 177 // Requesting a banner performs some simple tests, creates a data fetcher, | |
| 178 // and starts some asynchronous checks to test installability. It should | |
| 179 // be safe to start this in response to user input. | |
| 180 RequestAppBanner(url, false /* is_debug_mode */); | |
| 181 } | |
| 182 } | |
| 183 } | |
| 184 | |
| 118 void AppBannerManager::CancelActiveFetcher() { | 185 void AppBannerManager::CancelActiveFetcher() { |
| 119 if (data_fetcher_) { | 186 if (data_fetcher_) { |
| 120 data_fetcher_->Cancel(); | 187 data_fetcher_->Cancel(); |
| 121 data_fetcher_ = nullptr; | 188 data_fetcher_ = nullptr; |
| 122 } | 189 } |
| 123 } | 190 } |
| 124 | 191 |
| 125 } // namespace banners | 192 } // namespace banners |
| OLD | NEW |