summaryrefslogtreecommitdiffstats
path: root/mojo/examples/nesting_app/nesting_app.cc
blob: dee9497b10cecc818ecf889a23f9a4f6c8f09afc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/strings/stringprintf.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/node.h"
#include "mojo/services/public/cpp/view_manager/node_observer.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"
#include "mojo/services/public/cpp/view_manager/view_observer.h"
#include "ui/events/event_constants.h"
#include "url/gurl.h"

using mojo::view_manager::Node;
using mojo::view_manager::NodeObserver;
using mojo::view_manager::View;
using mojo::view_manager::ViewManager;
using mojo::view_manager::ViewManagerDelegate;
using mojo::view_manager::ViewObserver;

namespace mojo {
namespace examples {

namespace {
const char kEmbeddedAppURL[] = "mojo:mojo_embedded_app";
}

// An app that embeds another app.
class NestingApp : public Application,
                   public ViewManagerDelegate,
                   public ViewObserver {
 public:
  NestingApp() : nested_(NULL) {}
  virtual ~NestingApp() {}

 private:
  class Navigator : public InterfaceImpl<navigation::Navigator> {
   public:
    explicit Navigator(NestingApp* 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;
      }
      app_->color_ = url.path().substr(1);
      app_->NavigateChild();
    }
    NestingApp* app_;
    DISALLOW_COPY_AND_ASSIGN(Navigator);
  };

  // Overridden from Application:
  virtual void Initialize() MOJO_OVERRIDE {
    ViewManager::Create(this, this);
    ConnectTo<IWindowManager>("mojo:mojo_window_manager", &window_manager_);
    AddService<Navigator>(this);
  }

  // Overridden from ViewManagerDelegate:
  virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE {
    View* view = View::Create(view_manager);
    root->SetActiveView(view);
    view->SetColor(SK_ColorCYAN);
    view->AddObserver(this);

    nested_ = Node::Create(view_manager);
    root->AddChild(nested_);
    nested_->SetBounds(gfx::Rect(20, 20, 50, 50));
    nested_->Embed(kEmbeddedAppURL);

    if (!navigator_.get())
      ConnectTo(kEmbeddedAppURL, &navigator_);

    NavigateChild();
  }

  virtual void OnRootRemoved(ViewManager* view_manager, Node* root) OVERRIDE {
    // TODO(beng): reap views & child nodes.
    nested_ = NULL;
  }

  // Overridden from ViewObserver:
  virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE {
    if (event->action == ui::ET_MOUSE_RELEASED)
      window_manager_->CloseWindow(view->node()->id());
  }

  void NavigateChild() {
    if (!color_.empty() && nested_) {
      navigation::NavigationDetailsPtr details(
          navigation::NavigationDetails::New());
      details->url =
          base::StringPrintf("%s/%s", kEmbeddedAppURL, color_.c_str());
      navigator_->Navigate(nested_->id(), details.Pass());
    }
  }

  std::string color_;
  Node* nested_;
  navigation::NavigatorPtr navigator_;
  IWindowManagerPtr window_manager_;

  DISALLOW_COPY_AND_ASSIGN(NestingApp);
};

}  // namespace examples

// static
Application* Application::Create() {
  return new examples::NestingApp;
}

}  // namespace mojo