| 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 adcef0264bff9231bc917eb596293548b2638457..003cb82d0f5f192d0ab160cda373895c8188aa16 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"
|
| @@ -13,6 +16,8 @@
|
| #include "mojo/services/public/cpp/view_manager/view_tree_node.h"
|
| #include "mojo/services/public/cpp/view_manager/view_tree_node_observer.h"
|
| #include "ui/events/event_constants.h"
|
| +#include "url/gurl.h"
|
| +#include "url/url_util.h"
|
|
|
| using mojo::view_manager::View;
|
| using mojo::view_manager::ViewManager;
|
| @@ -23,28 +28,54 @@ 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) {
|
| + url::AddStandardScheme("mojo");
|
| + }
|
| virtual ~EmbeddedApp() {}
|
|
|
| + void SetNodeColor(uint32 node_id, SkColor color) {
|
| + pending_node_colors_[node_id] = color;
|
| + ProcessPendingNodeColor(node_id);
|
| + }
|
| +
|
| private:
|
| + class Navigator : public InterfaceImpl<navigation::Navigator> {
|
| + public:
|
| + explicit Navigator(EmbeddedApp* app) : app_(app) {}
|
| + private:
|
| + virtual void Navigate(uint32 node_id,
|
| + navigation::NavigationDetailsPtr details) OVERRIDE {
|
| + GURL url(details->url.To<std::string>());
|
| + if (!url.is_valid()) {
|
| + LOG(ERROR) << "URL is invalid.";
|
| + return;
|
| + }
|
| + // TODO(aa): Verify new URL is same origin as current origin.
|
| + SkColor color = 0x00;
|
| + if (!base::HexStringToUInt(url.path().substr(1), &color)) {
|
| + LOG(ERROR) << "Invalid URL, path not convertible to integer";
|
| + return;
|
| + }
|
| + app_->SetNodeColor(node_id, color);
|
| + }
|
| + EmbeddedApp* app_;
|
| + DISALLOW_COPY_AND_ASSIGN(Navigator);
|
| + };
|
| +
|
| // 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<Navigator>(this);
|
| }
|
|
|
| // Overridden from ViewManagerDelegate:
|
| @@ -54,11 +85,14 @@ class EmbeddedApp : public Application,
|
| view->AddObserver(this);
|
| root->SetActiveView(view);
|
| root->AddObserver(this);
|
| - size_t index = view_manager->GetRoots().size() - 1;
|
| - view->SetColor(kColors[index % arraysize(kColors)]);
|
| +
|
| + roots_[root->id()] = root;
|
| + ProcessPendingNodeColor(root->id());
|
| }
|
| virtual void OnRootRemoved(ViewManager* view_manager,
|
| ViewTreeNode* root) OVERRIDE {
|
| + roots_.erase(root->id());
|
| +
|
| std::map<ViewTreeNode*, View*>::const_iterator it =
|
| views_to_reap_.find(root);
|
| if (it != views_to_reap_.end())
|
| @@ -81,9 +115,31 @@ class EmbeddedApp : public Application,
|
| views_to_reap_[node] = old_view;
|
| }
|
|
|
| + void ProcessPendingNodeColor(uint32 node_id) {
|
| + RootMap::iterator root = roots_.find(node_id);
|
| + if (root == roots_.end())
|
| + return;
|
| +
|
| + PendingNodeColors::iterator color = pending_node_colors_.find(node_id);
|
| + if (color == pending_node_colors_.end())
|
| + return;
|
| +
|
| + root->second->active_view()->SetColor(color->second);
|
| + pending_node_colors_.erase(color);
|
| + }
|
| +
|
| +
|
| + view_manager::ViewManager* view_manager_;
|
| IWindowManagerPtr window_manager_;
|
| std::map<ViewTreeNode*, View*> views_to_reap_;
|
|
|
| + typedef std::map<uint32, ViewTreeNode*> RootMap;
|
| + RootMap roots_;
|
| +
|
| + // 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);
|
| };
|
|
|
|
|