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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "ios/shared/chrome/browser/tabs/web_state_list.h"
6
7 #include "base/logging.h"
8 #include "base/memory/ptr_util.h"
9 #import "ios/shared/chrome/browser/tabs/web_state_list_observer.h"
10 #import "ios/web/public/web_state/web_state.h"
11
12 #if !defined(__has_feature) || !__has_feature(objc_arc)
13 #error "This file requires ARC support."
14 #endif
15
16 // 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.
17 // dependending on the WebStateList ownership setting (should always be true
18 // once ownership of Tab is sane, see http://crbug.com/546222 for progress).
19 class WebStateList::WebStateWrapper {
20 public:
21 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.
22 ~WebStateWrapper();
23
24 web::WebState* web_state() const { return web_state_; }
25 void set_web_state(web::WebState* web_state) { web_state_ = web_state; }
26
27 private:
28 web::WebState* web_state_;
29 const bool own_web_state_;
30
31 DISALLOW_COPY_AND_ASSIGN(WebStateWrapper);
32 };
33
34 WebStateList::WebStateWrapper::WebStateWrapper(web::WebState* web_state,
35 bool own_web_state)
36 : web_state_(web_state), own_web_state_(own_web_state) {}
37
38 WebStateList::WebStateWrapper::~WebStateWrapper() {
39 if (own_web_state_)
40 delete web_state_;
41 }
42
43 WebStateList::WebStateList(WebStateOwnership ownership)
44 : web_state_ownership_(ownership) {}
45
46 WebStateList::~WebStateList() = default;
47
48 bool WebStateList::ContainsIndex(size_t index) const {
49 return index < count();
50 }
51
52 web::WebState* WebStateList::GetWebStateAt(size_t index) const {
53 DCHECK(ContainsIndex(index));
54 return web_state_wrappers_[index]->web_state();
55 }
56
57 size_t WebStateList::GetIndexOfWebState(const web::WebState* web_state) const {
58 for (size_t index = 0; index < web_state_wrappers_.size(); ++index) {
59 if (web_state_wrappers_[index]->web_state() == web_state)
60 return index;
61 }
62 return kInvalidIndex;
63 }
64
65 void WebStateList::InsertWebState(size_t index, web::WebState* web_state) {
66 DCHECK(ContainsIndex(index) || index == count());
67 web_state_wrappers_.insert(
68 web_state_wrappers_.begin() + index,
69 base::MakeUnique<WebStateWrapper>(web_state,
70 web_state_ownership_ == WebStateOwned));
71
72 for (auto& observer : observers_)
73 observer.WebStateInsertedAt(this, web_state, index);
74 }
75
76 void WebStateList::MoveWebStateAt(size_t from_index, size_t to_index) {
77 DCHECK(ContainsIndex(from_index));
78 DCHECK(ContainsIndex(to_index));
79 if (from_index == to_index)
80 return;
81
82 std::unique_ptr<WebStateWrapper> web_state_wrapper =
83 std::move(web_state_wrappers_[from_index]);
84 web::WebState* web_state = web_state_wrapper->web_state();
85 web_state_wrappers_.erase(web_state_wrappers_.begin() + from_index);
86 web_state_wrappers_.insert(web_state_wrappers_.begin() + to_index,
87 std::move(web_state_wrapper));
88
89 for (auto& observer : observers_)
90 observer.WebStateMoved(this, web_state, from_index, to_index);
91 }
92
93 web::WebState* WebStateList::ReplaceWebStateAt(size_t index,
94 web::WebState* web_state) {
95 DCHECK(ContainsIndex(index));
96 web::WebState* old_web_state = web_state_wrappers_[index]->web_state();
97 web_state_wrappers_[index]->set_web_state(web_state);
98
99 for (auto& observer : observers_)
100 observer.WebStateReplacedAt(this, old_web_state, web_state, index);
101
102 return old_web_state;
103 }
104
105 void WebStateList::RemoveWebStateAt(size_t index) {
106 DCHECK(ContainsIndex(index));
107 web::WebState* web_state = web_state_wrappers_[index]->web_state();
108 web_state_wrappers_.erase(web_state_wrappers_.begin() + index);
109
110 for (auto& observer : observers_)
111 observer.WebStateRemovedAt(this, web_state, index);
112 }
113
114 void WebStateList::AddObserver(WebStateListObserver* observer) {
115 observers_.AddObserver(observer);
116 }
117
118 void WebStateList::RemoveObserver(WebStateListObserver* observer) {
119 observers_.RemoveObserver(observer);
120 }
121
122 // static
123 const size_t WebStateList::kInvalidIndex;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698