OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "blimp/client/core/contents/blimp_contents_manager.h" | 5 #include "blimp/client/core/contents/blimp_contents_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "blimp/client/core/contents/tab_control_feature.h" |
10 #include "blimp/client/public/contents/blimp_contents_observer.h" | 11 #include "blimp/client/public/contents/blimp_contents_observer.h" |
11 | 12 |
12 namespace { | 13 namespace { |
13 const int kDummyTabId = 0; | 14 const int kDummyTabId = 0; |
14 } | 15 } |
15 | 16 |
16 namespace blimp { | 17 namespace blimp { |
17 namespace client { | 18 namespace client { |
18 | 19 |
19 class BlimpContentsManager::BlimpContentsDeletionObserver | 20 class BlimpContentsManager::BlimpContentsDeletionObserver |
(...skipping 13 matching lines...) Expand all Loading... |
33 | 34 |
34 BlimpContentsManager::BlimpContentsDeletionObserver:: | 35 BlimpContentsManager::BlimpContentsDeletionObserver:: |
35 BlimpContentsDeletionObserver(BlimpContentsManager* blimp_contents_manager, | 36 BlimpContentsDeletionObserver(BlimpContentsManager* blimp_contents_manager, |
36 BlimpContentsImpl* blimp_contents) | 37 BlimpContentsImpl* blimp_contents) |
37 : BlimpContentsObserver(blimp_contents), | 38 : BlimpContentsObserver(blimp_contents), |
38 blimp_contents_manager_(blimp_contents_manager) {} | 39 blimp_contents_manager_(blimp_contents_manager) {} |
39 | 40 |
40 void BlimpContentsManager::BlimpContentsDeletionObserver:: | 41 void BlimpContentsManager::BlimpContentsDeletionObserver:: |
41 OnContentsDestroyed() { | 42 OnContentsDestroyed() { |
42 BlimpContents* contents = blimp_contents(); | 43 BlimpContents* contents = blimp_contents(); |
43 int id = static_cast<BlimpContentsImpl*>(contents)->id(); | 44 blimp_contents_manager_->OnContentsDestroyed(contents); |
44 DCHECK(base::ThreadTaskRunnerHandle::Get()); | |
45 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
46 FROM_HERE, base::Bind(&BlimpContentsManager::EraseObserverFromMap, | |
47 blimp_contents_manager_->GetWeakPtr(), id)); | |
48 } | 45 } |
49 | 46 |
50 BlimpContentsManager::BlimpContentsManager() : weak_ptr_factory_(this) {} | 47 BlimpContentsManager::BlimpContentsManager( |
| 48 TabControlFeature* tab_control_feature) |
| 49 : tab_control_feature_(tab_control_feature), |
| 50 weak_ptr_factory_(this) {} |
51 | 51 |
52 BlimpContentsManager::~BlimpContentsManager() {} | 52 BlimpContentsManager::~BlimpContentsManager() {} |
53 | 53 |
54 std::unique_ptr<BlimpContentsImpl> BlimpContentsManager::CreateBlimpContents() { | 54 std::unique_ptr<BlimpContentsImpl> BlimpContentsManager::CreateBlimpContents() { |
55 int id = CreateBlimpContentsId(); | 55 int id = CreateBlimpContentsId(); |
| 56 |
56 std::unique_ptr<BlimpContentsImpl> new_contents = | 57 std::unique_ptr<BlimpContentsImpl> new_contents = |
57 base::MakeUnique<BlimpContentsImpl>(id); | 58 base::MakeUnique<BlimpContentsImpl>(id, tab_control_feature_); |
| 59 |
| 60 // Create an observer entry for the contents. |
58 std::unique_ptr<BlimpContentsDeletionObserver> observer = | 61 std::unique_ptr<BlimpContentsDeletionObserver> observer = |
59 base::MakeUnique<BlimpContentsDeletionObserver>(this, new_contents.get()); | 62 base::MakeUnique<BlimpContentsDeletionObserver>(this, new_contents.get()); |
60 observer_map_.insert( | 63 observer_map_.insert( |
61 std::pair<int, std::unique_ptr<BlimpContentsDeletionObserver>>( | 64 std::pair<int, std::unique_ptr<BlimpContentsDeletionObserver>>( |
62 id, std::move(observer))); | 65 id, std::move(observer))); |
| 66 |
| 67 // Notifies the engine that we've created a new BlimpContents. |
| 68 tab_control_feature_->CreateTab(id); |
| 69 |
63 return new_contents; | 70 return new_contents; |
64 } | 71 } |
65 | 72 |
66 BlimpContentsImpl* BlimpContentsManager::GetBlimpContents(int id) { | 73 BlimpContentsImpl* BlimpContentsManager::GetBlimpContents(int id) { |
67 if (observer_map_.find(id) == observer_map_.end()) return nullptr; | 74 if (observer_map_.find(id) == observer_map_.end()) return nullptr; |
68 | 75 |
69 BlimpContentsDeletionObserver* observer = observer_map_.at(id).get(); | 76 BlimpContentsDeletionObserver* observer = observer_map_.at(id).get(); |
70 // If the BlimpContents that the observer tracks is empty, it means | 77 // If the BlimpContents that the observer tracks is empty, it means |
71 // OnContentsDestroyed was called on this observer, but the task to erase | 78 // OnContentsDestroyed was called on this observer, but the task to erase |
72 // the observer from the map hasn't been run. | 79 // the observer from the map hasn't been run. |
73 if (observer->blimp_contents()) | 80 if (observer->blimp_contents()) |
74 return static_cast<BlimpContentsImpl*>(observer->blimp_contents()); | 81 return static_cast<BlimpContentsImpl*>(observer->blimp_contents()); |
75 | 82 |
76 return nullptr; | 83 return nullptr; |
77 } | 84 } |
78 | 85 |
79 int BlimpContentsManager::CreateBlimpContentsId() { | 86 int BlimpContentsManager::CreateBlimpContentsId() { |
80 // TODO(mlliu): currently, Blimp only supports a single tab, so returning a | 87 // TODO(mlliu): currently, Blimp only supports a single tab, so returning a |
81 // dummy tab id. Need to return real case id when Blimp supports multiple | 88 // dummy tab id. Need to return real case id when Blimp supports multiple |
82 // tabs. | 89 // tabs. |
83 return kDummyTabId; | 90 return kDummyTabId; |
84 } | 91 } |
85 | 92 |
86 void BlimpContentsManager::EraseObserverFromMap(int id) { | 93 void BlimpContentsManager::EraseObserverFromMap(int id) { |
87 observer_map_.erase(id); | 94 observer_map_.erase(id); |
88 } | 95 } |
89 | 96 |
| 97 void BlimpContentsManager::OnContentsDestroyed(BlimpContents* contents) { |
| 98 int id = static_cast<BlimpContentsImpl*>(contents)->id(); |
| 99 |
| 100 // Notify the engine that we've destroyed the BlimpContents. |
| 101 tab_control_feature_->CloseTab(id); |
| 102 |
| 103 // Destroy the observer entry from the observer_map_. |
| 104 DCHECK(base::ThreadTaskRunnerHandle::Get()); |
| 105 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 106 FROM_HERE, base::Bind(&BlimpContentsManager::EraseObserverFromMap, |
| 107 this->GetWeakPtr(), id)); |
| 108 } |
| 109 |
90 base::WeakPtr<BlimpContentsManager> BlimpContentsManager::GetWeakPtr() { | 110 base::WeakPtr<BlimpContentsManager> BlimpContentsManager::GetWeakPtr() { |
91 return weak_ptr_factory_.GetWeakPtr(); | 111 return weak_ptr_factory_.GetWeakPtr(); |
92 } | 112 } |
93 | 113 |
94 } // namespace client | 114 } // namespace client |
95 } // namespace blimp | 115 } // namespace blimp |
OLD | NEW |