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 "mojo/services/view_manager/view_manager_init_service_context.h" 6 7#include "base/auto_reset.h" 8#include "base/bind.h" 9#include "mojo/services/view_manager/connection_manager.h" 10#include "mojo/services/view_manager/view_manager_init_service_impl.h" 11 12namespace mojo { 13namespace service { 14 15ViewManagerInitServiceContext::ConnectParams::ConnectParams() {} 16 17ViewManagerInitServiceContext::ConnectParams::~ConnectParams() {} 18 19ViewManagerInitServiceContext::ViewManagerInitServiceContext() 20 : deleting_connection_(false) { 21} 22ViewManagerInitServiceContext::~ViewManagerInitServiceContext() {} 23 24void ViewManagerInitServiceContext::AddConnection( 25 ViewManagerInitServiceImpl* connection) { 26 DCHECK(std::find(connections_.begin(), connections_.end(), connection) == 27 connections_.end()); 28 connections_.push_back(connection); 29} 30 31void ViewManagerInitServiceContext::RemoveConnection( 32 ViewManagerInitServiceImpl* connection) { 33 if (!deleting_connection_) { 34 Connections::iterator it = 35 std::find(connections_.begin(), connections_.end(), connection); 36 DCHECK(it != connections_.end()); 37 connections_.erase(it); 38 } 39 40 // This object is owned by an object that outlives the current thread's 41 // message loop, so we need to destroy the ConnectionManager earlier, as it 42 // may attempt to post tasks during its destruction. 43 if (connections_.empty()) 44 connection_manager_.reset(); 45} 46 47void ViewManagerInitServiceContext::ConfigureIncomingConnection( 48 ApplicationConnection* connection) { 49 if (!connection_manager_.get()) { 50 connection_manager_.reset(new ConnectionManager( 51 connection, 52 base::Bind(&ViewManagerInitServiceContext::OnNativeViewportDeleted, 53 base::Unretained(this)))); 54 } 55} 56 57void ViewManagerInitServiceContext::Embed( 58 const String& url, 59 ServiceProviderPtr service_provider, 60 const Callback<void(bool)>& callback) { 61 connection_manager_->EmbedRoot(url, Get(&service_provider)); 62 callback.Run(true); 63} 64 65void ViewManagerInitServiceContext::OnNativeViewportDeleted() { 66 // Prevent the connection from modifying the connection list during manual 67 // teardown. 68 base::AutoReset<bool> deleting_connection(&deleting_connection_, true); 69 for (Connections::const_iterator it = connections_.begin(); 70 it != connections_.end(); ++it) { 71 delete *it; 72 } 73 connections_.clear(); 74 connection_manager_.reset(); 75} 76 77} // namespace service 78} // namespace mojo 79