Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Unified Diff: ios/shared/chrome/browser/tabs/web_state_list.mm

Issue 2680403005: Introduce WebStateList to manage a list of web::WebState. (Closed)
Patch Set: Rebase. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ios/shared/chrome/browser/tabs/web_state_list.mm
diff --git a/ios/shared/chrome/browser/tabs/web_state_list.mm b/ios/shared/chrome/browser/tabs/web_state_list.mm
new file mode 100644
index 0000000000000000000000000000000000000000..cb85ed433ee72233257fc0c3181adf4e9dfaca74
--- /dev/null
+++ b/ios/shared/chrome/browser/tabs/web_state_list.mm
@@ -0,0 +1,123 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "ios/shared/chrome/browser/tabs/web_state_list.h"
+
+#include "base/logging.h"
+#include "base/memory/ptr_util.h"
+#import "ios/shared/chrome/browser/tabs/web_state_list_observer.h"
+#import "ios/web/public/web_state/web_state.h"
+
+#if !defined(__has_feature) || !__has_feature(objc_arc)
+#error "This file requires ARC support."
+#endif
+
+// Wrapper around a WebState stored in a WebStateList. May own the WebState
+// dependending on the WebStateList ownership setting (should always be true
+// once ownership of Tab is sane, see http://crbug.com/546222 for progress).
+class WebStateList::WebStateWrapper {
+ public:
+ WebStateWrapper(web::WebState* web_state, bool assume_ownership);
+ ~WebStateWrapper();
+
+ web::WebState* web_state() const { return web_state_; }
+ void set_web_state(web::WebState* web_state) { web_state_ = web_state; }
+
+ private:
+ web::WebState* web_state_;
+ const bool has_web_state_ownership_;
+
+ DISALLOW_COPY_AND_ASSIGN(WebStateWrapper);
+};
+
+WebStateList::WebStateWrapper::WebStateWrapper(web::WebState* web_state,
+ bool assume_ownership)
+ : web_state_(web_state), has_web_state_ownership_(assume_ownership) {}
+
+WebStateList::WebStateWrapper::~WebStateWrapper() {
+ if (has_web_state_ownership_)
+ delete web_state_;
+}
+
+WebStateList::WebStateList(WebStateOwnership ownership)
+ : web_state_ownership_(ownership) {}
+
+WebStateList::~WebStateList() = default;
+
+bool WebStateList::ContainsIndex(int index) const {
+ return 0 <= index && index < count();
+}
+
+web::WebState* WebStateList::GetWebStateAt(int index) const {
+ DCHECK(ContainsIndex(index));
+ return web_state_wrappers_[index]->web_state();
+}
+
+int WebStateList::GetIndexOfWebState(const web::WebState* web_state) const {
+ for (int index = 0; index < count(); ++index) {
+ if (web_state_wrappers_[index]->web_state() == web_state)
+ return index;
+ }
+ return kInvalidIndex;
+}
+
+void WebStateList::InsertWebState(int index, web::WebState* web_state) {
+ DCHECK(ContainsIndex(index) || index == count());
+ web_state_wrappers_.insert(
+ web_state_wrappers_.begin() + index,
+ base::MakeUnique<WebStateWrapper>(web_state,
+ web_state_ownership_ == WebStateOwned));
+
+ for (auto& observer : observers_)
+ observer.WebStateInsertedAt(this, web_state, index);
+}
+
+void WebStateList::MoveWebStateAt(int from_index, int to_index) {
+ DCHECK(ContainsIndex(from_index));
+ DCHECK(ContainsIndex(to_index));
+ if (from_index == to_index)
+ return;
+
+ std::unique_ptr<WebStateWrapper> web_state_wrapper =
+ std::move(web_state_wrappers_[from_index]);
+ web::WebState* web_state = web_state_wrapper->web_state();
+ web_state_wrappers_.erase(web_state_wrappers_.begin() + from_index);
+ web_state_wrappers_.insert(web_state_wrappers_.begin() + to_index,
+ std::move(web_state_wrapper));
+
+ for (auto& observer : observers_)
+ observer.WebStateMoved(this, web_state, from_index, to_index);
+}
+
+web::WebState* WebStateList::ReplaceWebStateAt(int index,
+ web::WebState* web_state) {
+ DCHECK(ContainsIndex(index));
+ web::WebState* old_web_state = web_state_wrappers_[index]->web_state();
+ web_state_wrappers_[index]->set_web_state(web_state);
+
+ for (auto& observer : observers_)
+ observer.WebStateReplacedAt(this, old_web_state, web_state, index);
+
+ return old_web_state;
+}
+
+void WebStateList::DetachWebStateAt(int index) {
+ DCHECK(ContainsIndex(index));
+ web::WebState* web_state = web_state_wrappers_[index]->web_state();
+ web_state_wrappers_.erase(web_state_wrappers_.begin() + index);
+
+ for (auto& observer : observers_)
+ observer.WebStateDetachedAt(this, web_state, index);
+}
+
+void WebStateList::AddObserver(WebStateListObserver* observer) {
+ observers_.AddObserver(observer);
+}
+
+void WebStateList::RemoveObserver(WebStateListObserver* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+// static
+const int WebStateList::kInvalidIndex;

Powered by Google App Engine
This is Rietveld 408576698