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(); | |
|
Menglin
2016/08/05 23:37:15
here it needs to first make sure it contents is no
| |
| 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::WrapUnique(new BlimpContentsImpl(id)); | |
|
nyquist
2016/08/05 23:03:23
Could you use base::MakeUnique here and on the lin
Menglin
2016/08/06 00:57:00
Done.
| |
| 60 std::unique_ptr<BlimpContentsDeletionObserver> observer = base::WrapUnique( | |
| 61 new 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()) { | |
|
nyquist
2016/08/05 23:03:23
Could you just early out if this conditional fails
Menglin
2016/08/06 00:57:00
Done
| |
| 70 BlimpContentsDeletionObserver* observer = observer_map_.at(id).get(); | |
| 71 // If the BlimpContents that the observer tracks is empty, it means | |
| 72 // OnContentsDestroyed was called on this observer, but the task to erase | |
| 73 // the observer from the map hasn't been run. | |
| 74 if (observer->blimp_contents()) | |
| 75 return static_cast<BlimpContentsImpl*>(observer->blimp_contents()); | |
| 76 } | |
| 77 return nullptr; | |
| 78 } | |
| 79 | |
| 80 int BlimpContentsManager::CreateBlimpContentsId() { | |
| 81 // TODO(mlliu): currently, Blimp only supports a single tab, so returning a | |
| 82 // dummy tab id. Need to return real case id when Blimp supports multiple | |
| 83 // tabs. | |
| 84 return kDummyTabId; | |
| 85 } | |
| 86 | |
| 87 void BlimpContentsManager::EraseObserverFromMap(int id) { | |
| 88 observer_map_.erase(id); | |
| 89 } | |
| 90 | |
| 91 base::WeakPtr<BlimpContentsManager> BlimpContentsManager::GetWeakPtr() { | |
| 92 return weak_ptr_factory_.GetWeakPtr(); | |
| 93 } | |
| 94 | |
| 95 } // namespace client | |
| 96 } // namespace blimp | |
| OLD | NEW |