| 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/interface_factory_with_context.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::InterfaceFactoryWithContext<mojo::NetworkServiceImpl, |
| 37 mojo::NetworkContext> |
| 38 NetworkFactory; |
| 39 |
| 35 public: | 40 public: |
| 36 Delegate() {} | 41 Delegate() {} |
| 37 | 42 |
| 38 virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE { | 43 virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE { |
| 39 mojo::InterfacePtr<mojo::ProfileService> profile_service; | 44 mojo::InterfacePtr<mojo::ProfileService> profile_service; |
| 40 app->ConnectToService("mojo:profile_service", &profile_service); | 45 app->ConnectToService("mojo:profile_service", &profile_service); |
| 41 base::FilePath base_path; | 46 base::FilePath base_path; |
| 42 profile_service->GetPath(mojo::ProfileService::DIR_TEMP, | 47 profile_service->GetPath(mojo::ProfileService::DIR_TEMP, |
| 43 base::Bind(&OnPathReceived, | 48 base::Bind(&OnPathReceived, |
| 44 base::Unretained(&base_path))); | 49 base::Unretained(&base_path))); |
| 45 profile_service.WaitForIncomingMethodCall(); | 50 profile_service.WaitForIncomingMethodCall(); |
| 46 DCHECK(!base_path.value().empty()); | 51 DCHECK(!base_path.value().empty()); |
| 47 base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); | 52 base_path = base_path.Append(FILE_PATH_LITERAL("network_service")); |
| 48 context_.reset(new mojo::NetworkContext(base_path)); | 53 context_.reset(new mojo::NetworkContext(base_path)); |
| 54 network_service_factory_.reset(new NetworkFactory(context_.get())); |
| 49 } | 55 } |
| 50 | 56 |
| 57 // mojo::ApplicationDelegate implementation. |
| 51 virtual bool ConfigureIncomingConnection( | 58 virtual bool ConfigureIncomingConnection( |
| 52 mojo::ApplicationConnection* connection) MOJO_OVERRIDE { | 59 mojo::ApplicationConnection* connection) MOJO_OVERRIDE { |
| 53 DCHECK(context_); | 60 DCHECK(context_); |
| 54 connection->AddService<mojo::NetworkServiceImpl>(context_.get()); | 61 connection->AddServiceFactory(network_service_factory_.get()); |
| 55 return true; | 62 return true; |
| 56 } | 63 } |
| 57 | 64 |
| 58 private: | 65 private: |
| 59 scoped_ptr<mojo::NetworkContext> context_; | 66 scoped_ptr<mojo::NetworkContext> context_; |
| 67 scoped_ptr<NetworkFactory> network_service_factory_; |
| 60 }; | 68 }; |
| 61 | 69 |
| 62 extern "C" APPLICATION_EXPORT MojoResult CDECL MojoMain( | 70 extern "C" APPLICATION_EXPORT MojoResult CDECL MojoMain( |
| 63 MojoHandle shell_handle) { | 71 MojoHandle shell_handle) { |
| 64 base::CommandLine::Init(0, NULL); | 72 base::CommandLine::Init(0, NULL); |
| 65 #if !defined(COMPONENT_BUILD) | 73 #if !defined(COMPONENT_BUILD) |
| 66 base::AtExitManager at_exit; | 74 base::AtExitManager at_exit; |
| 67 #endif | 75 #endif |
| 68 | 76 |
| 69 // The IO message loop allows us to use net::URLRequest on this thread. | 77 // The IO message loop allows us to use net::URLRequest on this thread. |
| 70 base::MessageLoopForIO loop; | 78 base::MessageLoopForIO loop; |
| 71 | 79 |
| 72 Delegate delegate; | 80 Delegate delegate; |
| 73 mojo::ApplicationImpl app( | 81 mojo::ApplicationImpl app( |
| 74 &delegate, mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle))); | 82 &delegate, mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle))); |
| 75 | 83 |
| 76 loop.Run(); | 84 loop.Run(); |
| 77 return MOJO_RESULT_OK; | 85 return MOJO_RESULT_OK; |
| 78 } | 86 } |
| OLD | NEW |