1// Copyright (c) 2012 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_H_
6#define CHROME_BROWSER_UI_BROWSER_NAVIGATOR_H_
7
8#include <string>
9
10#include "base/memory/ref_counted.h"
11#include "base/memory/ref_counted_memory.h"
12#include "chrome/browser/ui/host_desktop.h"
13#include "content/public/browser/browser_context.h"
14#include "content/public/browser/global_request_id.h"
15#include "content/public/browser/page_navigator.h"
16#include "content/public/common/referrer.h"
17#include "ui/base/page_transition_types.h"
18#include "ui/base/window_open_disposition.h"
19#include "ui/gfx/rect.h"
20#include "url/gurl.h"
21
22class Browser;
23class Profile;
24
25namespace content {
26class WebContents;
27}
28
29namespace chrome {
30
31// Parameters that tell Navigate() what to do.
32//
33// Some basic examples:
34//
35// Simple Navigate to URL in current tab:
36// chrome::NavigateParams params(browser, GURL("http://www.google.com/"),
37//                               ui::PAGE_TRANSITION_LINK);
38// chrome::Navigate(&params);
39//
40// Open bookmark in new background tab:
41// chrome::NavigateParams params(browser, url,
42//                               ui::PAGE_TRANSITION_AUTO_BOOKMARK);
43// params.disposition = NEW_BACKGROUND_TAB;
44// chrome::Navigate(&params);
45//
46// Opens a popup WebContents:
47// chrome::NavigateParams params(browser, popup_contents);
48// params.source_contents = source_contents;
49// chrome::Navigate(&params);
50//
51// See browser_navigator_browsertest.cc for more examples.
52//
53struct NavigateParams {
54  NavigateParams(Browser* browser,
55                 const GURL& a_url,
56                 ui::PageTransition a_transition);
57  NavigateParams(Browser* browser,
58                 content::WebContents* a_target_contents);
59  NavigateParams(Profile* profile,
60                 const GURL& a_url,
61                 ui::PageTransition a_transition);
62  ~NavigateParams();
63
64  // The URL/referrer to be loaded. Ignored if |target_contents| is non-NULL.
65  GURL url;
66  content::Referrer referrer;
67
68  // The browser-global ID of the frame to navigate, or -1 for the main frame.
69  int64 frame_tree_node_id;
70
71  // Any redirect URLs that occurred for this navigation before |url|.
72  // Usually empty.
73  std::vector<GURL> redirect_chain;
74
75  // Indicates whether this navigation will be sent using POST.
76  // The POST method is limited support for basic POST data by leveraging
77  // NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST.
78  // It is not for things like file uploads.
79  bool uses_post;
80
81  // The post data when the navigation uses POST.
82  scoped_refptr<base::RefCountedMemory> browser_initiated_post_data;
83
84  // Extra headers to add to the request for this page.  Headers are
85  // represented as "<name>: <value>" and separated by \r\n.  The entire string
86  // is terminated by \r\n.  May be empty if no extra headers are needed.
87  std::string extra_headers;
88
89  // [in]  A WebContents to be navigated or inserted into the target
90  //       Browser's tabstrip. If NULL, |url| or the homepage will be used
91  //       instead. When non-NULL, Navigate() assumes it has already been
92  //       navigated to its intended destination and will not load any URL in it
93  //       (i.e. |url| is ignored).
94  //       Default is NULL.
95  // [out] The WebContents in which the navigation occurred or that was
96  //       inserted. Guaranteed non-NULL except for note below:
97  // Note: If this field is set to NULL by the caller and Navigate() creates
98  //       a new WebContents, this field will remain NULL and the
99  //       WebContents deleted if the WebContents it created is
100  //       not added to a TabStripModel before Navigate() returns.
101  content::WebContents* target_contents;
102
103  // [in]  The WebContents that initiated the Navigate() request if such
104  //       context is necessary. Default is NULL, i.e. no context.
105  // [out] If NULL, this value will be set to the selected WebContents in
106  //       the originating browser prior to the operation performed by
107  //       Navigate(). However, if the originating page is from a different
108  //       profile (e.g. an OFF_THE_RECORD page originating from a non-OTR
109  //       window), then |source_contents| is reset to NULL.
110  content::WebContents* source_contents;
111
112  // The disposition requested by the navigation source. Default is
113  // CURRENT_TAB. What follows is a set of coercions that happen to this value
114  // when other factors are at play:
115  //
116  // [in]:                Condition:                        [out]:
117  // NEW_BACKGROUND_TAB   target browser tabstrip is empty  NEW_FOREGROUND_TAB
118  // CURRENT_TAB          "     "     "                     NEW_FOREGROUND_TAB
119  // OFF_THE_RECORD       target browser profile is incog.  NEW_FOREGROUND_TAB
120  //
121  // If disposition is NEW_BACKGROUND_TAB, TabStripModel::ADD_ACTIVE is
122  // removed from |tabstrip_add_types| automatically.
123  // If disposition is one of NEW_WINDOW, NEW_POPUP, NEW_FOREGROUND_TAB or
124  // SINGLETON_TAB, then TabStripModel::ADD_ACTIVE is automatically added to
125  // |tabstrip_add_types|.
126  WindowOpenDisposition disposition;
127
128  // Sets browser->is_trusted_source. Default is false.
129  bool trusted_source;
130
131  // The transition type of the navigation. Default is
132  // ui::PAGE_TRANSITION_LINK when target_contents is specified in the
133  // constructor.
134  ui::PageTransition transition;
135
136  // Whether this navigation was initiated by the renderer process. Default is
137  // false.
138  bool is_renderer_initiated;
139
140  // The index the caller would like the tab to be positioned at in the
141  // TabStrip. The actual index will be determined by the TabHandler in
142  // accordance with |add_types|. Defaults to -1 (allows the TabHandler to
143  // decide).
144  int tabstrip_index;
145
146  // A bitmask of values defined in TabStripModel::AddTabTypes. Helps
147  // determine where to insert a new tab and whether or not it should be
148  // selected, among other properties. Default is ADD_ACTIVE.
149  int tabstrip_add_types;
150
151  // If non-empty, the new tab is an app tab.
152  std::string extension_app_id;
153
154  // If non-empty, specifies the desired initial position and size of the
155  // window if |disposition| == NEW_POPUP.
156  // TODO(beng): Figure out if this can be used to create Browser windows
157  //             for other callsites that use set_override_bounds, or
158  //             remove this comment.
159  gfx::Rect window_bounds;
160
161  // Determines if and how the target window should be made visible at the end
162  // of the call to Navigate().
163  enum WindowAction {
164    // Do not show or activate the browser window after navigating.
165    NO_ACTION,
166    // Show and activate the browser window after navigating.
167    SHOW_WINDOW,
168    // Show the browser window after navigating but do not activate.
169    SHOW_WINDOW_INACTIVE
170  };
171  // Default is NO_ACTION (don't show or activate the window).
172  // If disposition is NEW_WINDOW or NEW_POPUP, and |window_action| is set to
173  // NO_ACTION, |window_action| will be set to SHOW_WINDOW.
174  WindowAction window_action;
175
176  // If false then the navigation was not initiated by a user gesture.
177  // Default is true.
178  bool user_gesture;
179
180  // What to do with the path component of the URL for singleton navigations.
181  enum PathBehavior {
182    // Two URLs with differing paths are different.
183    RESPECT,
184    // Ignore path when finding existing tab, navigate to new URL.
185    IGNORE_AND_NAVIGATE,
186    // Ignore path when finding existing tab, don't navigate tab.
187    IGNORE_AND_STAY_PUT,
188  };
189  // Default is RESPECT.
190  PathBehavior path_behavior;
191
192  // What to do with the ref component of the URL for singleton navigations.
193  enum RefBehavior {
194    // Two URLs with differing refs are same.
195    IGNORE_REF,
196    // Two URLs with differing refs are different.
197    RESPECT_REF,
198  };
199  // Default is IGNORE.
200  RefBehavior ref_behavior;
201
202  // [in]  Specifies a Browser object where the navigation could occur or the
203  //       tab could be added. Navigate() is not obliged to use this Browser if
204  //       it is not compatible with the operation being performed. This can be
205  //       NULL, in which case |initiating_profile| must be provided.
206  // [out] Specifies the Browser object where the navigation occurred or the
207  //       tab was added. Guaranteed non-NULL unless the disposition did not
208  //       require a navigation, in which case this is set to NULL
209  //       (SUPPRESS_OPEN, SAVE_TO_DISK, IGNORE_ACTION).
210  // Note: If |show_window| is set to false and a new Browser is created by
211  //       Navigate(), the caller is responsible for showing it so that its
212  //       window can assume responsibility for the Browser's lifetime (Browser
213  //       objects are deleted when the user closes a visible browser window).
214  Browser* browser;
215
216  // The profile that is initiating the navigation. If there is a non-NULL
217  // browser passed in via |browser|, it's profile will be used instead.
218  Profile* initiating_profile;
219
220  // Refers to a navigation that was parked in the browser in order to be
221  // transferred to another RVH. Only used in case of a redirection of a request
222  // to a different site that created a new RVH.
223  content::GlobalRequestID transferred_global_request_id;
224
225  // Refers to which desktop this navigation should occur on. May be passed
226  // explicitly or inferred from an existing Browser instance.
227  chrome::HostDesktopType host_desktop_type;
228
229  // Indicates whether this navigation  should replace the current
230  // navigation entry.
231  bool should_replace_current_entry;
232
233  // Indicates whether |source_contents| should be set as opener when creating
234  // |target_contents|.
235  bool should_set_opener;
236
237 private:
238  NavigateParams();
239};
240
241// Copies fields from |params| struct to |nav_params| struct.
242void FillNavigateParamsFromOpenURLParams(chrome::NavigateParams* nav_params,
243                                         const content::OpenURLParams& params);
244
245// Navigates according to the configuration specified in |params|.
246void Navigate(NavigateParams* params);
247
248// Returns true if the url is allowed to open in incognito window.
249bool IsURLAllowedInIncognito(const GURL& url,
250                             content::BrowserContext* browser_context);
251
252}  // namespace chrome
253
254#endif  // CHROME_BROWSER_UI_BROWSER_NAVIGATOR_H_
255