| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 #include "blimp/client/core/contents/blimp_contents_manager.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "blimp/client/public/contents/blimp_contents_observer.h" |
| 11 |
| 12 namespace { |
| 13 const int kDummyTabId = 0; |
| 14 } |
| 15 |
| 16 namespace blimp { |
| 17 namespace client { |
| 18 |
| 19 BlimpContentsManager::BlimpContentsDeletionObserver:: |
| 20 BlimpContentsDeletionObserver(BlimpContentsManager* blimp_contents_manager, |
| 21 BlimpContentsImpl* blimp_contents) |
| 22 : blimp_contents_manager_(blimp_contents_manager), |
| 23 contents_(blimp_contents) { |
| 24 blimp_contents->AddObserver(this); |
| 25 } |
| 26 |
| 27 BlimpContentsManager::BlimpContentsDeletionObserver:: |
| 28 ~BlimpContentsDeletionObserver() { |
| 29 if (contents_) { |
| 30 contents_->RemoveObserver(this); |
| 31 } |
| 32 } |
| 33 |
| 34 void BlimpContentsManager::BlimpContentsDeletionObserver:: |
| 35 OnContentsDestroyed() { |
| 36 DCHECK(contents_); |
| 37 int id = contents_->id(); |
| 38 contents_ = nullptr; |
| 39 DCHECK(base::ThreadTaskRunnerHandle::Get()); |
| 40 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 41 FROM_HERE, base::Bind(&BlimpContentsManager::EraseObserverFromMap, |
| 42 base::Unretained(blimp_contents_manager_), id)); |
| 43 } |
| 44 |
| 45 BlimpContentsManager::BlimpContentsManager() {} |
| 46 |
| 47 BlimpContentsManager::~BlimpContentsManager() {} |
| 48 |
| 49 std::unique_ptr<BlimpContentsImpl> BlimpContentsManager::CreateBlimpContents() { |
| 50 int id = CreateBlimpContentsId(); |
| 51 std::unique_ptr<BlimpContentsImpl> new_contents = |
| 52 base::WrapUnique(new BlimpContentsImpl(id)); |
| 53 std::unique_ptr<BlimpContentsDeletionObserver> observer = base::WrapUnique( |
| 54 new BlimpContentsDeletionObserver(this, new_contents.get())); |
| 55 observer_map_.insert( |
| 56 std::pair<int, std::unique_ptr<BlimpContentsDeletionObserver>>( |
| 57 id, std::move(observer))); |
| 58 return new_contents; |
| 59 } |
| 60 |
| 61 BlimpContentsImpl* BlimpContentsManager::GetBlimpContents(int id) { |
| 62 if (observer_map_.find(id) != observer_map_.end()) { |
| 63 BlimpContentsDeletionObserver* observer = observer_map_.at(id).get(); |
| 64 // If the BlimpContents that the observer tracks is empty, it means |
| 65 // OnContentsDestroyed was called on this observer, but the task to erase |
| 66 // the observer from the map hasn't been run. |
| 67 if (observer->blimp_contents()) |
| 68 return observer->blimp_contents(); |
| 69 } |
| 70 return nullptr; |
| 71 } |
| 72 |
| 73 int BlimpContentsManager::CreateBlimpContentsId() { |
| 74 // TODO(mlliu): currently, Blimp only supports a single tab, so returning a |
| 75 // dummy tab id. Need to return real case id when Blimp supports multiple |
| 76 // tabs. |
| 77 return kDummyTabId; |
| 78 } |
| 79 |
| 80 void BlimpContentsManager::EraseObserverFromMap(int id) { |
| 81 observer_map_.erase(id); |
| 82 } |
| 83 |
| 84 } // namespace client |
| 85 } // namespace blimp |
| OLD | NEW |