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; } | |
34 private: | 40 private: |
sky
2014/07/31 20:59:33
nit: newline between 39 and 40.
tim (not reviewing)
2014/07/31 22:17:28
Done.
| |
35 class BackgroundLoader; | 41 class BackgroundLoader; |
36 | 42 |
43 // |base::DelegateSimpleThread::Delegate| method: | |
44 virtual void Run() OVERRIDE; | |
45 | |
37 // These functions are exected on the background thread. They call through | 46 // These functions are exected on the background thread. They call through |
38 // to |background_loader_| to do the actual loading. | 47 // to |background_loader_| to do the actual loading. |
39 // TODO: having this code take a |manager| is fragile (as ServiceManager isn't | 48 // TODO: having this code take a |manager| is fragile (as ServiceManager isn't |
40 // thread safe). | 49 // thread safe). |
41 void LoadServiceOnBackgroundThread( | 50 void LoadServiceOnBackgroundThread( |
42 ServiceManager* manager, | 51 ServiceManager* manager, |
43 const GURL& url, | 52 const GURL& url, |
44 ScopedMessagePipeHandle* shell_handle); | 53 ScopedMessagePipeHandle* shell_handle); |
45 void OnServiceErrorOnBackgroundThread(ServiceManager* manager, | 54 void OnServiceErrorOnBackgroundThread(ServiceManager* manager, |
46 const GURL& url); | 55 const GURL& url); |
47 void ShutdownOnBackgroundThread(); | 56 bool quit_on_shutdown_; |
57 bool loaded_service_; | |
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 |
51 base::MessageLoop::Type message_loop_type_; | 62 // Created on |thread_| during construction of |this|. Protected against |
63 // uninitialized use by |message_loop_created_|, and protected against | |
64 // use-after-free by holding a reference to the thread-safe object. Note | |
65 // that holding a reference won't hold |thread_| from exiting. | |
66 scoped_refptr<base::TaskRunner> task_runner_; | |
67 base::WaitableEvent message_loop_created_; | |
68 | |
69 base::DelegateSimpleThread thread_; | |
52 | 70 |
53 // Lives on |thread_|. Trivial interface that calls through to |loader_|. | 71 // Lives on |thread_|. Trivial interface that calls through to |loader_|. |
54 BackgroundLoader* background_loader_; | 72 BackgroundLoader* background_loader_; |
55 | 73 |
56 DISALLOW_COPY_AND_ASSIGN(BackgroundServiceLoader); | 74 DISALLOW_COPY_AND_ASSIGN(BackgroundShellServiceLoader); |
57 }; | 75 }; |
58 | 76 |
59 } // namespace mojo | 77 } // namespace mojo |
60 | 78 |
61 #endif // MOJO_SERVICE_MANAGER_BACKGROUND_SERVICE_LOADER_H_ | 79 #endif // MOJO_SERVICE_MANAGER_BACKGROUND_SHELL_SERVICE_LOADER_H_ |
OLD | NEW |