| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/cast_channel/cast_channel_api.h" | 5 #include "chrome/browser/extensions/api/cast_channel/cast_channel_api.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" | 11 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" |
| 12 #include "chrome/browser/net/chrome_net_log.h" | 12 #include "chrome/browser/net/chrome_net_log.h" |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 15 #include "extensions/browser/event_router.h" | 14 #include "extensions/browser/event_router.h" |
| 16 #include "extensions/browser/extension_system.h" | 15 #include "extensions/browser/extension_system.h" |
| 17 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 18 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 19 | 18 |
| 20 namespace extensions { | 19 namespace extensions { |
| 21 | 20 |
| 22 namespace Close = cast_channel::Close; | 21 namespace Close = cast_channel::Close; |
| 23 namespace OnError = cast_channel::OnError; | 22 namespace OnError = cast_channel::OnError; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 37 template <class T> | 36 template <class T> |
| 38 std::string ParamToString(const T& info) { | 37 std::string ParamToString(const T& info) { |
| 39 scoped_ptr<base::DictionaryValue> dict = info.ToValue(); | 38 scoped_ptr<base::DictionaryValue> dict = info.ToValue(); |
| 40 std::string out; | 39 std::string out; |
| 41 base::JSONWriter::Write(dict.get(), &out); | 40 base::JSONWriter::Write(dict.get(), &out); |
| 42 return out; | 41 return out; |
| 43 } | 42 } |
| 44 | 43 |
| 45 } // namespace | 44 } // namespace |
| 46 | 45 |
| 47 CastChannelAPI::CastChannelAPI(Profile* profile) | 46 CastChannelAPI::CastChannelAPI(content::BrowserContext* context) |
| 48 : profile_(profile) { | 47 : context_(context) { |
| 49 DCHECK(profile_); | 48 DCHECK(context_); |
| 50 } | 49 } |
| 51 | 50 |
| 52 // static | 51 // static |
| 53 CastChannelAPI* CastChannelAPI::Get(Profile* profile) { | 52 CastChannelAPI* CastChannelAPI::Get(content::BrowserContext* context) { |
| 54 return ProfileKeyedAPIFactory<CastChannelAPI>::GetForProfile(profile); | 53 return ProfileKeyedAPIFactory<CastChannelAPI>::GetForProfile(context); |
| 55 } | 54 } |
| 56 | 55 |
| 57 static base::LazyInstance<ProfileKeyedAPIFactory<CastChannelAPI> > g_factory = | 56 static base::LazyInstance<ProfileKeyedAPIFactory<CastChannelAPI> > g_factory = |
| 58 LAZY_INSTANCE_INITIALIZER; | 57 LAZY_INSTANCE_INITIALIZER; |
| 59 | 58 |
| 60 // static | 59 // static |
| 61 ProfileKeyedAPIFactory<CastChannelAPI>* CastChannelAPI::GetFactoryInstance() { | 60 ProfileKeyedAPIFactory<CastChannelAPI>* CastChannelAPI::GetFactoryInstance() { |
| 62 return g_factory.Pointer(); | 61 return g_factory.Pointer(); |
| 63 } | 62 } |
| 64 | 63 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 78 } | 77 } |
| 79 | 78 |
| 80 void CastChannelAPI::OnError(const CastSocket* socket, | 79 void CastChannelAPI::OnError(const CastSocket* socket, |
| 81 cast_channel::ChannelError error) { | 80 cast_channel::ChannelError error) { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 83 ChannelInfo channel_info; | 82 ChannelInfo channel_info; |
| 84 socket->FillChannelInfo(&channel_info); | 83 socket->FillChannelInfo(&channel_info); |
| 85 channel_info.error_state = error; | 84 channel_info.error_state = error; |
| 86 scoped_ptr<base::ListValue> results = OnError::Create(channel_info); | 85 scoped_ptr<base::ListValue> results = OnError::Create(channel_info); |
| 87 scoped_ptr<Event> event(new Event(OnError::kEventName, results.Pass())); | 86 scoped_ptr<Event> event(new Event(OnError::kEventName, results.Pass())); |
| 88 extensions::ExtensionSystem::Get(profile_)->event_router()-> | 87 extensions::ExtensionSystem::Get(context_) |
| 89 DispatchEventToExtension(socket->owner_extension_id(), event.Pass()); | 88 ->event_router() |
| 89 ->DispatchEventToExtension(socket->owner_extension_id(), event.Pass()); |
| 90 } | 90 } |
| 91 | 91 |
| 92 void CastChannelAPI::OnMessage(const CastSocket* socket, | 92 void CastChannelAPI::OnMessage(const CastSocket* socket, |
| 93 const MessageInfo& message_info) { | 93 const MessageInfo& message_info) { |
| 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 95 ChannelInfo channel_info; | 95 ChannelInfo channel_info; |
| 96 socket->FillChannelInfo(&channel_info); | 96 socket->FillChannelInfo(&channel_info); |
| 97 scoped_ptr<base::ListValue> results = | 97 scoped_ptr<base::ListValue> results = |
| 98 OnMessage::Create(channel_info, message_info); | 98 OnMessage::Create(channel_info, message_info); |
| 99 VLOG(1) << "Sending message " << ParamToString(message_info) | 99 VLOG(1) << "Sending message " << ParamToString(message_info) |
| 100 << " to channel " << ParamToString(channel_info); | 100 << " to channel " << ParamToString(channel_info); |
| 101 scoped_ptr<Event> event(new Event(OnMessage::kEventName, results.Pass())); | 101 scoped_ptr<Event> event(new Event(OnMessage::kEventName, results.Pass())); |
| 102 extensions::ExtensionSystem::Get(profile_)->event_router()-> | 102 extensions::ExtensionSystem::Get(context_) |
| 103 DispatchEventToExtension(socket->owner_extension_id(), event.Pass()); | 103 ->event_router() |
| 104 ->DispatchEventToExtension(socket->owner_extension_id(), event.Pass()); |
| 104 } | 105 } |
| 105 | 106 |
| 106 CastChannelAPI::~CastChannelAPI() {} | 107 CastChannelAPI::~CastChannelAPI() {} |
| 107 | 108 |
| 108 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction() | 109 CastChannelAsyncApiFunction::CastChannelAsyncApiFunction() |
| 109 : manager_(NULL), error_(cast_channel::CHANNEL_ERROR_NONE) { } | 110 : manager_(NULL), error_(cast_channel::CHANNEL_ERROR_NONE) { } |
| 110 | 111 |
| 111 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { } | 112 CastChannelAsyncApiFunction::~CastChannelAsyncApiFunction() { } |
| 112 | 113 |
| 113 bool CastChannelAsyncApiFunction::PrePrepare() { | 114 bool CastChannelAsyncApiFunction::PrePrepare() { |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 SetResultFromError(cast_channel::CHANNEL_ERROR_SOCKET_ERROR); | 259 SetResultFromError(cast_channel::CHANNEL_ERROR_SOCKET_ERROR); |
| 259 } else { | 260 } else { |
| 260 int channel_id = params_->channel.channel_id; | 261 int channel_id = params_->channel.channel_id; |
| 261 SetResultFromSocket(channel_id); | 262 SetResultFromSocket(channel_id); |
| 262 RemoveSocket(channel_id); | 263 RemoveSocket(channel_id); |
| 263 } | 264 } |
| 264 AsyncWorkCompleted(); | 265 AsyncWorkCompleted(); |
| 265 } | 266 } |
| 266 | 267 |
| 267 } // namespace extensions | 268 } // namespace extensions |
| OLD | NEW |