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