Chromium Code Reviews| Index: ios/chrome/browser/tabs/tab_model_list.mm |
| diff --git a/ios/chrome/browser/tabs/tab_model_list.mm b/ios/chrome/browser/tabs/tab_model_list.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..11773338596afbde9fd04a1ca826def09b686deb |
| --- /dev/null |
| +++ b/ios/chrome/browser/tabs/tab_model_list.mm |
| @@ -0,0 +1,87 @@ |
| +// 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/chrome/browser/tabs/tab_model_list.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/macros.h" |
| +#include "base/supports_user_data.h" |
| +#include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| +#import "ios/chrome/browser/tabs/tab_model.h" |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +namespace { |
| +// Wrapper around a NSMutableArray<TabModel> allowing to bind it to a |
| +// base::SupportsUserData. Any base::SupportsUserData that owns this |
| +// wrapper will owns the reference to the TabModels. |
| +class TabModelList : public base::SupportsUserData::Data { |
| + public: |
| + TabModelList(); |
| + ~TabModelList() override; |
| + |
| + static TabModelList* GetForBrowserState( |
| + ios::ChromeBrowserState* browser_state, |
| + bool create); |
| + |
| + NSMutableArray<TabModel*>* tab_models() const { return tab_models_; } |
|
marq (ping after 24h)
2017/01/11 15:56:47
Should this be a set instead of an array? That mea
sdefresne
2017/01/11 16:14:42
I need to be able to implement BrowserListIOS::Get
marq (ping after 24h)
2017/01/11 16:20:52
Even if there's an array, we don't have any way of
|
| + |
| + private: |
| + NSMutableArray<TabModel*>* tab_models_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TabModelList); |
| +}; |
| + |
| +const char kTabModelListKey = 0; |
| + |
| +TabModelList::TabModelList() : tab_models_([[NSMutableArray alloc] init]) {} |
| + |
| +TabModelList::~TabModelList() { |
| + // TabModelList is destroyed during base::SupportsUserData destruction. At |
| + // that point, all TabModels must have been unregistered already. |
| + DCHECK_EQ([tab_models_ count], 0u); |
| +} |
| + |
| +// static |
| +TabModelList* TabModelList::GetForBrowserState( |
| + ios::ChromeBrowserState* browser_state, |
| + bool create) { |
| + TabModelList* tab_model_list = |
| + static_cast<TabModelList*>(browser_state->GetUserData(&kTabModelListKey)); |
| + if (!tab_model_list && create) { |
| + // The ownership of TabModelList is transfered to base::SupportsUserData |
| + // via the SetUserData call (should take a std::unique_ptr<>). |
| + tab_model_list = new TabModelList; |
| + browser_state->SetUserData(&kTabModelListKey, tab_model_list); |
| + } |
| + return tab_model_list; |
| +} |
| +} // namespace |
| + |
| +void RegisterTabModelWithChromeBrowserState( |
| + ios::ChromeBrowserState* browser_state, |
| + TabModel* tab_model) { |
| + NSMutableArray<TabModel*>* tab_models = |
| + TabModelList::GetForBrowserState(browser_state, true)->tab_models(); |
| + DCHECK([tab_models indexOfObject:tab_model] == NSNotFound); |
| + [tab_models addObject:tab_model]; |
| +} |
| + |
| +void UnregisterTabModelFromChromeBrowserState( |
| + ios::ChromeBrowserState* browser_state, |
| + TabModel* tab_model) { |
| + NSMutableArray<TabModel*>* tab_models = |
| + TabModelList::GetForBrowserState(browser_state, false)->tab_models(); |
| + DCHECK([tab_models indexOfObject:tab_model] != NSNotFound); |
| + [tab_models removeObject:tab_model]; |
| +} |
| + |
| +NSArray<TabModel*>* GetTabModelsForChromeBrowserState( |
| + ios::ChromeBrowserState* browser_state) { |
| + TabModelList* tab_model_list = |
| + TabModelList::GetForBrowserState(browser_state, false); |
| + return tab_model_list ? [tab_model_list->tab_models() copy] : nil; |
| +} |