OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_SERVICE_MANAGER_BACKGROUND_SERVICE_LOADER_H_ | 5 #ifndef MOJO_SERVICE_MANAGER_BACKGROUND_SHELL_SERVICE_LOADER_H_ |
6 #define MOJO_SERVICE_MANAGER_BACKGROUND_SERVICE_LOADER_H_ | 6 #define MOJO_SERVICE_MANAGER_BACKGROUND_SHELL_SERVICE_LOADER_H_ |
7 | 7 |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/threading/thread.h" | 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "base/threading/simple_thread.h" |
11 #include "mojo/service_manager/service_loader.h" | 12 #include "mojo/service_manager/service_loader.h" |
12 | 13 |
13 namespace mojo { | 14 namespace mojo { |
14 | 15 |
15 class ServiceManager; | 16 // TODO(tim): Eventually this should be Android-only to support services |
16 | 17 // that we need to bundle with the shell (such as NetworkService). Perhaps |
17 // ServiceLoader implementation that creates a background thread and issues load | 18 // we should move it to shell/ as well. |
18 // requests there. | 19 class MOJO_SERVICE_MANAGER_EXPORT BackgroundShellServiceLoader : |
19 class MOJO_SERVICE_MANAGER_EXPORT BackgroundServiceLoader | 20 public ServiceLoader, |
20 : public ServiceLoader { | 21 public base::DelegateSimpleThread::Delegate { |
21 public: | 22 public: |
22 BackgroundServiceLoader(scoped_ptr<ServiceLoader> real_loader, | 23 BackgroundShellServiceLoader(scoped_ptr<ServiceLoader> real_loader, |
23 const char* thread_name, | 24 const std::string& thread_name, |
24 base::MessageLoop::Type message_loop_type); | 25 base::MessageLoop::Type message_loop_type); |
25 virtual ~BackgroundServiceLoader(); | 26 virtual ~BackgroundShellServiceLoader(); |
26 | 27 |
27 // ServiceLoader overrides: | 28 // ServiceLoader overrides: |
28 virtual void LoadService(ServiceManager* manager, | 29 virtual void LoadService(ServiceManager* manager, |
29 const GURL& url, | 30 const GURL& url, |
30 ScopedMessagePipeHandle shell_handle) OVERRIDE; | 31 ScopedMessagePipeHandle shell_handle) OVERRIDE; |
31 virtual void OnServiceError(ServiceManager* manager, | 32 virtual void OnServiceError(ServiceManager* manager, |
32 const GURL& url) OVERRIDE; | 33 const GURL& url) OVERRIDE; |
33 | 34 |
| 35 // Post a task to quit the MessageLoop we are running for the app when we |
| 36 // are destroyed. Most apps should quit themselves and not need this. |
| 37 // TODO(tim): Remove this method once applications are smart enough |
| 38 // to quit themselves. Bug 394477. |
| 39 void set_quit_on_shutdown() { quit_on_shutdown_ = true; } |
| 40 |
34 private: | 41 private: |
35 class BackgroundLoader; | 42 class BackgroundLoader; |
36 | 43 |
| 44 // |base::DelegateSimpleThread::Delegate| method: |
| 45 virtual void Run() OVERRIDE; |
| 46 |
37 // These functions are exected on the background thread. They call through | 47 // These functions are exected on the background thread. They call through |
38 // to |background_loader_| to do the actual loading. | 48 // to |background_loader_| to do the actual loading. |
39 // TODO: having this code take a |manager| is fragile (as ServiceManager isn't | 49 // TODO: having this code take a |manager| is fragile (as ServiceManager isn't |
40 // thread safe). | 50 // thread safe). |
41 void LoadServiceOnBackgroundThread( | 51 void LoadServiceOnBackgroundThread( |
42 ServiceManager* manager, | 52 ServiceManager* manager, |
43 const GURL& url, | 53 const GURL& url, |
44 ScopedMessagePipeHandle* shell_handle); | 54 ScopedMessagePipeHandle* shell_handle); |
45 void OnServiceErrorOnBackgroundThread(ServiceManager* manager, | 55 void OnServiceErrorOnBackgroundThread(ServiceManager* manager, |
46 const GURL& url); | 56 const GURL& url); |
47 void ShutdownOnBackgroundThread(); | 57 bool quit_on_shutdown_; |
| 58 scoped_ptr<ServiceLoader> loader_; |
48 | 59 |
49 scoped_ptr<ServiceLoader> loader_; | 60 const base::MessageLoop::Type message_loop_type_; |
50 base::Thread thread_; | 61 const std::string thread_name_; |
51 base::MessageLoop::Type message_loop_type_; | 62 |
| 63 // Created on |thread_| during construction of |this|. Protected against |
| 64 // uninitialized use by |message_loop_created_|, and protected against |
| 65 // use-after-free by holding a reference to the thread-safe object. Note |
| 66 // that holding a reference won't hold |thread_| from exiting. |
| 67 scoped_refptr<base::TaskRunner> task_runner_; |
| 68 base::WaitableEvent message_loop_created_; |
| 69 |
| 70 // Lives on |thread_|. |
| 71 base::Closure quit_closure_; |
| 72 |
| 73 scoped_ptr<base::DelegateSimpleThread> thread_; |
52 | 74 |
53 // Lives on |thread_|. Trivial interface that calls through to |loader_|. | 75 // Lives on |thread_|. Trivial interface that calls through to |loader_|. |
54 BackgroundLoader* background_loader_; | 76 BackgroundLoader* background_loader_; |
55 | 77 |
56 DISALLOW_COPY_AND_ASSIGN(BackgroundServiceLoader); | 78 DISALLOW_COPY_AND_ASSIGN(BackgroundShellServiceLoader); |
57 }; | 79 }; |
58 | 80 |
59 } // namespace mojo | 81 } // namespace mojo |
60 | 82 |
61 #endif // MOJO_SERVICE_MANAGER_BACKGROUND_SERVICE_LOADER_H_ | 83 #endif // MOJO_SERVICE_MANAGER_BACKGROUND_SHELL_SERVICE_LOADER_H_ |
OLD | NEW |