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

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: Fix "gn check". 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..009bd0ef62a5a4519574fb8f5999b2735fe051c3
--- /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 owns the WebState
marq (ping after 24h) 2017/02/13 16:32:53 s/owns/own/
sdefresne 2017/02/14 13:39:56 Done.
+// 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 own_web_state);
rohitrao (ping after 24h) 2017/02/13 20:54:29 "bool assume_ownership" maybe? "Own" doesn't alwa
sdefresne 2017/02/14 13:39:56 Done.
+ ~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 own_web_state_;
+
+ DISALLOW_COPY_AND_ASSIGN(WebStateWrapper);
+};
+
+WebStateList::WebStateWrapper::WebStateWrapper(web::WebState* web_state,
+ bool own_web_state)
+ : web_state_(web_state), own_web_state_(own_web_state) {}
+
+WebStateList::WebStateWrapper::~WebStateWrapper() {
+ if (own_web_state_)
+ delete web_state_;
+}
+
+WebStateList::WebStateList(WebStateOwnership ownership)
+ : web_state_ownership_(ownership) {}
+
+WebStateList::~WebStateList() = default;
+
+bool WebStateList::ContainsIndex(size_t index) const {
+ return index < count();
+}
+
+web::WebState* WebStateList::GetWebStateAt(size_t index) const {
+ DCHECK(ContainsIndex(index));
+ return web_state_wrappers_[index]->web_state();
+}
+
+size_t WebStateList::GetIndexOfWebState(const web::WebState* web_state) const {
+ for (size_t index = 0; index < web_state_wrappers_.size(); ++index) {
+ if (web_state_wrappers_[index]->web_state() == web_state)
+ return index;
+ }
+ return kInvalidIndex;
+}
+
+void WebStateList::InsertWebState(size_t 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(size_t from_index, size_t 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(size_t 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::RemoveWebStateAt(size_t 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.WebStateRemovedAt(this, web_state, index);
+}
+
+void WebStateList::AddObserver(WebStateListObserver* observer) {
+ observers_.AddObserver(observer);
+}
+
+void WebStateList::RemoveObserver(WebStateListObserver* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+// static
+const size_t WebStateList::kInvalidIndex;

Powered by Google App Engine
This is Rietveld 408576698