OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 5 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 27 matching lines...) Expand all Loading... | |
38 // any navigation that can be considered to be the start of a new task distinct | 38 // any navigation that can be considered to be the start of a new task distinct |
39 // from what had previously occurred in that tab). | 39 // from what had previously occurred in that tab). |
40 bool ShouldForgetOpenersForTransition(content::PageTransition transition) { | 40 bool ShouldForgetOpenersForTransition(content::PageTransition transition) { |
41 return transition == content::PAGE_TRANSITION_TYPED || | 41 return transition == content::PAGE_TRANSITION_TYPED || |
42 transition == content::PAGE_TRANSITION_AUTO_BOOKMARK || | 42 transition == content::PAGE_TRANSITION_AUTO_BOOKMARK || |
43 transition == content::PAGE_TRANSITION_GENERATED || | 43 transition == content::PAGE_TRANSITION_GENERATED || |
44 transition == content::PAGE_TRANSITION_KEYWORD || | 44 transition == content::PAGE_TRANSITION_KEYWORD || |
45 transition == content::PAGE_TRANSITION_AUTO_TOPLEVEL; | 45 transition == content::PAGE_TRANSITION_AUTO_TOPLEVEL; |
46 } | 46 } |
47 | 47 |
48 // CloseTracker is used when closing a set of WebContents. It listens for | |
49 // deletions of the WebContents and removes from the internal set any time one | |
50 // is deleted. | |
51 class CloseTracker : public content::NotificationObserver { | |
52 public: | |
53 typedef std::vector<content::WebContents*> Contents; | |
Avi (use Gerrit)
2013/06/03 18:41:35
Note that we have "using content::WebContents" abo
| |
54 | |
55 explicit CloseTracker(const Contents& contents); | |
56 virtual ~CloseTracker(); | |
57 | |
58 // Returns true if there is another WebContents in the Tracker. | |
59 bool HasNext() const; | |
60 | |
61 // Returns the next WebContents, or NULL if there are no more. | |
62 content::WebContents* Next(); | |
63 | |
64 private: | |
65 // NotificationObserver: | |
66 virtual void Observe(int type, | |
67 const content::NotificationSource& source, | |
68 const content::NotificationDetails& details) OVERRIDE; | |
69 | |
70 Contents contents_; | |
71 | |
72 content::NotificationRegistrar registrar_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(CloseTracker); | |
75 }; | |
76 | |
77 CloseTracker::CloseTracker(const Contents& contents) | |
78 : contents_(contents) { | |
79 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
80 content::NotificationService::AllBrowserContextsAndSources()); | |
81 } | |
82 | |
83 CloseTracker::~CloseTracker() { | |
84 } | |
85 | |
86 bool CloseTracker::HasNext() const { | |
87 return !contents_.empty(); | |
88 } | |
89 | |
90 content::WebContents* CloseTracker::Next() { | |
91 if (contents_.empty()) | |
92 return NULL; | |
93 | |
94 content::WebContents* web_contents = contents_[0]; | |
95 contents_.erase(contents_.begin()); | |
96 return web_contents; | |
97 } | |
98 | |
99 void CloseTracker::Observe(int type, | |
100 const content::NotificationSource& source, | |
101 const content::NotificationDetails& details) { | |
102 content::WebContents* web_contents = | |
103 content::Source<WebContents>(source).ptr(); | |
104 Contents::iterator i = | |
105 std::find(contents_.begin(), contents_.end(), web_contents); | |
106 if (i != contents_.end()) | |
107 contents_.erase(i); | |
108 } | |
109 | |
48 } // namespace | 110 } // namespace |
49 | 111 |
50 /////////////////////////////////////////////////////////////////////////////// | 112 /////////////////////////////////////////////////////////////////////////////// |
51 // TabStripModel, public: | 113 // TabStripModel, public: |
52 | 114 |
53 TabStripModel::TabStripModel(TabStripModelDelegate* delegate, Profile* profile) | 115 TabStripModel::TabStripModel(TabStripModelDelegate* delegate, Profile* profile) |
54 : delegate_(delegate), | 116 : delegate_(delegate), |
55 profile_(profile), | 117 profile_(profile), |
56 closing_all_(false) { | 118 closing_all_(false) { |
57 DCHECK(delegate_); | 119 DCHECK(delegate_); |
(...skipping 895 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
953 *browser_cmd = 0; | 1015 *browser_cmd = 0; |
954 return false; | 1016 return false; |
955 } | 1017 } |
956 | 1018 |
957 return true; | 1019 return true; |
958 } | 1020 } |
959 | 1021 |
960 /////////////////////////////////////////////////////////////////////////////// | 1022 /////////////////////////////////////////////////////////////////////////////// |
961 // TabStripModel, private: | 1023 // TabStripModel, private: |
962 | 1024 |
1025 std::vector<content::WebContents*> TabStripModel::GetWebContentsFromIndicies( | |
1026 const std::vector<int>& indices) const { | |
1027 std::vector<WebContents*> contents; | |
1028 for (size_t i = 0; i < indices.size(); ++i) | |
1029 contents.push_back(GetWebContentsAtImpl(indices[i])); | |
1030 return contents; | |
1031 } | |
1032 | |
963 void TabStripModel::GetIndicesWithSameDomain(int index, | 1033 void TabStripModel::GetIndicesWithSameDomain(int index, |
964 std::vector<int>* indices) { | 1034 std::vector<int>* indices) { |
965 std::string domain = GetWebContentsAt(index)->GetURL().host(); | 1035 std::string domain = GetWebContentsAt(index)->GetURL().host(); |
966 if (domain.empty()) | 1036 if (domain.empty()) |
967 return; | 1037 return; |
968 for (int i = 0; i < count(); ++i) { | 1038 for (int i = 0; i < count(); ++i) { |
969 if (i == index) | 1039 if (i == index) |
970 continue; | 1040 continue; |
971 if (GetWebContentsAt(i)->GetURL().host() == domain) | 1041 if (GetWebContentsAt(i)->GetURL().host() == domain) |
972 indices->push_back(i); | 1042 indices->push_back(i); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1007 url.host() == chrome::kChromeUINewTabHost && | 1077 url.host() == chrome::kChromeUINewTabHost && |
1008 contents == GetWebContentsAtImpl(count() - 1) && | 1078 contents == GetWebContentsAtImpl(count() - 1) && |
1009 contents->GetController().GetEntryCount() == 1; | 1079 contents->GetController().GetEntryCount() == 1; |
1010 } | 1080 } |
1011 | 1081 |
1012 bool TabStripModel::InternalCloseTabs(const std::vector<int>& indices, | 1082 bool TabStripModel::InternalCloseTabs(const std::vector<int>& indices, |
1013 uint32 close_types) { | 1083 uint32 close_types) { |
1014 if (indices.empty()) | 1084 if (indices.empty()) |
1015 return true; | 1085 return true; |
1016 | 1086 |
1017 // Map the indices to WebContentses, that way if deleting a tab deletes | 1087 CloseTracker close_tracker(GetWebContentsFromIndicies(indices)); |
1018 // other tabs we're ok. Crashes seem to indicate during tab deletion other | |
1019 // tabs are getting removed. | |
1020 std::vector<WebContents*> closing_contentses; | |
1021 for (size_t i = 0; i < indices.size(); ++i) | |
1022 closing_contentses.push_back(GetWebContentsAtImpl(indices[i])); | |
1023 | 1088 |
1024 // We only try the fast shutdown path if the whole browser process is *not* | 1089 // We only try the fast shutdown path if the whole browser process is *not* |
1025 // shutting down. Fast shutdown during browser termination is handled in | 1090 // shutting down. Fast shutdown during browser termination is handled in |
1026 // BrowserShutdown. | 1091 // BrowserShutdown. |
1027 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID) { | 1092 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID) { |
1028 // Construct a map of processes to the number of associated tabs that are | 1093 // Construct a map of processes to the number of associated tabs that are |
1029 // closing. | 1094 // closing. |
1030 std::map<content::RenderProcessHost*, size_t> processes; | 1095 std::map<content::RenderProcessHost*, size_t> processes; |
1031 for (size_t i = 0; i < indices.size(); ++i) { | 1096 for (size_t i = 0; i < indices.size(); ++i) { |
1032 WebContents* closing_contents = GetWebContentsAtImpl(indices[i]); | 1097 WebContents* closing_contents = GetWebContentsAtImpl(indices[i]); |
1033 content::RenderProcessHost* process = | 1098 content::RenderProcessHost* process = |
1034 closing_contents->GetRenderProcessHost(); | 1099 closing_contents->GetRenderProcessHost(); |
1035 ++processes[process]; | 1100 ++processes[process]; |
1036 } | 1101 } |
1037 | 1102 |
1038 // Try to fast shutdown the tabs that can close. | 1103 // Try to fast shutdown the tabs that can close. |
1039 for (std::map<content::RenderProcessHost*, size_t>::iterator iter = | 1104 for (std::map<content::RenderProcessHost*, size_t>::iterator iter = |
1040 processes.begin(); iter != processes.end(); ++iter) { | 1105 processes.begin(); iter != processes.end(); ++iter) { |
1041 iter->first->FastShutdownForPageCount(iter->second); | 1106 iter->first->FastShutdownForPageCount(iter->second); |
1042 } | 1107 } |
1043 } | 1108 } |
1044 | 1109 |
1045 // We now return to our regularly scheduled shutdown procedure. | 1110 // We now return to our regularly scheduled shutdown procedure. |
1046 bool retval = true; | 1111 bool retval = true; |
1047 for (size_t i = 0; i < closing_contentses.size(); ++i) { | 1112 while (close_tracker.HasNext()) { |
1048 WebContents* closing_contents = closing_contentses[i]; | 1113 WebContents* closing_contents = close_tracker.Next(); |
1049 int index = GetIndexOfWebContents(closing_contents); | 1114 int index = GetIndexOfWebContents(closing_contents); |
1050 // Make sure we still contain the tab. | 1115 // Make sure we still contain the tab. |
1051 if (index == kNoTab) | 1116 if (index == kNoTab) |
1052 continue; | 1117 continue; |
1053 | 1118 |
1054 closing_contents->OnCloseStarted(); | 1119 closing_contents->OnCloseStarted(); |
1055 | 1120 |
1056 // Update the explicitly closed state. If the unload handlers cancel the | 1121 // Update the explicitly closed state. If the unload handlers cancel the |
1057 // close the state is reset in Browser. We don't update the explicitly | 1122 // close the state is reset in Browser. We don't update the explicitly |
1058 // closed state if already marked as explicitly closed as unload handlers | 1123 // closed state if already marked as explicitly closed as unload handlers |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1219 void TabStripModel::ForgetOpenersAndGroupsReferencing( | 1284 void TabStripModel::ForgetOpenersAndGroupsReferencing( |
1220 const WebContents* tab) { | 1285 const WebContents* tab) { |
1221 for (WebContentsDataVector::const_iterator i = contents_data_.begin(); | 1286 for (WebContentsDataVector::const_iterator i = contents_data_.begin(); |
1222 i != contents_data_.end(); ++i) { | 1287 i != contents_data_.end(); ++i) { |
1223 if ((*i)->group == tab) | 1288 if ((*i)->group == tab) |
1224 (*i)->group = NULL; | 1289 (*i)->group = NULL; |
1225 if ((*i)->opener == tab) | 1290 if ((*i)->opener == tab) |
1226 (*i)->opener = NULL; | 1291 (*i)->opener = NULL; |
1227 } | 1292 } |
1228 } | 1293 } |
OLD | NEW |