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/ui/ash/cast_config_delegate_chromeos.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/extensions/api/cast_devices_private/cast_devices_private_api.h"
12 #include "chrome/browser/extensions/api/tab_capture/tab_capture_api.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/ui/browser_navigator.h"
15 #include "chrome/common/extensions/api/cast_devices_private.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/render_view_host.h"
19 #include "extensions/browser/event_router.h"
20 #include "extensions/browser/extension_host.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/browser/process_manager.h"
23 #include "extensions/common/extension.h"
28 Profile
* GetProfile() {
29 // TODO(jdufault): Figure out how to correctly handle multiprofile mode.
30 // See crbug.com/488751
31 return ProfileManager::GetActiveUserProfile();
34 // Returns the cast extension if it exists.
35 const extensions::Extension
* FindCastExtension() {
36 Profile
* profile
= GetProfile();
37 const extensions::ExtensionRegistry
* extension_registry
=
38 extensions::ExtensionRegistry::Get(profile
);
39 const extensions::ExtensionSet
& enabled_extensions
=
40 extension_registry
->enabled_extensions();
42 for (size_t i
= 0; i
< arraysize(extensions::kChromecastExtensionIds
); ++i
) {
43 const std::string
extension_id(extensions::kChromecastExtensionIds
[i
]);
44 if (enabled_extensions
.Contains(extension_id
)) {
45 return extension_registry
->GetExtensionById(
46 extension_id
, extensions::ExtensionRegistry::ENABLED
);
54 CastConfigDelegateChromeos::CastConfigDelegateChromeos() {
57 CastConfigDelegateChromeos::~CastConfigDelegateChromeos() {
60 bool CastConfigDelegateChromeos::HasCastExtension() const {
61 return FindCastExtension() != nullptr;
64 CastConfigDelegateChromeos::DeviceUpdateSubscription
65 CastConfigDelegateChromeos::RegisterDeviceUpdateObserver(
66 const ReceiversAndActivitesCallback
& callback
) {
67 auto listeners
= extensions::CastDeviceUpdateListeners::Get(GetProfile());
68 return listeners
->RegisterCallback(callback
);
71 void CastConfigDelegateChromeos::RequestDeviceRefresh() {
72 scoped_ptr
<base::ListValue
> args
=
73 extensions::api::cast_devices_private::UpdateDevicesRequested::Create();
74 scoped_ptr
<extensions::Event
> event(new extensions::Event(
75 extensions::events::CAST_DEVICES_PRIVATE_ON_UPDATE_DEVICES_REQUESTED
,
76 extensions::api::cast_devices_private::UpdateDevicesRequested::kEventName
,
78 extensions::EventRouter::Get(GetProfile())
79 ->DispatchEventToExtension(FindCastExtension()->id(), event
.Pass());
82 void CastConfigDelegateChromeos::CastToReceiver(
83 const std::string
& receiver_id
) {
84 scoped_ptr
<base::ListValue
> args
=
85 extensions::api::cast_devices_private::StartCast::Create(receiver_id
);
86 scoped_ptr
<extensions::Event
> event(new extensions::Event(
87 extensions::events::CAST_DEVICES_PRIVATE_ON_START_CAST
,
88 extensions::api::cast_devices_private::StartCast::kEventName
,
90 extensions::EventRouter::Get(GetProfile())
91 ->DispatchEventToExtension(FindCastExtension()->id(), event
.Pass());
94 void CastConfigDelegateChromeos::StopCasting() {
95 scoped_ptr
<base::ListValue
> args
=
96 extensions::api::cast_devices_private::StopCast::Create("user-stop");
97 scoped_ptr
<extensions::Event
> event(new extensions::Event(
98 extensions::events::CAST_DEVICES_PRIVATE_ON_STOP_CAST
,
99 extensions::api::cast_devices_private::StopCast::kEventName
,
101 extensions::EventRouter::Get(GetProfile())
102 ->DispatchEventToExtension(FindCastExtension()->id(), event
.Pass());
105 void CastConfigDelegateChromeos::LaunchCastOptions() {
106 chrome::NavigateParams
params(
107 ProfileManager::GetActiveUserProfile(),
108 FindCastExtension()->GetResourceURL("options.html"),
109 ui::PAGE_TRANSITION_LINK
);
110 params
.disposition
= NEW_FOREGROUND_TAB
;
111 params
.window_action
= chrome::NavigateParams::SHOW_WINDOW
;
112 chrome::Navigate(¶ms
);
115 } // namespace chromeos