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 #include "base/at_exit.h" | 5 #include "base/at_exit.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
11 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
12 #include "mojo/public/cpp/application/application_impl.h" | 12 #include "mojo/public/cpp/application/application_impl.h" |
| 13 #include "mojo/public/cpp/application/context_interface_provider.h" |
13 #include "mojo/public/cpp/bindings/interface_ptr.h" | 14 #include "mojo/public/cpp/bindings/interface_ptr.h" |
14 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" | 15 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" |
15 #include "mojo/services/network/network_context.h" | 16 #include "mojo/services/network/network_context.h" |
16 #include "mojo/services/network/network_service_impl.h" | 17 #include "mojo/services/network/network_service_impl.h" |
17 #include "mojo/services/public/interfaces/profile/profile_service.mojom.h" | 18 #include "mojo/services/public/interfaces/profile/profile_service.mojom.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 void OnPathReceived(base::FilePath* path, const mojo::String& path_as_string) { | 22 void OnPathReceived(base::FilePath* path, const mojo::String& path_as_string) { |
22 DCHECK(!path_as_string.is_null()); | 23 DCHECK(!path_as_string.is_null()); |
23 #if defined(OS_POSIX) | 24 #if defined(OS_POSIX) |
24 *path = base::FilePath(path_as_string); | 25 *path = base::FilePath(path_as_string); |
25 #elif defined(OS_WIN) | 26 #elif defined(OS_WIN) |
26 *path = base::FilePath::FromUTF8Unsafe(path_as_string); | 27 *path = base::FilePath::FromUTF8Unsafe(path_as_string); |
27 #else | 28 #else |
28 #error Not implemented | 29 #error Not implemented |
29 #endif | 30 #endif |
30 } | 31 } |
31 | 32 |
32 } // namespace | 33 } // namespace |
33 | 34 |
34 class Delegate : public mojo::ApplicationDelegate { | 35 class Delegate : public mojo::ApplicationDelegate { |
| 36 typedef mojo::ContextInterfaceProvider<mojo::NetworkServiceImpl, |
| 37 mojo::NetworkContext> NetworkProvider; |
| 38 |
35 public: | 39 public: |
36 Delegate() {} | 40 Delegate() {} |
37 | 41 |
38 virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE { | 42 virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE { |
39 mojo::InterfacePtr<mojo::ProfileService> profile_service; | 43 mojo::InterfacePtr<mojo::ProfileService> profile_service; |
40 app->ConnectToService("mojo:profile_service", &profile_service); | 44 app->ConnectToService("mojo:profile_service", &profile_service); |
41 base::FilePath base_path; | 45 base::FilePath base_path; |
42 profile_service->GetPath(mojo::ProfileService::DIR_TEMP, | 46 profile_service->GetPath(mojo::ProfileService::DIR_TEMP, |
43 base::Bind(&OnPathReceived, | 47 base::Bind(&OnPathReceived, |
44 base::Unretained(&base_path))); | 48 base::Unretained(&base_path))); |
45 profile_service.WaitForIncomingMethodCall(); | 49 profile_service.WaitForIncomingMethodCall(); |
46 DCHECK(!base_path.value().empty()); | 50 DCHECK(!base_path.value().empty()); |
47 base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); | 51 base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); |
48 context_.reset(new mojo::NetworkContext(base_path)); | 52 context_.reset(new mojo::NetworkContext(base_path)); |
| 53 network_service_provider_.reset(new NetworkProvider(context_.get())); |
49 } | 54 } |
50 | 55 |
| 56 // mojo::ApplicationDelegate implementation. |
51 virtual bool ConfigureIncomingConnection( | 57 virtual bool ConfigureIncomingConnection( |
52 mojo::ApplicationConnection* connection) MOJO_OVERRIDE { | 58 mojo::ApplicationConnection* connection) MOJO_OVERRIDE { |
53 DCHECK(context_); | 59 DCHECK(context_); |
54 connection->AddService<mojo::NetworkServiceImpl>(context_.get()); | 60 connection->AddServiceProvider(network_service_provider_.get()); |
55 return true; | 61 return true; |
56 } | 62 } |
57 | 63 |
58 private: | 64 private: |
59 scoped_ptr<mojo::NetworkContext> context_; | 65 scoped_ptr<mojo::NetworkContext> context_; |
| 66 scoped_ptr<NetworkProvider> network_service_provider_; |
60 }; | 67 }; |
61 | 68 |
62 extern "C" APPLICATION_EXPORT MojoResult CDECL MojoMain( | 69 extern "C" APPLICATION_EXPORT MojoResult CDECL MojoMain( |
63 MojoHandle shell_handle) { | 70 MojoHandle shell_handle) { |
64 base::CommandLine::Init(0, NULL); | 71 base::CommandLine::Init(0, NULL); |
65 #if !defined(COMPONENT_BUILD) | 72 #if !defined(COMPONENT_BUILD) |
66 base::AtExitManager at_exit; | 73 base::AtExitManager at_exit; |
67 #endif | 74 #endif |
68 | 75 |
69 // The IO message loop allows us to use net::URLRequest on this thread. | 76 // The IO message loop allows us to use net::URLRequest on this thread. |
70 base::MessageLoopForIO loop; | 77 base::MessageLoopForIO loop; |
71 | 78 |
72 Delegate delegate; | 79 Delegate delegate; |
73 mojo::ApplicationImpl app( | 80 mojo::ApplicationImpl app( |
74 &delegate, mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle))); | 81 &delegate, mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle))); |
75 | 82 |
76 loop.Run(); | 83 loop.Run(); |
77 return MOJO_RESULT_OK; | 84 return MOJO_RESULT_OK; |
78 } | 85 } |
OLD | NEW |