Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / tabs / tab_strip_model.cc
index dfad932..3298943 100644 (file)
 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h"
 #include "chrome/browser/ui/tabs/tab_strip_model_delegate.h"
 #include "chrome/browser/ui/tabs/tab_strip_model_order_controller.h"
+#include "chrome/browser/ui/tabs/tab_utils.h"
+#include "chrome/browser/ui/web_contents_sizer.h"
 #include "chrome/common/url_constants.h"
-#include "components/web_modal/web_contents_modal_dialog_manager.h"
+#include "components/web_modal/popup_manager.h"
 #include "content/public/browser/render_process_host.h"
 #include "content/public/browser/user_metrics.h"
 #include "content/public/browser/web_contents.h"
 #include "content/public/browser/web_contents_observer.h"
-#include "content/public/browser/web_contents_view.h"
-
 using base::UserMetricsAction;
 using content::WebContents;
 
@@ -37,12 +37,12 @@ namespace {
 // forgotten. This is generally any navigation that isn't a link click (i.e.
 // any navigation that can be considered to be the start of a new task distinct
 // from what had previously occurred in that tab).
-bool ShouldForgetOpenersForTransition(content::PageTransition transition) {
-  return transition == content::PAGE_TRANSITION_TYPED ||
-      transition == content::PAGE_TRANSITION_AUTO_BOOKMARK ||
-      transition == content::PAGE_TRANSITION_GENERATED ||
-      transition == content::PAGE_TRANSITION_KEYWORD ||
-      transition == content::PAGE_TRANSITION_AUTO_TOPLEVEL;
+bool ShouldForgetOpenersForTransition(ui::PageTransition transition) {
+  return transition == ui::PAGE_TRANSITION_TYPED ||
+      transition == ui::PAGE_TRANSITION_AUTO_BOOKMARK ||
+      transition == ui::PAGE_TRANSITION_GENERATED ||
+      transition == ui::PAGE_TRANSITION_KEYWORD ||
+      transition == ui::PAGE_TRANSITION_AUTO_TOPLEVEL;
 }
 
 // CloseTracker is used when closing a set of WebContents. It listens for
@@ -69,12 +69,9 @@ class CloseTracker {
           parent_(parent) {
     }
 
-    // Expose web_contents() publicly.
-    using content::WebContentsObserver::web_contents;
-
    private:
     // WebContentsObserver:
-    virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE {
+    virtual void WebContentsDestroyed() OVERRIDE {
       parent_->OnWebContentsDestroyed(this);
     }
 
@@ -162,7 +159,7 @@ class TabStripModel::WebContentsData : public content::WebContentsObserver {
  private:
   // Make sure that if someone deletes this WebContents out from under us, it
   // is properly removed from the tab strip.
-  virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE;
+  virtual void WebContentsDestroyed() OVERRIDE;
 
   // The WebContents being tracked by this WebContentsData. The
   // WebContentsObserver does keep a reference, but when the WebContents is
@@ -225,13 +222,12 @@ void TabStripModel::WebContentsData::SetWebContents(WebContents* contents) {
   Observe(contents);
 }
 
-void TabStripModel::WebContentsData::WebContentsDestroyed(
-    WebContents* web_contents) {
-  DCHECK_EQ(contents_, web_contents);
+void TabStripModel::WebContentsData::WebContentsDestroyed() {
+  DCHECK_EQ(contents_, web_contents());
 
   // Note that we only detach the contents here, not close it - it's
   // already been closed. We just want to undo our bookkeeping.
-  int index = tab_strip_model_->GetIndexOfWebContents(web_contents);
+  int index = tab_strip_model_->GetIndexOfWebContents(web_contents());
   DCHECK_NE(TabStripModel::kNoTab, index);
   tab_strip_model_->DetachWebContentsAt(index);
 }
@@ -243,7 +239,8 @@ TabStripModel::TabStripModel(TabStripModelDelegate* delegate, Profile* profile)
     : delegate_(delegate),
       profile_(profile),
       closing_all_(false),
-      in_notify_(false) {
+      in_notify_(false),
+      weak_factory_(this) {
   DCHECK(delegate_);
   order_controller_.reset(new TabStripModelOrderController(this));
 }
@@ -316,10 +313,13 @@ void TabStripModel::InsertWebContentsAt(int index,
     data->set_opener(active_contents);
   }
 
-  web_modal::WebContentsModalDialogManager* modal_dialog_manager =
-      web_modal::WebContentsModalDialogManager::FromWebContents(contents);
-  if (modal_dialog_manager)
-    data->set_blocked(modal_dialog_manager->IsDialogActive());
+  // TODO(gbillock): Ask the bubble manager whether the WebContents should be
+  // blocked, or just let the bubble manager make the blocking call directly
+  // and not use this at all.
+  web_modal::PopupManager* popup_manager =
+      web_modal::PopupManager::FromWebContents(contents);
+  if (popup_manager)
+    data->set_blocked(popup_manager->IsWebModalDialogActive(contents));
 
   contents_data_.insert(contents_data_.begin() + index, data);
 
@@ -606,7 +606,7 @@ int TabStripModel::GetIndexOfLastWebContentsOpenedBy(const WebContents* opener,
 }
 
 void TabStripModel::TabNavigating(WebContents* contents,
-                                  content::PageTransition transition) {
+                                  ui::PageTransition transition) {
   if (ShouldForgetOpenersForTransition(transition)) {
     // Don't forget the openers if this tab is a New Tab page opened at the
     // end of the TabStrip (e.g. by pressing Ctrl+T). Give the user one
@@ -784,14 +784,14 @@ void TabStripModel::SetSelectionFromModel(
 
 void TabStripModel::AddWebContents(WebContents* contents,
                                    int index,
-                                   content::PageTransition transition,
+                                   ui::PageTransition transition,
                                    int add_types) {
   // If the newly-opened tab is part of the same task as the parent tab, we want
   // to inherit the parent's "group" attribute, so that if this tab is then
   // closed we'll jump back to the parent tab.
   bool inherit_group = (add_types & ADD_INHERIT_GROUP) == ADD_INHERIT_GROUP;
 
-  if (transition == content::PAGE_TRANSITION_LINK &&
+  if (transition == ui::PAGE_TRANSITION_LINK &&
       (add_types & ADD_FORCE_INDEX) == 0) {
     // We assume tabs opened via link clicks are part of the same task as their
     // parent.  Note that when |force_index| is true (e.g. when the user
@@ -808,7 +808,7 @@ void TabStripModel::AddWebContents(WebContents* contents,
       index = count();
   }
 
-  if (transition == content::PAGE_TRANSITION_TYPED && index == count()) {
+  if (transition == ui::PAGE_TRANSITION_TYPED && index == count()) {
     // Also, any tab opened at the end of the TabStrip with a "TYPED"
     // transition inherit group as well. This covers the cases where the user
     // creates a New Tab (e.g. Ctrl+T, or clicks the New Tab button), or types
@@ -822,7 +822,7 @@ void TabStripModel::AddWebContents(WebContents* contents,
   // Reset the index, just in case insert ended up moving it on us.
   index = GetIndexOfWebContents(contents);
 
-  if (inherit_group && transition == content::PAGE_TRANSITION_TYPED)
+  if (inherit_group && transition == ui::PAGE_TRANSITION_TYPED)
     contents_data_[index]->set_reset_group_on_select(true);
 
   // TODO(sky): figure out why this is here and not in InsertWebContentsAt. When
@@ -837,8 +837,7 @@ void TabStripModel::AddWebContents(WebContents* contents,
   // new background tab.
   if (WebContents* old_contents = GetActiveWebContents()) {
     if ((add_types & ADD_ACTIVE) == 0) {
-      contents->GetView()->SizeContents(
-          old_contents->GetView()->GetContainerSize());
+      ResizeWebContents(contents, old_contents->GetContainerBounds().size());
     }
   }
 }
@@ -921,6 +920,15 @@ bool TabStripModel::IsContextMenuCommandEnabled(
       return false;
     }
 
+    case CommandToggleTabAudioMuted: {
+      std::vector<int> indices = GetIndicesForCommand(context_index);
+      for (size_t i = 0; i < indices.size(); ++i) {
+        if (!chrome::CanToggleAudioMute(GetWebContentsAt(indices[i])))
+          return false;
+      }
+      return true;
+    }
+
     case CommandBookmarkAllTabs:
       return browser_defaults::bookmarks_enabled &&
           delegate_->CanBookmarkAllTabs();
@@ -1028,6 +1036,20 @@ void TabStripModel::ExecuteContextMenuCommand(
       break;
     }
 
+    case CommandToggleTabAudioMuted: {
+      const std::vector<int>& indices = GetIndicesForCommand(context_index);
+      const bool mute = !chrome::AreAllTabsMuted(*this, indices);
+      if (mute)
+        content::RecordAction(UserMetricsAction("TabContextMenu_MuteTabs"));
+      else
+        content::RecordAction(UserMetricsAction("TabContextMenu_UnmuteTabs"));
+      for (std::vector<int>::const_iterator i = indices.begin();
+           i != indices.end(); ++i) {
+        chrome::SetTabAudioMuted(GetWebContentsAt(*i), mute);
+      }
+      break;
+    }
+
     case CommandBookmarkAllTabs: {
       content::RecordAction(
           UserMetricsAction("TabContextMenu_BookmarkAllTabs"));
@@ -1190,6 +1212,11 @@ bool TabStripModel::InternalCloseTabs(const std::vector<int>& indices,
 
   CloseTracker close_tracker(GetWebContentsFromIndices(indices));
 
+  base::WeakPtr<TabStripModel> ref(weak_factory_.GetWeakPtr());
+  const bool closing_all = indices.size() == contents_data_.size();
+  if (closing_all)
+    FOR_EACH_OBSERVER(TabStripModelObserver, observers_, WillCloseAllTabs());
+
   // We only try the fast shutdown path if the whole browser process is *not*
   // shutting down. Fast shutdown during browser termination is handled in
   // BrowserShutdown.
@@ -1244,6 +1271,11 @@ bool TabStripModel::InternalCloseTabs(const std::vector<int>& indices,
                      (close_types & CLOSE_CREATE_HISTORICAL_TAB) != 0);
   }
 
+  if (ref && closing_all && !retval) {
+    FOR_EACH_OBSERVER(TabStripModelObserver, observers_,
+                      CloseAllTabsCanceled());
+  }
+
   return retval;
 }