OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 #include "chrome/browser/extensions/api/cast_devices_private/cast_devices_privat
e_api.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/common/extensions/api/cast_devices_private.h" |
| 10 |
| 11 namespace extensions { |
| 12 |
| 13 namespace { |
| 14 |
| 15 ash::CastConfigDelegate::Receiver ConvertReceiverType( |
| 16 const api::cast_devices_private::Receiver& receiver) { |
| 17 ash::CastConfigDelegate::Receiver result; |
| 18 result.id = receiver.id; |
| 19 result.name = base::UTF8ToUTF16(receiver.name); |
| 20 return result; |
| 21 } |
| 22 |
| 23 ash::CastConfigDelegate::Activity ConvertActivityType( |
| 24 const api::cast_devices_private::Activity& activity) { |
| 25 ash::CastConfigDelegate::Activity result; |
| 26 result.id = activity.id; |
| 27 result.title = base::UTF8ToUTF16(activity.title); |
| 28 if (activity.tab_id) |
| 29 result.tab_id = *activity.tab_id; |
| 30 else |
| 31 result.tab_id = ash::CastConfigDelegate::Activity::TabId::UNKNOWN; |
| 32 return result; |
| 33 } |
| 34 |
| 35 ash::CastConfigDelegate::ReceiverAndActivity ConvertReceiverAndActivityType( |
| 36 const api::cast_devices_private::Receiver& receiver, |
| 37 const api::cast_devices_private::Activity* activity) { |
| 38 ash::CastConfigDelegate::ReceiverAndActivity result; |
| 39 result.receiver = ConvertReceiverType(receiver); |
| 40 if (activity) |
| 41 result.activity = ConvertActivityType(*activity); |
| 42 return result; |
| 43 } |
| 44 |
| 45 } // namespace |
| 46 |
| 47 static base::LazyInstance< |
| 48 BrowserContextKeyedAPIFactory<CastDeviceUpdateListeners>> g_factory = |
| 49 LAZY_INSTANCE_INITIALIZER; |
| 50 |
| 51 // static |
| 52 BrowserContextKeyedAPIFactory<CastDeviceUpdateListeners>* |
| 53 CastDeviceUpdateListeners::GetFactoryInstance() { |
| 54 return g_factory.Pointer(); |
| 55 } |
| 56 |
| 57 // static |
| 58 CastDeviceUpdateListeners* CastDeviceUpdateListeners::Get( |
| 59 content::BrowserContext* context) { |
| 60 return BrowserContextKeyedAPIFactory<CastDeviceUpdateListeners>::Get(context); |
| 61 } |
| 62 |
| 63 CastDeviceUpdateListeners::CastDeviceUpdateListeners( |
| 64 content::BrowserContext* context) {} |
| 65 |
| 66 CastDeviceUpdateListeners::~CastDeviceUpdateListeners() {} |
| 67 |
| 68 ash::CastConfigDelegate::DeviceUpdateSubscription |
| 69 CastDeviceUpdateListeners::RegisterCallback( |
| 70 const ash::CastConfigDelegate::ReceiversAndActivitesCallback& callback) { |
| 71 return callback_list_.Add(callback); |
| 72 } |
| 73 |
| 74 void CastDeviceUpdateListeners::NotifyCallbacks( |
| 75 const ReceiverAndActivityList& devices) { |
| 76 callback_list_.Notify(devices); |
| 77 } |
| 78 |
| 79 CastDevicesPrivateUpdateDevicesFunction:: |
| 80 CastDevicesPrivateUpdateDevicesFunction() {} |
| 81 |
| 82 CastDevicesPrivateUpdateDevicesFunction:: |
| 83 ~CastDevicesPrivateUpdateDevicesFunction() {} |
| 84 |
| 85 ExtensionFunction::ResponseAction |
| 86 CastDevicesPrivateUpdateDevicesFunction::Run() { |
| 87 auto params = |
| 88 api::cast_devices_private::UpdateDevices::Params::Create(*args_); |
| 89 |
| 90 CastDeviceUpdateListeners::ReceiverAndActivityList devices; |
| 91 for (linked_ptr<api::cast_devices_private::ReceiverActivity> device : |
| 92 params->devices) { |
| 93 devices.push_back(ConvertReceiverAndActivityType(device->receiver, |
| 94 device->activity.get())); |
| 95 } |
| 96 |
| 97 auto listeners = CastDeviceUpdateListeners::Get(browser_context()); |
| 98 listeners->NotifyCallbacks(devices); |
| 99 |
| 100 return RespondNow(NoArguments()); |
| 101 } |
| 102 |
| 103 } // namespace extensions |
OLD | NEW |