summaryrefslogtreecommitdiffstats
path: root/components/profile_service/profile_app.cc
blob: a58071163cb47e5c64029ae5f00dda1f43712d29 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2016 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 "components/profile_service/profile_app.h"

#include "base/bind.h"
#include "base/memory/weak_ptr.h"
#include "components/filesystem/lock_table.h"
#include "components/leveldb/leveldb_service_impl.h"
#include "components/profile_service/profile_service_impl.h"
#include "components/profile_service/user_id_map.h"
#include "mojo/public/cpp/bindings/callback.h"
#include "mojo/shell/public/cpp/connection.h"

namespace profile {

class ProfileApp::ProfileServiceObjects
    : public base::SupportsWeakPtr<ProfileServiceObjects> {
 public:
  // Created on the main thread.
  ProfileServiceObjects(base::FilePath profile_data_dir)
      : profile_data_dir_(profile_data_dir) {}

  // Destroyed on the |profile_service_runner_|.
  ~ProfileServiceObjects() {}

  // Called on the |profile_service_runner_|.
  void OnProfileServiceRequest(mojo::Connection* connection,
                               ProfileServiceRequest request) {
    if (!lock_table_)
      lock_table_ = new filesystem::LockTable;
    profile_service_bindings_.AddBinding(
        new ProfileServiceImpl(profile_data_dir_, lock_table_),
        std::move(request));
  }

 private:
  mojo::BindingSet<ProfileService> profile_service_bindings_;
  scoped_refptr<filesystem::LockTable> lock_table_;
  base::FilePath profile_data_dir_;

  DISALLOW_COPY_AND_ASSIGN(ProfileServiceObjects);
};

class ProfileApp::LevelDBServiceObjects
    : public base::SupportsWeakPtr<LevelDBServiceObjects> {
 public:
  // Created on the main thread.
  LevelDBServiceObjects() {}

  // Destroyed on the |leveldb_service_runner_|.
  ~LevelDBServiceObjects() {}

  // Called on the |leveldb_service_runner_|.
  void OnLevelDBServiceRequest(mojo::Connection* connection,
                               leveldb::LevelDBServiceRequest request) {
    if (!leveldb_service_)
      leveldb_service_.reset(new leveldb::LevelDBServiceImpl);
    leveldb_bindings_.AddBinding(leveldb_service_.get(), std::move(request));
  }

 private:
  // Variables that are only accessible on the |leveldb_service_runner_| thread.
  scoped_ptr<leveldb::LevelDBService> leveldb_service_;
  mojo::BindingSet<leveldb::LevelDBService> leveldb_bindings_;

  DISALLOW_COPY_AND_ASSIGN(LevelDBServiceObjects);
};

scoped_ptr<mojo::ShellClient> CreateProfileApp(
    scoped_refptr<base::SingleThreadTaskRunner> profile_service_runner,
    scoped_refptr<base::SingleThreadTaskRunner> leveldb_service_runner) {
  return make_scoped_ptr(new ProfileApp(
      std::move(profile_service_runner),
      std::move(leveldb_service_runner)));
}

ProfileApp::ProfileApp(
    scoped_refptr<base::SingleThreadTaskRunner> profile_service_runner,
    scoped_refptr<base::SingleThreadTaskRunner> leveldb_service_runner)
    : profile_service_runner_(std::move(profile_service_runner)),
      leveldb_service_runner_(std::move(leveldb_service_runner)) {}

ProfileApp::~ProfileApp() {
  profile_service_runner_->DeleteSoon(FROM_HERE, profile_objects_.release());
  leveldb_service_runner_->DeleteSoon(FROM_HERE, leveldb_objects_.release());
}

void ProfileApp::Initialize(mojo::Connector* connector,
                            const mojo::Identity& identity,
                            uint32_t id) {
  tracing_.Initialize(connector, identity.name());
  profile_objects_.reset(new ProfileApp::ProfileServiceObjects(
      GetProfileDirForUserID(identity.user_id())));
  leveldb_objects_.reset(new ProfileApp::LevelDBServiceObjects);
}

bool ProfileApp::AcceptConnection(mojo::Connection* connection) {
  connection->AddInterface<leveldb::LevelDBService>(this);
  connection->AddInterface<ProfileService>(this);
  return true;
}

void ProfileApp::Create(mojo::Connection* connection,
                        ProfileServiceRequest request) {
  profile_service_runner_->PostTask(
      FROM_HERE,
      base::Bind(&ProfileApp::ProfileServiceObjects::OnProfileServiceRequest,
                 profile_objects_->AsWeakPtr(), connection,
                 base::Passed(&request)));
}

void ProfileApp::Create(mojo::Connection* connection,
                        leveldb::LevelDBServiceRequest request) {
  leveldb_service_runner_->PostTask(
      FROM_HERE,
      base::Bind(&ProfileApp::LevelDBServiceObjects::OnLevelDBServiceRequest,
                 leveldb_objects_->AsWeakPtr(), connection,
                 base::Passed(&request)));
}

}  // namespace profile