OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/services/public/cpp/view_manager/view_manager.h" | 5 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" |
8 #include "mojo/public/cpp/application/application.h" | 9 #include "mojo/public/cpp/application/application.h" |
| 10 #include "mojo/services/public/cpp/view_manager/lib/view_manager_private.h" |
9 #include "mojo/services/public/cpp/view_manager/lib/view_manager_synchronizer.h" | 11 #include "mojo/services/public/cpp/view_manager/lib/view_manager_synchronizer.h" |
10 #include "mojo/services/public/cpp/view_manager/lib/view_tree_node_private.h" | 12 #include "mojo/services/public/cpp/view_manager/lib/view_tree_node_private.h" |
11 #include "mojo/services/public/cpp/view_manager/view.h" | 13 #include "mojo/services/public/cpp/view_manager/view.h" |
12 | 14 |
13 namespace mojo { | 15 namespace mojo { |
14 namespace view_manager { | 16 namespace view_manager { |
| 17 namespace { |
15 | 18 |
16 ViewManager::ViewManager(Application* application) | 19 void OnViewManagerReady(base::RunLoop* loop, ViewManager* manager) { |
17 : synchronizer_(NULL), | 20 loop->Quit(); |
18 tree_(NULL) { | |
19 application->AddService<ViewManagerSynchronizer>(this); | |
20 // Block in a nested message loop until the ViewManagerSynchronizer is set up. | |
21 base::MessageLoop::current()->Run(); | |
22 } | 21 } |
23 | 22 |
| 23 } // namespace |
| 24 |
| 25 //////////////////////////////////////////////////////////////////////////////// |
| 26 // ViewManager, public: |
| 27 |
24 ViewManager::~ViewManager() { | 28 ViewManager::~ViewManager() { |
25 while (!nodes_.empty()) { | 29 while (!nodes_.empty()) { |
26 IdToNodeMap::iterator it = nodes_.begin(); | 30 IdToNodeMap::iterator it = nodes_.begin(); |
27 if (synchronizer_->OwnsNode(it->second->id())) | 31 if (synchronizer_->OwnsNode(it->second->id())) |
28 it->second->Destroy(); | 32 it->second->Destroy(); |
29 else | 33 else |
30 nodes_.erase(it); | 34 nodes_.erase(it); |
31 } | 35 } |
32 while (!views_.empty()) { | 36 while (!views_.empty()) { |
33 IdToViewMap::iterator it = views_.begin(); | 37 IdToViewMap::iterator it = views_.begin(); |
34 if (synchronizer_->OwnsView(it->second->id())) | 38 if (synchronizer_->OwnsView(it->second->id())) |
35 it->second->Destroy(); | 39 it->second->Destroy(); |
36 else | 40 else |
37 views_.erase(it); | 41 views_.erase(it); |
38 } | 42 } |
39 } | 43 } |
40 | 44 |
| 45 // static |
| 46 ViewManager* ViewManager::CreateBlocking(Application* application) { |
| 47 base::RunLoop init_loop; |
| 48 ViewManager* manager = new ViewManager( |
| 49 application, |
| 50 base::Bind(&OnViewManagerReady, &init_loop)); |
| 51 init_loop.Run(); |
| 52 return manager; |
| 53 } |
| 54 |
| 55 // static |
| 56 void ViewManager::Create( |
| 57 Application* application, |
| 58 const base::Callback<void(ViewManager*)> ready_callback) { |
| 59 new ViewManager(application, ready_callback); |
| 60 } |
| 61 |
41 ViewTreeNode* ViewManager::GetNodeById(TransportNodeId id) { | 62 ViewTreeNode* ViewManager::GetNodeById(TransportNodeId id) { |
42 IdToNodeMap::const_iterator it = nodes_.find(id); | 63 IdToNodeMap::const_iterator it = nodes_.find(id); |
43 return it != nodes_.end() ? it->second : NULL; | 64 return it != nodes_.end() ? it->second : NULL; |
44 } | 65 } |
45 | 66 |
46 View* ViewManager::GetViewById(TransportViewId id) { | 67 View* ViewManager::GetViewById(TransportViewId id) { |
47 IdToViewMap::const_iterator it = views_.find(id); | 68 IdToViewMap::const_iterator it = views_.find(id); |
48 return it != views_.end() ? it->second : NULL; | 69 return it != views_.end() ? it->second : NULL; |
49 } | 70 } |
50 | 71 |
51 void ViewManager::Embed(const String& url, ViewTreeNode* node) { | 72 void ViewManager::Embed(const String& url, ViewTreeNode* node) { |
52 synchronizer_->Embed(url, node->id()); | 73 synchronizer_->Embed(url, node->id()); |
53 } | 74 } |
54 | 75 |
| 76 //////////////////////////////////////////////////////////////////////////////// |
| 77 // ViewManager, private: |
| 78 |
| 79 ViewManager::ViewManager( |
| 80 Application* application, |
| 81 const base::Callback<void(ViewManager*)> ready_callback) |
| 82 : ready_callback_(ready_callback), |
| 83 synchronizer_(NULL), |
| 84 tree_(NULL) { |
| 85 application->AddService<ViewManagerSynchronizer>(this); |
| 86 } |
| 87 |
55 } // namespace view_manager | 88 } // namespace view_manager |
56 } // namespace mojo | 89 } // namespace mojo |
OLD | NEW |