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.
4 
5 #ifndef CHROME_BROWSER_UI_BROWSER_NAVIGATOR_PARAMS_H_
6 #define CHROME_BROWSER_UI_BROWSER_NAVIGATOR_PARAMS_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/memory/ref_counted.h"
13 #include "base/optional.h"
14 #include "build/build_config.h"
15 #include "content/public/browser/global_request_id.h"
16 #include "content/public/browser/reload_type.h"
17 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/site_instance.h"
19 #include "content/public/common/impression.h"
20 #include "content/public/common/referrer.h"
21 #include "content/public/common/was_activated_option.mojom.h"
22 #include "services/network/public/cpp/resource_request_body.h"
23 #include "services/network/public/cpp/shared_url_loader_factory.h"
24 #include "ui/base/page_transition_types.h"
25 #include "ui/base/window_open_disposition.h"
26 #include "ui/gfx/geometry/rect.h"
27 #include "url/gurl.h"
28 
29 #if !defined(OS_ANDROID)
30 #include "chrome/browser/ui/tabs/tab_strip_model.h"
31 #include "components/tab_groups/tab_group_id.h"
32 #endif
33 
34 class Browser;
35 class Profile;
36 
37 namespace content {
38 class RenderFrameHost;
39 class WebContents;
40 struct OpenURLParams;
41 }  // namespace content
42 
43 // Parameters that tell Navigate() what to do.
44 //
45 // Some basic examples:
46 //
47 // Simple Navigate to URL in current tab:
48 // NavigateParams params(browser, GURL("http://www.google.com/"),
49 //                               ui::PAGE_TRANSITION_LINK);
50 // Navigate(&params);
51 //
52 // Open bookmark in new background tab:
53 // NavigateParams params(browser, url,
54 //                               ui::PAGE_TRANSITION_AUTO_BOOKMARK);
55 // params.disposition = NEW_BACKGROUND_TAB;
56 // Navigate(&params);
57 //
58 // Opens a popup WebContents:
59 // NavigateParams params(browser, popup_contents);
60 // params.source_contents = source_contents;
61 // Navigate(&params);
62 //
63 // See browser_navigator_browsertest.cc for more examples.
64 
65 // TODO(thestig): Split or ifdef out more fields that are not used on Android.
66 struct NavigateParams {
67 #if defined(OS_ANDROID)
68   explicit NavigateParams(
69       std::unique_ptr<content::WebContents> contents_to_insert);
70 #else
71   NavigateParams(Browser* browser,
72                  const GURL& a_url,
73                  ui::PageTransition a_transition);
74   NavigateParams(Browser* browser,
75                  std::unique_ptr<content::WebContents> contents_to_insert);
76 #endif
77   NavigateParams(Profile* profile,
78                  const GURL& a_url,
79                  ui::PageTransition a_transition);
80   NavigateParams(NavigateParams&& params);
81   ~NavigateParams();
82 
83   // Copies fields from |params| struct to |nav_params| struct.
84   void FillNavigateParamsFromOpenURLParams(
85       const content::OpenURLParams& params);
86 
87   // The URL/referrer to be loaded. Ignored if |contents_to_insert| is non-NULL.
88   GURL url;
89   content::Referrer referrer;
90 
91   // The routing id of the initiator of the navigation. This is best effort: it
92   // is only defined for some renderer-initiated navigations (e.g., not drag and
93   // drop), and the frame with the corresponding routing ID may have been
94   // deleted before the navigation begins.
95   content::GlobalFrameRoutingId initiator_routing_id;
96 
97   // The origin of the initiator of the navigation.
98   base::Optional<url::Origin> initiator_origin;
99 
100   // The frame name to be used for the main frame.
101   std::string frame_name;
102 
103   // The browser-global ID of the frame to navigate, or
104   // content::RenderFrameHost::kNoFrameTreeNodeId for the main frame.
105   int frame_tree_node_id = content::RenderFrameHost::kNoFrameTreeNodeId;
106 
107   // Any redirect URLs that occurred for this navigation before |url|.
108   // Usually empty.
109   std::vector<GURL> redirect_chain;
110 
111   // The post data when the navigation uses POST.
112   scoped_refptr<network::ResourceRequestBody> post_data;
113 
114   // Extra headers to add to the request for this page.  Headers are
115   // represented as "<name>: <value>" and separated by \r\n.  The entire string
116   // is terminated by \r\n.  May be empty if no extra headers are needed.
117   std::string extra_headers;
118 
119   // Input parameter.
120   // WebContents to be inserted into the target Browser's tabstrip. If NULL,
121   // |url| or the homepage will be used instead. When non-NULL, Navigate()
122   // assumes it has already been navigated to its intended destination and will
123   // not load any URL in it (i.e. |url| is ignored). Default is NULL.
124   std::unique_ptr<content::WebContents> contents_to_insert;
125 
126   // Input parameter.
127   // Only used by Singleton tabs. Causes a tab-switch in addition to navigation.
128   content::WebContents* switch_to_singleton_tab = nullptr;
129 
130   // Output parameter.
131   // The WebContents in which the navigation occurred or that was inserted.
132   // Guaranteed non-NULL except for note below:
133   //
134   // Note: If this field is set to NULL by the caller and Navigate() creates a
135   // new WebContents, this field will remain NULL and the WebContents deleted if
136   // the WebContents it created is not added to a TabStripModel before
137   // Navigate() returns.
138   content::WebContents* navigated_or_inserted_contents = nullptr;
139 
140   // [in]  The WebContents that initiated the Navigate() request if such
141   //       context is necessary. Default is NULL, i.e. no context.
142   // [out] If NULL, this value will be set to the selected WebContents in
143   //       the originating browser prior to the operation performed by
144   //       Navigate(). However, if the originating page is from a different
145   //       profile (e.g. an OFF_THE_RECORD page originating from a non-OTR
146   //       window), then |source_contents| is reset to NULL.
147   content::WebContents* source_contents = nullptr;
148 
149   // The disposition requested by the navigation source. Default is
150   // CURRENT_TAB. What follows is a set of coercions that happen to this value
151   // when other factors are at play:
152   //
153   // [in]:                Condition:                        [out]:
154   // NEW_BACKGROUND_TAB   target browser tabstrip is empty  NEW_FOREGROUND_TAB
155   // CURRENT_TAB          "     "     "                     NEW_FOREGROUND_TAB
156   // NEW_BACKGROUND_TAB   target browser is an app browser  NEW_FOREGROUND_TAB
157   // OFF_THE_RECORD       target browser profile is incog.  NEW_FOREGROUND_TAB
158   //
159   // If disposition is NEW_BACKGROUND_TAB, TabStripModel::ADD_ACTIVE is
160   // removed from |tabstrip_add_types| automatically.
161   // If disposition is one of NEW_WINDOW, NEW_POPUP, NEW_FOREGROUND_TAB or
162   // SINGLETON_TAB, then TabStripModel::ADD_ACTIVE is automatically added to
163   // |tabstrip_add_types|.
164   WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB;
165 
166   // Allows setting the opener for the case when new WebContents are created
167   // (i.e. when |disposition| asks for a new tab or window).
168   content::RenderFrameHost* opener = nullptr;
169 
170   // Sets browser->is_trusted_source.
171   bool trusted_source = false;
172 
173   // The transition type of the navigation.
174   ui::PageTransition transition = ui::PAGE_TRANSITION_LINK;
175 
176   // Whether this navigation was initiated by the renderer process.
177   bool is_renderer_initiated = false;
178 
179   // The index the caller would like the tab to be positioned at in the
180   // TabStrip. The actual index will be determined by the TabHandler in
181   // accordance with |add_types|. The default allows the TabHandler to decide.
182   int tabstrip_index = -1;
183 
184   // If non-empty, the new tab is an app tab.
185   std::string extension_app_id;
186 
187   // If non-empty, specifies the desired initial position and size of the
188   // window if |disposition| == NEW_POPUP.
189   // TODO(beng): Figure out if this can be used to create Browser windows
190   //             for other callsites that use set_override_bounds, or
191   //             remove this comment.
192   gfx::Rect window_bounds;
193 
194   // Determines if and how the target window should be made visible at the end
195   // of the call to Navigate().
196   enum WindowAction {
197     // Do not show or activate the browser window after navigating.
198     NO_ACTION,
199     // Show and activate the browser window after navigating.
200     SHOW_WINDOW,
201     // Show the browser window after navigating but do not activate.
202     SHOW_WINDOW_INACTIVE
203   };
204   // Default is NO_ACTION (don't show or activate the window).
205   // If disposition is NEW_WINDOW or NEW_POPUP, and |window_action| is set to
206   // NO_ACTION, |window_action| will be set to SHOW_WINDOW.
207   WindowAction window_action = NO_ACTION;
208 
209   // Whether the browser is being created for captive portal resolution. If
210   // true, |disposition| should be NEW_POPUP.
211   bool is_captive_portal_popup = false;
212 
213   // If false then the navigation was not initiated by a user gesture.
214   bool user_gesture = true;
215 
216   // What to do with the path component of the URL for singleton navigations.
217   enum PathBehavior {
218     // Two URLs with differing paths are different.
219     RESPECT,
220     // Ignore path when finding existing tab, navigate to new URL.
221     IGNORE_AND_NAVIGATE,
222   };
223   PathBehavior path_behavior = RESPECT;
224 
225 #if !defined(OS_ANDROID)
226   // [in]  Specifies a Browser object where the navigation could occur or the
227   //       tab could be added. Navigate() is not obliged to use this Browser if
228   //       it is not compatible with the operation being performed. This can be
229   //       NULL, in which case |initiating_profile| must be provided.
230   // [out] Specifies the Browser object where the navigation occurred or the
231   //       tab was added. Guaranteed non-NULL unless the disposition did not
232   //       require a navigation, in which case this is set to NULL
233   //       (SAVE_TO_DISK, IGNORE_ACTION).
234   // Note: If |show_window| is set to false and a new Browser is created by
235   //       Navigate(), the caller is responsible for showing it so that its
236   //       window can assume responsibility for the Browser's lifetime (Browser
237   //       objects are deleted when the user closes a visible browser window).
238   Browser* browser = nullptr;
239 
240   // The group the caller would like the tab to be added to.
241   base::Optional<tab_groups::TabGroupId> group;
242 
243   // A bitmask of values defined in TabStripModel::AddTabTypes. Helps
244   // determine where to insert a new tab and whether or not it should be
245   // selected, among other properties.
246   int tabstrip_add_types = TabStripModel::ADD_ACTIVE;
247 #endif
248 
249   // The profile that is initiating the navigation. If there is a non-NULL
250   // browser passed in via |browser|, it's profile will be used instead.
251   Profile* initiating_profile = nullptr;
252 
253   // Indicates whether this navigation  should replace the current
254   // navigation entry.
255   bool should_replace_current_entry = false;
256 
257   // Indicates whether |contents_to_insert| is being created with a
258   // window.opener.
259   bool created_with_opener = false;
260 
261   // Whether or not the related navigation was started in the context menu.
262   bool started_from_context_menu = false;
263 
264   // SiteInstance of the frame that initiated the navigation or null if we
265   // don't know it. This should be assigned from the OpenURLParams of the
266   // WebContentsDelegate::OpenURLFromTab implementation and is used to determine
267   // the SiteInstance that will be used for the resulting frame in the case of
268   // an about:blank or a data url navigation.
269   scoped_refptr<content::SiteInstance> source_site_instance;
270 
271   // Optional URLLoaderFactory to facilitate blob URL loading.
272   scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory;
273 
274   // Indicates that the navigation should happen in an pwa window if
275   // possible, i.e. if the is a PWA installed for the target URL.
276   bool open_pwa_window_if_possible = false;
277 
278   // The time when the input which led to the navigation occurred. Currently
279   // only set when a link is clicked or the navigation takes place from the
280   // desktop omnibox.
281   base::TimeTicks input_start;
282 
283   // Indicates that the new page should have a propagated user activation.
284   // This should be used when we want to pass an activation that occurred
285   // outside of the page and pass it to the page as if it happened on a prior
286   // page. For example, if the assistant opens a page we should treat the
287   // user's interaction with the assistant as a previous user activation.
288   content::mojom::WasActivatedOption was_activated =
289       content::mojom::WasActivatedOption::kUnknown;
290 
291   // If this navigation was initiated from a link that specified the
292   // hrefTranslate attribute, this contains the attribute's value (a BCP47
293   // language code). Empty otherwise.
294   std::string href_translate;
295 
296   // Indicates the reload type of this navigation.
297   content::ReloadType reload_type = content::ReloadType::NONE;
298 
299   // Optional impression associated with this navigation. Only set on
300   // navigations that originate from links with impression attributes. Used for
301   // conversion measurement.
302   base::Optional<content::Impression> impression;
303 
304  private:
305   NavigateParams();
306   DISALLOW_COPY_AND_ASSIGN(NavigateParams);
307 };
308 
309 #endif  // CHROME_BROWSER_UI_BROWSER_NAVIGATOR_PARAMS_H_
310