| 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 class BlimpContentsManager::BlimpContentsDeletionObserver |
| 20 : public BlimpContentsObserver { |
| 21 public: |
| 22 BlimpContentsDeletionObserver(BlimpContentsManager* blimp_contents_manager, |
| 23 BlimpContentsImpl* blimp_contents); |
| 24 |
| 25 void OnContentsDestroyed() override; |
| 26 |
| 27 private: |
| 28 // The BlimpContentsManager containing this BlimpContentsDeletionObserver |
| 29 BlimpContentsManager* blimp_contents_manager_; |
| 30 |
| 31 DISALLOW_COPY_AND_ASSIGN(BlimpContentsDeletionObserver); |
| 32 }; |
| 33 |
| 34 BlimpContentsManager::BlimpContentsDeletionObserver:: |
| 35 BlimpContentsDeletionObserver(BlimpContentsManager* blimp_contents_manager, |
| 36 BlimpContentsImpl* blimp_contents) |
| 37 : BlimpContentsObserver(blimp_contents), |
| 38 blimp_contents_manager_(blimp_contents_manager) {} |
| 39 |
| 40 void BlimpContentsManager::BlimpContentsDeletionObserver:: |
| 41 OnContentsDestroyed() { |
| 42 BlimpContents* contents = blimp_contents(); |
| 43 int id = static_cast<BlimpContentsImpl*>(contents)->id(); |
| 44 DCHECK(base::ThreadTaskRunnerHandle::Get()); |
| 45 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 46 FROM_HERE, base::Bind(&BlimpContentsManager::EraseObserverFromMap, |
| 47 blimp_contents_manager_->GetWeakPtr(), id)); |
| 48 } |
| 49 |
| 50 BlimpContentsManager::BlimpContentsManager() : weak_ptr_factory_(this) {} |
| 51 |
| 52 BlimpContentsManager::~BlimpContentsManager() {} |
| 53 |
| 54 std::unique_ptr<BlimpContentsImpl> BlimpContentsManager::CreateBlimpContents() { |
| 55 int id = CreateBlimpContentsId(); |
| 56 std::unique_ptr<BlimpContentsImpl> new_contents = |
| 57 base::MakeUnique<BlimpContentsImpl>(id); |
| 58 std::unique_ptr<BlimpContentsDeletionObserver> observer = |
| 59 base::MakeUnique<BlimpContentsDeletionObserver>(this, new_contents.get()); |
| 60 observer_map_.insert( |
| 61 std::pair<int, std::unique_ptr<BlimpContentsDeletionObserver>>( |
| 62 id, std::move(observer))); |
| 63 return new_contents; |
| 64 } |
| 65 |
| 66 BlimpContentsImpl* BlimpContentsManager::GetBlimpContents(int id) { |
| 67 if (observer_map_.find(id) == observer_map_.end()) return nullptr; |
| 68 |
| 69 BlimpContentsDeletionObserver* observer = observer_map_.at(id).get(); |
| 70 // If the BlimpContents that the observer tracks is empty, it means |
| 71 // OnContentsDestroyed was called on this observer, but the task to erase |
| 72 // the observer from the map hasn't been run. |
| 73 if (observer->blimp_contents()) |
| 74 return static_cast<BlimpContentsImpl*>(observer->blimp_contents()); |
| 75 |
| 76 return nullptr; |
| 77 } |
| 78 |
| 79 int BlimpContentsManager::CreateBlimpContentsId() { |
| 80 // TODO(mlliu): currently, Blimp only supports a single tab, so returning a |
| 81 // dummy tab id. Need to return real case id when Blimp supports multiple |
| 82 // tabs. |
| 83 return kDummyTabId; |
| 84 } |
| 85 |
| 86 void BlimpContentsManager::EraseObserverFromMap(int id) { |
| 87 observer_map_.erase(id); |
| 88 } |
| 89 |
| 90 base::WeakPtr<BlimpContentsManager> BlimpContentsManager::GetWeakPtr() { |
| 91 return weak_ptr_factory_.GetWeakPtr(); |
| 92 } |
| 93 |
| 94 } // namespace client |
| 95 } // namespace blimp |
| OLD | NEW |