Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / shell / lib / application.cc
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/public/cpp/shell/application.h"
6
7 namespace mojo {
8
9 Application::Application(ScopedMessagePipeHandle shell_handle)
10     : internal::ServiceConnectorBase::Owner(shell_handle.Pass()) {
11 }
12
13 Application::Application(MojoHandle shell_handle)
14     : internal::ServiceConnectorBase::Owner(
15           mojo::MakeScopedHandle(MessagePipeHandle(shell_handle)).Pass()) {}
16
17 Application::~Application() {
18   for (ServiceConnectorList::iterator it = service_connectors_.begin();
19        it != service_connectors_.end(); ++it) {
20     delete *it;
21   }
22 }
23
24 void Application::AddServiceConnector(
25     internal::ServiceConnectorBase* service_connector) {
26   service_connectors_.push_back(service_connector);
27   set_service_connector_owner(service_connector, this);
28 }
29
30 void Application::RemoveServiceConnector(
31     internal::ServiceConnectorBase* service_connector) {
32   for (ServiceConnectorList::iterator it = service_connectors_.begin();
33        it != service_connectors_.end(); ++it) {
34     if (*it == service_connector) {
35       service_connectors_.erase(it);
36       delete service_connector;
37       break;
38     }
39   }
40   if (service_connectors_.empty())
41     shell_.reset();
42 }
43
44 void Application::AcceptConnection(const mojo::String& url,
45                                    ScopedMessagePipeHandle client_handle) {
46   // TODO(davemoore): This method must be overridden by an Application subclass
47   // to dispatch to the right ServiceConnector. We need to figure out an
48   // approach to registration to make this better.
49   assert(1 == service_connectors_.size());
50   return service_connectors_.front()->AcceptConnection(url.To<std::string>(),
51                                                        client_handle.Pass());
52 }
53
54 }  // namespace mojo