| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "services/navigation/navigation.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/threading/thread_task_runner_handle.h" | |
| 11 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 12 #include "services/navigation/view_impl.h" | |
| 13 #include "services/service_manager/public/cpp/connector.h" | |
| 14 #include "services/service_manager/public/cpp/service_context.h" | |
| 15 | |
| 16 namespace navigation { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 void CreateViewOnViewTaskRunner( | |
| 21 std::unique_ptr<service_manager::Connector> connector, | |
| 22 const std::string& client_user_id, | |
| 23 mojom::ViewClientPtr client, | |
| 24 mojom::ViewRequest request, | |
| 25 std::unique_ptr<service_manager::ServiceContextRef> context_ref) { | |
| 26 mojo::MakeStrongBinding( | |
| 27 base::MakeUnique<ViewImpl>(std::move(connector), client_user_id, | |
| 28 std::move(client), std::move(context_ref)), | |
| 29 std::move(request)); | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 std::unique_ptr<service_manager::Service> CreateNavigationService() { | |
| 35 return base::MakeUnique<Navigation>(); | |
| 36 } | |
| 37 | |
| 38 Navigation::Navigation() | |
| 39 : view_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 40 ref_factory_(base::MessageLoop::QuitWhenIdleClosure()) { | |
| 41 bindings_.set_connection_error_handler( | |
| 42 base::Bind(&Navigation::ViewFactoryLost, base::Unretained(this))); | |
| 43 registry_.AddInterface<mojom::ViewFactory>( | |
| 44 base::Bind(&Navigation::BindViewFactoryRequest, base::Unretained(this))); | |
| 45 } | |
| 46 Navigation::~Navigation() {} | |
| 47 | |
| 48 void Navigation::OnBindInterface( | |
| 49 const service_manager::BindSourceInfo& source_info, | |
| 50 const std::string& interface_name, | |
| 51 mojo::ScopedMessagePipeHandle interface_pipe) { | |
| 52 std::string remote_user_id = source_info.identity.user_id(); | |
| 53 if (!client_user_id_.empty() && client_user_id_ != remote_user_id) { | |
| 54 LOG(ERROR) << "Must have a separate Navigation service instance for " | |
| 55 << "different BrowserContexts."; | |
| 56 return; | |
| 57 } | |
| 58 client_user_id_ = remote_user_id; | |
| 59 registry_.BindInterface(source_info, interface_name, | |
| 60 std::move(interface_pipe)); | |
| 61 } | |
| 62 | |
| 63 void Navigation::CreateView(mojom::ViewClientPtr client, | |
| 64 mojom::ViewRequest request) { | |
| 65 std::unique_ptr<service_manager::Connector> new_connector = | |
| 66 context()->connector()->Clone(); | |
| 67 std::unique_ptr<service_manager::ServiceContextRef> context_ref = | |
| 68 ref_factory_.CreateRef(); | |
| 69 view_task_runner_->PostTask( | |
| 70 FROM_HERE, | |
| 71 base::Bind(&CreateViewOnViewTaskRunner, base::Passed(&new_connector), | |
| 72 client_user_id_, base::Passed(&client), base::Passed(&request), | |
| 73 base::Passed(&context_ref))); | |
| 74 } | |
| 75 | |
| 76 void Navigation::BindViewFactoryRequest( | |
| 77 const service_manager::BindSourceInfo& source_info, | |
| 78 mojom::ViewFactoryRequest request) { | |
| 79 bindings_.AddBinding(this, std::move(request)); | |
| 80 refs_.insert(ref_factory_.CreateRef()); | |
| 81 } | |
| 82 | |
| 83 void Navigation::ViewFactoryLost() { | |
| 84 refs_.erase(refs_.begin()); | |
| 85 } | |
| 86 | |
| 87 } // navigation | |
| OLD | NEW |