summaryrefslogtreecommitdiffstats
path: root/mojo/services
diff options
context:
space:
mode:
authorsky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-04-22 21:58:10 +0000
committersky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-04-22 21:58:10 +0000
commitd0012d1f99eaf57dbc42c06361adc4f880c474ce (patch)
tree57dc537253b9e364e8fedc4f12bd05f7ac2d8e32 /mojo/services
parent0544ea95970c19bcc0abbd33be91bc595f672f3a (diff)
downloadchromium_src-d0012d1f99eaf57dbc42c06361adc4f880c474ce.zip
chromium_src-d0012d1f99eaf57dbc42c06361adc4f880c474ce.tar.gz
chromium_src-d0012d1f99eaf57dbc42c06361adc4f880c474ce.tar.bz2
Start of server side of viewmanager
I'll figure out tests then notifications next. BUG=365012 TEST=none R=ben@chromium.org Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=265180 Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=265300 Review URL: https://codereview.chromium.org/245663002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@265376 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/services')
-rw-r--r--mojo/services/public/interfaces/view_manager/view_manager.mojom37
-rw-r--r--mojo/services/view_manager/DEPS6
-rw-r--r--mojo/services/view_manager/root_view_manager.cc64
-rw-r--r--mojo/services/view_manager/root_view_manager.h65
-rw-r--r--mojo/services/view_manager/view.cc41
-rw-r--r--mojo/services/view_manager/view.h38
-rw-r--r--mojo/services/view_manager/view_manager.cc32
-rw-r--r--mojo/services/view_manager/view_manager_connection.cc85
-rw-r--r--mojo/services/view_manager/view_manager_connection.h68
9 files changed, 436 insertions, 0 deletions
diff --git a/mojo/services/public/interfaces/view_manager/view_manager.mojom b/mojo/services/public/interfaces/view_manager/view_manager.mojom
new file mode 100644
index 0000000..45a7e69
--- /dev/null
+++ b/mojo/services/public/interfaces/view_manager/view_manager.mojom
@@ -0,0 +1,37 @@
+// 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.
+
+module mojo.services.view_manager {
+
+// Each View is identified by the following pair. The |manager_id| is assigned
+// by the server and uniquely identifies the ViewManager. A value of 0 can be
+// used to indicate 'this' manager. The |view_id| is assigned by the client.
+// Non-negative values may be used. Server uses negative to identify special
+// values. For example, -1 is the root.
+struct ViewId {
+ int32 manager_id;
+ int32 view_id;
+};
+
+[Peer=ViewManagerClient]
+interface ViewManager {
+ // Creates a new view with the specified id. It is up to the client to ensure
+ // the id is unique to the connection (the id need not be globally unique).
+ CreateView(int32 view_id) => (bool success);
+
+ // Reparents a view.
+ AddView(ViewId parent, ViewId child) => (bool success);
+
+ // Removes a view from its current parent.
+ RemoveFromParent(ViewId view) => (bool success);
+};
+
+[Peer=ViewManager]
+interface ViewManagerClient {
+ // Invoked once the connection has been established. |manager_id| is the id
+ // used to uniquely identify the ViewManager.
+ OnConnectionEstablished(int32 manager_id);
+};
+
+}
diff --git a/mojo/services/view_manager/DEPS b/mojo/services/view_manager/DEPS
new file mode 100644
index 0000000..05af088
--- /dev/null
+++ b/mojo/services/view_manager/DEPS
@@ -0,0 +1,6 @@
+include_rules = [
+ "+mojo/services",
+ "+ui/aura",
+ "+ui/events",
+ "+ui/gfx",
+]
diff --git a/mojo/services/view_manager/root_view_manager.cc b/mojo/services/view_manager/root_view_manager.cc
new file mode 100644
index 0000000..d791cb3
--- /dev/null
+++ b/mojo/services/view_manager/root_view_manager.cc
@@ -0,0 +1,64 @@
+// 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 "mojo/services/view_manager/root_view_manager.h"
+
+#include "base/logging.h"
+#include "mojo/services/view_manager/view_manager_connection.h"
+
+namespace mojo {
+namespace services {
+namespace view_manager {
+namespace {
+
+// Identifies the root of the view hierarchy.
+const int32_t kRootId = -1;
+
+} // namespace
+
+RootViewManager::RootViewManager()
+ : next_connection_id_(1),
+ root_(kRootId) {
+}
+
+RootViewManager::~RootViewManager() {
+}
+
+int32_t RootViewManager::GetAndAdvanceNextConnectionId() {
+ return next_connection_id_++;
+}
+
+void RootViewManager::AddConnection(ViewManagerConnection* connection) {
+ DCHECK_EQ(0u, connection_map_.count(connection->id()));
+ connection_map_[connection->id()] = connection;
+}
+
+void RootViewManager::RemoveConnection(ViewManagerConnection* connection) {
+ connection_map_.erase(connection->id());
+}
+
+View* RootViewManager::GetView(int32_t manager_id, int32_t view_id) {
+ if (view_id == kRootId)
+ return &root_;
+ ConnectionMap::iterator i = connection_map_.find(manager_id);
+ return i == connection_map_.end() ? NULL : i->second->GetView(view_id);
+}
+
+void RootViewManager::OnCreated() {
+}
+
+void RootViewManager::OnDestroyed() {
+}
+
+void RootViewManager::OnBoundsChanged(const Rect& bounds) {
+}
+
+void RootViewManager::OnEvent(const Event& event,
+ const mojo::Callback<void()>& callback) {
+ callback.Run();
+}
+
+} // namespace view_manager
+} // namespace services
+} // namespace mojo
diff --git a/mojo/services/view_manager/root_view_manager.h b/mojo/services/view_manager/root_view_manager.h
new file mode 100644
index 0000000..65376ce
--- /dev/null
+++ b/mojo/services/view_manager/root_view_manager.h
@@ -0,0 +1,65 @@
+// 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.
+
+#ifndef MOJO_SERVICES_VIEW_MANAGER_ROOT_VIEW_MANAGER_H_
+#define MOJO_SERVICES_VIEW_MANAGER_ROOT_VIEW_MANAGER_H_
+
+#include <map>
+
+#include "base/basictypes.h"
+#include "mojo/services/native_viewport/native_viewport.mojom.h"
+#include "mojo/services/view_manager/view.h"
+
+namespace mojo {
+namespace services {
+namespace view_manager {
+
+class View;
+class ViewId;
+class ViewManagerConnection;
+
+// RootViewManager is responsible for managing the set of ViewManagerConnections
+// as well as providing the root of the View hierarchy.
+class RootViewManager : public NativeViewportClient {
+ public:
+ RootViewManager();
+ virtual ~RootViewManager();
+
+ // Returns the id for the next view manager.
+ int32_t GetAndAdvanceNextConnectionId();
+
+ void AddConnection(ViewManagerConnection* connection);
+ void RemoveConnection(ViewManagerConnection* connection);
+
+ // Returns the View identified by the specified pair, or NULL if there isn't
+ // one.
+ View* GetView(int32_t manager_id, int32_t view_id);
+
+ private:
+ typedef std::map<int32_t, ViewManagerConnection*> ConnectionMap;
+
+ // Overridden from NativeViewportClient:
+ virtual void OnCreated() OVERRIDE;
+ virtual void OnDestroyed() OVERRIDE;
+ virtual void OnBoundsChanged(const Rect& bounds) OVERRIDE;
+ virtual void OnEvent(const Event& event,
+ const mojo::Callback<void()>& callback) OVERRIDE;
+
+ // ID to use for next ViewManagerConnection.
+ int32_t next_connection_id_;
+
+ // Set of ViewManagerConnections.
+ ConnectionMap connection_map_;
+
+ // Root of Views that are displayed.
+ View root_;
+
+ DISALLOW_COPY_AND_ASSIGN(RootViewManager);
+};
+
+} // namespace view_manager
+} // namespace services
+} // namespace mojo
+
+#endif // MOJO_SERVICES_VIEW_MANAGER_ROOT_VIEW_MANAGER_H_
diff --git a/mojo/services/view_manager/view.cc b/mojo/services/view_manager/view.cc
new file mode 100644
index 0000000..6215854
--- /dev/null
+++ b/mojo/services/view_manager/view.cc
@@ -0,0 +1,41 @@
+// 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 "mojo/services/view_manager/view.h"
+
+#include "ui/aura/window_property.h"
+
+DECLARE_WINDOW_PROPERTY_TYPE(mojo::services::view_manager::View*);
+
+namespace mojo {
+namespace services {
+namespace view_manager {
+
+DEFINE_WINDOW_PROPERTY_KEY(View*, kViewKey, NULL);
+
+// TODO(sky): figure out window delegate.
+View::View(int32_t id) : id_(id), window_(NULL) {
+ window_.set_owned_by_parent(false);
+}
+
+View::~View() {
+}
+
+View* View::GetParent() {
+ if (!window_.parent())
+ return NULL;
+ return window_.parent()->GetProperty(kViewKey);
+}
+
+void View::Add(View* child) {
+ window_.AddChild(&child->window_);
+}
+
+void View::Remove(View* child) {
+ window_.RemoveChild(&child->window_);
+}
+
+} // namespace view_manager
+} // namespace services
+} // namespace mojo
diff --git a/mojo/services/view_manager/view.h b/mojo/services/view_manager/view.h
new file mode 100644
index 0000000..1815f7f
--- /dev/null
+++ b/mojo/services/view_manager/view.h
@@ -0,0 +1,38 @@
+// 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.
+
+#ifndef MOJO_SERVICES_VIEW_MANAGER_VIEW_H_
+#define MOJO_SERVICES_VIEW_MANAGER_VIEW_H_
+
+#include "base/logging.h"
+#include "ui/aura/window.h"
+
+namespace mojo {
+namespace services {
+namespace view_manager {
+
+class View {
+ public:
+ View(int32_t view_id);
+ ~View();
+
+ int32 id() const { return id_; }
+
+ void Add(View* child);
+ void Remove(View* child);
+
+ View* GetParent();
+
+ private:
+ const int32_t id_;
+ aura::Window window_;
+
+ DISALLOW_COPY_AND_ASSIGN(View);
+};
+
+} // namespace view_manager
+} // namespace services
+} // namespace mojo
+
+#endif // MOJO_SERVICES_VIEW_MANAGER_VIEW_H_
diff --git a/mojo/services/view_manager/view_manager.cc b/mojo/services/view_manager/view_manager.cc
new file mode 100644
index 0000000..5c026b9
--- /dev/null
+++ b/mojo/services/view_manager/view_manager.cc
@@ -0,0 +1,32 @@
+// 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/message_loop/message_loop.h"
+#include "mojo/public/cpp/shell/application.h"
+#include "mojo/services/view_manager/root_view_manager.h"
+#include "mojo/services/view_manager/view_manager_connection.h"
+
+#if defined(WIN32)
+#if !defined(CDECL)
+#define CDECL __cdecl)
+#endif
+#define VIEW_MANAGER_EXPORT __declspec(dllexport)
+#else
+#define CDECL
+#define VIEW_MANAGER_EXPORT __attribute__((visibility("default")))
+#endif
+
+extern "C" VIEW_MANAGER_EXPORT MojoResult CDECL MojoMain(
+ MojoHandle shell_handle) {
+ base::MessageLoop loop;
+ mojo::Application app(shell_handle);
+ mojo::services::view_manager::RootViewManager root_view_manager;
+ app.AddServiceConnector(new mojo::ServiceConnector
+ <mojo::services::view_manager::ViewManagerConnection,
+ mojo::services::view_manager::RootViewManager>(
+ &root_view_manager));
+ loop.Run();
+
+ return MOJO_RESULT_OK;
+}
diff --git a/mojo/services/view_manager/view_manager_connection.cc b/mojo/services/view_manager/view_manager_connection.cc
new file mode 100644
index 0000000..5b85d98
--- /dev/null
+++ b/mojo/services/view_manager/view_manager_connection.cc
@@ -0,0 +1,85 @@
+// 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 "mojo/services/view_manager/view_manager_connection.h"
+
+#include "base/stl_util.h"
+#include "mojo/services/view_manager/root_view_manager.h"
+#include "mojo/services/view_manager/view.h"
+
+namespace mojo {
+namespace services {
+namespace view_manager {
+
+ViewManagerConnection::ViewManagerConnection() : id_(0) {
+}
+
+ViewManagerConnection::~ViewManagerConnection() {
+ STLDeleteContainerPairSecondPointers(view_map_.begin(), view_map_.end());
+ context()->RemoveConnection(this);
+}
+
+void ViewManagerConnection::Initialize(
+ ServiceConnector<ViewManagerConnection, RootViewManager>* service_factory,
+ ScopedMessagePipeHandle client_handle) {
+ DCHECK_EQ(0, id_); // Should only get Initialize() once.
+ ServiceConnection<ViewManager, ViewManagerConnection, RootViewManager>::
+ Initialize(service_factory, client_handle.Pass());
+ context()->AddConnection(this);
+ id_ = context()->GetAndAdvanceNextConnectionId();
+ client()->OnConnectionEstablished(id_);
+}
+
+View* ViewManagerConnection::GetView(int32_t id) {
+ ViewMap::iterator i = view_map_.find(id);
+ return i == view_map_.end() ? NULL : i->second;
+}
+
+View* ViewManagerConnection::GetViewById(const ViewId& view_id) {
+ const int32_t manager_id =
+ view_id.manager_id() == 0 ? id_ : view_id.manager_id();
+ return context()->GetView(manager_id, view_id.view_id());
+}
+
+void ViewManagerConnection::CreateView(
+ int32_t view_id,
+ const mojo::Callback<void(bool)>& callback) {
+ // Negative values are reserved.
+ if (view_map_.find(view_id) != view_map_.end() || view_id < 0) {
+ callback.Run(false);
+ return;
+ }
+ view_map_[view_id] = new View(view_id);
+ callback.Run(true);
+}
+
+void ViewManagerConnection::AddView(
+ const ViewId& parent_id,
+ const ViewId& child_id,
+ const mojo::Callback<void(bool)>& callback) {
+ View* parent_view = GetViewById(parent_id);
+ View* child_view = GetViewById(child_id);
+ bool success = false;
+ if (parent_view && child_view && parent_view != child_view) {
+ parent_view->Add(child_view);
+ success = true;
+ }
+ callback.Run(success);
+}
+
+void ViewManagerConnection::RemoveFromParent(
+ const ViewId& view_id,
+ const mojo::Callback<void(bool)>& callback) {
+ View* view = GetViewById(view_id);
+ bool success = false;
+ if (view && view->GetParent()) {
+ view->GetParent()->Add(view);
+ success = true;
+ }
+ callback.Run(success);
+}
+
+} // namespace view_manager
+} // namespace services
+} // namespace mojo
diff --git a/mojo/services/view_manager/view_manager_connection.h b/mojo/services/view_manager/view_manager_connection.h
new file mode 100644
index 0000000..2b0d4c1
--- /dev/null
+++ b/mojo/services/view_manager/view_manager_connection.h
@@ -0,0 +1,68 @@
+// 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.
+
+#ifndef MOJO_SERVICES_VIEW_MANAGER_VIEW_MANAGER_CONNECTION_H_
+#define MOJO_SERVICES_VIEW_MANAGER_VIEW_MANAGER_CONNECTION_H_
+
+#include <set>
+
+#include "base/basictypes.h"
+#include "mojo/public/cpp/shell/service.h"
+#include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h"
+
+namespace mojo {
+namespace services {
+namespace view_manager {
+
+class RootViewManager;
+class View;
+
+// Manages a connection from the client.
+class ViewManagerConnection : public ServiceConnection<ViewManager,
+ ViewManagerConnection,
+ RootViewManager> {
+ public:
+ ViewManagerConnection();
+ virtual ~ViewManagerConnection();
+
+ int32_t id() const { return id_; }
+
+ // Invoked from Service when connection is established.
+ void Initialize(
+ ServiceConnector<ViewManagerConnection, RootViewManager>* service_factory,
+ ScopedMessagePipeHandle client_handle);
+
+ // Returns the View by id.
+ View* GetView(int32_t id);
+
+ private:
+ typedef std::map<int32_t, View*> ViewMap;
+
+ // Returns the View by ViewId.
+ View* GetViewById(const ViewId& view_id);
+
+ // Overridden from ViewManager:
+ virtual void CreateView(int32_t view_id,
+ const mojo::Callback<void(bool)>& callback) OVERRIDE;
+ virtual void AddView(const ViewId& parent_id,
+ const ViewId& child_id,
+ const mojo::Callback<void(bool)>& callback) OVERRIDE;
+ virtual void RemoveFromParent(
+ const ViewId& view_id,
+ const mojo::Callback<void(bool)>& callback) OVERRIDE;
+
+ // Id of this connection as assigned by RootViewManager. Assigned in
+ // Initialize().
+ int32_t id_;
+
+ ViewMap view_map_;
+
+ DISALLOW_COPY_AND_ASSIGN(ViewManagerConnection);
+};
+
+} // namespace view_manager
+} // namespace services
+} // namespace mojo
+
+#endif // MOJO_SERVICES_VIEW_MANAGER_VIEW_MANAGER_CONNECTION_H_