Skip to content

Commit

Permalink
Adds typedefs for view manager types
Browse files Browse the repository at this point in the history
BUG=365012
TEST=covered by tests
R=ben@chromium.org

Review URL: https://codereview.chromium.org/257383007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@267809 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
sky@chromium.org committed May 2, 2014
1 parent 27652a5 commit ef8c7ed
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 112 deletions.
11 changes: 11 additions & 0 deletions mojo/mojo_services.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@
}],
],
},
{
'target_name': 'mojo_view_manager_common',
'type': 'static_library',
'sources': [
'services/public/cpp/view_manager/view_manager_types.h',
],
},
{
'target_name': 'mojo_view_manager_bindings',
'type': 'static_library',
Expand All @@ -141,6 +148,7 @@
'dependencies': [
'../base/base.gyp:base',
'mojo_view_manager_bindings',
'mojo_view_manager_common',
],
'sources': [
'services/public/cpp/view_manager/lib/view.cc',
Expand All @@ -156,6 +164,7 @@
'services/public/cpp/view_manager/lib/view_tree_node_private.h',
'services/public/cpp/view_manager/view.h',
'services/public/cpp/view_manager/view_manager.h',
'services/public/cpp/view_manager/view_manager_types.h',
'services/public/cpp/view_manager/view_tree_host.h',
'services/public/cpp/view_manager/view_tree_node.h',
'services/public/cpp/view_manager/view_tree_node_observer.h',
Expand Down Expand Up @@ -198,6 +207,7 @@
'mojo_shell_client',
'mojo_system_impl',
'mojo_view_manager_bindings',
'mojo_view_manager_common',
],
'sources': [
'services/view_manager/ids.h',
Expand Down Expand Up @@ -231,6 +241,7 @@
'mojo_shell_test_support',
'mojo_system_impl',
'mojo_view_manager_bindings',
'mojo_view_manager_common',
],
'sources': [
'services/view_manager/view_manager_connection_unittest.cc',
Expand Down
33 changes: 33 additions & 0 deletions mojo/services/public/cpp/view_manager/view_manager_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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_PUBLIC_CPP_VIEW_MANAGER_VIEW_MANAGER_TYPES_H_
#define MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_VIEW_MANAGER_TYPES_H_

#include "base/basictypes.h"

// Typedefs for the transport types. These typedefs match that of the mojom
// file, see it for specifics.

namespace mojo {
namespace services {
namespace view_manager {

typedef uint32_t TransportChangeId;
typedef uint16_t TransportConnectionId;
typedef uint32_t TransportNodeId;
typedef uint32_t TransportViewId;

// These types are used in two cases:
// 1. when a connection is creating a node/view. In this case the connection id
// from the current connection is used, so no need to explicitly specific it.
// 2. When the connection id is stored along side this.
typedef uint16_t TransportConnectionSpecificNodeId;
typedef uint16_t TransportConnectionSpecificViewId;

} // namespace view_manager
} // namespace services
} // namespace mojo

#endif // MOJO_SERVICES_PUBLIC_CPP_VIEW_MANAGER_VIEW_MANAGER_TYPES_H_
25 changes: 13 additions & 12 deletions mojo/services/view_manager/ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
#ifndef MOJO_SERVICES_VIEW_MANAGER_IDS_H_
#define MOJO_SERVICES_VIEW_MANAGER_IDS_H_

#include "mojo/services/public/cpp/view_manager/view_manager_types.h"
#include "mojo/services/view_manager/view_manager_export.h"

namespace mojo {
namespace services {
namespace view_manager {

typedef uint32_t ChangeId;

// Adds a bit of type safety to node ids.
struct MOJO_VIEW_MANAGER_EXPORT NodeId {
NodeId(uint16_t connection_id, uint16_t node_id)
NodeId(TransportConnectionId connection_id,
TransportConnectionSpecificNodeId node_id)
: connection_id(connection_id),
node_id(node_id) {}
NodeId() : connection_id(0), node_id(0) {}
Expand All @@ -29,13 +29,14 @@ struct MOJO_VIEW_MANAGER_EXPORT NodeId {
return !(*this == other);
}

uint16_t connection_id;
uint16_t node_id;
TransportConnectionId connection_id;
TransportConnectionSpecificNodeId node_id;
};

// Adds a bit of type safety to view ids.
struct MOJO_VIEW_MANAGER_EXPORT ViewId {
ViewId(uint16_t connection_id, uint16_t view_id)
ViewId(TransportConnectionId connection_id,
TransportConnectionSpecificViewId view_id)
: connection_id(connection_id),
view_id(view_id) {}
ViewId() : connection_id(0), view_id(0) {}
Expand All @@ -49,8 +50,8 @@ struct MOJO_VIEW_MANAGER_EXPORT ViewId {
return !(*this == other);
}

uint16_t connection_id;
uint16_t view_id;
TransportConnectionId connection_id;
TransportConnectionSpecificViewId view_id;
};

// Functions for converting to/from structs and transport values.
Expand All @@ -62,19 +63,19 @@ inline uint16_t SecondIdFromTransportId(uint32_t id) {
return static_cast<uint16_t>(id & 0xFFFF);
}

inline NodeId NodeIdFromTransportId(uint32_t id) {
inline NodeId NodeIdFromTransportId(TransportNodeId id) {
return NodeId(FirstIdFromTransportId(id), SecondIdFromTransportId(id));
}

inline uint32_t NodeIdToTransportId(const NodeId& id) {
inline TransportNodeId NodeIdToTransportId(const NodeId& id) {
return (id.connection_id << 16) | id.node_id;
}

inline ViewId ViewIdFromTransportId(uint32_t id) {
inline ViewId ViewIdFromTransportId(TransportViewId id) {
return ViewId(FirstIdFromTransportId(id), SecondIdFromTransportId(id));
}

inline uint32_t ViewIdToTransportId(const ViewId& id) {
inline TransportViewId ViewIdToTransportId(const ViewId& id) {
return (id.connection_id << 16) | id.view_id;
}

Expand Down
23 changes: 13 additions & 10 deletions mojo/services/view_manager/root_node_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ namespace view_manager {
namespace {

// Id for the root node.
const uint16_t kRootId = 1;
const TransportConnectionSpecificNodeId kRootId = 1;

} // namespace

RootNodeManager::ScopedChange::ScopedChange(ViewManagerConnection* connection,
RootNodeManager* root,
ChangeId change_id)
TransportChangeId change_id)
: root_(root) {
root_->PrepareForChange(connection, change_id);
}
Expand All @@ -36,8 +36,8 @@ RootNodeManager::RootNodeManager()
RootNodeManager::~RootNodeManager() {
}

uint16_t RootNodeManager::GetAndAdvanceNextConnectionId() {
const uint16_t id = next_connection_id_++;
TransportConnectionId RootNodeManager::GetAndAdvanceNextConnectionId() {
const TransportConnectionId id = next_connection_id_++;
DCHECK_LT(id, next_connection_id_);
return id;
}
Expand All @@ -51,7 +51,8 @@ void RootNodeManager::RemoveConnection(ViewManagerConnection* connection) {
connection_map_.erase(connection->id());
}

ViewManagerConnection* RootNodeManager::GetConnection(uint16_t connection_id) {
ViewManagerConnection* RootNodeManager::GetConnection(
TransportConnectionId connection_id) {
ConnectionMap::iterator i = connection_map_.find(connection_id);
return i == connection_map_.end() ? NULL : i->second;
}
Expand All @@ -73,28 +74,30 @@ void RootNodeManager::NotifyNodeHierarchyChanged(const NodeId& node,
const NodeId& old_parent) {
for (ConnectionMap::iterator i = connection_map_.begin();
i != connection_map_.end(); ++i) {
const ChangeId change_id = (change_ && i->first == change_->connection_id) ?
const TransportChangeId change_id =
(change_ && i->first == change_->connection_id) ?
change_->change_id : 0;
i->second->NotifyNodeHierarchyChanged(
node, new_parent, old_parent, change_id);
}
}

void RootNodeManager::NotifyNodeViewReplaced(const NodeId& node,
const ViewId& new_view_id,
const ViewId& old_view_id) {
const ViewId& new_view_id,
const ViewId& old_view_id) {
// TODO(sky): make a macro for this.
for (ConnectionMap::iterator i = connection_map_.begin();
i != connection_map_.end(); ++i) {
const ChangeId change_id = (change_ && i->first == change_->connection_id) ?
const TransportChangeId change_id =
(change_ && i->first == change_->connection_id) ?
change_->change_id : 0;
i->second->NotifyNodeViewReplaced(node, new_view_id, old_view_id,
change_id);
}
}

void RootNodeManager::PrepareForChange(ViewManagerConnection* connection,
ChangeId change_id) {
TransportChangeId change_id) {
DCHECK(!change_.get()); // Should only ever have one change in flight.
change_.reset(new Change(connection->id(), change_id));
}
Expand Down
20 changes: 11 additions & 9 deletions mojo/services/view_manager/root_node_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "base/basictypes.h"
#include "mojo/services/native_viewport/native_viewport.mojom.h"
#include "mojo/services/view_manager/ids.h"
#include "mojo/services/view_manager/node.h"
#include "mojo/services/view_manager/node_delegate.h"
#include "mojo/services/view_manager/view_manager_export.h"
Expand All @@ -32,7 +33,7 @@ class MOJO_VIEW_MANAGER_EXPORT RootNodeManager
public:
ScopedChange(ViewManagerConnection* connection,
RootNodeManager* root,
ChangeId change_id);
TransportChangeId change_id);
~ScopedChange();

private:
Expand All @@ -45,13 +46,13 @@ class MOJO_VIEW_MANAGER_EXPORT RootNodeManager
virtual ~RootNodeManager();

// Returns the id for the next ViewManagerConnection.
uint16_t GetAndAdvanceNextConnectionId();
TransportConnectionId GetAndAdvanceNextConnectionId();

void AddConnection(ViewManagerConnection* connection);
void RemoveConnection(ViewManagerConnection* connection);

// Returns the connection by id.
ViewManagerConnection* GetConnection(uint16_t connection_id);
ViewManagerConnection* GetConnection(TransportConnectionId connection_id);

// Returns the Node identified by |id|.
Node* GetNode(const NodeId& id);
Expand All @@ -71,24 +72,25 @@ class MOJO_VIEW_MANAGER_EXPORT RootNodeManager
private:
// Tracks a change.
struct Change {
Change(int32_t connection_id, ChangeId change_id)
Change(TransportConnectionId connection_id, TransportChangeId change_id)
: connection_id(connection_id),
change_id(change_id) {
}

int32_t connection_id;
ChangeId change_id;
TransportConnectionId connection_id;
TransportChangeId change_id;
};

typedef std::map<uint16_t, ViewManagerConnection*> ConnectionMap;
typedef std::map<TransportConnectionId, ViewManagerConnection*> ConnectionMap;

// Invoked when a particular connection is about to make a change. Records
// the |change_id| so that it can be supplied to the clients by way of
// OnNodeHierarchyChanged().
// Changes should never nest, meaning each PrepareForChange() must be
// balanced with a call to FinishChange() with no PrepareForChange()
// in between.
void PrepareForChange(ViewManagerConnection* connection, ChangeId change_id);
void PrepareForChange(ViewManagerConnection* connection,
TransportChangeId change_id);

// Balances a call to PrepareForChange().
void FinishChange();
Expand All @@ -109,7 +111,7 @@ class MOJO_VIEW_MANAGER_EXPORT RootNodeManager
const ViewId& old_view_id) OVERRIDE;

// ID to use for next ViewManagerConnection.
uint16_t next_connection_id_;
TransportConnectionId next_connection_id_;

// Set of ViewManagerConnections.
ConnectionMap connection_map_;
Expand Down
Loading

0 comments on commit ef8c7ed

Please sign in to comment.