MSE: Remap single text track number changes in frame processor
[chromium-blink-merge.git] / mojo / service_manager / background_service_loader.cc
blob96a2c430a8980b43b7769be2cc83118b1e325e63
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.
5 #include "mojo/service_manager/background_service_loader.h"
7 #include "base/bind.h"
8 #include "mojo/service_manager/service_manager.h"
10 namespace mojo {
12 class BackgroundServiceLoader::BackgroundLoader {
13 public:
14 explicit BackgroundLoader(ServiceLoader* loader) : loader_(loader) {}
15 ~BackgroundLoader() {}
17 void LoadService(ServiceManager* manager,
18 const GURL& url,
19 ScopedMessagePipeHandle service_provider_handle) {
20 loader_->LoadService(manager, url, service_provider_handle.Pass());
23 void OnServiceError(ServiceManager* manager, const GURL& url) {
24 loader_->OnServiceError(manager, url);
27 private:
28 base::MessageLoop::Type message_loop_type_;
29 ServiceLoader* loader_; // Owned by BackgroundServiceLoader
31 DISALLOW_COPY_AND_ASSIGN(BackgroundLoader);
34 BackgroundServiceLoader::BackgroundServiceLoader(
35 scoped_ptr<ServiceLoader> real_loader,
36 const char* thread_name,
37 base::MessageLoop::Type message_loop_type)
38 : loader_(real_loader.Pass()),
39 thread_(thread_name),
40 message_loop_type_(message_loop_type),
41 background_loader_(NULL) {
44 BackgroundServiceLoader::~BackgroundServiceLoader() {
45 if (thread_.IsRunning()) {
46 thread_.message_loop()->PostTask(
47 FROM_HERE,
48 base::Bind(&BackgroundServiceLoader::ShutdownOnBackgroundThread,
49 base::Unretained(this)));
51 thread_.Stop();
54 void BackgroundServiceLoader::LoadService(
55 ServiceManager* manager,
56 const GURL& url,
57 ScopedMessagePipeHandle service_handle) {
58 const int kDefaultStackSize = 0;
59 if (!thread_.IsRunning())
60 thread_.StartWithOptions(
61 base::Thread::Options(message_loop_type_, kDefaultStackSize));
62 thread_.message_loop()->PostTask(
63 FROM_HERE,
64 base::Bind(&BackgroundServiceLoader::LoadServiceOnBackgroundThread,
65 base::Unretained(this), manager, url,
66 base::Owned(
67 new ScopedMessagePipeHandle(service_handle.Pass()))));
70 void BackgroundServiceLoader::OnServiceError(ServiceManager* manager,
71 const GURL& url) {
72 if (!thread_.IsRunning())
73 thread_.Start();
74 thread_.message_loop()->PostTask(
75 FROM_HERE,
76 base::Bind(&BackgroundServiceLoader::OnServiceErrorOnBackgroundThread,
77 base::Unretained(this), manager, url));
80 void BackgroundServiceLoader::LoadServiceOnBackgroundThread(
81 ServiceManager* manager,
82 const GURL& url,
83 ScopedMessagePipeHandle* service_provider_handle) {
84 if (!background_loader_)
85 background_loader_ = new BackgroundLoader(loader_.get());
86 background_loader_->LoadService(
87 manager, url, service_provider_handle->Pass());
90 void BackgroundServiceLoader::OnServiceErrorOnBackgroundThread(
91 ServiceManager* manager,
92 const GURL& url) {
93 if (!background_loader_)
94 background_loader_ = new BackgroundLoader(loader_.get());
95 background_loader_->OnServiceError(manager, url);
98 void BackgroundServiceLoader::ShutdownOnBackgroundThread() {
99 delete background_loader_;
100 // Destroy |loader_| on the thread it's actually used on.
101 loader_.reset();
104 } // namespace mojo