blob: b2bd3d47ed82af1b6cab2ef9b360ac666603f137 [file] [log] [blame]
clamy1e5574e2016-09-29 16:48:441// Copyright 2016 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.
4
5#include "chrome/browser/renderer_host/chrome_navigation_ui_data.h"
6
John Abd-El-Malekd2377982018-01-08 22:23:127#include "chrome/browser/prerender/prerender_contents.h"
8#include "chrome/browser/prerender/prerender_histograms.h"
Robert Ogden0f4d88682019-09-24 22:27:529#include "chrome/browser/previews/previews_lite_page_redirect_decider.h"
clamy1e5574e2016-09-29 16:48:4410#include "content/public/browser/navigation_handle.h"
Scott Violetc8240b02018-03-08 22:03:5911#include "extensions/buildflags/buildflags.h"
Giovanni Ortuño Urquidi640873402018-02-08 23:01:4412#include "ui/base/window_open_disposition.h"
clamy1e5574e2016-09-29 16:48:4413
brettw00899e62016-11-12 02:10:1714#if BUILDFLAG(ENABLE_EXTENSIONS)
Clark DuVall8f4c7522019-07-30 03:54:0015#include "extensions/browser/extensions_browser_client.h"
Karan Bhatia8418c662018-03-02 00:39:2616#include "extensions/common/constants.h"
17#endif
Giovanni Ortuño Urquidi640873402018-02-08 23:01:4418
19ChromeNavigationUIData::ChromeNavigationUIData()
20 : disposition_(WindowOpenDisposition::CURRENT_TAB) {}
21
22ChromeNavigationUIData::ChromeNavigationUIData(
23 content::NavigationHandle* navigation_handle)
24 : disposition_(WindowOpenDisposition::CURRENT_TAB) {
25 auto* web_contents = navigation_handle->GetWebContents();
26#if BUILDFLAG(ENABLE_EXTENSIONS)
Karan Bhatia8418c662018-03-02 00:39:2627 int tab_id = extension_misc::kUnknownTabId;
28 int window_id = extension_misc::kUnknownWindowId;
Clark DuVall8f4c7522019-07-30 03:54:0029 // The browser client can be null in unittests.
30 if (extensions::ExtensionsBrowserClient::Get()) {
31 extensions::ExtensionsBrowserClient::Get()->GetTabAndWindowIdForWebContents(
32 web_contents, &tab_id, &window_id);
33 }
Jeremy Romanec48d7a2018-03-01 17:35:0934 extension_data_ = std::make_unique<extensions::ExtensionNavigationUIData>(
clamy1e5574e2016-09-29 16:48:4435 navigation_handle, tab_id, window_id);
36#endif
John Abd-El-Malekd2377982018-01-08 22:23:1237
38 auto* prerender_contents =
39 prerender::PrerenderContents::FromWebContents(web_contents);
40 if (prerender_contents) {
41 prerender_mode_ = prerender_contents->prerender_mode();
42 prerender_histogram_prefix_ =
43 prerender::PrerenderHistograms::GetHistogramPrefix(
44 prerender_contents->origin());
45 }
rajendrant8c2be4d2019-04-19 21:36:5246 data_reduction_proxy_page_id_ =
Robert Ogden0f4d88682019-09-24 22:27:5247 PreviewsLitePageRedirectDecider::GeneratePageIdForWebContents(
48 web_contents);
clamy1e5574e2016-09-29 16:48:4449}
50
51ChromeNavigationUIData::~ChromeNavigationUIData() {}
52
Giovanni Ortuño Urquidi640873402018-02-08 23:01:4453// static
54std::unique_ptr<ChromeNavigationUIData>
55ChromeNavigationUIData::CreateForMainFrameNavigation(
56 content::WebContents* web_contents,
rajendrant43194072019-04-15 22:13:4357 WindowOpenDisposition disposition,
58 int64_t data_reduction_proxy_page_id) {
Giovanni Ortuño Urquidi640873402018-02-08 23:01:4459 auto navigation_ui_data = std::make_unique<ChromeNavigationUIData>();
60 navigation_ui_data->disposition_ = disposition;
rajendrant43194072019-04-15 22:13:4361 navigation_ui_data->data_reduction_proxy_page_id_ =
62 data_reduction_proxy_page_id;
Giovanni Ortuño Urquidi640873402018-02-08 23:01:4463
64#if BUILDFLAG(ENABLE_EXTENSIONS)
Karan Bhatia8418c662018-03-02 00:39:2665 int tab_id = extension_misc::kUnknownTabId;
66 int window_id = extension_misc::kUnknownWindowId;
Clark DuVall8f4c7522019-07-30 03:54:0067 // The browser client can be null in unittests.
68 if (extensions::ExtensionsBrowserClient::Get()) {
69 extensions::ExtensionsBrowserClient::Get()->GetTabAndWindowIdForWebContents(
70 web_contents, &tab_id, &window_id);
71 }
Giovanni Ortuño Urquidi640873402018-02-08 23:01:4472
73 navigation_ui_data->extension_data_ =
74 extensions::ExtensionNavigationUIData::CreateForMainFrameNavigation(
75 web_contents, tab_id, window_id);
76#endif
77
78 return navigation_ui_data;
79}
80
Lucas Furukawa Gadanid726e1e2019-05-08 16:20:0381std::unique_ptr<content::NavigationUIData> ChromeNavigationUIData::Clone() {
Karan Bhatia8418c662018-03-02 00:39:2682 auto copy = std::make_unique<ChromeNavigationUIData>();
clamy1e5574e2016-09-29 16:48:4483
Giovanni Ortuño Urquidi640873402018-02-08 23:01:4484 copy->disposition_ = disposition_;
rajendrant43194072019-04-15 22:13:4385 copy->data_reduction_proxy_page_id_ = data_reduction_proxy_page_id_;
Giovanni Ortuño Urquidi640873402018-02-08 23:01:4486
brettw00899e62016-11-12 02:10:1787#if BUILDFLAG(ENABLE_EXTENSIONS)
clamy1e5574e2016-09-29 16:48:4488 if (extension_data_)
89 copy->SetExtensionNavigationUIData(extension_data_->DeepCopy());
90#endif
91
Jian Li18173422017-11-08 03:00:0292#if BUILDFLAG(ENABLE_OFFLINE_PAGES)
93 if (offline_page_data_)
94 copy->SetOfflinePageNavigationUIData(offline_page_data_->DeepCopy());
95#endif
96
John Abd-El-Malekd2377982018-01-08 22:23:1297 copy->prerender_mode_ = prerender_mode_;
98 copy->prerender_histogram_prefix_ = prerender_histogram_prefix_;
99
clamy1e5574e2016-09-29 16:48:44100 return std::move(copy);
101}
102
brettw00899e62016-11-12 02:10:17103#if BUILDFLAG(ENABLE_EXTENSIONS)
clamy1e5574e2016-09-29 16:48:44104void ChromeNavigationUIData::SetExtensionNavigationUIData(
105 std::unique_ptr<extensions::ExtensionNavigationUIData> extension_data) {
106 extension_data_ = std::move(extension_data);
107}
108#endif
Jian Li18173422017-11-08 03:00:02109
110#if BUILDFLAG(ENABLE_OFFLINE_PAGES)
111void ChromeNavigationUIData::SetOfflinePageNavigationUIData(
112 std::unique_ptr<offline_pages::OfflinePageNavigationUIData>
113 offline_page_data) {
114 offline_page_data_ = std::move(offline_page_data);
115}
116#endif