Introduce new SPDY Version UMA histogram.
[chromium-blink-merge.git] / mojo / spy / spy_server_impl.cc
blob25e640ee8198b0ba3c69019d8b6a0ce0f4900c9d
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.
5 #include "mojo/spy/spy_server_impl.h"
7 #include "mojo/public/cpp/system/core.h"
9 namespace {
11 bool NextId(uint32_t* out_id) {
12 static uint32_t id = 1;
13 if (!++id)
14 return false;
15 *out_id = id;
16 return true;
19 } // namespace
21 namespace mojo {
23 struct SpyServerImpl::Item {
24 enum Type {
25 kServiceIntercept,
26 kMessage
29 uint32_t id;
30 Type type;
32 Item(uint32_t id, Type type) : id(id), type(type) {}
35 SpyServerImpl::SpyServerImpl() : has_session_(false) {
36 BindToPipe(this, pipe_.handle0.Pass());
39 SpyServerImpl::~SpyServerImpl() {
42 void SpyServerImpl::StartSession(
43 spy_api::VersionPtr version,
44 const mojo::Callback<void(spy_api::Result, mojo::String)>& callback) {
45 if (has_session_) {
46 callback.Run(spy_api::RESULT_RESOURCE_LIMIT, "");
47 return;
49 callback.Run(spy_api::RESULT_ALL_OK, "session 0");
50 has_session_ = true;
53 void SpyServerImpl::StopSession(
54 const mojo::Callback<void(spy_api::Result)>& callback) {
55 if (!has_session_) {
56 callback.Run(spy_api::RESULT_INVALID_CALL);
57 return;
59 callback.Run(spy_api::RESULT_ALL_OK);
60 has_session_ = false;
63 void SpyServerImpl::TrackConnection(
64 uint32_t id,
65 spy_api::ConnectionOptions options,
66 const mojo::Callback<void(spy_api::Result)>& callback) {
69 void SpyServerImpl::OnConnectionError() {
70 // Pipe got disconnected.
73 void SpyServerImpl::OnIntercept(const GURL& url) {
74 if (!has_session_)
75 return;
76 uint32_t id;
77 if (!NextId(&id)) {
78 client()->OnFatalError(spy_api::RESULT_NO_MORE_IDS);
79 return;
82 items_[id] = new Item(id, Item::kServiceIntercept);
83 client()->OnClientConnection(url.possibly_invalid_spec(),
84 id,
85 spy_api::CONNECTION_OPTIONS_PEEK_MESSAGES);
88 ScopedMessagePipeHandle SpyServerImpl::ServerPipe() {
89 return ScopedMessagePipeHandle(pipe_.handle1.Pass()).Pass();
92 } // namespace mojo