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 int id); | |
| 25 ~BlimpContentsDeletionObserver() override; | |
| 26 | |
| 27 void OnContentsDestroyed() override; | |
| 28 | |
| 29 BlimpContentsImpl* blimp_contents() { return contents_; } | |
| 30 | |
| 31 private: | |
| 32 // The BlimpContentsManager containing this BlimpContentsDeletionObserver | |
| 33 BlimpContentsManager* blimp_contents_manager_; | |
| 34 | |
| 35 // The BlimpContents being tracked by this BlimpContentsDeletionObserver. | |
| 36 BlimpContentsImpl* contents_; | |
| 37 | |
| 38 // id of the BlimpContents created by BlimpContentsManager. | |
| 39 int id_; | |
| 40 | |
| 41 DISALLOW_COPY_AND_ASSIGN(BlimpContentsDeletionObserver); | |
| 42 }; | |
| 43 | |
| 44 BlimpContentsManager::BlimpContentsDeletionObserver:: | |
| 45 BlimpContentsDeletionObserver(BlimpContentsManager* blimp_contents_manager, | |
| 46 BlimpContentsImpl* blimp_contents, | |
|
David Trainor- moved to gerrit
2016/08/03 18:55:42
Can we just make BlimpContentsObserver take a Blim
Menglin
2016/08/03 19:25:48
But BlimpContentsManager still needs to keep a map
Menglin
2016/08/03 23:49:59
Yeah. BlimpContentsDeletionObserver doesn't need t
David Trainor- moved to gerrit
2016/08/04 16:21:18
I think the problem is we're solving the observer
Menglin
2016/08/05 02:08:24
ok. got your point. yeah it makes sense to solve i
| |
| 47 int id) | |
| 48 : blimp_contents_manager_(blimp_contents_manager), | |
| 49 contents_(blimp_contents), | |
| 50 id_(id) { | |
| 51 blimp_contents->AddObserver(this); | |
| 52 } | |
| 53 | |
| 54 BlimpContentsManager::BlimpContentsDeletionObserver:: | |
| 55 ~BlimpContentsDeletionObserver() { | |
| 56 if (contents_) { | |
| 57 contents_->RemoveObserver(this); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 void BlimpContentsManager::BlimpContentsDeletionObserver:: | |
| 62 OnContentsDestroyed() { | |
| 63 DCHECK(contents_); | |
| 64 | |
| 65 contents_ = nullptr; | |
| 66 DCHECK(base::ThreadTaskRunnerHandle::Get()); | |
| 67 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 68 FROM_HERE, base::Bind(&BlimpContentsManager::EraseObserverFromMap, | |
| 69 base::Unretained(blimp_contents_manager_), id_)); | |
| 70 } | |
| 71 | |
| 72 BlimpContentsManager::BlimpContentsManager() {} | |
| 73 | |
| 74 BlimpContentsManager::~BlimpContentsManager() {} | |
| 75 | |
| 76 std::unique_ptr<BlimpContentsImpl> BlimpContentsManager::CreateBlimpContents() { | |
| 77 int id = CreateBlimpContentsId(); | |
| 78 std::unique_ptr<BlimpContentsImpl> new_contents = | |
| 79 base::WrapUnique(new BlimpContentsImpl(id)); | |
| 80 std::unique_ptr<BlimpContentsDeletionObserver> observer = base::WrapUnique( | |
| 81 new BlimpContentsDeletionObserver(this, new_contents.get(), id)); | |
| 82 observer_map_.insert( | |
| 83 std::pair<int, std::unique_ptr<BlimpContentsDeletionObserver>>( | |
| 84 id, std::move(observer))); | |
| 85 return new_contents; | |
| 86 } | |
| 87 | |
| 88 BlimpContentsImpl* BlimpContentsManager::GetBlimpContents(int id) { | |
| 89 if (observer_map_.find(id) != observer_map_.end()) { | |
| 90 BlimpContentsDeletionObserver* observer = observer_map_.at(id).get(); | |
| 91 // If the BlimpContents that the observer tracks is empty, it means | |
| 92 // OnContentsDestroyed was called on this observer, but the task to erase | |
| 93 // the observer from the map hasn't been run. | |
| 94 if (observer->blimp_contents()) | |
| 95 return observer->blimp_contents(); | |
| 96 } | |
| 97 return nullptr; | |
| 98 } | |
| 99 | |
| 100 int BlimpContentsManager::CreateBlimpContentsId() { | |
| 101 // TODO(mlliu): currently, Blimp only supports a single tab, so returning a | |
| 102 // dummy tab id. Need to return real case id when Blimp supports multiple | |
| 103 // tabs. | |
| 104 return kDummyTabId; | |
| 105 } | |
| 106 | |
| 107 void BlimpContentsManager::EraseObserverFromMap(int id) { | |
| 108 observer_map_.erase(id); | |
| 109 } | |
| 110 | |
| 111 } // namespace client | |
| 112 } // namespace blimp | |
| OLD | NEW |