Chromium Code Reviews| Index: blimp/client/core/contents/blimp_contents_manager.cc |
| diff --git a/blimp/client/core/contents/blimp_contents_manager.cc b/blimp/client/core/contents/blimp_contents_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5bdd38cd856cfd355db7e30b338bbffc1c0de492 |
| --- /dev/null |
| +++ b/blimp/client/core/contents/blimp_contents_manager.cc |
| @@ -0,0 +1,112 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "blimp/client/core/contents/blimp_contents_manager.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "blimp/client/public/contents/blimp_contents_observer.h" |
| + |
| +namespace { |
| +const int kDummyTabId = 0; |
| +} |
| + |
| +namespace blimp { |
| +namespace client { |
| + |
| +class BlimpContentsManager::BlimpContentsDeletionObserver |
| + : public BlimpContentsObserver { |
| + public: |
| + BlimpContentsDeletionObserver(BlimpContentsManager* blimp_contents_manager, |
| + BlimpContentsImpl* blimp_contents, |
| + int id); |
| + ~BlimpContentsDeletionObserver() override; |
| + |
| + void OnContentsDestroyed() override; |
| + |
| + BlimpContentsImpl* blimp_contents() { return contents_; } |
| + |
| + private: |
| + // The BlimpContentsManager containing this BlimpContentsDeletionObserver |
| + BlimpContentsManager* blimp_contents_manager_; |
| + |
| + // The BlimpContents being tracked by this BlimpContentsDeletionObserver. |
| + BlimpContentsImpl* contents_; |
| + |
| + // id of the BlimpContents created by BlimpContentsManager. |
| + int id_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BlimpContentsDeletionObserver); |
| +}; |
| + |
| +BlimpContentsManager::BlimpContentsDeletionObserver:: |
| + BlimpContentsDeletionObserver(BlimpContentsManager* blimp_contents_manager, |
| + 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
|
| + int id) |
| + : blimp_contents_manager_(blimp_contents_manager), |
| + contents_(blimp_contents), |
| + id_(id) { |
| + blimp_contents->AddObserver(this); |
| +} |
| + |
| +BlimpContentsManager::BlimpContentsDeletionObserver:: |
| + ~BlimpContentsDeletionObserver() { |
| + if (contents_) { |
| + contents_->RemoveObserver(this); |
| + } |
| +} |
| + |
| +void BlimpContentsManager::BlimpContentsDeletionObserver:: |
| + OnContentsDestroyed() { |
| + DCHECK(contents_); |
| + |
| + contents_ = nullptr; |
| + DCHECK(base::ThreadTaskRunnerHandle::Get()); |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, base::Bind(&BlimpContentsManager::EraseObserverFromMap, |
| + base::Unretained(blimp_contents_manager_), id_)); |
| +} |
| + |
| +BlimpContentsManager::BlimpContentsManager() {} |
| + |
| +BlimpContentsManager::~BlimpContentsManager() {} |
| + |
| +std::unique_ptr<BlimpContentsImpl> BlimpContentsManager::CreateBlimpContents() { |
| + int id = CreateBlimpContentsId(); |
| + std::unique_ptr<BlimpContentsImpl> new_contents = |
| + base::WrapUnique(new BlimpContentsImpl(id)); |
| + std::unique_ptr<BlimpContentsDeletionObserver> observer = base::WrapUnique( |
| + new BlimpContentsDeletionObserver(this, new_contents.get(), id)); |
| + observer_map_.insert( |
| + std::pair<int, std::unique_ptr<BlimpContentsDeletionObserver>>( |
| + id, std::move(observer))); |
| + return new_contents; |
| +} |
| + |
| +BlimpContentsImpl* BlimpContentsManager::GetBlimpContents(int id) { |
| + if (observer_map_.find(id) != observer_map_.end()) { |
| + BlimpContentsDeletionObserver* observer = observer_map_.at(id).get(); |
| + // If the BlimpContents that the observer tracks is empty, it means |
| + // OnContentsDestroyed was called on this observer, but the task to erase |
| + // the observer from the map hasn't been run. |
| + if (observer->blimp_contents()) |
| + return observer->blimp_contents(); |
| + } |
| + return nullptr; |
| +} |
| + |
| +int BlimpContentsManager::CreateBlimpContentsId() { |
| + // TODO(mlliu): currently, Blimp only supports a single tab, so returning a |
| + // dummy tab id. Need to return real case id when Blimp supports multiple |
| + // tabs. |
| + return kDummyTabId; |
| +} |
| + |
| +void BlimpContentsManager::EraseObserverFromMap(int id) { |
| + observer_map_.erase(id); |
| +} |
| + |
| +} // namespace client |
| +} // namespace blimp |