15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/stl_util.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/app/chrome_command_ids.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/browser_shutdown.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/defaults.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/tab_helper.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model_delegate.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model_order_controller.h"
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/ui/tabs/tab_utils.h"
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/ui/web_contents_sizer.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/web_modal/popup_manager.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/render_process_host.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/user_metrics.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "content/public/browser/web_contents_observer.h"
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using base::UserMetricsAction;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::WebContents;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if the specified transition is one of the types that cause the
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// opener relationships for the tab in which the transition occurred to be
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// forgotten. This is generally any navigation that isn't a link click (i.e.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// any navigation that can be considered to be the start of a new task distinct
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// from what had previously occurred in that tab).
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool ShouldForgetOpenersForTransition(ui::PageTransition transition) {
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return transition == ui::PAGE_TRANSITION_TYPED ||
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      transition == ui::PAGE_TRANSITION_AUTO_BOOKMARK ||
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      transition == ui::PAGE_TRANSITION_GENERATED ||
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      transition == ui::PAGE_TRANSITION_KEYWORD ||
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      transition == ui::PAGE_TRANSITION_AUTO_TOPLEVEL;
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// CloseTracker is used when closing a set of WebContents. It listens for
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// deletions of the WebContents and removes from the internal set any time one
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// is deleted.
510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)class CloseTracker {
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef std::vector<WebContents*> Contents;
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  explicit CloseTracker(const Contents& contents);
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual ~CloseTracker();
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns true if there is another WebContents in the Tracker.
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool HasNext() const;
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns the next WebContents, or NULL if there are no more.
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  WebContents* Next();
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  class DeletionObserver : public content::WebContentsObserver {
660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)   public:
670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    DeletionObserver(CloseTracker* parent, WebContents* web_contents)
680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        : WebContentsObserver(web_contents),
690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)          parent_(parent) {
700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    }
710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)   private:
730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    // WebContentsObserver:
74010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    virtual void WebContentsDestroyed() OVERRIDE {
750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      parent_->OnWebContentsDestroyed(this);
760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    }
770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    CloseTracker* parent_;
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(DeletionObserver);
810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  };
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void OnWebContentsDestroyed(DeletionObserver* observer);
840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  typedef std::vector<DeletionObserver*> Observers;
860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  Observers observers_;
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(CloseTracker);
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)CloseTracker::CloseTracker(const Contents& contents) {
920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  for (size_t i = 0; i < contents.size(); ++i)
930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    observers_.push_back(new DeletionObserver(this, contents[i]));
94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)CloseTracker::~CloseTracker() {
970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  DCHECK(observers_.empty());
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool CloseTracker::HasNext() const {
1010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return !observers_.empty();
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)WebContents* CloseTracker::Next() {
1050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (observers_.empty())
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return NULL;
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  DeletionObserver* observer = observers_[0];
1090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  WebContents* web_contents = observer->web_contents();
1100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  observers_.erase(observers_.begin());
1110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  delete observer;
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return web_contents;
113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void CloseTracker::OnWebContentsDestroyed(DeletionObserver* observer) {
1160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  Observers::iterator i =
1170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      std::find(observers_.begin(), observers_.end(), observer);
1180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (i != observers_.end()) {
1190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    delete *i;
1200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    observers_.erase(i);
1210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
1220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  }
1230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  NOTREACHED() << "WebContents destroyed that wasn't in the list";
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
1290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// WebContentsData
1300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// An object to hold a reference to a WebContents that is in a tabstrip, as
1320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// well as other various properties it has.
1330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)class TabStripModel::WebContentsData : public content::WebContentsObserver {
1340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) public:
1350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  WebContentsData(TabStripModel* tab_strip_model, WebContents* a_contents);
1360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Changes the WebContents that this WebContentsData tracks.
1380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void SetWebContents(WebContents* contents);
1390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  WebContents* web_contents() { return contents_; }
1400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Create a relationship between this WebContentsData and other
1420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // WebContentses. Used to identify which WebContents to select next after
1430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // one is closed.
1440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  WebContents* group() const { return group_; }
1450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void set_group(WebContents* value) { group_ = value; }
1460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  WebContents* opener() const { return opener_; }
1470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void set_opener(WebContents* value) { opener_ = value; }
1480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Alters the properties of the WebContents.
1500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool reset_group_on_select() const { return reset_group_on_select_; }
1510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void set_reset_group_on_select(bool value) { reset_group_on_select_ = value; }
1520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool pinned() const { return pinned_; }
1530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void set_pinned(bool value) { pinned_ = value; }
1540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool blocked() const { return blocked_; }
1550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void set_blocked(bool value) { blocked_ = value; }
1560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool discarded() const { return discarded_; }
1570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  void set_discarded(bool value) { discarded_ = value; }
1580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private:
1600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Make sure that if someone deletes this WebContents out from under us, it
1610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // is properly removed from the tab strip.
162010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  virtual void WebContentsDestroyed() OVERRIDE;
1630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // The WebContents being tracked by this WebContentsData. The
1650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // WebContentsObserver does keep a reference, but when the WebContents is
1660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // deleted, the WebContentsObserver reference is NULLed and thus inaccessible.
1670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  WebContents* contents_;
1680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // The TabStripModel containing this WebContents.
1700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  TabStripModel* tab_strip_model_;
1710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // The group is used to model a set of tabs spawned from a single parent
1730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // tab. This value is preserved for a given tab as long as the tab remains
1740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // navigated to the link it was initially opened at or some navigation from
1750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // that page (i.e. if the user types or visits a bookmark or some other
1760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // navigation within that tab, the group relationship is lost). This
1770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // property can safely be used to implement features that depend on a
1780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // logical group of related tabs.
1790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  WebContents* group_;
1800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // The owner models the same relationship as group, except it is more
1820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // easily discarded, e.g. when the user switches to a tab not part of the
1830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // same group. This property is used to determine what tab to select next
1840f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // when one is closed.
1850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  WebContents* opener_;
1860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // True if our group should be reset the moment selection moves away from
1880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // this tab. This is the case for tabs opened in the foreground at the end
1890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // of the TabStrip while viewing another Tab. If these tabs are closed
1900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // before selection moves elsewhere, their opener is selected. But if
1910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // selection shifts to _any_ tab (including their opener), the group
1920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // relationship is reset to avoid confusing close sequencing.
1930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool reset_group_on_select_;
1940f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Is the tab pinned?
1960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool pinned_;
1970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Is the tab interaction blocked by a modal dialog?
1990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool blocked_;
2000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Has the tab data been discarded to save memory?
2020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool discarded_;
2030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(WebContentsData);
2050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)};
2060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)TabStripModel::WebContentsData::WebContentsData(TabStripModel* tab_strip_model,
2080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                                WebContents* contents)
2090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    : content::WebContentsObserver(contents),
2100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      contents_(contents),
2110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      tab_strip_model_(tab_strip_model),
2120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      group_(NULL),
2130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      opener_(NULL),
2140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      reset_group_on_select_(false),
2150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      pinned_(false),
2160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      blocked_(false),
2170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      discarded_(false) {
2180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
2190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void TabStripModel::WebContentsData::SetWebContents(WebContents* contents) {
2210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  contents_ = contents;
2220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  Observe(contents);
2230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
2240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
225010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void TabStripModel::WebContentsData::WebContentsDestroyed() {
226010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK_EQ(contents_, web_contents());
2270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Note that we only detach the contents here, not close it - it's
2290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // already been closed. We just want to undo our bookkeeping.
230010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  int index = tab_strip_model_->GetIndexOfWebContents(web_contents());
2310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  DCHECK_NE(TabStripModel::kNoTab, index);
2320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  tab_strip_model_->DetachWebContentsAt(index);
2330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
2340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TabStripModel, public:
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TabStripModel::TabStripModel(TabStripModelDelegate* delegate, Profile* profile)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : delegate_(delegate),
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      profile_(profile),
241d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      closing_all_(false),
242c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      in_notify_(false),
243c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      weak_factory_(this) {
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(delegate_);
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  order_controller_.reset(new TabStripModelOrderController(this));
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TabStripModel::~TabStripModel() {
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    TabStripModelDeleted());
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  STLDeleteElements(&contents_data_);
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  order_controller_.reset();
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::AddObserver(TabStripModelObserver* observer) {
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  observers_.AddObserver(observer);
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::RemoveObserver(TabStripModelObserver* observer) {
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  observers_.RemoveObserver(observer);
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabStripModel::ContainsIndex(int index) const {
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return index >= 0 && index < count();
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TabStripModel::AppendWebContents(WebContents* contents,
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      bool foreground) {
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InsertWebContentsAt(count(), contents,
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      foreground ? (ADD_INHERIT_GROUP | ADD_ACTIVE) :
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   ADD_NONE);
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TabStripModel::InsertWebContentsAt(int index,
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        WebContents* contents,
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        int add_types) {
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delegate_->WillAddWebContents(contents);
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool active = add_types & ADD_ACTIVE;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Force app tabs to be pinned.
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extensions::TabHelper* extensions_tab_helper =
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      extensions::TabHelper::FromWebContents(contents);
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool pin = extensions_tab_helper->is_app() || add_types & ADD_PINNED;
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  index = ConstrainInsertionIndex(index, pin);
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // In tab dragging situations, if the last tab in the window was detached
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // then the user aborted the drag, we will have the |closing_all_| member
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // set (see DetachWebContentsAt) which will mess with our mojo here. We need
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to clear this bit.
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  closing_all_ = false;
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2920f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Have to get the active contents before we monkey with the contents
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // otherwise we run into problems when we try to change the active contents
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // since the old contents and the new contents will be the same...
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* active_contents = GetActiveWebContents();
2960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  WebContentsData* data = new WebContentsData(this, contents);
2970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  data->set_pinned(pin);
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if ((add_types & ADD_INHERIT_GROUP) && active_contents) {
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (active) {
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Forget any existing relationships, we don't want to make things too
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // confusing by having multiple groups active at the same time.
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ForgetAllOpeners();
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Anything opened by a link we deem to have an opener.
3050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    data->set_group(active_contents);
3060f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    data->set_opener(active_contents);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if ((add_types & ADD_INHERIT_OPENER) && active_contents) {
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (active) {
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Forget any existing relationships, we don't want to make things too
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // confusing by having multiple groups active at the same time.
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ForgetAllOpeners();
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    data->set_opener(active_contents);
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
316116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // TODO(gbillock): Ask the bubble manager whether the WebContents should be
317116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // blocked, or just let the bubble manager make the blocking call directly
318116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // and not use this at all.
319116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  web_modal::PopupManager* popup_manager =
320116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      web_modal::PopupManager::FromWebContents(contents);
321116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (popup_manager)
322116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    data->set_blocked(popup_manager->IsWebModalDialogActive(contents));
323424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  contents_data_.insert(contents_data_.begin() + index, data);
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  selection_model_.IncrementFrom(index);
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    TabInsertedAt(contents, index, active));
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (active) {
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::ListSelectionModel new_model;
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_model.Copy(selection_model_);
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_model.SetSelectedIndex(index);
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetSelection(new_model, NOTIFY_DEFAULT);
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)WebContents* TabStripModel::ReplaceWebContentsAt(int index,
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                 WebContents* new_contents) {
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  delegate_->WillAddWebContents(new_contents);
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WebContents* old_contents = GetWebContentsAtImpl(index);
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ForgetOpenersAndGroupsReferencing(old_contents);
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  contents_data_[index]->SetWebContents(new_contents);
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    TabReplacedAt(this, old_contents, new_contents, index));
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // When the active WebContents is replaced send out a selection notification
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // too. We do this as nearly all observers need to treat a replacement of the
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // selected contents as the selection changing.
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (active_index() == index) {
356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    FOR_EACH_OBSERVER(
357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        TabStripModelObserver,
358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        observers_,
359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        ActiveTabChanged(old_contents,
360c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         new_contents,
361c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         active_index(),
362c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         TabStripModelObserver::CHANGE_REASON_REPLACED));
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return old_contents;
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)WebContents* TabStripModel::DiscardWebContentsAt(int index) {
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Do not discard active tab.
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (active_index() == index)
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WebContents* null_contents =
3742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      WebContents::Create(WebContents::CreateParams(profile()));
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WebContents* old_contents = GetWebContentsAtImpl(index);
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Copy over the state from the navigation controller so we preserve the
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // back/forward history and continue to display the correct title/favicon.
3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  null_contents->GetController().CopyStateFrom(old_contents->GetController());
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Replace the tab we're discarding with the null version.
3802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ReplaceWebContentsAt(index, null_contents);
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Mark the tab so it will reload when we click.
3820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  contents_data_[index]->set_discarded(true);
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Discard the old tab's renderer.
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(jamescook): This breaks script connections with other tabs.
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We need to find a different approach that doesn't do that, perhaps based
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // on navigation to swappedout://.
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  delete old_contents;
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return null_contents;
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)WebContents* TabStripModel::DetachWebContentsAt(int index) {
392d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  CHECK(!in_notify_);
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (contents_data_.empty())
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WebContents* removed_contents = GetWebContentsAtImpl(index);
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool was_selected = IsTabSelected(index);
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int next_selected_index = order_controller_->DetermineNewSelectedIndex(index);
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  delete contents_data_[index];
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  contents_data_.erase(contents_data_.begin() + index);
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ForgetOpenersAndGroupsReferencing(removed_contents);
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (empty())
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    closing_all_ = true;
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    TabDetachedAt(removed_contents, index));
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (empty()) {
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    selection_model_.Clear();
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TabDetachedAt() might unregister observers, so send |TabStripEmpty()| in
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // a second pass.
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FOR_EACH_OBSERVER(TabStripModelObserver, observers_, TabStripEmpty());
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int old_active = active_index();
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    selection_model_.DecrementFrom(index);
4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ui::ListSelectionModel old_model;
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    old_model.Copy(selection_model_);
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (index == old_active) {
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NotifyIfTabDeactivated(removed_contents);
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!selection_model_.empty()) {
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // The active tab was removed, but there is still something selected.
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // Move the active and anchor to the first selected index.
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        selection_model_.set_active(selection_model_.selected_indices()[0]);
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        selection_model_.set_anchor(selection_model_.active());
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else {
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // The active tab was removed and nothing is selected. Reset the
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // selection and send out notification.
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        selection_model_.SetSelectedIndex(next_selected_index);
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NotifyIfActiveTabChanged(removed_contents, NOTIFY_DEFAULT);
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Sending notification in case the detached tab was selected. Using
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // NotifyIfActiveOrSelectionChanged() here would not guarantee that a
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // notification is sent even though the tab selection has changed because
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // |old_model| is stored after calling DecrementFrom().
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (was_selected) {
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        TabSelectionChanged(this, old_model));
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return removed_contents;
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::ActivateTabAt(int index, bool user_gesture) {
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
4472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::ListSelectionModel new_model;
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  new_model.Copy(selection_model_);
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  new_model.SetSelectedIndex(index);
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSelection(new_model, user_gesture ? NOTIFY_USER_GESTURE : NOTIFY_DEFAULT);
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::AddTabAtToSelection(int index) {
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::ListSelectionModel new_model;
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  new_model.Copy(selection_model_);
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  new_model.AddIndexToSelection(index);
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSelection(new_model, NOTIFY_DEFAULT);
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TabStripModel::MoveWebContentsAt(int index,
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      int to_position,
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      bool select_after_move) {
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (index == to_position)
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int first_non_mini_tab = IndexOfFirstNonMiniTab();
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if ((index < first_non_mini_tab && to_position >= first_non_mini_tab) ||
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (to_position < first_non_mini_tab && index >= first_non_mini_tab)) {
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This would result in mini tabs mixed with non-mini tabs. We don't allow
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // that.
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MoveWebContentsAtImpl(index, to_position, select_after_move);
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::MoveSelectedTabsTo(int index) {
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int total_mini_count = IndexOfFirstNonMiniTab();
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int selected_mini_count = 0;
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int selected_count =
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      static_cast<int>(selection_model_.selected_indices().size());
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (int i = 0; i < selected_count &&
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)           IsMiniTab(selection_model_.selected_indices()[i]); ++i) {
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    selected_mini_count++;
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // To maintain that all mini-tabs occur before non-mini-tabs we move them
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // first.
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (selected_mini_count > 0) {
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MoveSelectedTabsToImpl(
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        std::min(total_mini_count - selected_mini_count, index), 0u,
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        selected_mini_count);
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (index > total_mini_count - selected_mini_count) {
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // We're being told to drag mini-tabs to an invalid location. Adjust the
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // index such that non-mini-tabs end up at a location as though we could
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // move the mini-tabs to index. See description in header for more
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // details.
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      index += selected_mini_count;
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (selected_mini_count == selected_count)
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Then move the non-pinned tabs.
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MoveSelectedTabsToImpl(std::max(index, total_mini_count),
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         selected_mini_count,
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         selected_count - selected_mini_count);
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebContents* TabStripModel::GetActiveWebContents() const {
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetWebContentsAt(active_index());
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebContents* TabStripModel::GetWebContentsAt(int index) const {
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (ContainsIndex(index))
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return GetWebContentsAtImpl(index);
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TabStripModel::GetIndexOfWebContents(const WebContents* contents) const {
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < contents_data_.size(); ++i) {
5240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (contents_data_[i]->web_contents() == contents)
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return i;
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return kNoTab;
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TabStripModel::UpdateWebContentsStateAt(int index,
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TabStripModelObserver::TabChangeType change_type) {
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      TabChangedAt(GetWebContentsAtImpl(index), index, change_type));
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::CloseAllTabs() {
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set state so that observers can adjust their behavior to suit this
5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // specific condition when CloseWebContentsAt causes a flurry of
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Close/Detach/Select notifications to be sent.
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  closing_all_ = true;
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<int> closing_tabs;
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (int i = count() - 1; i >= 0; --i)
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    closing_tabs.push_back(i);
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InternalCloseTabs(closing_tabs, CLOSE_CREATE_HISTORICAL_TAB);
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool TabStripModel::CloseWebContentsAt(int index, uint32 close_types) {
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<int> closing_tabs;
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  closing_tabs.push_back(index);
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return InternalCloseTabs(closing_tabs, close_types);
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabStripModel::TabsAreLoading() const {
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (WebContentsDataVector::const_iterator iter = contents_data_.begin();
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       iter != contents_data_.end(); ++iter) {
5590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if ((*iter)->web_contents()->IsLoading())
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return true;
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebContents* TabStripModel::GetOpenerOfWebContentsAt(int index) {
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
5670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return contents_data_[index]->opener();
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::SetOpenerOfWebContentsAt(int index,
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             WebContents* opener) {
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(opener);
5740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  contents_data_[index]->set_opener(opener);
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TabStripModel::GetIndexOfNextWebContentsOpenedBy(const WebContents* opener,
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                     int start_index,
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                     bool use_group) const {
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(opener);
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(start_index));
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check tabs after start_index first.
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (int i = start_index + 1; i < count(); ++i) {
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (OpenerMatches(contents_data_[i], opener, use_group))
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return i;
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Then check tabs before start_index, iterating backwards.
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (int i = start_index - 1; i >= 0; --i) {
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (OpenerMatches(contents_data_[i], opener, use_group))
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return i;
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return kNoTab;
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TabStripModel::GetIndexOfLastWebContentsOpenedBy(const WebContents* opener,
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                     int start_index) const {
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(opener);
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(start_index));
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (int i = contents_data_.size() - 1; i > start_index; --i) {
6020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    if (contents_data_[i]->opener() == opener)
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return i;
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return kNoTab;
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TabStripModel::TabNavigating(WebContents* contents,
6091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                  ui::PageTransition transition) {
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (ShouldForgetOpenersForTransition(transition)) {
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Don't forget the openers if this tab is a New Tab page opened at the
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // end of the TabStrip (e.g. by pressing Ctrl+T). Give the user one
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // navigation of one of these transition types before resetting the
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // opener relationships (this allows for the use case of opening a new
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // tab to do a quick look-up of something while viewing a tab earlier in
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the strip). We can make this heuristic more permissive if need be.
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!IsNewTabAtEndOfTabStrip(contents)) {
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // If the user navigates the current tab to another page in any way
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // other than by clicking a link, we want to pro-actively forget all
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // TabStrip opener relationships since we assume they're beginning a
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // different task by reusing the current tab.
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ForgetAllOpeners();
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // In this specific case we also want to reset the group relationship,
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // since it is now technically invalid.
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ForgetGroup(contents);
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::ForgetAllOpeners() {
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Forget all opener memories so we don't do anything weird with tab
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // re-selection ordering.
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (WebContentsDataVector::const_iterator iter = contents_data_.begin();
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       iter != contents_data_.end(); ++iter)
6350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    (*iter)->set_opener(NULL);
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TabStripModel::ForgetGroup(WebContents* contents) {
6392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int index = GetIndexOfWebContents(contents);
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
6410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  contents_data_[index]->set_group(NULL);
6420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  contents_data_[index]->set_opener(NULL);
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool TabStripModel::ShouldResetGroupOnSelect(WebContents* contents) const {
6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int index = GetIndexOfWebContents(contents);
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
6480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return contents_data_[index]->reset_group_on_select();
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::SetTabBlocked(int index, bool blocked) {
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
6530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (contents_data_[index]->blocked() == blocked)
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
6550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  contents_data_[index]->set_blocked(blocked);
6560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  FOR_EACH_OBSERVER(
6570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      TabStripModelObserver, observers_,
6580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      TabBlockedStateChanged(contents_data_[index]->web_contents(),
6590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                             index));
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::SetTabPinned(int index, bool pinned) {
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
6640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (contents_data_[index]->pinned() == pinned)
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (IsAppTab(index)) {
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!pinned) {
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // App tabs should always be pinned.
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Changing the pinned state of an app tab doesn't affect its mini-tab
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // status.
6750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    contents_data_[index]->set_pinned(pinned);
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The tab is not an app tab, its position may have to change as the
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // mini-tab state is changing.
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int non_mini_tab_index = IndexOfFirstNonMiniTab();
6800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    contents_data_[index]->set_pinned(pinned);
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (pinned && index != non_mini_tab_index) {
6822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      MoveWebContentsAtImpl(index, non_mini_tab_index, false);
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      index = non_mini_tab_index;
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else if (!pinned && index + 1 != non_mini_tab_index) {
6852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      MoveWebContentsAtImpl(index, non_mini_tab_index - 1, false);
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      index = non_mini_tab_index - 1;
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
6900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                      TabMiniStateChanged(contents_data_[index]->web_contents(),
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          index));
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // else: the tab was at the boundary and its position doesn't need to change.
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
6960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                    TabPinnedStateChanged(contents_data_[index]->web_contents(),
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          index));
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabStripModel::IsTabPinned(int index) const {
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
7020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return contents_data_[index]->pinned();
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabStripModel::IsMiniTab(int index) const {
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return IsTabPinned(index) || IsAppTab(index);
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabStripModel::IsAppTab(int index) const {
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WebContents* contents = GetWebContentsAt(index);
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return contents && extensions::TabHelper::FromWebContents(contents)->is_app();
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabStripModel::IsTabBlocked(int index) const {
7150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return contents_data_[index]->blocked();
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabStripModel::IsTabDiscarded(int index) const {
7190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  return contents_data_[index]->discarded();
7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TabStripModel::IndexOfFirstNonMiniTab() const {
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < contents_data_.size(); ++i) {
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!IsMiniTab(static_cast<int>(i)))
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return static_cast<int>(i);
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // No mini-tabs.
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return count();
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TabStripModel::ConstrainInsertionIndex(int index, bool mini_tab) {
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return mini_tab ? std::min(std::max(0, index), IndexOfFirstNonMiniTab()) :
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::min(count(), std::max(index, IndexOfFirstNonMiniTab()));
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::ExtendSelectionTo(int index) {
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
7382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::ListSelectionModel new_model;
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  new_model.Copy(selection_model_);
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  new_model.SetSelectionFromAnchorTo(index);
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSelection(new_model, NOTIFY_DEFAULT);
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::ToggleSelectionAt(int index) {
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
7462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::ListSelectionModel new_model;
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  new_model.Copy(selection_model());
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (selection_model_.IsSelected(index)) {
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (selection_model_.size() == 1) {
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // One tab must be selected and this tab is currently selected so we can't
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // unselect it.
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_model.RemoveIndexFromSelection(index);
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_model.set_anchor(index);
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (new_model.active() == index ||
7572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        new_model.active() == ui::ListSelectionModel::kUnselectedIndex)
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_model.set_active(new_model.selected_indices()[0]);
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_model.AddIndexToSelection(index);
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_model.set_anchor(index);
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_model.set_active(index);
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSelection(new_model, NOTIFY_DEFAULT);
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::AddSelectionFromAnchorTo(int index) {
7682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ui::ListSelectionModel new_model;
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  new_model.Copy(selection_model_);
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  new_model.AddSelectionFromAnchorTo(index);
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSelection(new_model, NOTIFY_DEFAULT);
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabStripModel::IsTabSelected(int index) const {
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(ContainsIndex(index));
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return selection_model_.IsSelected(index);
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::SetSelectionFromModel(
7802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const ui::ListSelectionModel& source) {
7812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_NE(ui::ListSelectionModel::kUnselectedIndex, source.active());
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetSelection(source, NOTIFY_DEFAULT);
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TabStripModel::AddWebContents(WebContents* contents,
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   int index,
7871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                   ui::PageTransition transition,
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   int add_types) {
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the newly-opened tab is part of the same task as the parent tab, we want
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to inherit the parent's "group" attribute, so that if this tab is then
7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // closed we'll jump back to the parent tab.
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP;
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (transition == ui::PAGE_TRANSITION_LINK &&
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (add_types & ADD_FORCE_INDEX) == 0) {
7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We assume tabs opened via link clicks are part of the same task as their
7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // parent.  Note that when |force_index| is true (e.g. when the user
7985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // drag-and-drops a link to the tab strip), callers aren't really handling
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // link clicks, they just want to score the navigation like a link click in
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the history backend, so we don't inherit the group in this case.
8012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    index = order_controller_->DetermineInsertionIndex(transition,
8022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                       add_types & ADD_ACTIVE);
8035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    inherit_group = true;
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // For all other types, respect what was passed to us, normalizing -1s and
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // values that are too large.
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (index < 0 || index > count())
8082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      index = count();
8095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (transition == ui::PAGE_TRANSITION_TYPED && index == count()) {
8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Also, any tab opened at the end of the TabStrip with a "TYPED"
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // transition inherit group as well. This covers the cases where the user
8145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // creates a New Tab (e.g. Ctrl+T, or clicks the New Tab button), or types
8155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // in the address bar and presses Alt+Enter. This allows for opening a new
8165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Tab to quickly look up something. When this Tab is closed, the old one
8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // is re-selected, not the next-adjacent.
8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    inherit_group = true;
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InsertWebContentsAt(index, contents,
8212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      add_types | (inherit_group ? ADD_INHERIT_GROUP : 0));
8225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reset the index, just in case insert ended up moving it on us.
8232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  index = GetIndexOfWebContents(contents);
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (inherit_group && transition == ui::PAGE_TRANSITION_TYPED)
8260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    contents_data_[index]->set_reset_group_on_select(true);
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(sky): figure out why this is here and not in InsertWebContentsAt. When
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // here we seem to get failures in startup perf tests.
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Ensure that the new WebContentsView begins at the same size as the
8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // previous WebContentsView if it existed.  Otherwise, the initial WebKit
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // layout will be performed based on a width of 0 pixels, causing a
8335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // very long, narrow, inaccurate layout.  Because some scripts on pages (as
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // well as WebKit's anchor link location calculation) are run on the
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // initial layout and not recalculated later, we need to ensure the first
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // layout is performed with sane view dimensions even when we're opening a
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // new background tab.
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (WebContents* old_contents = GetActiveWebContents()) {
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if ((add_types & ADD_ACTIVE) == 0) {
8401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      ResizeWebContents(contents, old_contents->GetContainerBounds().size());
8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::CloseSelectedTabs() {
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InternalCloseTabs(selection_model_.selected_indices(),
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    CLOSE_CREATE_HISTORICAL_TAB | CLOSE_USER_GESTURE);
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::SelectNextTab() {
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SelectRelativeTab(true);
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::SelectPreviousTab() {
8555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SelectRelativeTab(false);
8565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::SelectLastTab() {
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ActivateTabAt(count() - 1, true);
8605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::MoveTabNext() {
8635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: this likely needs to be updated for multi-selection.
8645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int new_index = std::min(active_index() + 1, count() - 1);
8652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MoveWebContentsAt(active_index(), new_index, true);
8665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::MoveTabPrevious() {
8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: this likely needs to be updated for multi-selection.
8705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int new_index = std::max(active_index() - 1, 0);
8712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MoveWebContentsAt(active_index(), new_index, true);
8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Context menu functions.
8755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TabStripModel::IsContextMenuCommandEnabled(
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int context_index, ContextMenuCommand command_id) const {
8775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(command_id > CommandFirst && command_id < CommandLast);
8785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (command_id) {
8795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandNewTab:
8805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandCloseTab:
8815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return true;
8825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandReload: {
8845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::vector<int> indices = GetIndicesForCommand(context_index);
8855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (size_t i = 0; i < indices.size(); ++i) {
8865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        WebContents* tab = GetWebContentsAt(indices[i]);
8875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (tab) {
8885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          CoreTabHelperDelegate* core_delegate =
8895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              CoreTabHelper::FromWebContents(tab)->delegate();
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          if (!core_delegate || core_delegate->CanReloadContents(tab))
8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            return true;
8925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
8935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
8945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandCloseOtherTabs:
8985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandCloseTabsToRight:
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return !GetIndicesClosedByCommand(context_index, command_id).empty();
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandDuplicate: {
9025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::vector<int> indices = GetIndicesForCommand(context_index);
9035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (size_t i = 0; i < indices.size(); ++i) {
9045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (delegate_->CanDuplicateContentsAt(indices[i]))
9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          return true;
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
9075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
9085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
9095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandRestoreTab:
9112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return delegate_->GetRestoreTabType() !=
9122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          TabStripModelDelegate::RESTORE_NONE;
9135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandTogglePinned: {
9155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::vector<int> indices = GetIndicesForCommand(context_index);
9165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (size_t i = 0; i < indices.size(); ++i) {
9175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (!IsAppTab(indices[i]))
9185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          return true;
9195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
9205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
9215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
9225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    case CommandToggleTabAudioMuted: {
9241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      std::vector<int> indices = GetIndicesForCommand(context_index);
9251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      for (size_t i = 0; i < indices.size(); ++i) {
9261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        if (!chrome::CanToggleAudioMute(GetWebContentsAt(indices[i])))
9271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          return false;
9281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      }
9291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return true;
9301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
9311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
9325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandBookmarkAllTabs:
9335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return browser_defaults::bookmarks_enabled &&
9345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          delegate_->CanBookmarkAllTabs();
9355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandSelectByDomain:
9375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandSelectByOpener:
9385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return true;
9395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
9415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
9425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
9445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TabStripModel::ExecuteContextMenuCommand(
9475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int context_index, ContextMenuCommand command_id) {
9485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(command_id > CommandFirst && command_id < CommandLast);
9495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (command_id) {
9505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandNewTab:
9515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::RecordAction(UserMetricsAction("TabContextMenu_NewTab"));
9525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      UMA_HISTOGRAM_ENUMERATION("Tab.NewTab",
9535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                TabStripModel::NEW_TAB_CONTEXT_MENU,
9545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                TabStripModel::NEW_TAB_ENUM_COUNT);
955f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      delegate()->AddTabAt(GURL(), context_index + 1, true);
9565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
9575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandReload: {
9595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::RecordAction(UserMetricsAction("TabContextMenu_Reload"));
9605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::vector<int> indices = GetIndicesForCommand(context_index);
9615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (size_t i = 0; i < indices.size(); ++i) {
9625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        WebContents* tab = GetWebContentsAt(indices[i]);
9635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (tab) {
9645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          CoreTabHelperDelegate* core_delegate =
9655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              CoreTabHelper::FromWebContents(tab)->delegate();
9665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          if (!core_delegate || core_delegate->CanReloadContents(tab))
9675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            tab->GetController().Reload(true);
9685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
9695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
9705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
9715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
9725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case CommandDuplicate: {
9745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::RecordAction(UserMetricsAction("TabContextMenu_Duplicate"));
9755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::vector<int> indices = GetIndicesForCommand(context_index);
9765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Copy the WebContents off as the indices will change as tabs are
9775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // duplicated.
9785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::vector<WebContents*> tabs;
9795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (size_t i = 0; i < indices.size(); ++i)
9805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        tabs.push_back(GetWebContentsAt(indices[i]));
981