summaryrefslogtreecommitdiffstats
path: root/mojo/spy/spy_server_impl.cc
blob: 7fbce519280499594e65c3645084d040295db56f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/spy/spy_server_impl.h"

#include "mojo/public/cpp/bindings/allocation_scope.h"
#include "mojo/public/cpp/system/core.h"

namespace {

bool NextId(uint32_t* out_id) {
  static uint32_t id = 1;
  if (!++id)
    return false;
  *out_id = id;
  return true;
}

}  // namespace

namespace mojo {

struct SpyServerImpl::Item {
  enum Type {
    kServiceIntercept,
    kMessage
  };

  uint32_t id;
  Type type;

  Item(uint32_t id, Type type) : id(id), type(type) {}
};

SpyServerImpl::SpyServerImpl() : has_session_(false) {
  BindToPipe(this, pipe_.handle0.Pass());
}

SpyServerImpl::~SpyServerImpl() {
}

void SpyServerImpl::StartSession(
    const spy_api::Version& version,
    const mojo::Callback<void(spy_api::Result, mojo::String)>& callback) {
  AllocationScope scope;
  if (has_session_) {
    callback.Run(spy_api::RESOURCE_LIMIT, "");
    return;
  }
  callback.Run(spy_api::ALL_OK, "session 0");
  has_session_ = true;
}

void SpyServerImpl::StopSession(
  const mojo::Callback<void(spy_api::Result)>& callback) {
  AllocationScope scope;
  if (!has_session_) {
    callback.Run(spy_api::INVALID_CALL);
    return;
  }
  callback.Run(spy_api::ALL_OK);
  has_session_ = false;
}

void SpyServerImpl::TrackConnection(
  uint32_t id,
  spy_api::ConnectionOptions options,
  const mojo::Callback<void(spy_api::Result)>& callback) {
}

void SpyServerImpl::OnConnectionError() {
  // Pipe got disconnected.
}

void SpyServerImpl::OnIntercept(const GURL& url) {
  if (!has_session_)
    return;

  AllocationScope scope;
  uint32_t id;
  if (!NextId(&id)) {
    client()->OnFatalError(spy_api::NO_MORE_IDS);
    return;
  }

  items_[id] = new Item(id, Item::kServiceIntercept);
  client()->OnClientConnection(
      url.possibly_invalid_spec(), id, spy_api::PEEK_MESSAGES);
}

ScopedMessagePipeHandle SpyServerImpl::ServerPipe() {
  return ScopedMessagePipeHandle(pipe_.handle1.Pass()).Pass();
}

}  // namespace mojo