| 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/core/compositor/blimp_compositor_dependencies.h" | |
| 11 #include "blimp/client/core/contents/blimp_contents_impl.h" | |
| 12 #include "blimp/client/core/contents/navigation_feature.h" | |
| 13 #include "blimp/client/core/contents/tab_control_feature.h" | |
| 14 #include "blimp/client/core/render_widget/render_widget_feature.h" | |
| 15 #include "blimp/client/public/contents/blimp_contents_observer.h" | |
| 16 | |
| 17 namespace blimp { | |
| 18 namespace client { | |
| 19 | |
| 20 class BlimpContentsManager::BlimpContentsDeletionObserver | |
| 21 : public BlimpContentsObserver { | |
| 22 public: | |
| 23 BlimpContentsDeletionObserver(BlimpContentsManager* blimp_contents_manager, | |
| 24 BlimpContentsImpl* blimp_contents); | |
| 25 | |
| 26 void OnContentsDestroyed() override; | |
| 27 | |
| 28 private: | |
| 29 // The BlimpContentsManager containing this BlimpContentsDeletionObserver | |
| 30 BlimpContentsManager* blimp_contents_manager_; | |
| 31 | |
| 32 DISALLOW_COPY_AND_ASSIGN(BlimpContentsDeletionObserver); | |
| 33 }; | |
| 34 | |
| 35 BlimpContentsManager::BlimpContentsDeletionObserver:: | |
| 36 BlimpContentsDeletionObserver(BlimpContentsManager* blimp_contents_manager, | |
| 37 BlimpContentsImpl* blimp_contents) | |
| 38 : BlimpContentsObserver(blimp_contents), | |
| 39 blimp_contents_manager_(blimp_contents_manager) {} | |
| 40 | |
| 41 void BlimpContentsManager::BlimpContentsDeletionObserver:: | |
| 42 OnContentsDestroyed() { | |
| 43 BlimpContents* contents = blimp_contents(); | |
| 44 blimp_contents_manager_->OnContentsDestroyed(contents); | |
| 45 } | |
| 46 | |
| 47 BlimpContentsManager::BlimpContentsManager( | |
| 48 BlimpCompositorDependencies* blimp_compositor_dependencies, | |
| 49 ImeFeature* ime_feature, | |
| 50 NavigationFeature* nav_feature, | |
| 51 RenderWidgetFeature* render_widget_feature, | |
| 52 TabControlFeature* tab_control_feature) | |
| 53 : blimp_compositor_dependencies_(blimp_compositor_dependencies), | |
| 54 ime_feature_(ime_feature), | |
| 55 navigation_feature_(nav_feature), | |
| 56 render_widget_feature_(render_widget_feature), | |
| 57 tab_control_feature_(tab_control_feature), | |
| 58 weak_ptr_factory_(this) {} | |
| 59 | |
| 60 BlimpContentsManager::~BlimpContentsManager() {} | |
| 61 | |
| 62 std::unique_ptr<BlimpContentsImpl> BlimpContentsManager::CreateBlimpContents( | |
| 63 gfx::NativeWindow window) { | |
| 64 if (tab_exists_) return nullptr; | |
| 65 tab_exists_ = true; | |
| 66 | |
| 67 int id = CreateBlimpContentsId(); | |
| 68 | |
| 69 std::unique_ptr<BlimpContentsImpl> new_contents = | |
| 70 base::MakeUnique<BlimpContentsImpl>( | |
| 71 id, window, blimp_compositor_dependencies_, ime_feature_, | |
| 72 navigation_feature_, render_widget_feature_, tab_control_feature_); | |
| 73 | |
| 74 // Create an observer entry for the contents. | |
| 75 std::unique_ptr<BlimpContentsDeletionObserver> observer = | |
| 76 base::MakeUnique<BlimpContentsDeletionObserver>(this, new_contents.get()); | |
| 77 observer_map_.insert( | |
| 78 std::pair<int, std::unique_ptr<BlimpContentsDeletionObserver>>( | |
| 79 id, std::move(observer))); | |
| 80 | |
| 81 // Notifies the engine that we've created a new BlimpContents. | |
| 82 tab_control_feature_->CreateTab(id); | |
| 83 | |
| 84 return new_contents; | |
| 85 } | |
| 86 | |
| 87 BlimpContentsImpl* BlimpContentsManager::GetBlimpContents(int id) { | |
| 88 if (observer_map_.find(id) == observer_map_.end()) return nullptr; | |
| 89 | |
| 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 static_cast<BlimpContentsImpl*>(observer->blimp_contents()); | |
| 96 | |
| 97 return nullptr; | |
| 98 } | |
| 99 | |
| 100 std::vector<BlimpContentsImpl*> | |
| 101 BlimpContentsManager::GetAllActiveBlimpContents() { | |
| 102 std::vector<BlimpContentsImpl*> all_blimp_contents; | |
| 103 for (const auto& item : observer_map_) { | |
| 104 BlimpContentsImpl* blimp_contents = | |
| 105 static_cast<BlimpContentsImpl*>(item.second.get()->blimp_contents()); | |
| 106 if (!blimp_contents) { | |
| 107 continue; | |
| 108 } | |
| 109 all_blimp_contents.push_back(blimp_contents); | |
| 110 } | |
| 111 return all_blimp_contents; | |
| 112 } | |
| 113 | |
| 114 int BlimpContentsManager::CreateBlimpContentsId() { | |
| 115 return next_blimp_contents_id_++; | |
| 116 } | |
| 117 | |
| 118 void BlimpContentsManager::EraseObserverFromMap(int id) { | |
| 119 observer_map_.erase(id); | |
| 120 } | |
| 121 | |
| 122 void BlimpContentsManager::OnContentsDestroyed(BlimpContents* contents) { | |
| 123 DCHECK(tab_exists_); | |
| 124 tab_exists_ = false; | |
| 125 | |
| 126 int id = static_cast<BlimpContentsImpl*>(contents)->id(); | |
| 127 | |
| 128 // Notify the engine that we've destroyed the BlimpContents. | |
| 129 tab_control_feature_->CloseTab(id); | |
| 130 | |
| 131 // Destroy the observer entry from the observer_map_. | |
| 132 DCHECK(base::ThreadTaskRunnerHandle::Get()); | |
| 133 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 134 FROM_HERE, base::Bind(&BlimpContentsManager::EraseObserverFromMap, | |
| 135 this->GetWeakPtr(), id)); | |
| 136 } | |
| 137 | |
| 138 base::WeakPtr<BlimpContentsManager> BlimpContentsManager::GetWeakPtr() { | |
| 139 return weak_ptr_factory_.GetWeakPtr(); | |
| 140 } | |
| 141 | |
| 142 } // namespace client | |
| 143 } // namespace blimp | |
| OLD | NEW |