| OLD | NEW | 
|    1 // Copyright 2016 The Chromium Authors. All rights reserved. |    1 // Copyright 2016 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 <stddef.h> |    5 #include <stddef.h> | 
|    6 #include <stdint.h> |    6 #include <stdint.h> | 
|    7  |    7  | 
|    8 #include <memory> |    8 #include <memory> | 
|    9 #include <utility> |    9 #include <utility> | 
|   10  |   10  | 
| (...skipping 12 matching lines...) Expand all  Loading... | 
|   23 #include "services/service_manager/public/cpp/service_runner.h" |   23 #include "services/service_manager/public/cpp/service_runner.h" | 
|   24 #include "services/service_manager/public/interfaces/service_factory.mojom.h" |   24 #include "services/service_manager/public/interfaces/service_factory.mojom.h" | 
|   25 #include "services/service_manager/tests/connect/connect_test.mojom.h" |   25 #include "services/service_manager/tests/connect/connect_test.mojom.h" | 
|   26  |   26  | 
|   27 // Tests that multiple services can be packaged in a single service by |   27 // Tests that multiple services can be packaged in a single service by | 
|   28 // implementing ServiceFactory; that these services can be specified by |   28 // implementing ServiceFactory; that these services can be specified by | 
|   29 // the package's manifest and are thus registered with the PackageManager. |   29 // the package's manifest and are thus registered with the PackageManager. | 
|   30  |   30  | 
|   31 namespace service_manager { |   31 namespace service_manager { | 
|   32  |   32  | 
 |   33 namespace { | 
 |   34  | 
 |   35 void QuitLoop(base::RunLoop* loop) { | 
 |   36   loop->Quit(); | 
 |   37 } | 
 |   38  | 
 |   39 }  // namespace | 
 |   40  | 
|   33 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; |   41 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; | 
|   34  |   42  | 
|   35 class ProvidedService |   43 class ProvidedService | 
|   36     : public Service, |   44     : public Service, | 
|   37       public InterfaceFactory<test::mojom::ConnectTestService>, |   45       public InterfaceFactory<test::mojom::ConnectTestService>, | 
|   38       public InterfaceFactory<test::mojom::BlockedInterface>, |   46       public InterfaceFactory<test::mojom::BlockedInterface>, | 
|   39       public InterfaceFactory<test::mojom::UserIdTest>, |   47       public InterfaceFactory<test::mojom::UserIdTest>, | 
|   40       public test::mojom::ConnectTestService, |   48       public test::mojom::ConnectTestService, | 
|   41       public test::mojom::BlockedInterface, |   49       public test::mojom::BlockedInterface, | 
|   42       public test::mojom::UserIdTest, |   50       public test::mojom::UserIdTest, | 
|   43       public base::SimpleThread { |   51       public base::SimpleThread { | 
|   44  public: |   52  public: | 
|   45   ProvidedService(const std::string& title, mojom::ServiceRequest request) |   53   ProvidedService(const std::string& title, mojom::ServiceRequest request) | 
|   46       : base::SimpleThread(title), |   54       : base::SimpleThread(title), | 
|   47         title_(title), |   55         title_(title), | 
|   48         request_(std::move(request)) { |   56         request_(std::move(request)) { | 
|   49     Start(); |   57     Start(); | 
|   50   } |   58   } | 
|   51  |  | 
|   52   ~ProvidedService() override { |   59   ~ProvidedService() override { | 
|   53     Join(); |   60     Join(); | 
|   54   } |   61   } | 
|   55  |   62  | 
|   56  private: |   63  private: | 
|   57   class ForwardingServiceImpl : public Service { |  | 
|   58    public: |  | 
|   59     explicit ForwardingServiceImpl(Service* service) |  | 
|   60         : service_(service) {} |  | 
|   61     ~ForwardingServiceImpl() override {} |  | 
|   62  |  | 
|   63     // Service: |  | 
|   64     void OnStart(ServiceContext* context) override { |  | 
|   65       service_->OnStart(context); |  | 
|   66     } |  | 
|   67  |  | 
|   68     bool OnConnect(const ServiceInfo& remote_info, |  | 
|   69                    InterfaceRegistry* registry) override { |  | 
|   70       return service_->OnConnect(remote_info, registry); |  | 
|   71     } |  | 
|   72  |  | 
|   73     bool OnStop() override { return service_->OnStop(); } |  | 
|   74  |  | 
|   75    private: |  | 
|   76     Service* const service_; |  | 
|   77  |  | 
|   78     DISALLOW_COPY_AND_ASSIGN(ForwardingServiceImpl); |  | 
|   79   }; |  | 
|   80  |  | 
|   81   // service_manager::Service: |   64   // service_manager::Service: | 
|   82   void OnStart(ServiceContext* context) override { |   65   void OnStart(ServiceContext* context) override { | 
|   83     context_ = context; |   66     context_ = context; | 
|   84     bindings_.set_connection_error_handler( |   67     bindings_.set_connection_error_handler( | 
|   85         base::Bind(&ProvidedService::OnConnectionError, |   68         base::Bind(&ProvidedService::OnConnectionError, | 
|   86                    base::Unretained(this))); |   69                    base::Unretained(this))); | 
|   87   } |   70   } | 
|   88  |   71  | 
|   89   bool OnConnect(const ServiceInfo& remote_info, |   72   bool OnConnect(const ServiceInfo& remote_info, | 
|   90                  InterfaceRegistry* registry) override { |   73                  InterfaceRegistry* registry) override { | 
| (...skipping 28 matching lines...) Expand all  Loading... | 
|  119   // InterfaceFactory<test::mojom::UserIdTest>: |  102   // InterfaceFactory<test::mojom::UserIdTest>: | 
|  120   void Create(const Identity& remote_identity, |  103   void Create(const Identity& remote_identity, | 
|  121               test::mojom::UserIdTestRequest request) override { |  104               test::mojom::UserIdTestRequest request) override { | 
|  122     user_id_test_bindings_.AddBinding(this, std::move(request)); |  105     user_id_test_bindings_.AddBinding(this, std::move(request)); | 
|  123   } |  106   } | 
|  124  |  107  | 
|  125   // test::mojom::ConnectTestService: |  108   // test::mojom::ConnectTestService: | 
|  126   void GetTitle(const GetTitleCallback& callback) override { |  109   void GetTitle(const GetTitleCallback& callback) override { | 
|  127     callback.Run(title_); |  110     callback.Run(title_); | 
|  128   } |  111   } | 
|  129  |  | 
|  130   void GetInstance(const GetInstanceCallback& callback) override { |  112   void GetInstance(const GetInstanceCallback& callback) override { | 
|  131     callback.Run(context_->identity().instance()); |  113     callback.Run(context_->identity().instance()); | 
|  132   } |  114   } | 
|  133  |  115  | 
|  134   // test::mojom::BlockedInterface: |  116   // test::mojom::BlockedInterface: | 
|  135   void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { |  117   void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { | 
|  136     callback.Run("Called Blocked Interface!"); |  118     callback.Run("Called Blocked Interface!"); | 
|  137   } |  119   } | 
|  138  |  120  | 
|  139   // test::mojom::UserIdTest: |  121   // test::mojom::UserIdTest: | 
|  140   void ConnectToClassAppAsDifferentUser( |  122   void ConnectToClassAppAsDifferentUser( | 
|  141       const service_manager::Identity& target, |  123       const service_manager::Identity& target, | 
|  142       const ConnectToClassAppAsDifferentUserCallback& callback) override { |  124       const ConnectToClassAppAsDifferentUserCallback& callback) override { | 
|  143     Connector::ConnectParams params(target); |  125     Connector::ConnectParams params(target); | 
|  144     std::unique_ptr<Connection> connection = |  126     std::unique_ptr<Connection> connection = | 
|  145         context_->connector()->Connect(¶ms); |  127         context_->connector()->Connect(¶ms); | 
|  146     { |  128     { | 
|  147       base::RunLoop loop; |  129       base::RunLoop loop; | 
|  148       connection->AddConnectionCompletedClosure(loop.QuitClosure()); |  130       connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); | 
|  149       base::MessageLoop::ScopedNestableTaskAllower allow( |  131       base::MessageLoop::ScopedNestableTaskAllower allow( | 
|  150           base::MessageLoop::current()); |  132           base::MessageLoop::current()); | 
|  151       loop.Run(); |  133       loop.Run(); | 
|  152     } |  134     } | 
|  153     callback.Run(static_cast<int32_t>(connection->GetResult()), |  135     callback.Run(static_cast<int32_t>(connection->GetResult()), | 
|  154                  connection->GetRemoteIdentity()); |  136                  connection->GetRemoteIdentity()); | 
|  155   } |  137   } | 
|  156  |  138  | 
|  157   // base::SimpleThread: |  139   // base::SimpleThread: | 
|  158   void Run() override { |  140   void Run() override { | 
|  159     ServiceRunner(new ForwardingServiceImpl(this)).Run( |  141     ServiceRunner(this).Run(request_.PassMessagePipe().release().value(), | 
|  160         request_.PassMessagePipe().release().value(), false); |  142                             false); | 
|  161     caller_.reset(); |  143     delete this; | 
|  162     bindings_.CloseAllBindings(); |  | 
|  163     blocked_bindings_.CloseAllBindings(); |  | 
|  164     user_id_test_bindings_.CloseAllBindings(); |  | 
|  165   } |  144   } | 
|  166  |  145  | 
|  167   void OnConnectionError() { |  146   void OnConnectionError() { | 
|  168     if (bindings_.empty()) |  147     if (bindings_.empty()) | 
|  169       base::MessageLoop::current()->QuitWhenIdle(); |  148       base::MessageLoop::current()->QuitWhenIdle(); | 
|  170   } |  149   } | 
|  171  |  150  | 
|  172   ServiceContext* context_ = nullptr; |  151   ServiceContext* context_ = nullptr; | 
|  173   const std::string title_; |  152   const std::string title_; | 
|  174   mojom::ServiceRequest request_; |  153   mojom::ServiceRequest request_; | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
|  186       public InterfaceFactory<test::mojom::ConnectTestService>, |  165       public InterfaceFactory<test::mojom::ConnectTestService>, | 
|  187       public mojom::ServiceFactory, |  166       public mojom::ServiceFactory, | 
|  188       public test::mojom::ConnectTestService { |  167       public test::mojom::ConnectTestService { | 
|  189  public: |  168  public: | 
|  190   ConnectTestService() {} |  169   ConnectTestService() {} | 
|  191   ~ConnectTestService() override {} |  170   ~ConnectTestService() override {} | 
|  192  |  171  | 
|  193  private: |  172  private: | 
|  194   // service_manager::Service: |  173   // service_manager::Service: | 
|  195   void OnStart(ServiceContext* context) override { |  174   void OnStart(ServiceContext* context) override { | 
|  196     context_ = context; |  175     identity_ = context->identity(); | 
|  197  |  176     bindings_.set_connection_error_handler( | 
|  198     base::Closure error_handler = |  | 
|  199         base::Bind(&ConnectTestService::OnConnectionError, |  177         base::Bind(&ConnectTestService::OnConnectionError, | 
|  200                    base::Unretained(this)); |  178                    base::Unretained(this))); | 
|  201     bindings_.set_connection_error_handler(error_handler); |  | 
|  202     service_factory_bindings_.set_connection_error_handler(error_handler); |  | 
|  203   } |  179   } | 
|  204  |  | 
|  205   bool OnConnect(const ServiceInfo& remote_info, |  180   bool OnConnect(const ServiceInfo& remote_info, | 
|  206                  InterfaceRegistry* registry) override { |  181                  InterfaceRegistry* registry) override { | 
|  207     registry->AddInterface<ServiceFactory>(this); |  182     registry->AddInterface<ServiceFactory>(this); | 
|  208     registry->AddInterface<test::mojom::ConnectTestService>(this); |  183     registry->AddInterface<test::mojom::ConnectTestService>(this); | 
|  209     return true; |  184     return true; | 
|  210   } |  185   } | 
|  211  |  186  | 
|  212   bool OnStop() override { |  | 
|  213     provided_services_.clear(); |  | 
|  214     return true; |  | 
|  215   } |  | 
|  216  |  | 
|  217   // InterfaceFactory<mojom::ServiceFactory>: |  187   // InterfaceFactory<mojom::ServiceFactory>: | 
|  218   void Create(const Identity& remote_identity, |  188   void Create(const Identity& remote_identity, | 
|  219               mojom::ServiceFactoryRequest request) override { |  189               mojom::ServiceFactoryRequest request) override { | 
|  220     service_factory_bindings_.AddBinding(this, std::move(request)); |  190     service_factory_bindings_.AddBinding(this, std::move(request)); | 
|  221   } |  191   } | 
|  222  |  192  | 
|  223   // InterfaceFactory<test::mojom::ConnectTestService>: |  193   // InterfaceFactory<test::mojom::ConnectTestService>: | 
|  224   void Create(const Identity& remote_identity, |  194   void Create(const Identity& remote_identity, | 
|  225               test::mojom::ConnectTestServiceRequest request) override { |  195               test::mojom::ConnectTestServiceRequest request) override { | 
|  226     bindings_.AddBinding(this, std::move(request)); |  196     bindings_.AddBinding(this, std::move(request)); | 
|  227   } |  197   } | 
|  228  |  198  | 
|  229   // mojom::ServiceFactory: |  199   // mojom::ServiceFactory: | 
|  230   void CreateService(mojom::ServiceRequest request, |  200   void CreateService(mojom::ServiceRequest request, | 
|  231                      const std::string& name) override { |  201                          const std::string& name) override { | 
|  232     if (name == "service:connect_test_a") { |  202     if (name == "service:connect_test_a") | 
|  233       provided_services_.emplace_back( |  203       new ProvidedService("A", std::move(request)); | 
|  234           base::MakeUnique<ProvidedService>("A", std::move(request))); |  204     else if (name == "service:connect_test_b") | 
|  235     } else if (name == "service:connect_test_b") { |  205       new ProvidedService("B", std::move(request)); | 
|  236       provided_services_.emplace_back( |  | 
|  237           base::MakeUnique<ProvidedService>("B", std::move(request))); |  | 
|  238     } |  | 
|  239   } |  206   } | 
|  240  |  207  | 
|  241   // test::mojom::ConnectTestService: |  208   // test::mojom::ConnectTestService: | 
|  242   void GetTitle(const GetTitleCallback& callback) override { |  209   void GetTitle(const GetTitleCallback& callback) override { | 
|  243     callback.Run("ROOT"); |  210     callback.Run("ROOT"); | 
|  244   } |  211   } | 
|  245  |  | 
|  246   void GetInstance(const GetInstanceCallback& callback) override { |  212   void GetInstance(const GetInstanceCallback& callback) override { | 
|  247     callback.Run(context_->identity().instance()); |  213     callback.Run(identity_.instance()); | 
|  248   } |  214   } | 
|  249  |  215  | 
|  250   void OnConnectionError() { |  216   void OnConnectionError() { | 
|  251     if (bindings_.empty() && service_factory_bindings_.empty()) |  217     if (bindings_.empty()) | 
|  252       context_->RequestQuit(); |  218       base::MessageLoop::current()->QuitWhenIdle(); | 
|  253   } |  219   } | 
|  254  |  220  | 
|  255   ServiceContext* context_ = nullptr; |  221   Identity identity_; | 
|  256   std::vector<std::unique_ptr<Service>> delegates_; |  222   std::vector<std::unique_ptr<Service>> delegates_; | 
|  257   mojo::BindingSet<mojom::ServiceFactory> service_factory_bindings_; |  223   mojo::BindingSet<mojom::ServiceFactory> service_factory_bindings_; | 
|  258   mojo::BindingSet<test::mojom::ConnectTestService> bindings_; |  224   mojo::BindingSet<test::mojom::ConnectTestService> bindings_; | 
|  259   std::list<std::unique_ptr<ProvidedService>> provided_services_; |  | 
|  260  |  225  | 
|  261   DISALLOW_COPY_AND_ASSIGN(ConnectTestService); |  226   DISALLOW_COPY_AND_ASSIGN(ConnectTestService); | 
|  262 }; |  227 }; | 
|  263  |  228  | 
|  264 }  // namespace service_manager |  229 }  // namespace service_manager | 
|  265  |  230  | 
|  266 MojoResult ServiceMain(MojoHandle service_request_handle) { |  231 MojoResult ServiceMain(MojoHandle service_request_handle) { | 
|  267   service_manager::ServiceRunner runner( |  232   service_manager::ServiceRunner runner( | 
|  268       new service_manager::ConnectTestService); |  233       new service_manager::ConnectTestService); | 
|  269   return runner.Run(service_request_handle); |  234   return runner.Run(service_request_handle); | 
|  270 } |  235 } | 
| OLD | NEW |