1 // Copyright 2013 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.
5 #include "chrome/browser/extensions/api/cast_channel/cast_channel_api.h"
7 #include "base/json/json_writer.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h"
12 #include "chrome/browser/extensions/event_router.h"
13 #include "chrome/browser/extensions/extension_system.h"
14 #include "chrome/browser/net/chrome_net_log.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "net/base/net_errors.h"
20 namespace extensions {
22 namespace Close = cast_channel::Close;
23 namespace OnError = cast_channel::OnError;
24 namespace OnMessage = cast_channel::OnMessage;
25 namespace Open = cast_channel::Open;
26 namespace Send = cast_channel::Send;
27 using cast_channel::CastSocket;
28 using cast_channel::ChannelError;
29 using cast_channel::ChannelInfo;
30 using cast_channel::MessageInfo;
31 using cast_channel::ReadyState;
32 using content::BrowserThread;
36 // T is an extension dictionary (MessageInfo or ChannelInfo)
38 std::string ParamToString(const T& info) {
39 scoped_ptr<base::DictionaryValue> dict = info.ToValue();
41 base::JSONWriter::Write(dict.get(), &out);
47 CastChannelAPI::CastChannelAPI(Profile* profile)
53 CastChannelAPI* CastChannelAPI::Get(Profile* profile) {
54 return ProfileKeyedAPIFactory<CastChannelAPI>::GetForProfile(profile);
57 static base::LazyInstance<ProfileKeyedAPIFactory<CastChannelAPI> > g_factory =
58 LAZY_INSTANCE_INITIALIZER;
61 ProfileKeyedAPIFactory<CastChannelAPI>* CastChannelAPI::GetFactoryInstance() {
62 return &g_factory.Get();
65 scoped_ptr<CastSocket> CastChannelAPI::CreateCastSocket(
66 const std::string& extension_id, const GURL& url) {
67 if (socket_for_test_.get()) {
68 return socket_for_test_.Pass();
70 return scoped_ptr<CastSocket>(
71 new CastSocket(extension_id, url, this,
72 g_browser_process->net_log()));
76 void CastChannelAPI::SetSocketForTest(scoped_ptr<CastSocket> socket_for_test) {
77 socket_for_test_ = socket_for_test.Pass();
80 void CastChannelAPI::OnError(const CastSocket* socket,
81 cast_channel::ChannelError error) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
83 ChannelInfo channel_info;
84 socket->FillChannelInfo(&channel_info);
85 channel_info.error_state = error;
86 scoped_ptr<base::ListValue> results = OnError::Create(channel_info);
87 scoped_ptr<Event> event(new Event(OnError::kEventName, results.Pass()));
88 extensions::ExtensionSystem::Get(profile_)->event_router()->
89 DispatchEventToExtension(socket->owner_extension_id(), event.Pass());
91 // Destroy the socket that caused the error.
92 ApiResourceManager<CastSocket>* manager =
93 ApiResourceManager<CastSocket>::Get(profile_);
94 manager->Remove(socket->owner_extension_id(), socket->id());
97 void CastChannelAPI::OnMessage(const CastSocket* socket,
98 const MessageInfo& message_info) {
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
100 ChannelInfo channel_info;
101 socket->FillChannelInfo(&channel_info);
102 scoped_ptr<base::ListValue> results =
103 OnMessage::Create(channel_info, message_info);
104 DVLOG(1) << "Sending message " << ParamToString(message_info)
105 << " to channel " << ParamToString(channel_info);
106 scoped_ptr<Event> event(new Event(OnMessage::kEventName, results.Pass()));
107 extensions::ExtensionSystem::Get(profile_)->event_router()->
108 DispatchEventToExtension(socket->owner_extension_id(), event.Pass());
111 CastChannelAPI::~CastChannelAPI() {}
113 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction()
114 : manager_(NULL), error_(cast_channel::CHANNEL_ERROR_NONE) { }
116 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { }
118 bool CastChannelAsyncApiFunction::PrePrepare() {
119 manager_ = ApiResourceManager<CastSocket>::Get(GetProfile());
123 bool CastChannelAsyncApiFunction::Respond() {
124 return error_ != cast_channel::CHANNEL_ERROR_NONE;
127 CastSocket* CastChannelAsyncApiFunction::GetSocketOrCompleteWithError(
129 CastSocket* socket = GetSocket(channel_id);
131 SetResultFromError(cast_channel::CHANNEL_ERROR_INVALID_CHANNEL_ID);
132 AsyncWorkCompleted();
137 int CastChannelAsyncApiFunction::AddSocket(CastSocket* socket) {
138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
141 const int id = manager_->Add(socket);
146 void CastChannelAsyncApiFunction::RemoveSocket(int channel_id) {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
149 manager_->Remove(extension_->id(), channel_id);
152 void CastChannelAsyncApiFunction::SetResultFromSocket(int channel_id) {
153 CastSocket* socket = GetSocket(channel_id);
155 ChannelInfo channel_info;
156 socket->FillChannelInfo(&channel_info);
157 error_ = socket->error_state();
158 SetResultFromChannelInfo(channel_info);
161 void CastChannelAsyncApiFunction::SetResultFromError(ChannelError error) {
162 ChannelInfo channel_info;
163 channel_info.channel_id = -1;
164 channel_info.url = "";
165 channel_info.ready_state = cast_channel::READY_STATE_CLOSED;
166 channel_info.error_state = error;
167 SetResultFromChannelInfo(channel_info);
171 CastSocket* CastChannelAsyncApiFunction::GetSocket(int channel_id) {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
174 return manager_->Get(extension_->id(), channel_id);
177 void CastChannelAsyncApiFunction::SetResultFromChannelInfo(
178 const ChannelInfo& channel_info) {
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
180 SetResult(channel_info.ToValue().release());
183 CastChannelOpenFunction::CastChannelOpenFunction()
184 : new_channel_id_(0) { }
186 CastChannelOpenFunction::~CastChannelOpenFunction() { }
188 bool CastChannelOpenFunction::PrePrepare() {
189 api_ = CastChannelAPI::Get(GetProfile());
190 return CastChannelAsyncApiFunction::PrePrepare();
193 bool CastChannelOpenFunction::Prepare() {
194 params_ = Open::Params::Create(*args_);
195 EXTENSION_FUNCTION_VALIDATE(params_.get());
199 void CastChannelOpenFunction::AsyncWorkStart() {
201 scoped_ptr<CastSocket> socket = api_->CreateCastSocket(extension_->id(),
203 new_channel_id_ = AddSocket(socket.release());
204 GetSocket(new_channel_id_)->Connect(
205 base::Bind(&CastChannelOpenFunction::OnOpen, this));
208 void CastChannelOpenFunction::OnOpen(int result) {
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
210 SetResultFromSocket(new_channel_id_);
211 AsyncWorkCompleted();
214 CastChannelSendFunction::CastChannelSendFunction() { }
216 CastChannelSendFunction::~CastChannelSendFunction() { }
218 bool CastChannelSendFunction::Prepare() {
219 params_ = Send::Params::Create(*args_);
220 EXTENSION_FUNCTION_VALIDATE(params_.get());
224 void CastChannelSendFunction::AsyncWorkStart() {
225 CastSocket* socket = GetSocketOrCompleteWithError(
226 params_->channel.channel_id);
228 socket->SendMessage(params_->message,
229 base::Bind(&CastChannelSendFunction::OnSend, this));
232 void CastChannelSendFunction::OnSend(int result) {
233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
235 SetResultFromError(cast_channel::CHANNEL_ERROR_SOCKET_ERROR);
237 SetResultFromSocket(params_->channel.channel_id);
239 AsyncWorkCompleted();
242 CastChannelCloseFunction::CastChannelCloseFunction() { }
244 CastChannelCloseFunction::~CastChannelCloseFunction() { }
246 bool CastChannelCloseFunction::Prepare() {
247 params_ = Close::Params::Create(*args_);
248 EXTENSION_FUNCTION_VALIDATE(params_.get());
252 void CastChannelCloseFunction::AsyncWorkStart() {
253 CastSocket* socket = GetSocketOrCompleteWithError(
254 params_->channel.channel_id);
256 socket->Close(base::Bind(&CastChannelCloseFunction::OnClose, this));
259 void CastChannelCloseFunction::OnClose(int result) {
260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
261 DVLOG(1) << "CastChannelCloseFunction::OnClose result = " << result;
263 SetResultFromError(cast_channel::CHANNEL_ERROR_SOCKET_ERROR);
265 int channel_id = params_->channel.channel_id;
266 SetResultFromSocket(channel_id);
267 RemoveSocket(channel_id);
269 AsyncWorkCompleted();
272 } // namespace extensions