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"
19 bool gDisableSecureCheckForTesting
= false;
20 } // anonymous namespace
24 bool AppBannerManager::URLsAreForTheSamePage(const GURL
& first
,
26 return first
.GetWithEmptyPath() == second
.GetWithEmptyPath() &&
27 first
.path() == second
.path() && first
.query() == second
.query();
30 AppBannerManager::AppBannerManager(int ideal_icon_size_in_dp
)
31 : ideal_icon_size_in_dp_(ideal_icon_size_in_dp
),
32 data_fetcher_(nullptr),
34 AppBannerSettingsHelper::UpdateFromFieldTrial();
37 AppBannerManager::AppBannerManager(content::WebContents
* web_contents
,
38 int ideal_icon_size_in_dp
)
39 : content::WebContentsObserver(web_contents
),
40 ideal_icon_size_in_dp_(ideal_icon_size_in_dp
),
41 data_fetcher_(nullptr),
43 AppBannerSettingsHelper::UpdateFromFieldTrial();
46 AppBannerManager::~AppBannerManager() {
47 CancelActiveFetcher();
50 void AppBannerManager::DidCommitProvisionalLoadForFrame(
51 content::RenderFrameHost
* render_frame_host
,
53 ui::PageTransition transition_type
) {
54 last_transition_type_
= transition_type
;
57 void AppBannerManager::DidFinishLoad(
58 content::RenderFrameHost
* render_frame_host
,
59 const GURL
& validated_url
) {
60 if (render_frame_host
->GetParent())
63 if (data_fetcher_
.get() && data_fetcher_
->is_active() &&
64 URLsAreForTheSamePage(data_fetcher_
->validated_url(), validated_url
)) {
68 // A secure origin is required to show banners, so exit early if we see the
70 if (!content::IsOriginSecure(validated_url
) &&
71 !gDisableSecureCheckForTesting
) {
72 OutputDeveloperNotShownMessage(web_contents(), kNotServedFromSecureOrigin
);
76 // Kick off the data retrieval pipeline.
77 data_fetcher_
= CreateAppBannerDataFetcher(weak_factory_
.GetWeakPtr(),
78 ideal_icon_size_in_dp_
);
79 data_fetcher_
->Start(validated_url
, last_transition_type_
);
82 bool AppBannerManager::HandleNonWebApp(const std::string
& platform
,
84 const std::string
& id
) {
88 void AppBannerManager::ReplaceWebContents(content::WebContents
* web_contents
) {
89 Observe(web_contents
);
90 if (data_fetcher_
.get())
91 data_fetcher_
.get()->ReplaceWebContents(web_contents
);
94 void AppBannerManager::CancelActiveFetcher() {
95 if (data_fetcher_
!= nullptr) {
96 data_fetcher_
->Cancel();
97 data_fetcher_
= nullptr;
101 bool AppBannerManager::IsFetcherActive() {
102 return data_fetcher_
!= nullptr && data_fetcher_
->is_active();
105 void AppBannerManager::DisableSecureSchemeCheckForTesting() {
106 gDisableSecureCheckForTesting
= true;
109 void AppBannerManager::SetEngagementWeights(double direct_engagement
,
110 double indirect_engagement
) {
111 AppBannerSettingsHelper::SetEngagementWeights(direct_engagement
,
112 indirect_engagement
);
115 } // namespace banners