Chromium Code Reviews| Index: mojo/examples/embedded_app/embedded_app.cc |
| diff --git a/mojo/examples/embedded_app/embedded_app.cc b/mojo/examples/embedded_app/embedded_app.cc |
| index 3f463072b62e098f08e683d8316bb56e46657066..c8c584d471e86cee8f97522c62ac3c0ad07bdcd1 100644 |
| --- a/mojo/examples/embedded_app/embedded_app.cc |
| +++ b/mojo/examples/embedded_app/embedded_app.cc |
| @@ -4,8 +4,11 @@ |
| #include "base/basictypes.h" |
| #include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/strings/string_number_conversions.h" |
| #include "mojo/examples/window_manager/window_manager.mojom.h" |
| #include "mojo/public/cpp/application/application.h" |
| +#include "mojo/services/navigation/navigation.mojom.h" |
| #include "mojo/services/public/cpp/view_manager/view.h" |
| #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| @@ -23,39 +26,74 @@ using mojo::view_manager::ViewTreeNodeObserver; |
| namespace mojo { |
| namespace examples { |
| -namespace { |
| - |
| -const SkColor kColors[] = { SK_ColorYELLOW, |
| - SK_ColorRED, |
| - SK_ColorGREEN, |
| - SK_ColorMAGENTA }; |
| - |
| -} // namespace |
| class EmbeddedApp : public Application, |
| public ViewManagerDelegate, |
| public ViewObserver, |
| public ViewTreeNodeObserver { |
| public: |
| - EmbeddedApp() {} |
| + EmbeddedApp() : view_manager_(NULL) {} |
| virtual ~EmbeddedApp() {} |
| + void SetNodeColor(uint32 node_id, SkColor color) { |
| + pending_node_colors_[node_id] = color; |
| + ProcessPendingNodeColor(node_id); |
| + } |
| + |
| private: |
| + class ViewNavigator : public InterfaceImpl<navigation::ViewNavigator> { |
| + public: |
| + explicit ViewNavigator(EmbeddedApp* app) : app_(app) { |
| + } |
| + |
| + private: |
| + virtual void Navigate(uint32 node_id, |
| + navigation::NavigationDetailsPtr details) OVERRIDE { |
| + const std::string& url = details->url.To<std::string>(); |
|
DaveMoore
2014/06/09 19:59:32
Nit: LOG(INFOS)'s should be removed before checkin
Aaron Boodman
2014/06/09 20:08:58
Done.
|
| + LOG(INFO) << "Navigate received for view " << node_id << " to: " << url; |
| + |
| + // TODO(aa): Verify new URL is same origin as current origin. |
| + |
| + size_t path_start = url.rfind('/'); |
| + if (path_start == url.npos) { |
| + LOG(ERROR) << "Invalid URL, no path separator"; |
| + return; |
| + } |
| + |
| + SkColor color = 0x00; |
| + if (!base::HexStringToUInt(url.substr(path_start + 1), &color)) { |
| + LOG(ERROR) << "Invalid URL, path not convertible to integer"; |
| + return; |
| + } |
| + |
| + app_->SetNodeColor(node_id, color); |
| + } |
| + |
| + EmbeddedApp* app_; |
| + }; |
| + |
| // Overridden from Application: |
| virtual void Initialize() MOJO_OVERRIDE { |
| ViewManager::Create(this, this); |
| + // TODO(aa): Weird for embeddee to talk to embedder by URL. Seems like |
| + // embedder should be able to specify the SP embeddee receives, then |
| + // communication can be anonymous. |
| ConnectTo<IWindowManager>("mojo:mojo_window_manager", &window_manager_); |
| + AddService<ViewNavigator>(this); |
| } |
| // Overridden from ViewManagerDelegate: |
| virtual void OnRootAdded(ViewManager* view_manager, |
| ViewTreeNode* root) OVERRIDE { |
| + if (!view_manager_) |
| + view_manager_ = view_manager; |
| + DCHECK_EQ(view_manager_, view_manager); |
| + |
| View* view = View::Create(view_manager); |
| view->AddObserver(this); |
| root->SetActiveView(view); |
| root->AddObserver(this); |
| - size_t index = view_manager->roots().size() - 1; |
| - view->SetColor(kColors[index % arraysize(kColors)]); |
| + ProcessPendingNodeColor(root->id()); |
| } |
| virtual void OnRootRemoved(ViewManager* view_manager, |
| ViewTreeNode* root) OVERRIDE { |
| @@ -81,9 +119,34 @@ class EmbeddedApp : public Application, |
| views_to_reap_[node] = old_view; |
| } |
| + void ProcessPendingNodeColor(uint32 node_id) { |
| + if (!view_manager_) { |
|
DaveMoore
2014/06/09 19:59:32
Nit: remove extra braces
Aaron Boodman
2014/06/09 20:08:58
Grump.
|
| + return; |
| + } |
| + |
| + ViewTreeNode* node = view_manager_->GetNodeById(node_id); |
| + if (!node) { |
| + return; |
| + } |
| + |
| + PendingNodeColors::iterator iter = pending_node_colors_.find(node_id); |
| + if (iter == pending_node_colors_.end()) { |
| + return; |
| + } |
| + |
| + node->active_view()->SetColor(iter->second); |
| + pending_node_colors_.erase(iter); |
| + } |
| + |
| + |
| + view_manager::ViewManager* view_manager_; |
| IWindowManagerPtr window_manager_; |
| std::map<ViewTreeNode*, View*> views_to_reap_; |
| + // We can receive navigations for nodes we don't have yet. |
| + typedef std::map<uint32, SkColor> PendingNodeColors; |
| + PendingNodeColors pending_node_colors_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); |
| }; |
| @@ -94,4 +157,4 @@ Application* Application::Create() { |
| return new examples::EmbeddedApp; |
| } |
| -} // namespace mojo |
| +} // namespace mojo |