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

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

Issue 8743017: Real (but naive) UDP socket sending. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Initial. 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/socket_api.h" 5 #include "chrome/browser/extensions/socket_api.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/browser/extensions/socket_api_constants.h"
Mihai Parparita -not on Chrome 2011/12/01 23:39:32 Per Aaron separate constants.h/.cc files are no lo
10 #include "chrome/browser/extensions/socket_api_controller.h"
11 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
10 13
11 using content::BrowserThread; 14 using content::BrowserThread;
12 15
13 namespace extensions { 16 namespace extensions {
14 17
15 SocketCreateFunction::SocketCreateFunction() { 18 namespace constants = socket_api_constants;
16 }
17
18 SocketCreateFunction::~SocketCreateFunction() {
19 }
20 19
21 bool SocketCreateFunction::RunImpl() { 20 bool SocketCreateFunction::RunImpl() {
22 std::string socket_type; 21 std::string socket_type;
23 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type)); 22 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type));
24 23
25 // TODO(miket): this constitutes a second form of truth as to the 24 // TODO(miket): this constitutes a second form of truth as to the enum
26 // enum validity. But our unit-test framework skips the enum 25 // validity. But our unit-test framework skips the enum validation. So in
27 // validation. So in order to get an invalid-enum test to pass, we 26 // order to get an invalid-enum test to pass, we need duplicative
28 // need duplicative value-checking. Too bad. Fix this if/when the 27 // value-checking. Too bad. Fix this if/when the argument validation code is
29 // argument validation code is moved to C++ rather than its current 28 // moved to C++ rather than its current JavaScript form.
30 // JavaScript form. 29 if (socket_type != constants::kUdpSocketType) {
Mihai Parparita -not on Chrome 2011/12/01 23:39:32 Per Aaron's feedback in http://codereview.chromium
miket_OOO 2011/12/02 21:06:36 OK. I must have picked the wrong examples in the s
31 if (socket_type != "udp") {
32 return false; 30 return false;
33 } 31 }
34 32
35 bool rv = BrowserThread::PostTask( 33 bool rv = BrowserThread::PostTask(
36 BrowserThread::IO, FROM_HERE, 34 BrowserThread::IO, FROM_HERE,
37 base::Bind(&SocketCreateFunction::WorkOnIOThread, this)); 35 base::Bind(&SocketCreateFunction::WorkOnIOThread, this));
38 DCHECK(rv); 36 DCHECK(rv);
39 return true; 37 return true;
40 } 38 }
41 39
42 void SocketCreateFunction::WorkOnIOThread() { 40 void SocketCreateFunction::WorkOnIOThread() {
Mihai Parparita -not on Chrome 2011/12/01 23:39:32 Still too early to refactor this to avoid some of
miket_OOO 2011/12/02 21:06:36 Yes. But don't worry, it'll be done.
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
44 DictionaryValue* result = new DictionaryValue(); 42 DictionaryValue* result = new DictionaryValue();
45 result->SetInteger("socketId", 42); 43 SocketController* controller = SocketController::GetInstance();
44
45 int socket_id = controller->CreateUdp(profile(), extension_id(),
46 source_url());
47 result->SetInteger(constants::kSocketIdKey, socket_id);
46 result_.reset(result); 48 result_.reset(result);
49
47 bool rv = BrowserThread::PostTask( 50 bool rv = BrowserThread::PostTask(
48 BrowserThread::UI, FROM_HERE, 51 BrowserThread::UI, FROM_HERE,
49 base::Bind(&SocketCreateFunction::RespondOnUIThread, this)); 52 base::Bind(&SocketCreateFunction::RespondOnUIThread, this));
50 DCHECK(rv); 53 DCHECK(rv);
51 } 54 }
52 55
53 void SocketCreateFunction::RespondOnUIThread() { 56 void SocketCreateFunction::RespondOnUIThread() {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
55 SendResponse(true); 58 SendResponse(true);
56 } 59 }
57 60
58 SocketConnectFunction::SocketConnectFunction() { 61 bool SocketDestroyFunction::RunImpl() {
62 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
63
64 bool rv = BrowserThread::PostTask(
65 BrowserThread::IO, FROM_HERE,
66 base::Bind(&SocketDestroyFunction::WorkOnIOThread, this));
67 DCHECK(rv);
68 return true;
59 } 69 }
60 70
61 SocketConnectFunction::~SocketConnectFunction() { 71 void SocketDestroyFunction::WorkOnIOThread() {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
73 SocketController* controller = SocketController::GetInstance();
74 controller->DestroyUdp(socket_id_);
75
76 bool rv = BrowserThread::PostTask(
77 BrowserThread::UI, FROM_HERE,
78 base::Bind(&SocketDestroyFunction::RespondOnUIThread, this));
79 DCHECK(rv);
80 }
81
82 void SocketDestroyFunction::RespondOnUIThread() {
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
84 SendResponse(true);
62 } 85 }
63 86
64 bool SocketConnectFunction::RunImpl() { 87 bool SocketConnectFunction::RunImpl() {
65 std::string socket_type; 88 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
66 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type)); 89 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_));
90 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_));
67 91
68 bool rv = BrowserThread::PostTask( 92 bool rv = BrowserThread::PostTask(
69 BrowserThread::IO, FROM_HERE, 93 BrowserThread::IO, FROM_HERE,
70 base::Bind(&SocketConnectFunction::WorkOnIOThread, this)); 94 base::Bind(&SocketConnectFunction::WorkOnIOThread, this));
71 DCHECK(rv); 95 DCHECK(rv);
72 return true; 96 return true;
73 } 97 }
74 98
75 void SocketConnectFunction::WorkOnIOThread() { 99 void SocketConnectFunction::WorkOnIOThread() {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
77 result_.reset(Value::CreateIntegerValue(4)); 101
102 SocketController* controller = SocketController::GetInstance();
103 bool result = controller->ConnectUdp(socket_id_, address_, port_);
104 result_.reset(Value::CreateBooleanValue(result));
105
78 bool rv = BrowserThread::PostTask( 106 bool rv = BrowserThread::PostTask(
79 BrowserThread::UI, FROM_HERE, 107 BrowserThread::UI, FROM_HERE,
80 base::Bind(&SocketConnectFunction::RespondOnUIThread, this)); 108 base::Bind(&SocketConnectFunction::RespondOnUIThread, this));
81 DCHECK(rv); 109 DCHECK(rv);
82 } 110 }
83 111
84 void SocketConnectFunction::RespondOnUIThread() { 112 void SocketConnectFunction::RespondOnUIThread() {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
86 SendResponse(true); 114 SendResponse(true);
87 } 115 }
88 116
89 SocketDisconnectFunction::SocketDisconnectFunction() { 117 bool SocketCloseFunction::RunImpl() {
90 } 118 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
91
92 SocketDisconnectFunction::~SocketDisconnectFunction() {
93 }
94
95 bool SocketDisconnectFunction::RunImpl() {
96 std::string socket_type;
97 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type));
98 119
99 bool rv = BrowserThread::PostTask( 120 bool rv = BrowserThread::PostTask(
100 BrowserThread::IO, FROM_HERE, 121 BrowserThread::IO, FROM_HERE,
101 base::Bind(&SocketDisconnectFunction::WorkOnIOThread, this)); 122 base::Bind(&SocketCloseFunction::WorkOnIOThread, this));
102 DCHECK(rv); 123 DCHECK(rv);
103 return true; 124 return true;
104 } 125 }
105 126
106 void SocketDisconnectFunction::WorkOnIOThread() { 127 void SocketCloseFunction::WorkOnIOThread() {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
108 result_.reset(Value::CreateIntegerValue(4)); 129
130 SocketController* controller = SocketController::GetInstance();
131 controller->CloseUdp(socket_id_);
132 result_.reset(Value::CreateNullValue());
133
109 bool rv = BrowserThread::PostTask( 134 bool rv = BrowserThread::PostTask(
110 BrowserThread::UI, FROM_HERE, 135 BrowserThread::UI, FROM_HERE,
111 base::Bind(&SocketDisconnectFunction::RespondOnUIThread, this)); 136 base::Bind(&SocketCloseFunction::RespondOnUIThread, this));
112 DCHECK(rv); 137 DCHECK(rv);
113 } 138 }
114 139
115 void SocketDisconnectFunction::RespondOnUIThread() { 140 void SocketCloseFunction::RespondOnUIThread() {
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
117 SendResponse(true); 142 SendResponse(true);
118 } 143 }
119 144
120 SocketSendFunction::SocketSendFunction() { 145 bool SocketWriteFunction::RunImpl() {
121 } 146 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
122 147 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &message_));
123 SocketSendFunction::~SocketSendFunction() {
124 }
125
126 bool SocketSendFunction::RunImpl() {
127 std::string socket_type;
128 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type));
129 148
130 bool rv = BrowserThread::PostTask( 149 bool rv = BrowserThread::PostTask(
131 BrowserThread::IO, FROM_HERE, 150 BrowserThread::IO, FROM_HERE,
132 base::Bind(&SocketSendFunction::WorkOnIOThread, this)); 151 base::Bind(&SocketWriteFunction::WorkOnIOThread, this));
133 DCHECK(rv); 152 DCHECK(rv);
134 return true; 153 return true;
135 } 154 }
136 155
137 void SocketSendFunction::WorkOnIOThread() { 156 void SocketWriteFunction::WorkOnIOThread() {
138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
139 result_.reset(Value::CreateIntegerValue(4)); 158
159 SocketController* controller = SocketController::GetInstance();
160 int bytesWritten = controller->WriteUdp(socket_id_, message_);
161
162 DictionaryValue* result = new DictionaryValue();
163 result->SetInteger(constants::kBytesWrittenKey, bytesWritten);
164 result_.reset(result);
140 bool rv = BrowserThread::PostTask( 165 bool rv = BrowserThread::PostTask(
141 BrowserThread::UI, FROM_HERE, 166 BrowserThread::UI, FROM_HERE,
142 base::Bind(&SocketSendFunction::RespondOnUIThread, this)); 167 base::Bind(&SocketWriteFunction::RespondOnUIThread, this));
143 DCHECK(rv); 168 DCHECK(rv);
144 } 169 }
145 170
146 void SocketSendFunction::RespondOnUIThread() { 171 void SocketWriteFunction::RespondOnUIThread() {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
148 SendResponse(true); 173 SendResponse(true);
149 } 174 }
150 175
151 } // namespace extensions 176 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698