OLD | NEW |
1 # Service Manager User Guide | 1 # Service Manager User Guide |
2 | 2 |
3 ## What is the Service Manager? | 3 ## What is the Service Manager? |
4 | 4 |
5 The Service Manager is a tool that brokers connections and capabilities between | 5 The Service Manager is a tool that brokers connections and capabilities between |
6 and manages instances of components, referred to henceforth as services. | 6 and manages instances of components, referred to henceforth as services. |
7 | 7 |
8 The Service Manager performs the following functions: | 8 The Service Manager performs the following functions: |
9 | 9 |
10 * Brokering connections between services, including communicating policies such | 10 * Brokering connections between services, including communicating policies such |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 pair. In Chrome an example of this would be multiple instances of the renderer | 61 pair. In Chrome an example of this would be multiple instances of the renderer |
62 or the same profile. | 62 or the same profile. |
63 | 63 |
64 A Service implements the Mojo interface service_manager.mojom.Service, which is | 64 A Service implements the Mojo interface service_manager.mojom.Service, which is |
65 the primary means the Service Manager has of communicating with its service. | 65 the primary means the Service Manager has of communicating with its service. |
66 Service has two methods: OnStart(), called once at when the Service Manager | 66 Service has two methods: OnStart(), called once at when the Service Manager |
67 first learns about the service, and OnConnect(), which the Service Manager calls | 67 first learns about the service, and OnConnect(), which the Service Manager calls |
68 every time some other service tries to connect to this one. | 68 every time some other service tries to connect to this one. |
69 | 69 |
70 Services have a link back to the Service Manager too, primarily in the form of | 70 Services have a link back to the Service Manager too, primarily in the form of |
71 the service_manager.mojom.Connector interface. The Connector allows services to | 71 the service_manager.mojom.Connector interface. The Connector allows services to |
72 open connections to other services. | 72 open connections to other services. |
73 | 73 |
74 A unique connection from the Service Manager to a service is called an | 74 A unique connection from the Service Manager to a service is called an |
75 instance, each with its own unique identifier, called an instance id. Every | 75 instance, each with its own unique identifier, called an instance id. Every |
76 instance has a unique Identity. It is possible to locate an existing instance | 76 instance has a unique Identity. It is possible to locate an existing instance |
77 purely using its Identity. | 77 purely using its Identity. |
78 | 78 |
79 Services define their own lifetimes. Services in processes started by other | 79 Services define their own lifetimes. Services in processes started by other |
80 services (rather than the Service Manager) may even outlive the connection with | 80 services (rather than the Service Manager) may even outlive the connection with |
81 the Service Manager. For processes launched by the Service Manager, when a | 81 the Service Manager. For processes launched by the Service Manager, when a |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 } | 133 } |
134 | 134 |
135 service_manifest("manifest") { | 135 service_manifest("manifest") { |
136 name = "my_service" | 136 name = "my_service" |
137 source = "manifest.json" | 137 source = "manifest.json" |
138 } | 138 } |
139 | 139 |
140 What does all this do? Building the app target produces two files in the output | 140 What does all this do? Building the app target produces two files in the output |
141 directory: Packages/my_service/my_service.library and | 141 directory: Packages/my_service/my_service.library and |
142 Packages/my_service/manifest.json. app.library is a DSO loaded by the Service | 142 Packages/my_service/manifest.json. app.library is a DSO loaded by the Service |
143 Manager in its own process when another service connects to the | 143 Manager in its own process when another service connects to the |
144 service:my_service name. This is not the only way (nor even the most likely one) | 144 service:my_service name. This is not the only way (nor even the most likely one) |
145 you can implement a Service, but it's the simplest and easiest to reason about. | 145 you can implement a Service, but it's the simplest and easiest to reason about. |
146 | 146 |
147 This service doesn't do much. Its implementation of OnStart() is empty, and its | 147 This service doesn't do much. Its implementation of OnStart() is empty, and its |
148 implementation of OnConnect just returns true to allow the inbound connection to | 148 implementation of OnConnect just returns true to allow the inbound connection to |
149 complete. Let's study the parameters to these methods though, since they'll be | 149 complete. Let's study the parameters to these methods though, since they'll be |
150 important as we begin to do more in our service. | 150 important as we begin to do more in our service. |
151 | 151 |
152 ##### OnStart Parameters | 152 ##### OnStart Parameters |
153 | 153 |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 | 637 |
638 base::FilePath target_path; | 638 base::FilePath target_path; |
639 base::PathService::Get(base::DIR_EXE, &target_path); | 639 base::PathService::Get(base::DIR_EXE, &target_path); |
640 target_path = target_path.Append(FILE_PATH_LITERAL("target.exe")); | 640 target_path = target_path.Append(FILE_PATH_LITERAL("target.exe")); |
641 base::CommandLine target_command_line(target_path); | 641 base::CommandLine target_command_line(target_path); |
642 | 642 |
643 mojo::edk::PlatformChannelPair pair; | 643 mojo::edk::PlatformChannelPair pair; |
644 mojo::edk::HandlePassingInformation info; | 644 mojo::edk::HandlePassingInformation info; |
645 pair.PrepareToPassClientHandleToChildProcess(&target_command_line, &info); | 645 pair.PrepareToPassClientHandleToChildProcess(&target_command_line, &info); |
646 | 646 |
647 std::string token = mojo::edk::GenerateRandomToken(); | 647 mojo::edk::PendingProcessConnection connection; |
| 648 std::string token; |
| 649 mojo::ScopedMessagePipeHandle pipe = connection.CreateMessagePipe(&token); |
648 target_command_line.AppendSwitchASCII(switches::kPrimordialPipeToken, | 650 target_command_line.AppendSwitchASCII(switches::kPrimordialPipeToken, |
649 token); | 651 token); |
650 | 652 |
651 mojo::ScopedMessagePipeHandle pipe = | 653 service_manager::Identity target("exe:target", |
652 mojo::edk::CreateParentMessagePipe(token); | 654 service_manager::mojom::kInheritUserID); |
653 | |
654 service_manager::mojom::ServiceFactoryPtr factory; | |
655 factory.Bind( | |
656 mojo::InterfacePtrInfo<service_manager::mojom::ServiceFactory>( | |
657 std::move(pipe), 0u)); | |
658 service_manager::mojom::PIDReceiverPtr receiver; | 655 service_manager::mojom::PIDReceiverPtr receiver; |
659 | 656 connector->RegisterService(target, std::move(pipe), MakeRequest(&receiver)); |
660 service_manager::Identity target("exe:target",service_manager::mojom::kInher
itUserID); | |
661 service_manager::Connector::ConnectParams params(target); | |
662 params.set_client_process_connection(std::move(factory), | |
663 MakeRequest(&receiver)); | |
664 std::unique_ptr<service_manager::Connection> connection = connector->Connect
(¶ms); | |
665 | 657 |
666 base::LaunchOptions options; | 658 base::LaunchOptions options; |
667 options.handles_to_inherit = &info; | 659 options.handles_to_inherit = &info; |
668 base::Process process = base::LaunchProcess(target_command_line, options); | 660 base::Process process = base::LaunchProcess(target_command_line, options); |
669 mojo::edk::ChildProcessLaunched(process.Handle(), pair.PassServerHandle()); | 661 connection.Connect(process.Handle(), pair.PassServerHandle()); |
670 | 662 |
671 That's a lot. But it boils down to these steps: | 663 That's a lot. But it boils down to these steps: |
672 1. Creating the message pipe to connect the target process and the Service | 664 1. Creating the message pipe to connect the target process and the Service |
673 Manager. | 665 Manager. |
674 2. Putting the server end of the pipe onto the command line to the target | 666 2. Putting the server end of the pipe onto the command line to the target |
675 process. | 667 process. |
676 3. Binding the client end to a ServiceFactoryPtr, constructing an Identity for | 668 3. Binding the client end to a ServiceFactoryPtr, constructing an Identity for |
677 the target process and passing both through Connector::Connect(). | 669 the target process and passing both through Connector::Connect(). |
678 4. Starting the process with the configured command line. | 670 4. Starting the process with the configured command line. |
679 | 671 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 Under the Hood | 795 Under the Hood |
804 Four major components: Service Manager API (Mojom), Service Manager, Catalog, | 796 Four major components: Service Manager API (Mojom), Service Manager, Catalog, |
805 Service Manager Client Lib. | 797 Service Manager Client Lib. |
806 The connect flow, catalog, etc. | 798 The connect flow, catalog, etc. |
807 Capability brokering in the Service Manager | 799 Capability brokering in the Service Manager |
808 Userids | 800 Userids |
809 | 801 |
810 Finer points: | 802 Finer points: |
811 | 803 |
812 Service Names: mojo, exe | 804 Service Names: mojo, exe |
OLD | NEW |