| OLD | NEW | 
 | (Empty) | 
|    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. |  | 
|    4  |  | 
|    5 #include "services/view_manager/view_manager_service_impl.h" |  | 
|    6  |  | 
|    7 #include "base/bind.h" |  | 
|    8 #include "base/stl_util.h" |  | 
|    9 #include "base/trace_event/trace_event.h" |  | 
|   10 #include "mojo/converters/geometry/geometry_type_converters.h" |  | 
|   11 #include "mojo/converters/input_events/input_events_type_converters.h" |  | 
|   12 #include "mojo/converters/surfaces/surfaces_type_converters.h" |  | 
|   13 #include "mojo/services/window_manager/interfaces/window_manager_internal.mojom.
     h" |  | 
|   14 #include "services/view_manager/connection_manager.h" |  | 
|   15 #include "services/view_manager/default_access_policy.h" |  | 
|   16 #include "services/view_manager/display_manager.h" |  | 
|   17 #include "services/view_manager/server_view.h" |  | 
|   18 #include "services/view_manager/window_manager_access_policy.h" |  | 
|   19  |  | 
|   20 using mojo::Array; |  | 
|   21 using mojo::Callback; |  | 
|   22 using mojo::Id; |  | 
|   23 using mojo::InterfaceRequest; |  | 
|   24 using mojo::OrderDirection; |  | 
|   25 using mojo::ServiceProvider; |  | 
|   26 using mojo::ServiceProviderPtr; |  | 
|   27 using mojo::String; |  | 
|   28 using mojo::ViewDataPtr; |  | 
|   29  |  | 
|   30 namespace view_manager { |  | 
|   31  |  | 
|   32 ViewManagerServiceImpl::ViewManagerServiceImpl( |  | 
|   33     ConnectionManager* connection_manager, |  | 
|   34     mojo::ConnectionSpecificId creator_id, |  | 
|   35     const std::string& creator_url, |  | 
|   36     const std::string& url, |  | 
|   37     const ViewId& root_id) |  | 
|   38     : connection_manager_(connection_manager), |  | 
|   39       id_(connection_manager_->GetAndAdvanceNextConnectionId()), |  | 
|   40       url_(url), |  | 
|   41       creator_id_(creator_id), |  | 
|   42       creator_url_(creator_url), |  | 
|   43       client_(nullptr) { |  | 
|   44   CHECK(GetView(root_id)); |  | 
|   45   root_.reset(new ViewId(root_id)); |  | 
|   46   if (root_id == RootViewId()) |  | 
|   47     access_policy_.reset(new WindowManagerAccessPolicy(id_, this)); |  | 
|   48   else |  | 
|   49     access_policy_.reset(new DefaultAccessPolicy(id_, this)); |  | 
|   50 } |  | 
|   51  |  | 
|   52 ViewManagerServiceImpl::~ViewManagerServiceImpl() { |  | 
|   53   DestroyViews(); |  | 
|   54 } |  | 
|   55  |  | 
|   56 void ViewManagerServiceImpl::Init(mojo::ViewManagerClient* client, |  | 
|   57                                   mojo::ViewManagerServicePtr service_ptr, |  | 
|   58                                   InterfaceRequest<ServiceProvider> services, |  | 
|   59                                   ServiceProviderPtr exposed_services) { |  | 
|   60   TRACE_EVENT0("view_manager", __func__); |  | 
|   61   DCHECK(!client_); |  | 
|   62   client_ = client; |  | 
|   63   std::vector<const ServerView*> to_send; |  | 
|   64   if (root_.get()) |  | 
|   65     GetUnknownViewsFrom(GetView(*root_), &to_send); |  | 
|   66  |  | 
|   67   mojo::MessagePipe pipe; |  | 
|   68   connection_manager_->wm_internal()->CreateWindowManagerForViewManagerClient( |  | 
|   69       id_, pipe.handle1.Pass()); |  | 
|   70   client->OnEmbed(id_, creator_url_, ViewToViewData(to_send.front()), |  | 
|   71                   service_ptr.Pass(), services.Pass(), exposed_services.Pass(), |  | 
|   72                   pipe.handle0.Pass()); |  | 
|   73 } |  | 
|   74  |  | 
|   75 const ServerView* ViewManagerServiceImpl::GetView(const ViewId& id) const { |  | 
|   76   TRACE_EVENT0("view_manager", __func__); |  | 
|   77   if (id_ == id.connection_id) { |  | 
|   78     ViewMap::const_iterator i = view_map_.find(id.view_id); |  | 
|   79     return i == view_map_.end() ? NULL : i->second; |  | 
|   80   } |  | 
|   81   return connection_manager_->GetView(id); |  | 
|   82 } |  | 
|   83  |  | 
|   84 bool ViewManagerServiceImpl::IsRoot(const ViewId& id) const { |  | 
|   85   return root_.get() && *root_ == id; |  | 
|   86 } |  | 
|   87  |  | 
|   88 void ViewManagerServiceImpl::OnWillDestroyViewManagerServiceImpl( |  | 
|   89     ViewManagerServiceImpl* connection) { |  | 
|   90   if (creator_id_ == connection->id()) |  | 
|   91     creator_id_ = kInvalidConnectionId; |  | 
|   92   if (connection->root_ && connection->root_->connection_id == id_ && |  | 
|   93       view_map_.count(connection->root_->view_id) > 0) { |  | 
|   94     client()->OnEmbeddedAppDisconnected( |  | 
|   95         ViewIdToTransportId(*connection->root_)); |  | 
|   96   } |  | 
|   97   if (root_.get() && root_->connection_id == connection->id()) |  | 
|   98     root_.reset(); |  | 
|   99 } |  | 
|  100  |  | 
|  101 mojo::ErrorCode ViewManagerServiceImpl::CreateView(const ViewId& view_id) { |  | 
|  102   if (view_id.connection_id != id_) |  | 
|  103     return mojo::ErrorCode::ILLEGAL_ARGUMENT; |  | 
|  104   if (view_map_.find(view_id.view_id) != view_map_.end()) |  | 
|  105     return mojo::ErrorCode::VALUE_IN_USE; |  | 
|  106   view_map_[view_id.view_id] = connection_manager_->CreateServerView(view_id); |  | 
|  107   known_views_.insert(ViewIdToTransportId(view_id)); |  | 
|  108   return mojo::ErrorCode::NONE; |  | 
|  109 } |  | 
|  110  |  | 
|  111 bool ViewManagerServiceImpl::AddView(const ViewId& parent_id, |  | 
|  112                                      const ViewId& child_id) { |  | 
|  113   ServerView* parent = GetView(parent_id); |  | 
|  114   ServerView* child = GetView(child_id); |  | 
|  115   if (parent && child && child->parent() != parent && |  | 
|  116       !child->Contains(parent) && access_policy_->CanAddView(parent, child)) { |  | 
|  117     ConnectionManager::ScopedChange change(this, connection_manager_, false); |  | 
|  118     parent->Add(child); |  | 
|  119     return true; |  | 
|  120   } |  | 
|  121   return false; |  | 
|  122 } |  | 
|  123  |  | 
|  124 std::vector<const ServerView*> ViewManagerServiceImpl::GetViewTree( |  | 
|  125     const ViewId& view_id) const { |  | 
|  126   const ServerView* view = GetView(view_id); |  | 
|  127   std::vector<const ServerView*> views; |  | 
|  128   if (view) |  | 
|  129     GetViewTreeImpl(view, &views); |  | 
|  130   return views; |  | 
|  131 } |  | 
|  132  |  | 
|  133 bool ViewManagerServiceImpl::SetViewVisibility(const ViewId& view_id, |  | 
|  134                                                bool visible) { |  | 
|  135   ServerView* view = GetView(view_id); |  | 
|  136   if (!view || view->visible() == visible || |  | 
|  137       !access_policy_->CanChangeViewVisibility(view)) { |  | 
|  138     return false; |  | 
|  139   } |  | 
|  140   ConnectionManager::ScopedChange change(this, connection_manager_, false); |  | 
|  141   view->SetVisible(visible); |  | 
|  142   return true; |  | 
|  143 } |  | 
|  144  |  | 
|  145 bool ViewManagerServiceImpl::EmbedUrl( |  | 
|  146     const std::string& url, |  | 
|  147     const ViewId& view_id, |  | 
|  148     InterfaceRequest<ServiceProvider> services, |  | 
|  149     ServiceProviderPtr exposed_services) { |  | 
|  150   TRACE_EVENT0("view_manager", __func__); |  | 
|  151   if (!PrepareForEmbed(view_id)) |  | 
|  152     return false; |  | 
|  153   connection_manager_->EmbedAtView(id_, url, view_id, services.Pass(), |  | 
|  154                                    exposed_services.Pass()); |  | 
|  155   return true; |  | 
|  156 } |  | 
|  157  |  | 
|  158 bool ViewManagerServiceImpl::Embed(const ViewId& view_id, |  | 
|  159                                    mojo::ViewManagerClientPtr client) { |  | 
|  160   TRACE_EVENT0("view_manager", __func__); |  | 
|  161   if (!client.get() || !PrepareForEmbed(view_id)) |  | 
|  162     return false; |  | 
|  163   connection_manager_->EmbedAtView(id_, view_id, client.Pass()); |  | 
|  164   return true; |  | 
|  165 } |  | 
|  166  |  | 
|  167 void ViewManagerServiceImpl::ProcessViewBoundsChanged( |  | 
|  168     const ServerView* view, |  | 
|  169     const gfx::Rect& old_bounds, |  | 
|  170     const gfx::Rect& new_bounds, |  | 
|  171     bool originated_change) { |  | 
|  172   if (originated_change || !IsViewKnown(view)) |  | 
|  173     return; |  | 
|  174   client()->OnViewBoundsChanged(ViewIdToTransportId(view->id()), |  | 
|  175                                 mojo::Rect::From(old_bounds), |  | 
|  176                                 mojo::Rect::From(new_bounds)); |  | 
|  177 } |  | 
|  178  |  | 
|  179 void ViewManagerServiceImpl::ProcessViewportMetricsChanged( |  | 
|  180     const mojo::ViewportMetrics& old_metrics, |  | 
|  181     const mojo::ViewportMetrics& new_metrics, |  | 
|  182     bool originated_change) { |  | 
|  183   client()->OnViewViewportMetricsChanged(old_metrics.Clone(), |  | 
|  184                                          new_metrics.Clone()); |  | 
|  185 } |  | 
|  186  |  | 
|  187 void ViewManagerServiceImpl::ProcessWillChangeViewHierarchy( |  | 
|  188     const ServerView* view, |  | 
|  189     const ServerView* new_parent, |  | 
|  190     const ServerView* old_parent, |  | 
|  191     bool originated_change) { |  | 
|  192   if (originated_change) |  | 
|  193     return; |  | 
|  194  |  | 
|  195   const bool old_drawn = view->IsDrawn(connection_manager_->root()); |  | 
|  196   const bool new_drawn = view->visible() && new_parent && |  | 
|  197                          new_parent->IsDrawn(connection_manager_->root()); |  | 
|  198   if (old_drawn == new_drawn) |  | 
|  199     return; |  | 
|  200  |  | 
|  201   NotifyDrawnStateChanged(view, new_drawn); |  | 
|  202 } |  | 
|  203  |  | 
|  204 void ViewManagerServiceImpl::ProcessViewPropertyChanged( |  | 
|  205     const ServerView* view, |  | 
|  206     const std::string& name, |  | 
|  207     const std::vector<uint8_t>* new_data, |  | 
|  208     bool originated_change) { |  | 
|  209   if (originated_change) |  | 
|  210     return; |  | 
|  211  |  | 
|  212   Array<uint8_t> data; |  | 
|  213   if (new_data) |  | 
|  214     data = Array<uint8_t>::From(*new_data); |  | 
|  215  |  | 
|  216   client()->OnViewSharedPropertyChanged(ViewIdToTransportId(view->id()), |  | 
|  217                                         String(name), data.Pass()); |  | 
|  218 } |  | 
|  219  |  | 
|  220 void ViewManagerServiceImpl::ProcessViewHierarchyChanged( |  | 
|  221     const ServerView* view, |  | 
|  222     const ServerView* new_parent, |  | 
|  223     const ServerView* old_parent, |  | 
|  224     bool originated_change) { |  | 
|  225   if (originated_change && !IsViewKnown(view) && new_parent && |  | 
|  226       IsViewKnown(new_parent)) { |  | 
|  227     std::vector<const ServerView*> unused; |  | 
|  228     GetUnknownViewsFrom(view, &unused); |  | 
|  229   } |  | 
|  230   if (originated_change || connection_manager_->is_processing_delete_view() || |  | 
|  231       connection_manager_->DidConnectionMessageClient(id_)) { |  | 
|  232     return; |  | 
|  233   } |  | 
|  234  |  | 
|  235   if (!access_policy_->ShouldNotifyOnHierarchyChange(view, &new_parent, |  | 
|  236                                                      &old_parent)) { |  | 
|  237     return; |  | 
|  238   } |  | 
|  239   // Inform the client of any new views and update the set of views we know |  | 
|  240   // about. |  | 
|  241   std::vector<const ServerView*> to_send; |  | 
|  242   if (!IsViewKnown(view)) |  | 
|  243     GetUnknownViewsFrom(view, &to_send); |  | 
|  244   const ViewId new_parent_id(new_parent ? new_parent->id() : ViewId()); |  | 
|  245   const ViewId old_parent_id(old_parent ? old_parent->id() : ViewId()); |  | 
|  246   client()->OnViewHierarchyChanged( |  | 
|  247       ViewIdToTransportId(view->id()), ViewIdToTransportId(new_parent_id), |  | 
|  248       ViewIdToTransportId(old_parent_id), ViewsToViewDatas(to_send)); |  | 
|  249   connection_manager_->OnConnectionMessagedClient(id_); |  | 
|  250 } |  | 
|  251  |  | 
|  252 void ViewManagerServiceImpl::ProcessViewReorder(const ServerView* view, |  | 
|  253                                                 const ServerView* relative_view, |  | 
|  254                                                 OrderDirection direction, |  | 
|  255                                                 bool originated_change) { |  | 
|  256   if (originated_change || !IsViewKnown(view) || !IsViewKnown(relative_view)) |  | 
|  257     return; |  | 
|  258  |  | 
|  259   client()->OnViewReordered(ViewIdToTransportId(view->id()), |  | 
|  260                             ViewIdToTransportId(relative_view->id()), |  | 
|  261                             direction); |  | 
|  262 } |  | 
|  263  |  | 
|  264 void ViewManagerServiceImpl::ProcessViewDeleted(const ViewId& view, |  | 
|  265                                                 bool originated_change) { |  | 
|  266   if (view.connection_id == id_) |  | 
|  267     view_map_.erase(view.view_id); |  | 
|  268  |  | 
|  269   const bool in_known = known_views_.erase(ViewIdToTransportId(view)) > 0; |  | 
|  270  |  | 
|  271   if (IsRoot(view)) |  | 
|  272     root_.reset(); |  | 
|  273  |  | 
|  274   if (originated_change) |  | 
|  275     return; |  | 
|  276  |  | 
|  277   if (in_known) { |  | 
|  278     client()->OnViewDeleted(ViewIdToTransportId(view)); |  | 
|  279     connection_manager_->OnConnectionMessagedClient(id_); |  | 
|  280   } |  | 
|  281 } |  | 
|  282  |  | 
|  283 void ViewManagerServiceImpl::ProcessWillChangeViewVisibility( |  | 
|  284     const ServerView* view, |  | 
|  285     bool originated_change) { |  | 
|  286   if (originated_change) |  | 
|  287     return; |  | 
|  288  |  | 
|  289   if (IsViewKnown(view)) { |  | 
|  290     client()->OnViewVisibilityChanged(ViewIdToTransportId(view->id()), |  | 
|  291                                       !view->visible()); |  | 
|  292     return; |  | 
|  293   } |  | 
|  294  |  | 
|  295   bool view_target_drawn_state; |  | 
|  296   if (view->visible()) { |  | 
|  297     // View is being hidden, won't be drawn. |  | 
|  298     view_target_drawn_state = false; |  | 
|  299   } else { |  | 
|  300     // View is being shown. View will be drawn if its parent is drawn. |  | 
|  301     view_target_drawn_state = |  | 
|  302         view->parent() && view->parent()->IsDrawn(connection_manager_->root()); |  | 
|  303   } |  | 
|  304  |  | 
|  305   NotifyDrawnStateChanged(view, view_target_drawn_state); |  | 
|  306 } |  | 
|  307  |  | 
|  308 bool ViewManagerServiceImpl::IsViewKnown(const ServerView* view) const { |  | 
|  309   return known_views_.count(ViewIdToTransportId(view->id())) > 0; |  | 
|  310 } |  | 
|  311  |  | 
|  312 bool ViewManagerServiceImpl::CanReorderView(const ServerView* view, |  | 
|  313                                             const ServerView* relative_view, |  | 
|  314                                             OrderDirection direction) const { |  | 
|  315   if (!view || !relative_view) |  | 
|  316     return false; |  | 
|  317  |  | 
|  318   if (!view->parent() || view->parent() != relative_view->parent()) |  | 
|  319     return false; |  | 
|  320  |  | 
|  321   if (!access_policy_->CanReorderView(view, relative_view, direction)) |  | 
|  322     return false; |  | 
|  323  |  | 
|  324   std::vector<const ServerView*> children = view->parent()->GetChildren(); |  | 
|  325   const size_t child_i = |  | 
|  326       std::find(children.begin(), children.end(), view) - children.begin(); |  | 
|  327   const size_t target_i = |  | 
|  328       std::find(children.begin(), children.end(), relative_view) - |  | 
|  329       children.begin(); |  | 
|  330   if ((direction == mojo::OrderDirection::ABOVE && child_i == target_i + 1) || |  | 
|  331       (direction == mojo::OrderDirection::BELOW && child_i + 1 == target_i)) { |  | 
|  332     return false; |  | 
|  333   } |  | 
|  334  |  | 
|  335   return true; |  | 
|  336 } |  | 
|  337  |  | 
|  338 bool ViewManagerServiceImpl::DeleteViewImpl(ViewManagerServiceImpl* source, |  | 
|  339                                             ServerView* view) { |  | 
|  340   DCHECK(view); |  | 
|  341   DCHECK_EQ(view->id().connection_id, id_); |  | 
|  342   ConnectionManager::ScopedChange change(source, connection_manager_, true); |  | 
|  343   delete view; |  | 
|  344   return true; |  | 
|  345 } |  | 
|  346  |  | 
|  347 void ViewManagerServiceImpl::GetUnknownViewsFrom( |  | 
|  348     const ServerView* view, |  | 
|  349     std::vector<const ServerView*>* views) { |  | 
|  350   if (IsViewKnown(view) || !access_policy_->CanGetViewTree(view)) |  | 
|  351     return; |  | 
|  352   views->push_back(view); |  | 
|  353   known_views_.insert(ViewIdToTransportId(view->id())); |  | 
|  354   if (!access_policy_->CanDescendIntoViewForViewTree(view)) |  | 
|  355     return; |  | 
|  356   std::vector<const ServerView*> children(view->GetChildren()); |  | 
|  357   for (size_t i = 0; i < children.size(); ++i) |  | 
|  358     GetUnknownViewsFrom(children[i], views); |  | 
|  359 } |  | 
|  360  |  | 
|  361 void ViewManagerServiceImpl::RemoveFromKnown( |  | 
|  362     const ServerView* view, |  | 
|  363     std::vector<ServerView*>* local_views) { |  | 
|  364   if (view->id().connection_id == id_) { |  | 
|  365     if (local_views) |  | 
|  366       local_views->push_back(GetView(view->id())); |  | 
|  367     return; |  | 
|  368   } |  | 
|  369   known_views_.erase(ViewIdToTransportId(view->id())); |  | 
|  370   std::vector<const ServerView*> children = view->GetChildren(); |  | 
|  371   for (size_t i = 0; i < children.size(); ++i) |  | 
|  372     RemoveFromKnown(children[i], local_views); |  | 
|  373 } |  | 
|  374  |  | 
|  375 void ViewManagerServiceImpl::RemoveRoot() { |  | 
|  376   CHECK(root_.get()); |  | 
|  377   const ViewId root_id(*root_); |  | 
|  378   root_.reset(); |  | 
|  379   // No need to do anything if we created the view. |  | 
|  380   if (root_id.connection_id == id_) |  | 
|  381     return; |  | 
|  382  |  | 
|  383   client()->OnViewDeleted(ViewIdToTransportId(root_id)); |  | 
|  384   connection_manager_->OnConnectionMessagedClient(id_); |  | 
|  385  |  | 
|  386   // This connection no longer knows about the view. Unparent any views that |  | 
|  387   // were parented to views in the root. |  | 
|  388   std::vector<ServerView*> local_views; |  | 
|  389   RemoveFromKnown(GetView(root_id), &local_views); |  | 
|  390   for (size_t i = 0; i < local_views.size(); ++i) |  | 
|  391     local_views[i]->parent()->Remove(local_views[i]); |  | 
|  392 } |  | 
|  393  |  | 
|  394 void ViewManagerServiceImpl::RemoveChildrenAsPartOfEmbed( |  | 
|  395     const ViewId& view_id) { |  | 
|  396   ServerView* view = GetView(view_id); |  | 
|  397   CHECK(view); |  | 
|  398   CHECK(view->id().connection_id == view_id.connection_id); |  | 
|  399   std::vector<ServerView*> children = view->GetChildren(); |  | 
|  400   for (size_t i = 0; i < children.size(); ++i) |  | 
|  401     view->Remove(children[i]); |  | 
|  402 } |  | 
|  403  |  | 
|  404 Array<ViewDataPtr> ViewManagerServiceImpl::ViewsToViewDatas( |  | 
|  405     const std::vector<const ServerView*>& views) { |  | 
|  406   auto array = Array<ViewDataPtr>::New(views.size()); |  | 
|  407   for (size_t i = 0; i < views.size(); ++i) |  | 
|  408     array[i] = ViewToViewData(views[i]).Pass(); |  | 
|  409   return array; |  | 
|  410 } |  | 
|  411  |  | 
|  412 ViewDataPtr ViewManagerServiceImpl::ViewToViewData(const ServerView* view) { |  | 
|  413   DCHECK(IsViewKnown(view)); |  | 
|  414   const ServerView* parent = view->parent(); |  | 
|  415   // If the parent isn't known, it means the parent is not visible to us (not |  | 
|  416   // in roots), and should not be sent over. |  | 
|  417   if (parent && !IsViewKnown(parent)) |  | 
|  418     parent = NULL; |  | 
|  419   ViewDataPtr view_data(mojo::ViewData::New()); |  | 
|  420   view_data->parent_id = ViewIdToTransportId(parent ? parent->id() : ViewId()); |  | 
|  421   view_data->view_id = ViewIdToTransportId(view->id()); |  | 
|  422   view_data->bounds = mojo::Rect::From(view->bounds()); |  | 
|  423   view_data->properties = |  | 
|  424       mojo::Map<String, Array<uint8_t>>::From(view->properties()); |  | 
|  425   view_data->visible = view->visible(); |  | 
|  426   view_data->drawn = view->IsDrawn(connection_manager_->root()); |  | 
|  427   view_data->viewport_metrics = |  | 
|  428       connection_manager_->display_manager()->GetViewportMetrics().Clone(); |  | 
|  429   return view_data; |  | 
|  430 } |  | 
|  431  |  | 
|  432 void ViewManagerServiceImpl::GetViewTreeImpl( |  | 
|  433     const ServerView* view, |  | 
|  434     std::vector<const ServerView*>* views) const { |  | 
|  435   DCHECK(view); |  | 
|  436  |  | 
|  437   if (!access_policy_->CanGetViewTree(view)) |  | 
|  438     return; |  | 
|  439  |  | 
|  440   views->push_back(view); |  | 
|  441  |  | 
|  442   if (!access_policy_->CanDescendIntoViewForViewTree(view)) |  | 
|  443     return; |  | 
|  444  |  | 
|  445   std::vector<const ServerView*> children(view->GetChildren()); |  | 
|  446   for (size_t i = 0; i < children.size(); ++i) |  | 
|  447     GetViewTreeImpl(children[i], views); |  | 
|  448 } |  | 
|  449  |  | 
|  450 void ViewManagerServiceImpl::NotifyDrawnStateChanged(const ServerView* view, |  | 
|  451                                                      bool new_drawn_value) { |  | 
|  452   // Even though we don't know about view, it may be an ancestor of our root, in |  | 
|  453   // which case the change may effect our roots drawn state. |  | 
|  454   if (!root_.get()) |  | 
|  455     return; |  | 
|  456  |  | 
|  457   const ServerView* root = GetView(*root_); |  | 
|  458   DCHECK(root); |  | 
|  459   if (view->Contains(root) && |  | 
|  460       (new_drawn_value != root->IsDrawn(connection_manager_->root()))) { |  | 
|  461     client()->OnViewDrawnStateChanged(ViewIdToTransportId(root->id()), |  | 
|  462                                       new_drawn_value); |  | 
|  463   } |  | 
|  464 } |  | 
|  465  |  | 
|  466 void ViewManagerServiceImpl::DestroyViews() { |  | 
|  467   if (!view_map_.empty()) { |  | 
|  468     ConnectionManager::ScopedChange change(this, connection_manager_, true); |  | 
|  469     // If we get here from the destructor we're not going to get |  | 
|  470     // ProcessViewDeleted(). Copy the map and delete from the copy so that we |  | 
|  471     // don't have to worry about whether |view_map_| changes or not. |  | 
|  472     ViewMap view_map_copy; |  | 
|  473     view_map_.swap(view_map_copy); |  | 
|  474     STLDeleteValues(&view_map_copy); |  | 
|  475   } |  | 
|  476 } |  | 
|  477  |  | 
|  478 bool ViewManagerServiceImpl::PrepareForEmbed(const ViewId& view_id) { |  | 
|  479   const ServerView* view = GetView(view_id); |  | 
|  480   if (!view || !access_policy_->CanEmbed(view)) |  | 
|  481     return false; |  | 
|  482  |  | 
|  483   // Only allow a node to be the root for one connection. |  | 
|  484   ViewManagerServiceImpl* existing_owner = |  | 
|  485       connection_manager_->GetConnectionWithRoot(view_id); |  | 
|  486  |  | 
|  487   ConnectionManager::ScopedChange change(this, connection_manager_, true); |  | 
|  488   RemoveChildrenAsPartOfEmbed(view_id); |  | 
|  489   if (existing_owner) { |  | 
|  490     // Never message the originating connection. |  | 
|  491     connection_manager_->OnConnectionMessagedClient(id_); |  | 
|  492     existing_owner->RemoveRoot(); |  | 
|  493   } |  | 
|  494   return true; |  | 
|  495 } |  | 
|  496  |  | 
|  497 void ViewManagerServiceImpl::CreateView( |  | 
|  498     Id transport_view_id, |  | 
|  499     const Callback<void(mojo::ErrorCode)>& callback) { |  | 
|  500   callback.Run(CreateView(ViewIdFromTransportId(transport_view_id))); |  | 
|  501 } |  | 
|  502  |  | 
|  503 void ViewManagerServiceImpl::DeleteView(Id transport_view_id, |  | 
|  504                                         const Callback<void(bool)>& callback) { |  | 
|  505   ServerView* view = GetView(ViewIdFromTransportId(transport_view_id)); |  | 
|  506   bool success = false; |  | 
|  507   if (view && access_policy_->CanDeleteView(view)) { |  | 
|  508     ViewManagerServiceImpl* connection = |  | 
|  509         connection_manager_->GetConnection(view->id().connection_id); |  | 
|  510     success = connection && connection->DeleteViewImpl(this, view); |  | 
|  511   } |  | 
|  512   callback.Run(success); |  | 
|  513 } |  | 
|  514  |  | 
|  515 void ViewManagerServiceImpl::AddView(Id parent_id, |  | 
|  516                                      Id child_id, |  | 
|  517                                      const Callback<void(bool)>& callback) { |  | 
|  518   callback.Run(AddView(ViewIdFromTransportId(parent_id), |  | 
|  519                        ViewIdFromTransportId(child_id))); |  | 
|  520 } |  | 
|  521  |  | 
|  522 void ViewManagerServiceImpl::RemoveViewFromParent( |  | 
|  523     Id view_id, |  | 
|  524     const Callback<void(bool)>& callback) { |  | 
|  525   bool success = false; |  | 
|  526   ServerView* view = GetView(ViewIdFromTransportId(view_id)); |  | 
|  527   if (view && view->parent() && access_policy_->CanRemoveViewFromParent(view)) { |  | 
|  528     success = true; |  | 
|  529     ConnectionManager::ScopedChange change(this, connection_manager_, false); |  | 
|  530     view->parent()->Remove(view); |  | 
|  531   } |  | 
|  532   callback.Run(success); |  | 
|  533 } |  | 
|  534  |  | 
|  535 void ViewManagerServiceImpl::ReorderView(Id view_id, |  | 
|  536                                          Id relative_view_id, |  | 
|  537                                          OrderDirection direction, |  | 
|  538                                          const Callback<void(bool)>& callback) { |  | 
|  539   bool success = false; |  | 
|  540   ServerView* view = GetView(ViewIdFromTransportId(view_id)); |  | 
|  541   ServerView* relative_view = GetView(ViewIdFromTransportId(relative_view_id)); |  | 
|  542   if (CanReorderView(view, relative_view, direction)) { |  | 
|  543     success = true; |  | 
|  544     ConnectionManager::ScopedChange change(this, connection_manager_, false); |  | 
|  545     view->parent()->Reorder(view, relative_view, direction); |  | 
|  546     connection_manager_->ProcessViewReorder(view, relative_view, direction); |  | 
|  547   } |  | 
|  548   callback.Run(success); |  | 
|  549 } |  | 
|  550  |  | 
|  551 void ViewManagerServiceImpl::GetViewTree( |  | 
|  552     Id view_id, |  | 
|  553     const Callback<void(Array<ViewDataPtr>)>& callback) { |  | 
|  554   std::vector<const ServerView*> views( |  | 
|  555       GetViewTree(ViewIdFromTransportId(view_id))); |  | 
|  556   callback.Run(ViewsToViewDatas(views)); |  | 
|  557 } |  | 
|  558  |  | 
|  559 void ViewManagerServiceImpl::SetViewSurfaceId( |  | 
|  560     Id view_id, |  | 
|  561     mojo::SurfaceIdPtr surface_id, |  | 
|  562     const Callback<void(bool)>& callback) { |  | 
|  563   // TODO(sky): add coverage of not being able to set for random node. |  | 
|  564   ServerView* view = GetView(ViewIdFromTransportId(view_id)); |  | 
|  565   if (!view || !access_policy_->CanSetViewSurfaceId(view)) { |  | 
|  566     callback.Run(false); |  | 
|  567     return; |  | 
|  568   } |  | 
|  569   view->SetSurfaceId(surface_id.To<cc::SurfaceId>()); |  | 
|  570   callback.Run(true); |  | 
|  571 } |  | 
|  572  |  | 
|  573 void ViewManagerServiceImpl::SetViewBounds( |  | 
|  574     Id view_id, |  | 
|  575     mojo::RectPtr bounds, |  | 
|  576     const Callback<void(bool)>& callback) { |  | 
|  577   ServerView* view = GetView(ViewIdFromTransportId(view_id)); |  | 
|  578   const bool success = view && access_policy_->CanSetViewBounds(view); |  | 
|  579   if (success) { |  | 
|  580     ConnectionManager::ScopedChange change(this, connection_manager_, false); |  | 
|  581     view->SetBounds(bounds.To<gfx::Rect>()); |  | 
|  582   } |  | 
|  583   callback.Run(success); |  | 
|  584 } |  | 
|  585  |  | 
|  586 void ViewManagerServiceImpl::SetViewVisibility( |  | 
|  587     Id transport_view_id, |  | 
|  588     bool visible, |  | 
|  589     const Callback<void(bool)>& callback) { |  | 
|  590   callback.Run( |  | 
|  591       SetViewVisibility(ViewIdFromTransportId(transport_view_id), visible)); |  | 
|  592 } |  | 
|  593  |  | 
|  594 void ViewManagerServiceImpl::SetViewProperty( |  | 
|  595     uint32_t view_id, |  | 
|  596     const mojo::String& name, |  | 
|  597     mojo::Array<uint8_t> value, |  | 
|  598     const mojo::Callback<void(bool)>& callback) { |  | 
|  599   ServerView* view = GetView(ViewIdFromTransportId(view_id)); |  | 
|  600   const bool success = view && access_policy_->CanSetViewProperties(view); |  | 
|  601   if (success) { |  | 
|  602     ConnectionManager::ScopedChange change(this, connection_manager_, false); |  | 
|  603  |  | 
|  604     if (value.is_null()) { |  | 
|  605       view->SetProperty(name, nullptr); |  | 
|  606     } else { |  | 
|  607       std::vector<uint8_t> data = value.To<std::vector<uint8_t>>(); |  | 
|  608       view->SetProperty(name, &data); |  | 
|  609     } |  | 
|  610   } |  | 
|  611   callback.Run(success); |  | 
|  612 } |  | 
|  613  |  | 
|  614 void ViewManagerServiceImpl::EmbedUrl( |  | 
|  615     const String& url, |  | 
|  616     Id transport_view_id, |  | 
|  617     InterfaceRequest<ServiceProvider> services, |  | 
|  618     ServiceProviderPtr exposed_services, |  | 
|  619     const Callback<void(bool)>& callback) { |  | 
|  620   callback.Run(EmbedUrl(url.To<std::string>(), |  | 
|  621                         ViewIdFromTransportId(transport_view_id), |  | 
|  622                         services.Pass(), exposed_services.Pass())); |  | 
|  623 } |  | 
|  624  |  | 
|  625 void ViewManagerServiceImpl::Embed(mojo::Id transport_view_id, |  | 
|  626                                    mojo::ViewManagerClientPtr client, |  | 
|  627                                    const mojo::Callback<void(bool)>& callback) { |  | 
|  628   callback.Run(Embed(ViewIdFromTransportId(transport_view_id), client.Pass())); |  | 
|  629 } |  | 
|  630  |  | 
|  631 void ViewManagerServiceImpl::PerformAction( |  | 
|  632     mojo::Id transport_view_id, |  | 
|  633     const mojo::String& action, |  | 
|  634     const mojo::Callback<void(bool)>& callback) { |  | 
|  635   TRACE_EVENT0("view_manager", __func__); |  | 
|  636   connection_manager_->GetWindowManagerViewManagerClient()->OnPerformAction( |  | 
|  637       transport_view_id, action, callback); |  | 
|  638 } |  | 
|  639  |  | 
|  640 bool ViewManagerServiceImpl::IsRootForAccessPolicy(const ViewId& id) const { |  | 
|  641   return IsRoot(id); |  | 
|  642 } |  | 
|  643  |  | 
|  644 bool ViewManagerServiceImpl::IsViewKnownForAccessPolicy( |  | 
|  645     const ServerView* view) const { |  | 
|  646   return IsViewKnown(view); |  | 
|  647 } |  | 
|  648  |  | 
|  649 bool ViewManagerServiceImpl::IsViewRootOfAnotherConnectionForAccessPolicy( |  | 
|  650     const ServerView* view) const { |  | 
|  651   ViewManagerServiceImpl* connection = |  | 
|  652       connection_manager_->GetConnectionWithRoot(view->id()); |  | 
|  653   return connection && connection != this; |  | 
|  654 } |  | 
|  655  |  | 
|  656 }  // namespace view_manager |  | 
| OLD | NEW |