Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / banners / app_banner_manager.cc
blobe984f2de63ec6015b6c1e87f305da057e163a619
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
3 // found in the LICENSE file.
5 #include "chrome/browser/banners/app_banner_manager.h"
7 #include "chrome/browser/banners/app_banner_data_fetcher.h"
8 #include "chrome/browser/banners/app_banner_debug_log.h"
9 #include "chrome/browser/banners/app_banner_settings_helper.h"
10 #include "content/public/browser/navigation_details.h"
11 #include "content/public/browser/render_frame_host.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/frame_navigate_params.h"
14 #include "content/public/common/origin_util.h"
15 #include "net/base/load_flags.h"
16 #include "ui/gfx/screen.h"
18 namespace {
19 bool gDisableSecureCheckForTesting = false;
20 } // anonymous namespace
22 namespace banners {
24 void AppBannerManager::DisableSecureSchemeCheckForTesting() {
25 gDisableSecureCheckForTesting = true;
28 void AppBannerManager::SetEngagementWeights(double direct_engagement,
29 double indirect_engagement) {
30 AppBannerSettingsHelper::SetEngagementWeights(direct_engagement,
31 indirect_engagement);
34 bool AppBannerManager::URLsAreForTheSamePage(const GURL& first,
35 const GURL& second) {
36 return first.GetWithEmptyPath() == second.GetWithEmptyPath() &&
37 first.path() == second.path() && first.query() == second.query();
40 AppBannerManager::AppBannerManager()
41 : data_fetcher_(nullptr),
42 weak_factory_(this) {
43 AppBannerSettingsHelper::UpdateFromFieldTrial();
46 AppBannerManager::AppBannerManager(content::WebContents* web_contents)
47 : content::WebContentsObserver(web_contents),
48 data_fetcher_(nullptr),
49 weak_factory_(this) {
50 AppBannerSettingsHelper::UpdateFromFieldTrial();
53 AppBannerManager::~AppBannerManager() {
54 CancelActiveFetcher();
57 void AppBannerManager::ReplaceWebContents(content::WebContents* web_contents) {
58 Observe(web_contents);
59 if (data_fetcher_.get())
60 data_fetcher_.get()->ReplaceWebContents(web_contents);
63 bool AppBannerManager::IsFetcherActive() {
64 return data_fetcher_ != nullptr && data_fetcher_->is_active();
67 void AppBannerManager::DidNavigateMainFrame(
68 const content::LoadCommittedDetails& details,
69 const content::FrameNavigateParams& params) {
70 last_transition_type_ = params.transition;
73 void AppBannerManager::DidFinishLoad(
74 content::RenderFrameHost* render_frame_host,
75 const GURL& validated_url) {
76 if (render_frame_host->GetParent())
77 return;
79 if (data_fetcher_.get() && data_fetcher_->is_active() &&
80 URLsAreForTheSamePage(data_fetcher_->validated_url(), validated_url)) {
81 return;
84 // A secure origin is required to show banners, so exit early if we see the
85 // URL is invalid.
86 if (!content::IsOriginSecure(validated_url) &&
87 !gDisableSecureCheckForTesting) {
88 OutputDeveloperNotShownMessage(web_contents(), kNotServedFromSecureOrigin);
89 return;
92 // Kick off the data retrieval pipeline.
93 data_fetcher_ = CreateAppBannerDataFetcher(weak_factory_.GetWeakPtr());
94 data_fetcher_->Start(validated_url, last_transition_type_);
97 bool AppBannerManager::HandleNonWebApp(const std::string& platform,
98 const GURL& url,
99 const std::string& id) {
100 return false;
103 void AppBannerManager::CancelActiveFetcher() {
104 if (data_fetcher_ != nullptr) {
105 data_fetcher_->Cancel();
106 data_fetcher_ = nullptr;
110 } // namespace banners