| 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 banner_request_queued_(false), |
| 45 load_finished_(false), |
| 48 weak_factory_(this) { | 46 weak_factory_(this) { |
| 49 AppBannerSettingsHelper::UpdateFromFieldTrial(); | 47 AppBannerSettingsHelper::UpdateFromFieldTrial(); |
| 50 } | 48 } |
| 51 | 49 |
| 52 AppBannerManager::~AppBannerManager() { | 50 AppBannerManager::~AppBannerManager() { |
| 53 CancelActiveFetcher(); | 51 CancelActiveFetcher(); |
| 54 } | 52 } |
| 55 | 53 |
| 56 void AppBannerManager::ReplaceWebContents(content::WebContents* web_contents) { | 54 void AppBannerManager::ReplaceWebContents(content::WebContents* web_contents) { |
| 57 Observe(web_contents); | 55 content::WebContentsObserver::Observe(web_contents); |
| 58 if (data_fetcher_.get()) | 56 if (data_fetcher_.get()) |
| 59 data_fetcher_.get()->ReplaceWebContents(web_contents); | 57 data_fetcher_.get()->ReplaceWebContents(web_contents); |
| 60 } | 58 } |
| 61 | 59 |
| 62 bool AppBannerManager::IsFetcherActive() { | 60 bool AppBannerManager::IsFetcherActive() { |
| 63 return data_fetcher_ && data_fetcher_->is_active(); | 61 return data_fetcher_ && data_fetcher_->is_active(); |
| 64 } | 62 } |
| 65 | 63 |
| 66 void AppBannerManager::DidNavigateMainFrame( | 64 void AppBannerManager::RequestAppBanner(const GURL& validated_url, |
| 67 const content::LoadCommittedDetails& details, | 65 bool is_debug_mode) { |
| 68 const content::FrameNavigateParams& params) { | 66 content::WebContents* contents = web_contents(); |
| 69 last_transition_type_ = params.transition; | 67 if (contents->GetMainFrame()->GetParent()) { |
| 70 } | 68 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); | 69 is_debug_mode); |
| 79 return; | 70 return; |
| 80 } | 71 } |
| 81 | 72 |
| 82 if (data_fetcher_.get() && data_fetcher_->is_active() && | 73 if (data_fetcher_.get() && data_fetcher_->is_active() && |
| 83 URLsAreForTheSamePage(data_fetcher_->validated_url(), validated_url) && | 74 URLsAreForTheSamePage(data_fetcher_->validated_url(), validated_url) && |
| 84 !is_debug_mode) { | 75 !is_debug_mode) { |
| 85 return; | 76 return; |
| 86 } | 77 } |
| 87 | 78 |
| 88 // A secure origin is required to show banners, so exit early if we see the | 79 // A secure origin is required to show banners, so exit early if we see the |
| 89 // URL is invalid. | 80 // URL is invalid. |
| 90 if (!content::IsOriginSecure(validated_url) && | 81 if (!content::IsOriginSecure(validated_url) && |
| 91 !gDisableSecureCheckForTesting) { | 82 !gDisableSecureCheckForTesting) { |
| 92 OutputDeveloperNotShownMessage(web_contents(), kNotServedFromSecureOrigin, | 83 OutputDeveloperNotShownMessage(contents, kNotServedFromSecureOrigin, |
| 93 is_debug_mode); | 84 is_debug_mode); |
| 94 return; | 85 return; |
| 95 } | 86 } |
| 96 | 87 |
| 97 // Kick off the data retrieval pipeline. | 88 // Kick off the data retrieval pipeline. |
| 98 data_fetcher_ = | 89 data_fetcher_ = |
| 99 CreateAppBannerDataFetcher(weak_factory_.GetWeakPtr(), is_debug_mode); | 90 CreateAppBannerDataFetcher(weak_factory_.GetWeakPtr(), is_debug_mode); |
| 100 data_fetcher_->Start(validated_url, last_transition_type_); | 91 data_fetcher_->Start(validated_url, last_transition_type_); |
| 101 } | 92 } |
| 102 | 93 |
| 94 void AppBannerManager::DidStartNavigation( |
| 95 content::NavigationHandle* navigation_handle) { |
| 96 if (!navigation_handle->IsInMainFrame()) |
| 97 return; |
| 98 |
| 99 load_finished_ = false; |
| 100 if (AppBannerSettingsHelper::ShouldUseSiteEngagementScore() && |
| 101 GetSiteEngagementService() == nullptr) { |
| 102 // Ensure that we are observing the site engagement service on navigation |
| 103 // start. This may be the first navigation, or we may have stopped |
| 104 // observing if the banner flow was triggered on the previous page. |
| 105 SiteEngagementObserver::Observe(SiteEngagementService::Get( |
| 106 Profile::FromBrowserContext(web_contents()->GetBrowserContext()))); |
| 107 } |
| 108 } |
| 109 |
| 110 void AppBannerManager::DidFinishNavigation( |
| 111 content::NavigationHandle* navigation_handle) { |
| 112 if (navigation_handle->HasCommitted()) |
| 113 last_transition_type_ = navigation_handle->GetPageTransition(); |
| 114 } |
| 115 |
| 103 void AppBannerManager::DidFinishLoad( | 116 void AppBannerManager::DidFinishLoad( |
| 104 content::RenderFrameHost* render_frame_host, | 117 content::RenderFrameHost* render_frame_host, |
| 105 const GURL& validated_url) { | 118 const GURL& validated_url) { |
| 106 // The third argument is the is_debug_mode boolean value, which is true only | 119 // 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. | 120 if (render_frame_host->GetParent()) |
| 108 RequestAppBanner(render_frame_host, validated_url, false /* is_debug_mode */); | 121 return; |
| 122 |
| 123 load_finished_ = true; |
| 124 if (!AppBannerSettingsHelper::ShouldUseSiteEngagementScore() || |
| 125 banner_request_queued_) { |
| 126 banner_request_queued_ = false; |
| 127 |
| 128 // The third argument is the is_debug_mode boolean value, which is true only |
| 129 // when it is triggered by the developer's action in DevTools. |
| 130 RequestAppBanner(validated_url, false /* is_debug_mode */); |
| 131 } |
| 132 } |
| 133 |
| 134 void AppBannerManager::MediaStartedPlaying(const MediaPlayerId& id) { |
| 135 active_media_players_.push_back(id); |
| 136 } |
| 137 |
| 138 void AppBannerManager::MediaStoppedPlaying(const MediaPlayerId& id) { |
| 139 active_media_players_.erase(std::remove(active_media_players_.begin(), |
| 140 active_media_players_.end(), id), |
| 141 active_media_players_.end()); |
| 109 } | 142 } |
| 110 | 143 |
| 111 bool AppBannerManager::HandleNonWebApp(const std::string& platform, | 144 bool AppBannerManager::HandleNonWebApp(const std::string& platform, |
| 112 const GURL& url, | 145 const GURL& url, |
| 113 const std::string& id, | 146 const std::string& id, |
| 114 bool is_debug_mode) { | 147 bool is_debug_mode) { |
| 115 return false; | 148 return false; |
| 116 } | 149 } |
| 117 | 150 |
| 151 void AppBannerManager::OnEngagementIncreased(content::WebContents* contents, |
| 152 const GURL& url, |
| 153 double score) { |
| 154 // Only trigger a banner using site engagement if: |
| 155 // 1. engagement increased for the web contents which we are attached to; and |
| 156 // 2. there are no currently active media players; and |
| 157 // 3. we have accumulated sufficient engagement. |
| 158 if (web_contents() == contents && active_media_players_.empty() && |
| 159 AppBannerSettingsHelper::HasSufficientEngagement(score)) { |
| 160 // Stop observing so we don't double-trigger the banner. |
| 161 SiteEngagementObserver::Observe(nullptr); |
| 162 |
| 163 if (!load_finished_) { |
| 164 // Wait until the main frame finishes loading before requesting a banner. |
| 165 banner_request_queued_ = true; |
| 166 } else { |
| 167 // Requesting a banner performs some simple tests, creates a data fetcher, |
| 168 // and starts some asynchronous checks to test installability. It should |
| 169 // be safe to start this in response to user input. |
| 170 RequestAppBanner(url, false /* is_debug_mode */); |
| 171 } |
| 172 } |
| 173 } |
| 174 |
| 118 void AppBannerManager::CancelActiveFetcher() { | 175 void AppBannerManager::CancelActiveFetcher() { |
| 119 if (data_fetcher_) { | 176 if (data_fetcher_) { |
| 120 data_fetcher_->Cancel(); | 177 data_fetcher_->Cancel(); |
| 121 data_fetcher_ = nullptr; | 178 data_fetcher_ = nullptr; |
| 122 } | 179 } |
| 123 } | 180 } |
| 124 | 181 |
| 125 } // namespace banners | 182 } // namespace banners |
| OLD | NEW |