OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "mojo/spy/spy_server_impl.h" | |
6 | |
7 #include "mojo/public/cpp/bindings/allocation_scope.h" | |
8 #include "mojo/public/cpp/system/core.h" | |
9 | |
10 namespace { | |
11 | |
12 bool NextId(uint32_t* out_id) { | |
13 static uint32_t id = 1; | |
14 if (!++id) | |
15 return false; | |
16 *out_id = id; | |
17 return true; | |
18 } | |
19 | |
20 } // namespace | |
21 | |
22 namespace mojo { | |
23 | |
24 struct SpyServerImpl::Item { | |
25 enum Type { | |
26 kServiceIntercept, | |
27 kMessage | |
28 }; | |
29 | |
30 uint32_t id; | |
31 Type type; | |
32 | |
33 Item(uint32_t id, Type type) : id(id), type(type) {} | |
34 }; | |
35 | |
36 SpyServerImpl::SpyServerImpl() : has_session_(false) { | |
37 BindToPipe(this, pipe_.handle0.Pass()); | |
38 } | |
39 | |
40 SpyServerImpl::~SpyServerImpl() { | |
41 } | |
42 | |
43 void SpyServerImpl::StartSession( | |
44 const spy_api::Version& version, | |
45 const mojo::Callback<void(spy_api::Result, mojo::String)>& callback) { | |
46 AllocationScope scope; | |
47 if (has_session_) { | |
48 callback.Run(spy_api::RESOURCE_LIMIT, ""); | |
49 return; | |
50 } | |
51 callback.Run(spy_api::ALL_OK, "session 0"); | |
52 has_session_ = true; | |
53 } | |
54 | |
55 void SpyServerImpl::StopSession( | |
56 const mojo::Callback<void(spy_api::Result)>& callback) { | |
57 AllocationScope scope; | |
58 if (!has_session_) { | |
59 callback.Run(spy_api::INVALID_CALL); | |
60 return; | |
61 } | |
62 callback.Run(spy_api::ALL_OK); | |
63 has_session_ = false; | |
64 } | |
65 | |
66 void SpyServerImpl::TrackConnection( | |
67 uint32_t id, | |
68 spy_api::ConnectionOptions options, | |
69 const mojo::Callback<void(spy_api::Result)>& callback) { | |
70 } | |
71 | |
72 void SpyServerImpl::OnConnectionError() { | |
73 // Pipe got disconnected. | |
74 } | |
75 | |
76 void SpyServerImpl::OnIntercept(const GURL& url) { | |
77 if (!has_session_) | |
78 return; | |
79 | |
80 AllocationScope scope; | |
81 uint32_t id; | |
82 if (!NextId(&id)) { | |
83 client()->OnFatalError(spy_api::NO_MORE_IDS); | |
84 return; | |
85 } | |
86 | |
87 items_[id] = new Item(id, Item::kServiceIntercept); | |
88 client()->OnClientConnection( | |
89 url.possibly_invalid_spec(), id, spy_api::PEEK_MESSAGES); | |
90 } | |
91 | |
92 ScopedMessagePipeHandle SpyServerImpl::ServerPipe() { | |
93 return ScopedMessagePipeHandle(pipe_.handle1.Pass()).Pass(); | |
94 } | |
95 | |
96 } // namespace mojo | |
OLD | NEW |