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