Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / cast_devices_private / cast_devices_private_api.cc
blobbab8394e7b78b72c24c2e7fcf6bc3815728bff41
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.
5 #include "chrome/browser/extensions/api/cast_devices_private/cast_devices_private_api.h"
7 #include "base/lazy_instance.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/common/extensions/api/cast_devices_private.h"
11 namespace extensions {
13 namespace {
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;
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;
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;
45 } // namespace
47 static base::LazyInstance<
48 BrowserContextKeyedAPIFactory<CastDeviceUpdateListeners>> g_factory =
49 LAZY_INSTANCE_INITIALIZER;
51 // static
52 BrowserContextKeyedAPIFactory<CastDeviceUpdateListeners>*
53 CastDeviceUpdateListeners::GetFactoryInstance() {
54 return g_factory.Pointer();
57 // static
58 CastDeviceUpdateListeners* CastDeviceUpdateListeners::Get(
59 content::BrowserContext* context) {
60 return BrowserContextKeyedAPIFactory<CastDeviceUpdateListeners>::Get(context);
63 CastDeviceUpdateListeners::CastDeviceUpdateListeners(
64 content::BrowserContext* context) {}
66 CastDeviceUpdateListeners::~CastDeviceUpdateListeners() {}
68 ash::CastConfigDelegate::DeviceUpdateSubscription
69 CastDeviceUpdateListeners::RegisterCallback(
70 const ash::CastConfigDelegate::ReceiversAndActivitesCallback& callback) {
71 return callback_list_.Add(callback);
74 void CastDeviceUpdateListeners::NotifyCallbacks(
75 const ReceiverAndActivityList& devices) {
76 callback_list_.Notify(devices);
79 CastDevicesPrivateUpdateDevicesFunction::
80 CastDevicesPrivateUpdateDevicesFunction() {}
82 CastDevicesPrivateUpdateDevicesFunction::
83 ~CastDevicesPrivateUpdateDevicesFunction() {}
85 ExtensionFunction::ResponseAction
86 CastDevicesPrivateUpdateDevicesFunction::Run() {
87 auto params =
88 api::cast_devices_private::UpdateDevices::Params::Create(*args_);
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()));
97 auto listeners = CastDeviceUpdateListeners::Get(browser_context());
98 listeners->NotifyCallbacks(devices);
100 return RespondNow(NoArguments());
103 } // namespace extensions