summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/socket/socket_event_notifier.cc
blob: 9018674f12cd3737857005d52e6ac6eb505acc8e (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
97
98
99
100
101
102
103
// Copyright (c) 2012 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 "chrome/browser/extensions/api/socket/socket_event_notifier.h"

#include "base/json/json_writer.h"
#include "chrome/browser/extensions/extension_event_router.h"
#include "chrome/browser/profiles/profile.h"

namespace events {
const char kOnSocketEvent[] = "experimental.socket.onEvent";
};

namespace extensions {

const char kEventTypeKey[] = "type";
const char kEventTypeReadComplete[] = "readComplete";
const char kEventTypeWriteComplete[] = "writeComplete";

const char kSrcIdKey[] = "srcId";
const char kIsFinalEventKey[] = "isFinalEvent";

const char kResultCodeKey[] = "resultCode";
const char kMessageKey[] = "message";

SocketEventNotifier::SocketEventNotifier(ExtensionEventRouter* router,
                                         Profile* profile,
                                         const std::string& src_extension_id,
                                         int src_id,
                                         const GURL& src_url)
    : router_(router),
      profile_(profile),
      src_extension_id_(src_extension_id),
      src_id_(src_id),
      src_url_(src_url) {}

SocketEventNotifier::~SocketEventNotifier() {}

void SocketEventNotifier::OnReadComplete(int result_code,
                                         const std::string& message) {
  // Do we have a destination for this event? There will be one if a source id
  // was injected by the request handler for socket.create in
  // schema_generated_bindings.js, which will in turn be the case if the caller
  // of socket.create provided an onEvent closure.
  if (src_id_ < 0)
    return;

  DictionaryValue* event = CreateSocketEvent(SOCKET_EVENT_READ_COMPLETE);
  event->SetInteger(kResultCodeKey, result_code);
  event->SetString(kMessageKey, message);
  DispatchEvent(event);
}

void SocketEventNotifier::OnWriteComplete(int result_code) {
  if (src_id_ < 0)
    return;

  DictionaryValue* event = CreateSocketEvent(SOCKET_EVENT_WRITE_COMPLETE);
  event->SetInteger(kResultCodeKey, result_code);
  DispatchEvent(event);
}

void SocketEventNotifier::DispatchEvent(DictionaryValue* event) {
  ListValue args;

  args.Set(0, event);
  std::string json_args;
  base::JSONWriter::Write(&args, false, &json_args);
  router_->DispatchEventToExtension(src_extension_id_, events::kOnSocketEvent,
                                    json_args, profile_, src_url_);
}

DictionaryValue* SocketEventNotifier::CreateSocketEvent(
    SocketEventType event_type) {
  DictionaryValue* event = new DictionaryValue();
  event->SetString(kEventTypeKey, SocketEventTypeToString(event_type));
  event->SetInteger(kSrcIdKey, src_id_);

  // TODO(miket): Signal that it's OK to clean up onEvent listeners. This is
  // the framework we'll use, but we need to start using it.
  event->SetBoolean(kIsFinalEventKey, false);

  // The caller owns the created event, which typically is then given to a
  // ListValue to dispose of.
  return event;
}

// static
std::string SocketEventNotifier::SocketEventTypeToString(
    SocketEventType event_type) {
  switch (event_type) {
    case SOCKET_EVENT_READ_COMPLETE:
      return kEventTypeReadComplete;
    case SOCKET_EVENT_WRITE_COMPLETE:
      return kEventTypeWriteComplete;
  }

  NOTREACHED();
  return std::string();
}

}  // namespace extensions