| 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/serial/serial_event_dispatcher.h" | 5 #include "extensions/browser/api/serial/serial_event_dispatcher.h" |
| 6 | 6 |
| 7 #include "chrome/browser/browser_process.h" | 7 #include "extensions/browser/api/serial/serial_connection.h" |
| 8 #include "chrome/browser/extensions/api/serial/serial_connection.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/profiles/profile_manager.h" | |
| 11 #include "extensions/browser/event_router.h" | 8 #include "extensions/browser/event_router.h" |
| 9 #include "extensions/browser/extensions_browser_client.h" |
| 12 | 10 |
| 13 namespace extensions { | 11 namespace extensions { |
| 14 | 12 |
| 15 namespace api { | 13 namespace core_api { |
| 16 | 14 |
| 17 namespace { | 15 namespace { |
| 18 | 16 |
| 19 bool ShouldPauseOnReceiveError(serial::ReceiveError error) { | 17 bool ShouldPauseOnReceiveError(serial::ReceiveError error) { |
| 20 return error == serial::RECEIVE_ERROR_DEVICE_LOST || | 18 return error == serial::RECEIVE_ERROR_DEVICE_LOST || |
| 21 error == serial::RECEIVE_ERROR_SYSTEM_ERROR || | 19 error == serial::RECEIVE_ERROR_SYSTEM_ERROR || |
| 22 error == serial::RECEIVE_ERROR_DISCONNECTED; | 20 error == serial::RECEIVE_ERROR_DISCONNECTED; |
| 23 } | 21 } |
| 24 | 22 |
| 25 } // namespace | 23 } // namespace |
| 26 | 24 |
| 27 static base::LazyInstance<BrowserContextKeyedAPIFactory<SerialEventDispatcher> > | 25 static base::LazyInstance<BrowserContextKeyedAPIFactory<SerialEventDispatcher> > |
| 28 g_factory = LAZY_INSTANCE_INITIALIZER; | 26 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 29 | 27 |
| 30 // static | 28 // static |
| 31 BrowserContextKeyedAPIFactory<SerialEventDispatcher>* | 29 BrowserContextKeyedAPIFactory<SerialEventDispatcher>* |
| 32 SerialEventDispatcher::GetFactoryInstance() { | 30 SerialEventDispatcher::GetFactoryInstance() { |
| 33 return g_factory.Pointer(); | 31 return g_factory.Pointer(); |
| 34 } | 32 } |
| 35 | 33 |
| 36 // static | 34 // static |
| 37 SerialEventDispatcher* SerialEventDispatcher::Get( | 35 SerialEventDispatcher* SerialEventDispatcher::Get( |
| 38 content::BrowserContext* context) { | 36 content::BrowserContext* context) { |
| 39 return BrowserContextKeyedAPIFactory<SerialEventDispatcher>::Get(context); | 37 return BrowserContextKeyedAPIFactory<SerialEventDispatcher>::Get(context); |
| 40 } | 38 } |
| 41 | 39 |
| 42 SerialEventDispatcher::SerialEventDispatcher(content::BrowserContext* context) | 40 SerialEventDispatcher::SerialEventDispatcher(content::BrowserContext* context) |
| 43 : thread_id_(SerialConnection::kThreadId), | 41 : thread_id_(SerialConnection::kThreadId), context_(context) { |
| 44 profile_(Profile::FromBrowserContext(context)) { | |
| 45 ApiResourceManager<SerialConnection>* manager = | 42 ApiResourceManager<SerialConnection>* manager = |
| 46 ApiResourceManager<SerialConnection>::Get(profile_); | 43 ApiResourceManager<SerialConnection>::Get(context_); |
| 47 DCHECK(manager) << "No serial connection manager."; | 44 DCHECK(manager) << "No serial connection manager."; |
| 48 connections_ = manager->data_; | 45 connections_ = manager->data_; |
| 49 } | 46 } |
| 50 | 47 |
| 51 SerialEventDispatcher::~SerialEventDispatcher() {} | 48 SerialEventDispatcher::~SerialEventDispatcher() { |
| 49 } |
| 52 | 50 |
| 53 SerialEventDispatcher::ReceiveParams::ReceiveParams() {} | 51 SerialEventDispatcher::ReceiveParams::ReceiveParams() { |
| 52 } |
| 54 | 53 |
| 55 SerialEventDispatcher::ReceiveParams::~ReceiveParams() {} | 54 SerialEventDispatcher::ReceiveParams::~ReceiveParams() { |
| 55 } |
| 56 | 56 |
| 57 void SerialEventDispatcher::PollConnection(const std::string& extension_id, | 57 void SerialEventDispatcher::PollConnection(const std::string& extension_id, |
| 58 int connection_id) { | 58 int connection_id) { |
| 59 DCHECK_CURRENTLY_ON(thread_id_); | 59 DCHECK_CURRENTLY_ON(thread_id_); |
| 60 | 60 |
| 61 ReceiveParams params; | 61 ReceiveParams params; |
| 62 params.thread_id = thread_id_; | 62 params.thread_id = thread_id_; |
| 63 params.profile_id = profile_; | 63 params.browser_context_id = context_; |
| 64 params.extension_id = extension_id; | 64 params.extension_id = extension_id; |
| 65 params.connections = connections_; | 65 params.connections = connections_; |
| 66 params.connection_id = connection_id; | 66 params.connection_id = connection_id; |
| 67 | 67 |
| 68 StartReceive(params); | 68 StartReceive(params); |
| 69 } | 69 } |
| 70 | 70 |
| 71 // static | 71 // static |
| 72 void SerialEventDispatcher::StartReceive(const ReceiveParams& params) { | 72 void SerialEventDispatcher::StartReceive(const ReceiveParams& params) { |
| 73 DCHECK_CURRENTLY_ON(params.thread_id); | 73 DCHECK_CURRENTLY_ON(params.thread_id); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 PostEvent(params, event.Pass()); | 113 PostEvent(params, event.Pass()); |
| 114 if (ShouldPauseOnReceiveError(error)) { | 114 if (ShouldPauseOnReceiveError(error)) { |
| 115 SerialConnection* connection = | 115 SerialConnection* connection = |
| 116 params.connections->Get(params.extension_id, params.connection_id); | 116 params.connections->Get(params.extension_id, params.connection_id); |
| 117 if (connection) | 117 if (connection) |
| 118 connection->set_paused(true); | 118 connection->set_paused(true); |
| 119 } | 119 } |
| 120 } | 120 } |
| 121 | 121 |
| 122 // Queue up the next read operation. | 122 // Queue up the next read operation. |
| 123 BrowserThread::PostTask(params.thread_id, | 123 BrowserThread::PostTask( |
| 124 FROM_HERE, | 124 params.thread_id, FROM_HERE, base::Bind(&StartReceive, params)); |
| 125 base::Bind(&StartReceive, params)); | |
| 126 } | 125 } |
| 127 | 126 |
| 128 // static | 127 // static |
| 129 void SerialEventDispatcher::PostEvent(const ReceiveParams& params, | 128 void SerialEventDispatcher::PostEvent(const ReceiveParams& params, |
| 130 scoped_ptr<extensions::Event> event) { | 129 scoped_ptr<extensions::Event> event) { |
| 131 DCHECK_CURRENTLY_ON(params.thread_id); | 130 DCHECK_CURRENTLY_ON(params.thread_id); |
| 132 | 131 |
| 133 BrowserThread::PostTask( | 132 BrowserThread::PostTask(BrowserThread::UI, |
| 134 BrowserThread::UI, FROM_HERE, | 133 FROM_HERE, |
| 135 base::Bind(&DispatchEvent, | 134 base::Bind(&DispatchEvent, |
| 136 params.profile_id, | 135 params.browser_context_id, |
| 137 params.extension_id, | 136 params.extension_id, |
| 138 base::Passed(event.Pass()))); | 137 base::Passed(event.Pass()))); |
| 139 } | 138 } |
| 140 | 139 |
| 141 // static | 140 // static |
| 142 void SerialEventDispatcher::DispatchEvent(void* profile_id, | 141 void SerialEventDispatcher::DispatchEvent(void* browser_context_id, |
| 143 const std::string& extension_id, | 142 const std::string& extension_id, |
| 144 scoped_ptr<extensions::Event> event) { | 143 scoped_ptr<extensions::Event> event) { |
| 145 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 144 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 146 | 145 |
| 147 Profile* profile = reinterpret_cast<Profile*>(profile_id); | 146 content::BrowserContext* context = |
| 148 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) | 147 reinterpret_cast<content::BrowserContext*>(browser_context_id); |
| 148 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) |
| 149 return; | 149 return; |
| 150 | 150 |
| 151 EventRouter* router = EventRouter::Get(profile); | 151 EventRouter* router = EventRouter::Get(context); |
| 152 if (router) | 152 if (router) |
| 153 router->DispatchEventToExtension(extension_id, event.Pass()); | 153 router->DispatchEventToExtension(extension_id, event.Pass()); |
| 154 } | 154 } |
| 155 | 155 |
| 156 } // namespace api | 156 } // namespace core_api |
| 157 | 157 |
| 158 } // namespace extensions | 158 } // namespace extensions |
| OLD | NEW |