OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "mojo/shell/context.h" | 5 #include "mojo/shell/context.h" |
6 | 6 |
| 7 #include <vector> |
| 8 |
7 #include "build/build_config.h" | 9 #include "build/build_config.h" |
8 #include "base/command_line.h" | 10 #include "base/command_line.h" |
9 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
10 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 13 #include "base/strings/string_split.h" |
11 #include "mojo/embedder/embedder.h" | 14 #include "mojo/embedder/embedder.h" |
12 #include "mojo/gles2/gles2_support_impl.h" | 15 #include "mojo/gles2/gles2_support_impl.h" |
13 #include "mojo/public/cpp/application/application_impl.h" | 16 #include "mojo/public/cpp/application/application_impl.h" |
14 #include "mojo/service_manager/background_shell_service_loader.h" | 17 #include "mojo/service_manager/background_shell_service_loader.h" |
15 #include "mojo/service_manager/service_loader.h" | 18 #include "mojo/service_manager/service_loader.h" |
16 #include "mojo/service_manager/service_manager.h" | 19 #include "mojo/service_manager/service_manager.h" |
17 #include "mojo/services/native_viewport/native_viewport_service.h" | 20 #include "mojo/services/native_viewport/native_viewport_service.h" |
18 #include "mojo/shell/dynamic_service_loader.h" | 21 #include "mojo/shell/dynamic_service_loader.h" |
19 #include "mojo/shell/in_process_dynamic_service_runner.h" | 22 #include "mojo/shell/in_process_dynamic_service_runner.h" |
20 #include "mojo/shell/out_of_process_dynamic_service_runner.h" | 23 #include "mojo/shell/out_of_process_dynamic_service_runner.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 57 |
55 ~Setup() { | 58 ~Setup() { |
56 } | 59 } |
57 | 60 |
58 private: | 61 private: |
59 DISALLOW_COPY_AND_ASSIGN(Setup); | 62 DISALLOW_COPY_AND_ASSIGN(Setup); |
60 }; | 63 }; |
61 | 64 |
62 static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER; | 65 static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER; |
63 | 66 |
| 67 void InitContentHandlers(DynamicServiceLoader* loader, |
| 68 base::CommandLine* command_line) { |
| 69 std::string handlers_spec = command_line->GetSwitchValueASCII( |
| 70 switches::kContentHandlers); |
| 71 if (handlers_spec.empty()) |
| 72 return; |
| 73 |
| 74 std::vector<std::string> parts; |
| 75 base::SplitString(handlers_spec, ',', &parts); |
| 76 if (parts.size() % 2 != 0) { |
| 77 LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers |
| 78 << ": must be a comma-separated list of mimetype/url pairs."; |
| 79 return; |
| 80 } |
| 81 |
| 82 for (size_t i = 0; i < parts.size(); i += 2) { |
| 83 GURL url(parts[i + 1]); |
| 84 if (!url.is_valid()) { |
| 85 LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers |
| 86 << ": '" << parts[i + 1] << "' is not a valid URL."; |
| 87 return; |
| 88 } |
| 89 loader->RegisterContentHandler(parts[i], url); |
| 90 } |
| 91 } |
| 92 |
64 } // namespace | 93 } // namespace |
65 | 94 |
66 class Context::NativeViewportServiceLoader : public ServiceLoader { | 95 class Context::NativeViewportServiceLoader : public ServiceLoader { |
67 public: | 96 public: |
68 NativeViewportServiceLoader() {} | 97 NativeViewportServiceLoader() {} |
69 virtual ~NativeViewportServiceLoader() {} | 98 virtual ~NativeViewportServiceLoader() {} |
70 | 99 |
71 private: | 100 private: |
72 virtual void LoadService(ServiceManager* manager, | 101 virtual void Load(ServiceManager* manager, |
73 const GURL& url, | 102 const GURL& url, |
74 ScopedMessagePipeHandle shell_handle) OVERRIDE { | 103 scoped_refptr<LoadCallbacks> callbacks) OVERRIDE { |
75 app_.reset(services::CreateNativeViewportService(shell_handle.Pass())); | 104 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); |
| 105 if (shell_handle.is_valid()) |
| 106 app_.reset(services::CreateNativeViewportService(shell_handle.Pass())); |
76 } | 107 } |
77 | 108 |
78 virtual void OnServiceError(ServiceManager* manager, | 109 virtual void OnServiceError(ServiceManager* manager, |
79 const GURL& url) OVERRIDE { | 110 const GURL& url) OVERRIDE { |
80 } | 111 } |
81 | 112 |
82 scoped_ptr<ApplicationImpl> app_; | 113 scoped_ptr<ApplicationImpl> app_; |
83 DISALLOW_COPY_AND_ASSIGN(NativeViewportServiceLoader); | 114 DISALLOW_COPY_AND_ASSIGN(NativeViewportServiceLoader); |
84 }; | 115 }; |
85 | 116 |
86 Context::Context() { | 117 Context::Context() { |
87 DCHECK(!base::MessageLoop::current()); | 118 DCHECK(!base::MessageLoop::current()); |
88 } | 119 } |
89 | 120 |
90 void Context::Init() { | 121 void Context::Init() { |
91 setup.Get(); | 122 setup.Get(); |
92 task_runners_.reset( | 123 task_runners_.reset( |
93 new TaskRunners(base::MessageLoop::current()->message_loop_proxy())); | 124 new TaskRunners(base::MessageLoop::current()->message_loop_proxy())); |
94 | 125 |
95 for (size_t i = 0; i < arraysize(kLocalMojoURLs); ++i) | 126 for (size_t i = 0; i < arraysize(kLocalMojoURLs); ++i) |
96 mojo_url_resolver_.AddLocalFileMapping(GURL(kLocalMojoURLs[i])); | 127 mojo_url_resolver_.AddLocalFileMapping(GURL(kLocalMojoURLs[i])); |
97 | 128 |
98 base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | 129 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
99 scoped_ptr<DynamicServiceRunnerFactory> runner_factory; | 130 scoped_ptr<DynamicServiceRunnerFactory> runner_factory; |
100 if (cmdline->HasSwitch(switches::kEnableMultiprocess)) | 131 if (command_line->HasSwitch(switches::kEnableMultiprocess)) |
101 runner_factory.reset(new OutOfProcessDynamicServiceRunnerFactory()); | 132 runner_factory.reset(new OutOfProcessDynamicServiceRunnerFactory()); |
102 else | 133 else |
103 runner_factory.reset(new InProcessDynamicServiceRunnerFactory()); | 134 runner_factory.reset(new InProcessDynamicServiceRunnerFactory()); |
104 | 135 |
| 136 DynamicServiceLoader* dynamic_service_loader = |
| 137 new DynamicServiceLoader(this, runner_factory.Pass()); |
| 138 InitContentHandlers(dynamic_service_loader, command_line); |
105 service_manager_.set_default_loader( | 139 service_manager_.set_default_loader( |
106 scoped_ptr<ServiceLoader>( | 140 scoped_ptr<ServiceLoader>(dynamic_service_loader)); |
107 new DynamicServiceLoader(this, runner_factory.Pass()))); | 141 |
108 // The native viewport service synchronously waits for certain messages. If we | 142 // The native viewport service synchronously waits for certain messages. If we |
109 // don't run it on its own thread we can easily deadlock. Long term native | 143 // don't run it on its own thread we can easily deadlock. Long term native |
110 // viewport should run its own process so that this isn't an issue. | 144 // viewport should run its own process so that this isn't an issue. |
111 #if defined(OS_ANDROID) | 145 #if defined(OS_ANDROID) |
112 service_manager_.SetLoaderForURL( | 146 service_manager_.SetLoaderForURL( |
113 scoped_ptr<ServiceLoader>( | 147 scoped_ptr<ServiceLoader>( |
114 new UIServiceLoader( | 148 new UIServiceLoader( |
115 scoped_ptr<ServiceLoader>(new NativeViewportServiceLoader()), | 149 scoped_ptr<ServiceLoader>(new NativeViewportServiceLoader()), |
116 this)), | 150 this)), |
117 GURL("mojo:mojo_native_viewport_service")); | 151 GURL("mojo:mojo_native_viewport_service")); |
(...skipping 15 matching lines...) Expand all Loading... |
133 scoped_ptr<ServiceLoader>(new ViewManagerLoader()), | 167 scoped_ptr<ServiceLoader>(new ViewManagerLoader()), |
134 GURL("mojo:mojo_view_manager")); | 168 GURL("mojo:mojo_view_manager")); |
135 #endif | 169 #endif |
136 | 170 |
137 #if defined(OS_LINUX) | 171 #if defined(OS_LINUX) |
138 service_manager_.SetLoaderForScheme( | 172 service_manager_.SetLoaderForScheme( |
139 scoped_ptr<ServiceLoader>(new DBusServiceLoader(this)), | 173 scoped_ptr<ServiceLoader>(new DBusServiceLoader(this)), |
140 "dbus"); | 174 "dbus"); |
141 #endif // defined(OS_LINUX) | 175 #endif // defined(OS_LINUX) |
142 | 176 |
143 if (cmdline->HasSwitch(switches::kSpy)) { | 177 if (command_line->HasSwitch(switches::kSpy)) { |
144 spy_.reset(new mojo::Spy(&service_manager_, | 178 spy_.reset(new mojo::Spy( |
145 cmdline->GetSwitchValueASCII(switches::kSpy))); | 179 &service_manager_, command_line->GetSwitchValueASCII(switches::kSpy))); |
146 } | 180 } |
147 | 181 |
148 #if defined(OS_ANDROID) | 182 #if defined(OS_ANDROID) |
149 // On android, the network service is bundled with the shell because the | 183 // On android, the network service is bundled with the shell because the |
150 // network stack depends on the android runtime. | 184 // network stack depends on the android runtime. |
151 { | 185 { |
152 scoped_ptr<BackgroundShellServiceLoader> loader( | 186 scoped_ptr<BackgroundShellServiceLoader> loader( |
153 new BackgroundShellServiceLoader( | 187 new BackgroundShellServiceLoader( |
154 scoped_ptr<ServiceLoader>(new NetworkServiceLoader()), | 188 scoped_ptr<ServiceLoader>(new NetworkServiceLoader()), |
155 "network_service", | 189 "network_service", |
156 base::MessageLoop::TYPE_IO)); | 190 base::MessageLoop::TYPE_IO)); |
157 service_manager_.SetLoaderForURL(loader.PassAs<ServiceLoader>(), | 191 service_manager_.SetLoaderForURL(loader.PassAs<ServiceLoader>(), |
158 GURL("mojo:mojo_network_service")); | 192 GURL("mojo:mojo_network_service")); |
159 } | 193 } |
160 #endif | 194 #endif |
161 } | 195 } |
162 | 196 |
163 Context::~Context() { | 197 Context::~Context() { |
164 DCHECK(!base::MessageLoop::current()); | 198 DCHECK(!base::MessageLoop::current()); |
165 } | 199 } |
166 | 200 |
167 } // namespace shell | 201 } // namespace shell |
168 } // namespace mojo | 202 } // namespace mojo |
OLD | NEW |