Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: chrome/browser/extensions/api/socket/socket_api.cc

Issue 8743017: Real (but naive) UDP socket sending. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Post-try-server fixes. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "chrome/browser/extensions/api/socket/socket_api.h"
6
7 #include "base/bind.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/api/socket/socket_api_controller.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/browser_thread.h"
12
13 using content::BrowserThread;
14
15 namespace extensions {
16
17 const char kBytesWrittenKey[] = "bytesWritten";
18 const char kSocketIdKey[] = "socketId";
19 const char kUDPSocketType[] = "udp";
20
21 SocketCreateFunction::SocketCreateFunction() {
22 }
23
24 SocketCreateFunction::~SocketCreateFunction() {
25 }
26
27 bool SocketCreateFunction::RunImpl() {
28 std::string socket_type;
29 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type));
30
31 // TODO(miket): this constitutes a second form of truth as to the enum
32 // validity. But our unit-test framework skips the enum validation. So in
33 // order to get an invalid-enum test to pass, we need duplicative
34 // value-checking. Too bad. Fix this if/when the argument validation code is
35 // moved to C++ rather than its current JavaScript form.
36 if (socket_type != kUDPSocketType) {
37 return false;
38 }
39
40 bool rv = BrowserThread::PostTask(
41 BrowserThread::IO, FROM_HERE,
42 base::Bind(&SocketCreateFunction::WorkOnIOThread, this));
43 DCHECK(rv);
44 return true;
45 }
46
47 void SocketCreateFunction::WorkOnIOThread() {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
49 DictionaryValue* result = new DictionaryValue();
50 SocketController* controller = SocketController::GetInstance();
51
52 int socket_id = controller->CreateUdp(profile(), extension_id(),
53 source_url());
54 result->SetInteger(kSocketIdKey, socket_id);
55 result_.reset(result);
56
57 bool rv = BrowserThread::PostTask(
58 BrowserThread::UI, FROM_HERE,
59 base::Bind(&SocketCreateFunction::RespondOnUIThread, this));
60 DCHECK(rv);
61 }
62
63 void SocketCreateFunction::RespondOnUIThread() {
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
65 SendResponse(true);
66 }
67
68 SocketDestroyFunction::SocketDestroyFunction() {
69 }
70
71 SocketDestroyFunction::~SocketDestroyFunction() {
72 }
73
74 bool SocketDestroyFunction::RunImpl() {
75 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
76
77 bool rv = BrowserThread::PostTask(
78 BrowserThread::IO, FROM_HERE,
79 base::Bind(&SocketDestroyFunction::WorkOnIOThread, this));
80 DCHECK(rv);
81 return true;
82 }
83
84 void SocketDestroyFunction::WorkOnIOThread() {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
86 SocketController* controller = SocketController::GetInstance();
87 controller->DestroyUdp(socket_id_);
88
89 bool rv = BrowserThread::PostTask(
90 BrowserThread::UI, FROM_HERE,
91 base::Bind(&SocketDestroyFunction::RespondOnUIThread, this));
92 DCHECK(rv);
93 }
94
95 void SocketDestroyFunction::RespondOnUIThread() {
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
97 SendResponse(true);
98 }
99
100 SocketConnectFunction::SocketConnectFunction() {
101 }
102
103 SocketConnectFunction::~SocketConnectFunction() {
104 }
105
106 bool SocketConnectFunction::RunImpl() {
107 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
108 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_));
109 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_));
110
111 bool rv = BrowserThread::PostTask(
112 BrowserThread::IO, FROM_HERE,
113 base::Bind(&SocketConnectFunction::WorkOnIOThread, this));
114 DCHECK(rv);
115 return true;
116 }
117
118 void SocketConnectFunction::WorkOnIOThread() {
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
120
121 SocketController* controller = SocketController::GetInstance();
122 bool result = controller->ConnectUdp(socket_id_, address_, port_);
123 result_.reset(Value::CreateBooleanValue(result));
124
125 bool rv = BrowserThread::PostTask(
126 BrowserThread::UI, FROM_HERE,
127 base::Bind(&SocketConnectFunction::RespondOnUIThread, this));
128 DCHECK(rv);
129 }
130
131 void SocketConnectFunction::RespondOnUIThread() {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
133 SendResponse(true);
134 }
135
136 SocketCloseFunction::SocketCloseFunction() {
137 }
138
139 SocketCloseFunction::~SocketCloseFunction() {
140 }
141
142 bool SocketCloseFunction::RunImpl() {
143 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
144
145 bool rv = BrowserThread::PostTask(
146 BrowserThread::IO, FROM_HERE,
147 base::Bind(&SocketCloseFunction::WorkOnIOThread, this));
148 DCHECK(rv);
149 return true;
150 }
151
152 void SocketCloseFunction::WorkOnIOThread() {
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
154
155 SocketController* controller = SocketController::GetInstance();
156 controller->CloseUdp(socket_id_);
157 result_.reset(Value::CreateNullValue());
158
159 bool rv = BrowserThread::PostTask(
160 BrowserThread::UI, FROM_HERE,
161 base::Bind(&SocketCloseFunction::RespondOnUIThread, this));
162 DCHECK(rv);
163 }
164
165 void SocketCloseFunction::RespondOnUIThread() {
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
167 SendResponse(true);
168 }
169
170 SocketWriteFunction::SocketWriteFunction() {
171 }
172
173 SocketWriteFunction::~SocketWriteFunction() {
174 }
175
176 bool SocketWriteFunction::RunImpl() {
177 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
178 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &message_));
179
180 bool rv = BrowserThread::PostTask(
181 BrowserThread::IO, FROM_HERE,
182 base::Bind(&SocketWriteFunction::WorkOnIOThread, this));
183 DCHECK(rv);
184 return true;
185 }
186
187 void SocketWriteFunction::WorkOnIOThread() {
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
189
190 SocketController* controller = SocketController::GetInstance();
191 int bytesWritten = controller->WriteUdp(socket_id_, message_);
192
193 DictionaryValue* result = new DictionaryValue();
194 result->SetInteger(kBytesWrittenKey, bytesWritten);
195 result_.reset(result);
196 bool rv = BrowserThread::PostTask(
197 BrowserThread::UI, FROM_HERE,
198 base::Bind(&SocketWriteFunction::RespondOnUIThread, this));
199 DCHECK(rv);
200 }
201
202 void SocketWriteFunction::RespondOnUIThread() {
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
204 SendResponse(true);
205 }
206
207 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698