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 #include "chrome/browser/ui/browser_tabstrip.h"
      6 
      7 #include "base/command_line.h"
      8 #include "chrome/browser/profiles/profile.h"
      9 #include "chrome/browser/ui/blocked_content/blocked_content_tab_helper.h"
     10 #include "chrome/browser/ui/browser.h"
     11 #include "chrome/browser/ui/browser_navigator.h"
     12 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
     13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     14 #include "chrome/common/chrome_switches.h"
     15 #include "chrome/common/url_constants.h"
     16 #include "content/public/browser/navigation_entry.h"
     17 #include "content/public/browser/render_view_host.h"
     18 #include "content/public/browser/web_contents.h"
     19 
     20 namespace chrome {
     21 
     22 void AddBlankTabAt(Browser* browser, int index, bool foreground) {
     23   // Time new tab page creation time.  We keep track of the timing data in
     24   // WebContents, but we want to include the time it takes to create the
     25   // WebContents object too.
     26   base::TimeTicks new_tab_start_time = base::TimeTicks::Now();
     27   chrome::NavigateParams params(browser, GURL(chrome::kChromeUINewTabURL),
     28                                 content::PAGE_TRANSITION_TYPED);
     29   params.disposition = foreground ? NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB;
     30   params.tabstrip_index = index;
     31   chrome::Navigate(&params);
     32   CoreTabHelper* core_tab_helper =
     33       CoreTabHelper::FromWebContents(params.target_contents);
     34   core_tab_helper->set_new_tab_start_time(new_tab_start_time);
     35 }
     36 
     37 content::WebContents* AddSelectedTabWithURL(
     38     Browser* browser,
     39     const GURL& url,
     40     content::PageTransition transition) {
     41   NavigateParams params(browser, url, transition);
     42   params.disposition = NEW_FOREGROUND_TAB;
     43   Navigate(&params);
     44   return params.target_contents;
     45 }
     46 
     47 void AddWebContents(Browser* browser,
     48                     content::WebContents* source_contents,
     49                     content::WebContents* new_contents,
     50                     WindowOpenDisposition disposition,
     51                     const gfx::Rect& initial_pos,
     52                     bool user_gesture,
     53                     bool* was_blocked) {
     54   // No code for this yet.
     55   DCHECK(disposition != SAVE_TO_DISK);
     56   // Can't create a new contents for the current tab - invalid case.
     57   DCHECK(disposition != CURRENT_TAB);
     58 
     59   BlockedContentTabHelper* source_blocked_content = NULL;
     60   if (source_contents) {
     61     source_blocked_content =
     62         BlockedContentTabHelper::FromWebContents(source_contents);
     63   }
     64 
     65   if (source_blocked_content) {
     66     // Handle blocking of tabs.
     67     if (source_blocked_content->all_contents_blocked()) {
     68       source_blocked_content->AddWebContents(
     69           new_contents, disposition, initial_pos, user_gesture);
     70       if (was_blocked)
     71         *was_blocked = true;
     72       return;
     73     }
     74 
     75     // Handle blocking of popups.
     76     if ((disposition == NEW_POPUP || disposition == NEW_FOREGROUND_TAB ||
     77          disposition == NEW_BACKGROUND_TAB) && !user_gesture &&
     78         !CommandLine::ForCurrentProcess()->HasSwitch(
     79             switches::kDisablePopupBlocking) &&
     80         CommandLine::ForCurrentProcess()->HasSwitch(
     81             switches::kDisableBetterPopupBlocking)) {
     82       // Unrequested popups from normal pages are constrained unless they're in
     83       // the white list.  The popup owner will handle checking this.
     84       source_blocked_content->AddPopup(
     85           new_contents, disposition, initial_pos, user_gesture);
     86       if (was_blocked)
     87         *was_blocked = true;
     88       return;
     89     }
     90 
     91     new_contents->GetRenderViewHost()->DisassociateFromPopupCount();
     92   }
     93 
     94   NavigateParams params(browser, new_contents);
     95   params.source_contents = source_contents;
     96   params.disposition = disposition;
     97   params.window_bounds = initial_pos;
     98   params.window_action = NavigateParams::SHOW_WINDOW;
     99   params.user_gesture = user_gesture;
    100   Navigate(&params);
    101 }
    102 
    103 void CloseWebContents(Browser* browser,
    104                       content::WebContents* contents,
    105                       bool add_to_history) {
    106   int index = browser->tab_strip_model()->GetIndexOfWebContents(contents);
    107   if (index == TabStripModel::kNoTab) {
    108     NOTREACHED() << "CloseWebContents called for tab not in our strip";
    109     return;
    110   }
    111 
    112   browser->tab_strip_model()->CloseWebContentsAt(
    113       index,
    114       add_to_history ? TabStripModel::CLOSE_CREATE_HISTORICAL_TAB
    115                      : TabStripModel::CLOSE_NONE);
    116 }
    117 
    118 }  // namespace chrome
    119