Chromium Code Reviews| 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 DCHECK(contents); | |
| 44 int id = static_cast<BlimpContentsImpl*>(contents)->id(); | |
| 45 ClearBlimpContents(); | |
|
David Trainor- moved to gerrit
2016/08/06 04:32:58
This should happen in the base class. Maybe do so
Menglin
2016/08/09 00:31:18
Done.
| |
| 46 DCHECK(base::ThreadTaskRunnerHandle::Get()); | |
| 47 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 48 FROM_HERE, base::Bind(&BlimpContentsManager::EraseObserverFromMap, | |
| 49 blimp_contents_manager_->GetWeakPtr(), id)); | |
| 50 } | |
| 51 | |
| 52 BlimpContentsManager::BlimpContentsManager() : weak_ptr_factory_(this) {} | |
| 53 | |
| 54 BlimpContentsManager::~BlimpContentsManager() {} | |
| 55 | |
| 56 std::unique_ptr<BlimpContentsImpl> BlimpContentsManager::CreateBlimpContents() { | |
| 57 int id = CreateBlimpContentsId(); | |
| 58 std::unique_ptr<BlimpContentsImpl> new_contents = | |
| 59 base::MakeUnique<BlimpContentsImpl>(id); | |
| 60 std::unique_ptr<BlimpContentsDeletionObserver> observer = | |
| 61 base::MakeUnique<BlimpContentsDeletionObserver>(this, new_contents.get()); | |
| 62 observer_map_.insert( | |
| 63 std::pair<int, std::unique_ptr<BlimpContentsDeletionObserver>>( | |
| 64 id, std::move(observer))); | |
| 65 return new_contents; | |
| 66 } | |
| 67 | |
| 68 BlimpContentsImpl* BlimpContentsManager::GetBlimpContents(int id) { | |
| 69 if (observer_map_.find(id) == observer_map_.end()) return nullptr; | |
| 70 | |
| 71 BlimpContentsDeletionObserver* observer = observer_map_.at(id).get(); | |
| 72 // If the BlimpContents that the observer tracks is empty, it means | |
| 73 // OnContentsDestroyed was called on this observer, but the task to erase | |
| 74 // the observer from the map hasn't been run. | |
| 75 if (observer->blimp_contents()) | |
| 76 return static_cast<BlimpContentsImpl*>(observer->blimp_contents()); | |
| 77 | |
| 78 return nullptr; | |
| 79 } | |
| 80 | |
| 81 int BlimpContentsManager::CreateBlimpContentsId() { | |
| 82 // TODO(mlliu): currently, Blimp only supports a single tab, so returning a | |
| 83 // dummy tab id. Need to return real case id when Blimp supports multiple | |
| 84 // tabs. | |
| 85 return kDummyTabId; | |
| 86 } | |
| 87 | |
| 88 void BlimpContentsManager::EraseObserverFromMap(int id) { | |
| 89 observer_map_.erase(id); | |
| 90 } | |
| 91 | |
| 92 base::WeakPtr<BlimpContentsManager> BlimpContentsManager::GetWeakPtr() { | |
| 93 return weak_ptr_factory_.GetWeakPtr(); | |
| 94 } | |
| 95 | |
| 96 } // namespace client | |
| 97 } // namespace blimp | |
| OLD | NEW |