| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 and the Service Manager destroys its corresponding instance and asks the process | 83 and the Service Manager destroys its corresponding instance and asks the process |
| 84 to exit. | 84 to exit. |
| 85 | 85 |
| 86 #### A simple Service example | 86 #### A simple Service example |
| 87 | 87 |
| 88 Consider this simple application that implements the Service interface: | 88 Consider this simple application that implements the Service interface: |
| 89 | 89 |
| 90 **app.cc:** | 90 **app.cc:** |
| 91 | 91 |
| 92 #include "mojo/public/c/system/main.h" | 92 #include "mojo/public/c/system/main.h" |
| 93 #include "services/shell/public/cpp/application_runner.h" | 93 #include "services/service_manager/public/cpp/application_runner.h" |
| 94 #include "services/shell/public/cpp/connector.h" | 94 #include "services/service_manager/public/cpp/connector.h" |
| 95 #include "services/shell/public/cpp/connection.h" | 95 #include "services/service_manager/public/cpp/connection.h" |
| 96 #include "services/shell/public/cpp/identity.h" | 96 #include "services/service_manager/public/cpp/identity.h" |
| 97 #include "services/shell/public/cpp/service.h" | 97 #include "services/service_manager/public/cpp/service.h" |
| 98 | 98 |
| 99 class Service : public shell::Service { | 99 class Service : public shell::Service { |
| 100 public: | 100 public: |
| 101 Service() {} | 101 Service() {} |
| 102 ~Service() override {} | 102 ~Service() override {} |
| 103 | 103 |
| 104 // Overridden from shell::Service: | 104 // Overridden from shell::Service: |
| 105 void OnStart(const shell::Identity& identity) override { | 105 void OnStart(const shell::Identity& identity) override { |
| 106 } | 106 } |
| 107 bool OnConnect(shell::Connection* connection) override { | 107 bool OnConnect(shell::Connection* connection) override { |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 | 430 |
| 431 TEST_F(Test, Basic) { | 431 TEST_F(Test, Basic) { |
| 432 mojom::SomeInterface some_interface; | 432 mojom::SomeInterface some_interface; |
| 433 connector()->ConnectToInterface("mojo:service", &some_interface); | 433 connector()->ConnectToInterface("mojo:service", &some_interface); |
| 434 base::RunLoop loop; | 434 base::RunLoop loop; |
| 435 some_interface->Foo(base::Bind(&QuitLoop, &loop)); | 435 some_interface->Foo(base::Bind(&QuitLoop, &loop)); |
| 436 loop.Run(); | 436 loop.Run(); |
| 437 } | 437 } |
| 438 | 438 |
| 439 The BUILD.gn for this test file looks like any other using the test() template. | 439 The BUILD.gn for this test file looks like any other using the test() template. |
| 440 It must also depend on //services/shell/public/cpp:shell_test_support. | 440 It must also depend on //services/service_manager/public/cpp:shell_test_support. |
| 441 | 441 |
| 442 ServiceTest does a few things, but most importantly it register the test itself | 442 ServiceTest does a few things, but most importantly it register the test itself |
| 443 as a Service, with the name you pass it via its constructor. In the example | 443 as a Service, with the name you pass it via its constructor. In the example |
| 444 above, we supplied the name exe:service_unittest. This name is has no special | 444 above, we supplied the name exe:service_unittest. This name is has no special |
| 445 meaning other than that henceforth it will be used to identify the test service. | 445 meaning other than that henceforth it will be used to identify the test service. |
| 446 | 446 |
| 447 Behind the scenes, ServiceTest spins up the Service Manager on a background | 447 Behind the scenes, ServiceTest spins up the Service Manager on a background |
| 448 thread, and asks it to create an instance for the test service on the main | 448 thread, and asks it to create an instance for the test service on the main |
| 449 thread, with the name supplied. ServiceTest blocks the main thread while the | 449 thread, with the name supplied. ServiceTest blocks the main thread while the |
| 450 Service Manager thread does this initialization. Once the Service Manager has | 450 Service Manager thread does this initialization. Once the Service Manager has |
| 451 created the instance, it calls OnStart() (as for any other service), and the | 451 created the instance, it calls OnStart() (as for any other service), and the |
| 452 main thread continues, running the test. At this point accessors defined in | 452 main thread continues, running the test. At this point accessors defined in |
| 453 service_test.h like connector() can be used to connect to other services. | 453 service_test.h like connector() can be used to connect to other services. |
| 454 | 454 |
| 455 Youll note in the example above I made Foo() take a callback, this is to give | 455 Youll note in the example above I made Foo() take a callback, this is to give |
| 456 the test something interesting to do. In the mojom for SomeInterface wed have | 456 the test something interesting to do. In the mojom for SomeInterface wed have |
| 457 the Foo() method return an empty response. In mojo:service, wed have Foo() take | 457 the Foo() method return an empty response. In mojo:service, wed have Foo() take |
| 458 the callback as a parameter, and run it. In the test, we spin a RunLoop until we | 458 the callback as a parameter, and run it. In the test, we spin a RunLoop until we |
| 459 get that response. In real world cases we can pass back state & validate | 459 get that response. In real world cases we can pass back state & validate |
| 460 expectations. You can see real examples of this test framework in use in the | 460 expectations. You can see real examples of this test framework in use in the |
| 461 Service Managers own suite of tests, under //services/shell/tests. | 461 Service Managers own suite of tests, under //services/service_manager/tests. |
| 462 | 462 |
| 463 ### Packaging | 463 ### Packaging |
| 464 | 464 |
| 465 By default a .library statically links its dependencies, so having many of them | 465 By default a .library statically links its dependencies, so having many of them |
| 466 will yield an installed product many times larger than Chrome today. For this | 466 will yield an installed product many times larger than Chrome today. For this |
| 467 reason its desirable to package several Services together in a single binary. | 467 reason its desirable to package several Services together in a single binary. |
| 468 The Service Manager provides an interface **shell.mojom.ServiceFactory**: | 468 The Service Manager provides an interface **shell.mojom.ServiceFactory**: |
| 469 | 469 |
| 470 interface ServiceFactory { | 470 interface ServiceFactory { |
| 471 CreateService(Service& service, string name); | 471 CreateService(Service& service, string name); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 to have the Service Manager start the executable itself, the second is to have | 550 to have the Service Manager start the executable itself, the second is to have |
| 551 some other executable start the process and then tell the Service Manager about | 551 some other executable start the process and then tell the Service Manager about |
| 552 it. In both cases, the target executable has to perform a handshake with the | 552 it. In both cases, the target executable has to perform a handshake with the |
| 553 Service Manager early on so it can bind the Service request the Service Manager | 553 Service Manager early on so it can bind the Service request the Service Manager |
| 554 sends it. | 554 sends it. |
| 555 | 555 |
| 556 Assuming you have an executable that properly initializes the Mojo EDK, you add | 556 Assuming you have an executable that properly initializes the Mojo EDK, you add |
| 557 the following lines at some point early in application startup to establish the | 557 the following lines at some point early in application startup to establish the |
| 558 connection with the Service Manager: | 558 connection with the Service Manager: |
| 559 | 559 |
| 560 #include "services/shell/public/cpp/service.h" | 560 #include "services/service_manager/public/cpp/service.h" |
| 561 #include "services/shell/public/cpp/service_context.h" | 561 #include "services/service_manager/public/cpp/service_context.h" |
| 562 #include "services/shell/runner/child/runner_connection.h" | 562 #include "services/service_manager/runner/child/runner_connection.h" |
| 563 | 563 |
| 564 class MyClient : public shell::Service { | 564 class MyClient : public shell::Service { |
| 565 .. | 565 .. |
| 566 }; | 566 }; |
| 567 | 567 |
| 568 shell::mojom::ServiceRequest request; | 568 shell::mojom::ServiceRequest request; |
| 569 scoped_ptr<shell::RunnerConnection> connection( | 569 scoped_ptr<shell::RunnerConnection> connection( |
| 570 shell::RunnerConnection::ConnectToRunner( | 570 shell::RunnerConnection::ConnectToRunner( |
| 571 &request, ScopedMessagePipeHandle())); | 571 &request, ScopedMessagePipeHandle())); |
| 572 MyService service; | 572 MyService service; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 669 Under the Hood | 669 Under the Hood |
| 670 Four major components: Shell API (Mojom), Shell, Catalog, Shell Client Lib. | 670 Four major components: Shell API (Mojom), Shell, Catalog, Shell Client Lib. |
| 671 The connect flow, catalog, etc. | 671 The connect flow, catalog, etc. |
| 672 Capability brokering in the shell | 672 Capability brokering in the shell |
| 673 Userids | 673 Userids |
| 674 | 674 |
| 675 Finer points: | 675 Finer points: |
| 676 | 676 |
| 677 Mojo Names: mojo, exe | 677 Mojo Names: mojo, exe |
| 678 Exposing services on outbound connections | 678 Exposing services on outbound connections |
| OLD | NEW |