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 "base/metrics/field_trial.h"
8 #include "chrome/browser/banners/app_banner_data_fetcher.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 "net/base/load_flags.h"
15 #include "ui/gfx/screen.h"
18 bool gDisableSecureCheckForTesting
= false;
19 } // anonymous namespace
23 bool AppBannerManager::URLsAreForTheSamePage(const GURL
& first
,
25 return first
.GetWithEmptyPath() == second
.GetWithEmptyPath() &&
26 first
.path() == second
.path() && first
.query() == second
.query();
29 AppBannerManager::AppBannerManager(int icon_size
)
30 : ideal_icon_size_(icon_size
),
31 data_fetcher_(nullptr),
35 AppBannerManager::~AppBannerManager() {
36 CancelActiveFetcher();
39 void AppBannerManager::DidFinishLoad(
40 content::RenderFrameHost
* render_frame_host
,
41 const GURL
& validated_url
) {
42 if (render_frame_host
->GetParent())
45 if (data_fetcher_
.get() && data_fetcher_
->is_active() &&
46 URLsAreForTheSamePage(data_fetcher_
->validated_url(), validated_url
)) {
50 // A secure scheme is required to show banners, so exit early if we see the
52 if (!validated_url
.SchemeIsSecure() && !gDisableSecureCheckForTesting
)
55 // Kick off the data retrieval pipeline.
56 data_fetcher_
= CreateAppBannerDataFetcher(weak_factory_
.GetWeakPtr(),
58 data_fetcher_
->Start(validated_url
);
62 bool AppBannerManager::OnInvalidManifest(AppBannerDataFetcher
* fetcher
) {
66 void AppBannerManager::ReplaceWebContents(content::WebContents
* web_contents
) {
67 Observe(web_contents
);
68 if (data_fetcher_
.get())
69 data_fetcher_
.get()->ReplaceWebContents(web_contents
);
72 AppBannerDataFetcher
* AppBannerManager::CreateAppBannerDataFetcher(
73 base::WeakPtr
<AppBannerDataFetcher::Delegate
> weak_delegate
,
74 const int ideal_icon_size
) {
75 return new AppBannerDataFetcher(web_contents(), weak_delegate
,
79 void AppBannerManager::CancelActiveFetcher() {
80 if (data_fetcher_
!= nullptr) {
81 data_fetcher_
->Cancel();
82 data_fetcher_
= nullptr;
86 bool AppBannerManager::IsFetcherActive() {
87 return data_fetcher_
!= nullptr && data_fetcher_
->is_active();
90 void AppBannerManager::DisableSecureSchemeCheckForTesting() {
91 gDisableSecureCheckForTesting
= true;
94 bool AppBannerManager::IsEnabled() {
95 return base::FieldTrialList::FindFullName("AppBanners") == "Enabled";
98 } // namespace banners