summaryrefslogtreecommitdiffstats
path: root/mojo/public/cpp/application/lib/application.cc
blob: 72ce58028fb4bf1253dda580da0f047772e96204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 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.

#include "mojo/public/cpp/application/application.h"

namespace mojo {

Application::Application(ScopedMessagePipeHandle service_provider_handle)
    : internal::ServiceConnectorBase::Owner(service_provider_handle.Pass()) {
}

Application::Application(MojoHandle service_provider_handle)
    : internal::ServiceConnectorBase::Owner(
          mojo::MakeScopedHandle(
              MessagePipeHandle(service_provider_handle)).Pass()) {}

Application::~Application() {
  for (ServiceConnectorList::iterator it = service_connectors_.begin();
       it != service_connectors_.end(); ++it) {
    delete *it;
  }
}

void Application::AddServiceConnector(
    internal::ServiceConnectorBase* service_connector) {
  service_connectors_.push_back(service_connector);
  set_service_connector_owner(service_connector, this);
}

void Application::RemoveServiceConnector(
    internal::ServiceConnectorBase* service_connector) {
  for (ServiceConnectorList::iterator it = service_connectors_.begin();
       it != service_connectors_.end(); ++it) {
    if (*it == service_connector) {
      service_connectors_.erase(it);
      delete service_connector;
      break;
    }
  }
  if (service_connectors_.empty())
    service_provider_.reset();
}

void Application::ConnectToService(const mojo::String& url,
                                   ScopedMessagePipeHandle client_handle) {
  // TODO(davemoore): This method must be overridden by an Application subclass
  // to dispatch to the right ServiceConnector. We need to figure out an
  // approach to registration to make this better.
  assert(1 == service_connectors_.size());
  return service_connectors_.front()->ConnectToService(url.To<std::string>(),
                                                       client_handle.Pass());
}

}  // namespace mojo