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

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

Issue 8857004: Delete UDPClientSocket on same thread as creation. Also refactor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge to fix hunk failure. 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/api/socket/socket_api.h" 5 #include "chrome/browser/extensions/api/socket/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/api/socket/socket_api_controller.h" 9 #include "chrome/browser/extensions/api/socket/socket_api_controller.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
12 14
13 using content::BrowserThread; 15 using content::BrowserThread;
14 16
15 namespace extensions { 17 namespace extensions {
16 18
17 const char kBytesWrittenKey[] = "bytesWritten"; 19 const char kBytesWrittenKey[] = "bytesWritten";
18 const char kSocketIdKey[] = "socketId"; 20 const char kSocketIdKey[] = "socketId";
19 const char kUDPSocketType[] = "udp"; 21 const char kUDPSocketType[] = "udp";
20 22
21 SocketCreateFunction::SocketCreateFunction() { 23 SocketController* SocketApiFunction::controller() {
24 return profile()->GetExtensionService()->socket_controller();
22 } 25 }
23 26
24 SocketCreateFunction::~SocketCreateFunction() { 27 bool SocketApiFunction::RunImpl() {
28 if (!Prepare()) {
29 return false;
30 }
31 bool rv = BrowserThread::PostTask(
32 BrowserThread::IO, FROM_HERE,
33 base::Bind(&SocketApiFunction::WorkOnIOThread, this));
34 DCHECK(rv);
35 return true;
25 } 36 }
26 37
27 bool SocketCreateFunction::RunImpl() { 38 void SocketApiFunction::WorkOnIOThread() {
39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
40 Work();
41 bool rv = BrowserThread::PostTask(
42 BrowserThread::UI, FROM_HERE,
43 base::Bind(&SocketApiFunction::RespondOnUIThread, this));
44 DCHECK(rv);
45 }
46
47 void SocketApiFunction::RespondOnUIThread() {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
49 SendResponse(Respond());
50 }
51
52 bool SocketCreateFunction::Prepare() {
28 std::string socket_type; 53 std::string socket_type;
29 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type)); 54 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &socket_type));
30 55
31 // TODO(miket): this constitutes a second form of truth as to the enum 56 // 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 57 // 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 58 // 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 59 // value-checking. Too bad. Fix this if/when the argument validation code is
35 // moved to C++ rather than its current JavaScript form. 60 // moved to C++ rather than its current JavaScript form.
36 if (socket_type != kUDPSocketType) { 61 if (socket_type != kUDPSocketType) {
37 return false; 62 return false;
38 } 63 }
39
40 bool rv = BrowserThread::PostTask(
41 BrowserThread::IO, FROM_HERE,
42 base::Bind(&SocketCreateFunction::WorkOnIOThread, this));
43 DCHECK(rv);
44 return true; 64 return true;
45 } 65 }
46 66
47 void SocketCreateFunction::WorkOnIOThread() { 67 void SocketCreateFunction::Work() {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
49 DictionaryValue* result = new DictionaryValue(); 68 DictionaryValue* result = new DictionaryValue();
50 SocketController* controller = SocketController::GetInstance(); 69 int socket_id = controller()->CreateUdp(profile(), extension_id(),
51 70 source_url());
52 int socket_id = controller->CreateUdp(profile(), extension_id(),
53 source_url());
54 result->SetInteger(kSocketIdKey, socket_id); 71 result->SetInteger(kSocketIdKey, socket_id);
55 result_.reset(result); 72 result_.reset(result);
56 73
57 bool rv = BrowserThread::PostTask(
58 BrowserThread::UI, FROM_HERE,
59 base::Bind(&SocketCreateFunction::RespondOnUIThread, this));
60 DCHECK(rv);
61 } 74 }
62 75
63 void SocketCreateFunction::RespondOnUIThread() { 76 bool SocketCreateFunction::Respond() {
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; 77 return true;
82 } 78 }
83 79
84 void SocketDestroyFunction::WorkOnIOThread() { 80 bool SocketDestroyFunction::Prepare() {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 81 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
86 SocketController* controller = SocketController::GetInstance(); 82 return true;
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 } 83 }
94 84
95 void SocketDestroyFunction::RespondOnUIThread() { 85 void SocketDestroyFunction::Work() {
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 86 controller()->DestroyUdp(socket_id_);
97 SendResponse(true);
98 } 87 }
99 88
100 SocketConnectFunction::SocketConnectFunction() { 89 bool SocketDestroyFunction::Respond() {
90 return true;
101 } 91 }
102 92
103 SocketConnectFunction::~SocketConnectFunction() { 93 bool SocketConnectFunction::Prepare() {
104 }
105
106 bool SocketConnectFunction::RunImpl() {
107 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_)); 94 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
108 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_)); 95 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &address_));
109 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(2, &port_)); 96 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; 97 return true;
116 } 98 }
117 99
118 void SocketConnectFunction::WorkOnIOThread() { 100 void SocketConnectFunction::Work() {
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 101 bool result = controller()->ConnectUdp(socket_id_, address_, port_);
120
121 SocketController* controller = SocketController::GetInstance();
122 bool result = controller->ConnectUdp(socket_id_, address_, port_);
123 result_.reset(Value::CreateBooleanValue(result)); 102 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 } 103 }
130 104
131 void SocketConnectFunction::RespondOnUIThread() { 105 bool SocketConnectFunction::Respond() {
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; 106 return true;
150 } 107 }
151 108
152 void SocketCloseFunction::WorkOnIOThread() { 109 bool SocketCloseFunction::Prepare() {
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_)); 110 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; 111 return true;
185 } 112 }
186 113
187 void SocketWriteFunction::WorkOnIOThread() { 114 void SocketCloseFunction::Work() {
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 115 controller()->CloseUdp(socket_id_);
116 result_.reset(Value::CreateNullValue());
117 }
189 118
190 SocketController* controller = SocketController::GetInstance(); 119 bool SocketCloseFunction::Respond() {
191 int bytesWritten = controller->WriteUdp(socket_id_, message_); 120 return true;
121 }
122
123 bool SocketWriteFunction::Prepare() {
124 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &socket_id_));
125 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &message_));
126 return true;
127 }
128
129 void SocketWriteFunction::Work() {
130 int bytesWritten = controller()->WriteUdp(socket_id_, message_);
192 131
193 DictionaryValue* result = new DictionaryValue(); 132 DictionaryValue* result = new DictionaryValue();
194 result->SetInteger(kBytesWrittenKey, bytesWritten); 133 result->SetInteger(kBytesWrittenKey, bytesWritten);
195 result_.reset(result); 134 result_.reset(result);
196 bool rv = BrowserThread::PostTask(
197 BrowserThread::UI, FROM_HERE,
198 base::Bind(&SocketWriteFunction::RespondOnUIThread, this));
199 DCHECK(rv);
200 } 135 }
201 136
202 void SocketWriteFunction::RespondOnUIThread() { 137 bool SocketWriteFunction::Respond() {
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 138 return true;
204 SendResponse(true);
205 } 139 }
206 140
207 } // namespace extensions 141 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/socket/socket_api.h ('k') | chrome/browser/extensions/api/socket/socket_api_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698