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 "services/navigation/navigation.h" | 5 #include "services/navigation/navigation.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/threading/thread_task_runner_handle.h" | |
10 #include "services/navigation/view_impl.h" | 9 #include "services/navigation/view_impl.h" |
11 #include "services/shell/public/cpp/connector.h" | |
12 | 10 |
13 namespace navigation { | 11 namespace navigation { |
14 | 12 |
15 namespace { | |
16 | |
17 void CreateViewOnViewTaskRunner( | |
18 std::unique_ptr<shell::Connector> connector, | |
19 const std::string& client_user_id, | |
20 mojom::ViewClientPtr client, | |
21 mojom::ViewRequest request, | |
22 std::unique_ptr<shell::ServiceContextRef> context_ref) { | |
23 // Owns itself. | |
24 new ViewImpl(std::move(connector), client_user_id, std::move(client), | |
25 std::move(request), std::move(context_ref)); | |
26 } | |
27 | |
28 } // namespace | |
29 | |
30 Navigation::Navigation() | 13 Navigation::Navigation() |
31 : view_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 14 : ref_factory_(base::MessageLoop::QuitWhenIdleClosure()) { |
32 ref_factory_(base::MessageLoop::QuitWhenIdleClosure()) { | |
33 bindings_.set_connection_error_handler( | 15 bindings_.set_connection_error_handler( |
34 base::Bind(&Navigation::ViewFactoryLost, base::Unretained(this))); | 16 base::Bind(&Navigation::ViewFactoryLost, base::Unretained(this))); |
35 } | 17 } |
36 Navigation::~Navigation() {} | 18 Navigation::~Navigation() {} |
37 | 19 |
38 bool Navigation::OnConnect(shell::Connection* connection, | 20 void Navigation::OnStart(shell::Connector* connector, |
39 shell::Connector* connector) { | 21 const shell::Identity& identity, |
| 22 uint32_t instance_id) { |
| 23 connector_ = connector; |
| 24 } |
| 25 |
| 26 bool Navigation::OnConnect(shell::Connection* connection) { |
40 std::string remote_user_id = connection->GetRemoteIdentity().user_id(); | 27 std::string remote_user_id = connection->GetRemoteIdentity().user_id(); |
41 if (!client_user_id_.empty() && client_user_id_ != remote_user_id) { | 28 if (!client_user_id_.empty() && client_user_id_ != remote_user_id) { |
42 LOG(ERROR) << "Must have a separate Navigation service instance for " | 29 LOG(ERROR) << "Must have a separate Navigation service instance for " |
43 << "different BrowserContexts."; | 30 << "different BrowserContexts."; |
44 return false; | 31 return false; |
45 } | 32 } |
46 client_user_id_ = remote_user_id; | 33 client_user_id_ = remote_user_id; |
47 | 34 |
48 connection->AddInterface<mojom::ViewFactory>(this); | 35 connection->AddInterface<mojom::ViewFactory>(this); |
49 return true; | 36 return true; |
50 } | 37 } |
51 | 38 |
52 void Navigation::Create(shell::Connection* connection, | 39 void Navigation::Create(shell::Connection* connection, |
53 mojom::ViewFactoryRequest request) { | 40 mojom::ViewFactoryRequest request) { |
54 bindings_.AddBinding(this, std::move(request)); | 41 bindings_.AddBinding(this, std::move(request)); |
55 refs_.insert(ref_factory_.CreateRef()); | 42 refs_.insert(ref_factory_.CreateRef()); |
56 } | 43 } |
57 | 44 |
58 void Navigation::CreateView(mojom::ViewClientPtr client, | 45 void Navigation::CreateView(mojom::ViewClientPtr client, |
59 mojom::ViewRequest request) { | 46 mojom::ViewRequest request) { |
60 std::unique_ptr<shell::Connector> new_connector = connector_->Clone(); | 47 new ViewImpl(connector_, client_user_id_, std::move(client), |
61 std::unique_ptr<shell::ServiceContextRef> context_ref = | 48 std::move(request), ref_factory_.CreateRef()); |
62 ref_factory_.CreateRef(); | |
63 view_task_runner_->PostTask( | |
64 FROM_HERE, | |
65 base::Bind(&CreateViewOnViewTaskRunner, base::Passed(&new_connector), | |
66 client_user_id_, base::Passed(&client), base::Passed(&request), | |
67 base::Passed(&context_ref))); | |
68 } | 49 } |
69 | 50 |
70 void Navigation::ViewFactoryLost() { | 51 void Navigation::ViewFactoryLost() { |
71 refs_.erase(refs_.begin()); | 52 refs_.erase(refs_.begin()); |
72 } | 53 } |
73 | 54 |
74 } // navigation | 55 } // navigation |
OLD | NEW |