1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_VIEW_MANAGER_SERVER_VIEW_H_
6 #define COMPONENTS_VIEW_MANAGER_SERVER_VIEW_H_
10 #include "base/logging.h"
11 #include "base/observer_list.h"
12 #include "cc/surfaces/surface_factory.h"
13 #include "cc/surfaces/surface_factory_client.h"
14 #include "cc/surfaces/surface_id.h"
15 #include "cc/surfaces/surface_id_allocator.h"
16 #include "components/view_manager/ids.h"
17 #include "components/view_manager/public/interfaces/view_tree.mojom.h"
18 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
19 #include "ui/gfx/geometry/rect.h"
20 #include "ui/gfx/transform.h"
21 #include "ui/platform_window/text_input_state.h"
23 namespace view_manager
{
25 class ServerViewDelegate
;
26 class ServerViewObserver
;
28 // Server side representation of a view. Delegate is informed of interesting
31 // It is assumed that all functions that mutate the tree have validated the
32 // mutation is possible before hand. For example, Reorder() assumes the supplied
33 // view is a child and not already in position.
35 // ServerViews do not own their children. If you delete a view that has children
36 // the children are implicitly removed. Similarly if a view has a parent and the
37 // view is deleted the deleted view is implicitly removed from the parent.
38 class ServerView
: public mojo::Surface
,
39 public cc::SurfaceFactoryClient
{
41 ServerView(ServerViewDelegate
* delegate
, const ViewId
& id
);
42 ~ServerView() override
;
44 void AddObserver(ServerViewObserver
* observer
);
45 void RemoveObserver(ServerViewObserver
* observer
);
47 // Sets the access policy that is used the next time Embed() is called.
48 void set_pending_access_policy(uint32_t policy
) {
49 pending_access_policy_
= policy
;
52 // Returns |pending_access_policy_| and resets it to the default.
53 uint32_t get_and_clear_pending_access_policy() {
54 const uint32_t policy
= pending_access_policy_
;
55 pending_access_policy_
= mojo::ViewTree::ACCESS_POLICY_DEFAULT
;
59 // Binds the provided |request| to |this| object. If an interface is already
60 // bound to this ServerView then the old connection is closed first.
61 void Bind(mojo::InterfaceRequest
<Surface
> request
,
62 mojo::SurfaceClientPtr client
);
64 const ViewId
& id() const { return id_
; }
66 void Add(ServerView
* child
);
67 void Remove(ServerView
* child
);
68 void Reorder(ServerView
* child
,
70 mojo::OrderDirection direction
);
72 const gfx::Rect
& bounds() const { return bounds_
; }
73 void SetBounds(const gfx::Rect
& bounds
);
75 const ServerView
* parent() const { return parent_
; }
76 ServerView
* parent() { return parent_
; }
78 const ServerView
* GetRoot() const;
79 ServerView
* GetRoot() {
80 return const_cast<ServerView
*>(
81 const_cast<const ServerView
*>(this)->GetRoot());
84 std::vector
<const ServerView
*> GetChildren() const;
85 std::vector
<ServerView
*> GetChildren();
87 // Returns true if this contains |view| or is |view|.
88 bool Contains(const ServerView
* view
) const;
90 // Returns true if the window is visible. This does not consider visibility
92 bool visible() const { return visible_
; }
93 void SetVisible(bool value
);
95 float opacity() const { return opacity_
; }
96 void SetOpacity(float value
);
98 const gfx::Transform
& transform() const { return transform_
; }
99 void SetTransform(const gfx::Transform
& transform
);
101 const std::map
<std::string
, std::vector
<uint8_t>>& properties() const {
104 void SetProperty(const std::string
& name
, const std::vector
<uint8_t>* value
);
106 void SetTextInputState(const ui::TextInputState
& state
);
107 const ui::TextInputState
& text_input_state() const {
108 return text_input_state_
;
111 // Returns true if this view is attached to a root and all ancestors are
113 bool IsDrawn() const;
115 void SetSurfaceId(cc::SurfaceId surface_id
);
116 const cc::SurfaceId
& surface_id() const { return surface_id_
; }
118 const gfx::Size
& last_submitted_frame_size() {
119 return last_submitted_frame_size_
;
123 void SubmitCompositorFrame(
124 mojo::CompositorFramePtr frame
,
125 const SubmitCompositorFrameCallback
& callback
) override
;
128 std::string
GetDebugWindowHierarchy() const;
129 void BuildDebugInfo(const std::string
& depth
, std::string
* result
) const;
133 typedef std::vector
<ServerView
*> Views
;
135 // SurfaceFactoryClient implementation.
136 void ReturnResources(const cc::ReturnedResourceArray
& resources
) override
;
138 // Implementation of removing a view. Doesn't send any notification.
139 void RemoveImpl(ServerView
* view
);
141 ServerViewDelegate
* delegate_
;
147 cc::SurfaceId surface_id_
;
148 scoped_ptr
<cc::SurfaceIdAllocator
> surface_id_allocator_
;
149 scoped_ptr
<cc::SurfaceFactory
> surface_factory_
;
151 gfx::Transform transform_
;
152 uint32_t pending_access_policy_
;
153 ui::TextInputState text_input_state_
;
154 gfx::Size last_submitted_frame_size_
;
156 std::map
<std::string
, std::vector
<uint8_t>> properties_
;
158 base::ObserverList
<ServerViewObserver
> observers_
;
159 mojo::SurfaceClientPtr client_
;
160 mojo::Binding
<Surface
> binding_
;
162 DISALLOW_COPY_AND_ASSIGN(ServerView
);
165 } // namespace view_manager
167 #endif // COMPONENTS_VIEW_MANAGER_SERVER_VIEW_H_