| 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 void CastDeviceUpdateListeners::AddObserver( | |
| 69 ash::CastConfigDelegate::Observer* observer) { | |
| 70 observer_list_.AddObserver(observer); | |
| 71 } | |
| 72 | |
| 73 void CastDeviceUpdateListeners::RemoveObserver( | |
| 74 ash::CastConfigDelegate::Observer* observer) { | |
| 75 observer_list_.RemoveObserver(observer); | |
| 76 } | |
| 77 | |
| 78 void CastDeviceUpdateListeners::NotifyCallbacks( | |
| 79 const ReceiverAndActivityList& devices) { | |
| 80 for (auto& observer : observer_list_) | |
| 81 observer.OnDevicesUpdated(devices); | |
| 82 } | |
| 83 | |
| 84 CastDevicesPrivateUpdateDevicesFunction:: | |
| 85 CastDevicesPrivateUpdateDevicesFunction() {} | |
| 86 | |
| 87 CastDevicesPrivateUpdateDevicesFunction:: | |
| 88 ~CastDevicesPrivateUpdateDevicesFunction() {} | |
| 89 | |
| 90 ExtensionFunction::ResponseAction | |
| 91 CastDevicesPrivateUpdateDevicesFunction::Run() { | |
| 92 auto params = | |
| 93 api::cast_devices_private::UpdateDevices::Params::Create(*args_); | |
| 94 | |
| 95 CastDeviceUpdateListeners::ReceiverAndActivityList devices; | |
| 96 for (const api::cast_devices_private::ReceiverActivity& device : | |
| 97 params->devices) { | |
| 98 devices.push_back( | |
| 99 ConvertReceiverAndActivityType(device.receiver, device.activity.get())); | |
| 100 } | |
| 101 | |
| 102 auto* listeners = CastDeviceUpdateListeners::Get(browser_context()); | |
| 103 listeners->NotifyCallbacks(devices); | |
| 104 | |
| 105 return RespondNow(NoArguments()); | |
| 106 } | |
| 107 | |
| 108 } // namespace extensions | |
| OLD | NEW |