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

Side by Side Diff: mojo/service_manager/background_service_loader.cc

Issue 437493002: mojo: allow BackgroundServiceLoader-loaded apps to Quit themselves. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 4 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 | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "mojo/service_manager/background_service_loader.h"
6
7 #include "base/bind.h"
8 #include "mojo/service_manager/service_manager.h"
9
10 namespace mojo {
11
12 class BackgroundServiceLoader::BackgroundLoader {
13 public:
14 explicit BackgroundLoader(ServiceLoader* loader) : loader_(loader) {}
15 ~BackgroundLoader() {}
16
17 void LoadService(ServiceManager* manager,
18 const GURL& url,
19 ScopedMessagePipeHandle shell_handle) {
20 loader_->LoadService(manager, url, shell_handle.Pass());
21 }
22
23 void OnServiceError(ServiceManager* manager, const GURL& url) {
24 loader_->OnServiceError(manager, url);
25 }
26
27 private:
28 base::MessageLoop::Type message_loop_type_;
29 ServiceLoader* loader_; // Owned by BackgroundServiceLoader
30
31 DISALLOW_COPY_AND_ASSIGN(BackgroundLoader);
32 };
33
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) {
42 }
43
44 BackgroundServiceLoader::~BackgroundServiceLoader() {
45 if (thread_.IsRunning()) {
46 thread_.message_loop()->PostTask(
47 FROM_HERE,
48 base::Bind(&BackgroundServiceLoader::ShutdownOnBackgroundThread,
49 base::Unretained(this)));
50 }
51 thread_.Stop();
52 }
53
54 void BackgroundServiceLoader::LoadService(
55 ServiceManager* manager,
56 const GURL& url,
57 ScopedMessagePipeHandle shell_handle) {
58 const int kDefaultStackSize = 0;
59 if (!thread_.IsRunning()) {
60 thread_.StartWithOptions(
61 base::Thread::Options(message_loop_type_, kDefaultStackSize));
62 }
63 thread_.message_loop()->PostTask(
64 FROM_HERE,
65 base::Bind(&BackgroundServiceLoader::LoadServiceOnBackgroundThread,
66 base::Unretained(this), manager, url,
67 base::Owned(
68 new ScopedMessagePipeHandle(shell_handle.Pass()))));
69 }
70
71 void BackgroundServiceLoader::OnServiceError(ServiceManager* manager,
72 const GURL& url) {
73 if (!thread_.IsRunning())
74 thread_.Start();
75 thread_.message_loop()->PostTask(
76 FROM_HERE,
77 base::Bind(&BackgroundServiceLoader::OnServiceErrorOnBackgroundThread,
78 base::Unretained(this), manager, url));
79 }
80
81 void BackgroundServiceLoader::LoadServiceOnBackgroundThread(
82 ServiceManager* manager,
83 const GURL& url,
84 ScopedMessagePipeHandle* shell_handle) {
85 if (!background_loader_)
86 background_loader_ = new BackgroundLoader(loader_.get());
87 background_loader_->LoadService(
88 manager, url, shell_handle->Pass());
89 }
90
91 void BackgroundServiceLoader::OnServiceErrorOnBackgroundThread(
92 ServiceManager* manager,
93 const GURL& url) {
94 if (!background_loader_)
95 background_loader_ = new BackgroundLoader(loader_.get());
96 background_loader_->OnServiceError(manager, url);
97 }
98
99 void BackgroundServiceLoader::ShutdownOnBackgroundThread() {
100 delete background_loader_;
101 // Destroy |loader_| on the thread it's actually used on.
102 loader_.reset();
103 }
104
105 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/service_manager/background_service_loader.h ('k') | mojo/service_manager/background_shell_service_loader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698