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..c68274d332b5a3bef3bf02e7ab63911f038b6b96 |
| --- /dev/null |
| +++ b/blimp/client/core/contents/blimp_contents_manager.cc |
| @@ -0,0 +1,96 @@ |
| +// 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); |
| + |
| + void OnContentsDestroyed() override; |
| + |
| + private: |
| + // The BlimpContentsManager containing this BlimpContentsDeletionObserver |
| + BlimpContentsManager* blimp_contents_manager_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BlimpContentsDeletionObserver); |
| +}; |
| + |
| +BlimpContentsManager::BlimpContentsDeletionObserver:: |
| + BlimpContentsDeletionObserver(BlimpContentsManager* blimp_contents_manager, |
| + BlimpContentsImpl* blimp_contents) |
| + : BlimpContentsObserver(blimp_contents), |
| + blimp_contents_manager_(blimp_contents_manager) {} |
| + |
| +void BlimpContentsManager::BlimpContentsDeletionObserver:: |
| + OnContentsDestroyed() { |
| + BlimpContents* contents = blimp_contents(); |
| + DCHECK(contents); |
| + int id = static_cast<BlimpContentsImpl*>(contents)->id(); |
| + ClearBlimpContents(); |
|
Menglin
2016/08/05 23:37:15
here it needs to first make sure it contents is no
|
| + DCHECK(base::ThreadTaskRunnerHandle::Get()); |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, base::Bind(&BlimpContentsManager::EraseObserverFromMap, |
| + blimp_contents_manager_->GetWeakPtr(), id)); |
| +} |
| + |
| +BlimpContentsManager::BlimpContentsManager() : weak_ptr_factory_(this) {} |
| + |
| +BlimpContentsManager::~BlimpContentsManager() {} |
| + |
| +std::unique_ptr<BlimpContentsImpl> BlimpContentsManager::CreateBlimpContents() { |
| + int id = CreateBlimpContentsId(); |
| + std::unique_ptr<BlimpContentsImpl> new_contents = |
| + 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.
|
| + std::unique_ptr<BlimpContentsDeletionObserver> observer = base::WrapUnique( |
| + new BlimpContentsDeletionObserver(this, new_contents.get())); |
| + 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()) { |
|
nyquist
2016/08/05 23:03:23
Could you just early out if this conditional fails
Menglin
2016/08/06 00:57:00
Done
|
| + 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 static_cast<BlimpContentsImpl*>(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); |
| +} |
| + |
| +base::WeakPtr<BlimpContentsManager> BlimpContentsManager::GetWeakPtr() { |
| + return weak_ptr_factory_.GetWeakPtr(); |
| +} |
| + |
| +} // namespace client |
| +} // namespace blimp |