OLD | NEW |
(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/clean/chrome/browser/model/browser_list.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." |
| 15 #endif |
| 16 |
| 17 namespace { |
| 18 const char kBrowserListKey = 0; |
| 19 } |
| 20 |
| 21 BrowserList::BrowserList(ios::ChromeBrowserState* browser_state) |
| 22 : browser_state_(browser_state) { |
| 23 DCHECK(browser_state_); |
| 24 } |
| 25 |
| 26 BrowserList::~BrowserList() = default; |
| 27 |
| 28 // static |
| 29 BrowserList* BrowserList::FromBrowserState( |
| 30 ios::ChromeBrowserState* browser_state) { |
| 31 base::SupportsUserData::Data* data = |
| 32 browser_state->GetUserData(&kBrowserListKey); |
| 33 if (!data) { |
| 34 browser_state->SetUserData(&kBrowserListKey, |
| 35 base::MakeUnique<BrowserList>(browser_state)); |
| 36 data = browser_state->GetUserData(&kBrowserListKey); |
| 37 } |
| 38 DCHECK(data); |
| 39 return static_cast<BrowserList*>(data); |
| 40 } |
| 41 |
| 42 int BrowserList::GetBrowserCount() const { |
| 43 DCHECK_LE(browsers_.size(), static_cast<size_t>(INT_MAX)); |
| 44 return static_cast<int>(browsers_.size()); |
| 45 } |
| 46 |
| 47 int BrowserList::ContainsIndex(int index) const { |
| 48 return 0 <= index && index < GetBrowserCount(); |
| 49 } |
| 50 |
| 51 Browser* BrowserList::GetBrowserAtIndex(int index) const { |
| 52 DCHECK(ContainsIndex(index)); |
| 53 return browsers_[index].get(); |
| 54 } |
| 55 |
| 56 Browser* BrowserList::CreateNewBrowser() { |
| 57 browsers_.push_back(base::MakeUnique<Browser>(browser_state_)); |
| 58 return browsers_.back().get(); |
| 59 } |
| 60 |
| 61 void BrowserList::CloseBrowserAtIndex(int index) { |
| 62 DCHECK(ContainsIndex(index)); |
| 63 browsers_.erase(browsers_.begin() + index); |
| 64 } |
OLD | NEW |