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
|
// 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/service_manager/background_service_loader.h"
#include "base/bind.h"
#include "mojo/service_manager/service_manager.h"
namespace mojo {
class BackgroundServiceLoader::BackgroundLoader {
public:
explicit BackgroundLoader(ServiceLoader* loader) : loader_(loader) {}
~BackgroundLoader() {}
void LoadService(ServiceManager* manager,
const GURL& url,
ScopedShellHandle shell_handle) {
loader_->LoadService(manager, url, shell_handle.Pass());
}
void OnServiceError(ServiceManager* manager, const GURL& url) {
loader_->OnServiceError(manager, url);
}
private:
ServiceLoader* loader_; // Owned by BackgroundServiceLoader
DISALLOW_COPY_AND_ASSIGN(BackgroundLoader);
};
BackgroundServiceLoader::BackgroundServiceLoader(
scoped_ptr<ServiceLoader> real_loader,
const char* thread_name)
: loader_(real_loader.Pass()),
thread_(thread_name),
background_loader_(NULL) {
}
BackgroundServiceLoader::~BackgroundServiceLoader() {
if (thread_.IsRunning()) {
thread_.message_loop()->PostTask(
FROM_HERE,
base::Bind(&BackgroundServiceLoader::ShutdownOnBackgroundThread,
base::Unretained(this)));
}
thread_.Stop();
}
void BackgroundServiceLoader::LoadService(ServiceManager* manager,
const GURL& url,
ScopedShellHandle service_handle) {
if (!thread_.IsRunning())
thread_.Start();
thread_.message_loop()->PostTask(
FROM_HERE,
base::Bind(&BackgroundServiceLoader::LoadServiceOnBackgroundThread,
base::Unretained(this), manager, url,
base::Owned(new ScopedShellHandle(service_handle.Pass()))));
}
void BackgroundServiceLoader::OnServiceError(ServiceManager* manager,
const GURL& url) {
if (!thread_.IsRunning())
thread_.Start();
thread_.message_loop()->PostTask(
FROM_HERE,
base::Bind(&BackgroundServiceLoader::OnServiceErrorOnBackgroundThread,
base::Unretained(this), manager, url));
}
void BackgroundServiceLoader::LoadServiceOnBackgroundThread(
ServiceManager* manager,
const GURL& url,
ScopedShellHandle* shell_handle) {
if (!background_loader_)
background_loader_ = new BackgroundLoader(loader_.get());
background_loader_->LoadService(manager, url, shell_handle->Pass());
}
void BackgroundServiceLoader::OnServiceErrorOnBackgroundThread(
ServiceManager* manager,
const GURL& url) {
if (!background_loader_)
background_loader_ = new BackgroundLoader(loader_.get());
background_loader_->OnServiceError(manager, url);
}
void BackgroundServiceLoader::ShutdownOnBackgroundThread() {
delete background_loader_;
}
} // namespace mojo
|