Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(215)

Side by Side Diff: services/resource_coordinator/public/cpp/resource_coordinator_interface.cc

Issue 2798713002: Global Resource Coordinator: Basic service internals (Closed)
Patch Set: Review fixes Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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/resource_coordinator/public/cpp/resource_coordinator_interfac e.h"
6
7 #include "base/atomic_sequence_num.h"
8 #include "base/process/process_handle.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "mojo/public/cpp/bindings/binding.h"
11 #include "services/resource_coordinator/public/interfaces/coordination_unit_prov ider.mojom.h"
12 #include "services/resource_coordinator/public/interfaces/service_constants.mojo m.h"
13 #include "services/service_manager/public/cpp/connector.h"
14
15 namespace {
16
17 void OnConnectionError() {
18 CHECK(false);
19 }
20
21 base::StaticAtomicSequenceNumber g_next_id;
22
23 } // namespace
24
25 namespace resource_coordinator {
26
27 ResourceCoordinatorInterface::ResourceCoordinatorInterface(
28 service_manager::Connector* connector,
29 const CoordinationUnitType& type)
30 : weak_ptr_factory_(this) {
31 std::string id = base::IntToString(base::GetCurrentProcId()) + "." +
Primiano Tucci (use gerrit) 2017/04/11 18:04:52 isn't GetCurrentProcId going to be always 1 in the
oystein (OOO til 10th of July) 2017/04/12 19:15:31 Yep that makes perfect sense; moved to the service
32 base::IntToString(g_next_id.GetNext());
33 ConnectToService(connector, type, id);
34 }
35
36 ResourceCoordinatorInterface::~ResourceCoordinatorInterface() = default;
37
38 void ResourceCoordinatorInterface::ConnectToService(
39 service_manager::Connector* connector,
40 const CoordinationUnitType& type,
41 const std::string& id) {
42 DCHECK(thread_checker_.CalledOnValidThread());
43 DCHECK(connector);
44 mojom::CoordinationUnitProviderPtr provider;
45 connector->BindInterface(mojom::kServiceName, mojo::MakeRequest(&provider));
46
47 CoordinationUnitID new_cu_id(type, id);
48
49 provider->CreateCoordinationUnit(mojo::MakeRequest(&service_), new_cu_id);
50
51 service_.set_connection_error_handler(base::Bind(&OnConnectionError));
52 }
53
54 void ResourceCoordinatorInterface::SendEvent(
55 const resource_coordinator::EventType& event_type) {
56 DCHECK(thread_checker_.CalledOnValidThread());
57 mojom::EventPtr event = mojom::Event::New();
58 event->type = event_type;
59
60 service_->SendEvent(std::move(event));
61 }
62
63 void ResourceCoordinatorInterface::AddChild(
64 const ResourceCoordinatorInterface& child) {
65 DCHECK(thread_checker_.CalledOnValidThread());
66 DCHECK(service_);
67 // We could keep the ID around ourselves, but this hop ensures that the child
68 // has been created on the service-side.
69 child.service()->GetID(base::Bind(&ResourceCoordinatorInterface::AddChildByID,
70 weak_ptr_factory_.GetWeakPtr()));
71 }
72
73 void ResourceCoordinatorInterface::AddChildByID(
74 const CoordinationUnitID& child_id) {
75 DCHECK(thread_checker_.CalledOnValidThread());
76 service_->AddChild(child_id);
77 }
78
79 } // namespace resource_coordinator
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698