| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/ui/ash/cast_config_delegate_chromeos.h" | 5 #include "chrome/browser/ui/ash/cast_config_delegate_chromeos.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/extensions/api/cast_devices_private/cast_devices_privat
e_api.h" |
| 11 #include "chrome/browser/extensions/api/tab_capture/tab_capture_api.h" | 12 #include "chrome/browser/extensions/api/tab_capture/tab_capture_api.h" |
| 12 #include "chrome/browser/profiles/profile_manager.h" | 13 #include "chrome/browser/profiles/profile_manager.h" |
| 13 #include "chrome/browser/ui/browser_navigator.h" | 14 #include "chrome/browser/ui/browser_navigator.h" |
| 15 #include "chrome/common/extensions/api/cast_devices_private.h" |
| 14 #include "content/public/browser/browser_context.h" | 16 #include "content/public/browser/browser_context.h" |
| 15 #include "content/public/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
| 16 #include "content/public/browser/render_view_host.h" | 18 #include "content/public/browser/render_view_host.h" |
| 19 #include "extensions/browser/event_router.h" |
| 17 #include "extensions/browser/extension_host.h" | 20 #include "extensions/browser/extension_host.h" |
| 18 #include "extensions/browser/extension_registry.h" | 21 #include "extensions/browser/extension_registry.h" |
| 19 #include "extensions/browser/process_manager.h" | 22 #include "extensions/browser/process_manager.h" |
| 20 #include "extensions/common/extension.h" | 23 #include "extensions/common/extension.h" |
| 21 | 24 |
| 22 namespace chromeos { | 25 namespace chromeos { |
| 23 namespace { | 26 namespace { |
| 24 | 27 |
| 25 using JavaScriptResultCallback = | 28 Profile* GetProfile() { |
| 26 content::RenderFrameHost::JavaScriptResultCallback; | 29 // TODO(jdufault): Figure out how to correctly handle multiprofile mode. |
| 30 // See crbug.com/488751 |
| 31 return ProfileManager::GetActiveUserProfile(); |
| 32 } |
| 27 | 33 |
| 28 // Returns the cast extension if it exists. | 34 // Returns the cast extension if it exists. |
| 29 const extensions::Extension* FindCastExtension() { | 35 const extensions::Extension* FindCastExtension() { |
| 30 // TODO(jdufault): Figure out how to correctly handle multiprofile mode. | 36 Profile* profile = GetProfile(); |
| 31 // See crbug.com/488751 | |
| 32 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
| 33 const extensions::ExtensionRegistry* extension_registry = | 37 const extensions::ExtensionRegistry* extension_registry = |
| 34 extensions::ExtensionRegistry::Get(profile); | 38 extensions::ExtensionRegistry::Get(profile); |
| 35 const extensions::ExtensionSet& enabled_extensions = | 39 const extensions::ExtensionSet& enabled_extensions = |
| 36 extension_registry->enabled_extensions(); | 40 extension_registry->enabled_extensions(); |
| 37 | 41 |
| 38 for (size_t i = 0; i < arraysize(extensions::kChromecastExtensionIds); ++i) { | 42 for (size_t i = 0; i < arraysize(extensions::kChromecastExtensionIds); ++i) { |
| 39 const std::string extension_id(extensions::kChromecastExtensionIds[i]); | 43 const std::string extension_id(extensions::kChromecastExtensionIds[i]); |
| 40 if (enabled_extensions.Contains(extension_id)) { | 44 if (enabled_extensions.Contains(extension_id)) { |
| 41 return extension_registry->GetExtensionById( | 45 return extension_registry->GetExtensionById( |
| 42 extension_id, extensions::ExtensionRegistry::ENABLED); | 46 extension_id, extensions::ExtensionRegistry::ENABLED); |
| 43 } | 47 } |
| 44 } | 48 } |
| 45 return nullptr; | 49 return nullptr; |
| 46 } | 50 } |
| 47 | 51 |
| 48 // Utility method that returns the currently active RenderViewHost. | |
| 49 content::RenderViewHost* GetRenderViewHost() { | |
| 50 const extensions::Extension* extension = FindCastExtension(); | |
| 51 if (!extension) | |
| 52 return nullptr; | |
| 53 // TODO(jdufault): Figure out how to correctly handle multiprofile mode. | |
| 54 // See crbug.com/488751 | |
| 55 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
| 56 if (!profile) | |
| 57 return nullptr; | |
| 58 extensions::ProcessManager* pm = extensions::ProcessManager::Get(profile); | |
| 59 return pm->GetBackgroundHostForExtension(extension->id())->render_view_host(); | |
| 60 } | |
| 61 | |
| 62 // Executes JavaScript in the context of the cast extension's background page. | |
| 63 void ExecuteJavaScript(const std::string& javascript) { | |
| 64 auto rvh = GetRenderViewHost(); | |
| 65 if (!rvh) | |
| 66 return; | |
| 67 rvh->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(javascript)); | |
| 68 } | |
| 69 | |
| 70 // Executes JavaScript in the context of the cast extension's background page. | |
| 71 // Invokes |callback| with the return value of the invoked javascript. | |
| 72 void ExecuteJavaScriptWithCallback(const std::string& javascript, | |
| 73 const JavaScriptResultCallback& callback) { | |
| 74 auto rvh = GetRenderViewHost(); | |
| 75 if (!rvh) | |
| 76 return; | |
| 77 rvh->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(javascript), | |
| 78 callback); | |
| 79 } | |
| 80 | |
| 81 // Handler for GetReceiversAndActivities. | |
| 82 void GetReceiversAndActivitiesCallback( | |
| 83 const ash::CastConfigDelegate::ReceiversAndActivitesCallback& callback, | |
| 84 const base::Value* value) { | |
| 85 ash::CastConfigDelegate::ReceiversAndActivites receiver_activites; | |
| 86 const base::ListValue* ra_list = nullptr; | |
| 87 if (value->GetAsList(&ra_list)) { | |
| 88 for (auto i = ra_list->begin(); i != ra_list->end(); ++i) { | |
| 89 const base::DictionaryValue* ra_dict = nullptr; | |
| 90 if ((*i)->GetAsDictionary(&ra_dict)) { | |
| 91 const base::DictionaryValue* receiver_dict(nullptr), | |
| 92 *activity_dict(nullptr); | |
| 93 ash::CastConfigDelegate::ReceiverAndActivity receiver_activity; | |
| 94 if (ra_dict->GetDictionary("receiver", &receiver_dict)) { | |
| 95 receiver_dict->GetString("name", &receiver_activity.receiver.name); | |
| 96 receiver_dict->GetString("id", &receiver_activity.receiver.id); | |
| 97 } | |
| 98 if (ra_dict->GetDictionary("activity", &activity_dict) && | |
| 99 !activity_dict->empty()) { | |
| 100 activity_dict->GetString("id", &receiver_activity.activity.id); | |
| 101 activity_dict->GetString("title", &receiver_activity.activity.title); | |
| 102 activity_dict->GetString("activityType", | |
| 103 &receiver_activity.activity.activity_type); | |
| 104 activity_dict->GetBoolean("allowStop", | |
| 105 &receiver_activity.activity.allow_stop); | |
| 106 activity_dict->GetInteger("tabId", | |
| 107 &receiver_activity.activity.tab_id); | |
| 108 } | |
| 109 receiver_activites[receiver_activity.receiver.id] = receiver_activity; | |
| 110 } | |
| 111 } | |
| 112 } | |
| 113 callback.Run(receiver_activites); | |
| 114 } | |
| 115 | |
| 116 } // namespace | 52 } // namespace |
| 117 | 53 |
| 118 CastConfigDelegateChromeos::CastConfigDelegateChromeos() { | 54 CastConfigDelegateChromeos::CastConfigDelegateChromeos() { |
| 119 } | 55 } |
| 120 | 56 |
| 121 CastConfigDelegateChromeos::~CastConfigDelegateChromeos() { | 57 CastConfigDelegateChromeos::~CastConfigDelegateChromeos() { |
| 122 } | 58 } |
| 123 | 59 |
| 124 bool CastConfigDelegateChromeos::HasCastExtension() const { | 60 bool CastConfigDelegateChromeos::HasCastExtension() const { |
| 125 // TODO(jdufault): Temporarily disable the cast tray integration until we | 61 return FindCastExtension() != nullptr; |
| 126 // figure out how to get ExecuteJavaScriptInIsolatedWorld to work as expected. | |
| 127 // See crbug.com/514952. | |
| 128 return false; | |
| 129 } | 62 } |
| 130 | 63 |
| 131 void CastConfigDelegateChromeos::GetReceiversAndActivities( | 64 CastConfigDelegateChromeos::DeviceUpdateSubscription |
| 65 CastConfigDelegateChromeos::RegisterDeviceUpdateObserver( |
| 132 const ReceiversAndActivitesCallback& callback) { | 66 const ReceiversAndActivitesCallback& callback) { |
| 133 // The methods in backgroundSetup are renamed during minification, so we have | 67 auto listeners = extensions::CastDeviceUpdateListeners::Get(GetProfile()); |
| 134 // to bind the exported global API methods to backgroundSetup using call(). | 68 return listeners->RegisterCallback(callback); |
| 135 ExecuteJavaScriptWithCallback( | 69 } |
| 136 "getMirrorCapableReceiversAndActivities.call(backgroundSetup);", | 70 |
| 137 base::Bind(&GetReceiversAndActivitiesCallback, 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, |
| 77 args.Pass())); |
| 78 extensions::EventRouter::Get(GetProfile()) |
| 79 ->DispatchEventToExtension(FindCastExtension()->id(), event.Pass()); |
| 138 } | 80 } |
| 139 | 81 |
| 140 void CastConfigDelegateChromeos::CastToReceiver( | 82 void CastConfigDelegateChromeos::CastToReceiver( |
| 141 const std::string& receiver_id) { | 83 const std::string& receiver_id) { |
| 142 // The methods in backgroundSetup are renamed during minification, so we have | 84 scoped_ptr<base::ListValue> args = |
| 143 // to bind the exported global API methods to backgroundSetup using call(). | 85 extensions::api::cast_devices_private::StartCast::Create(receiver_id); |
| 144 ExecuteJavaScript("launchDesktopMirroring.call(backgroundSetup, '" + | 86 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 145 receiver_id + "');"); | 87 extensions::events::CAST_DEVICES_PRIVATE_ON_START_CAST, |
| 88 extensions::api::cast_devices_private::StartCast::kEventName, |
| 89 args.Pass())); |
| 90 extensions::EventRouter::Get(GetProfile()) |
| 91 ->DispatchEventToExtension(FindCastExtension()->id(), event.Pass()); |
| 146 } | 92 } |
| 147 | 93 |
| 148 void CastConfigDelegateChromeos::StopCasting() { | 94 void CastConfigDelegateChromeos::StopCasting() { |
| 149 // The methods in backgroundSetup are renamed during minification, so we have | 95 scoped_ptr<base::ListValue> args = |
| 150 // to bind the exported global API methods to backgroundSetup using call(). | 96 extensions::api::cast_devices_private::StopCast::Create("user-stop"); |
| 151 ExecuteJavaScript("stopMirroring.call(backgroundSetup, 'user-stop');"); | 97 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 152 | 98 extensions::events::CAST_DEVICES_PRIVATE_ON_STOP_CAST, |
| 153 // TODO(jdufault): Remove this after the beta/release versions of the | 99 extensions::api::cast_devices_private::StopCast::kEventName, |
| 154 // cast extension have been updated so that they properly export the | 100 args.Pass())); |
| 155 // stopMirroring function. For now, we try to invoke all of the other | 101 extensions::EventRouter::Get(GetProfile()) |
| 156 // names that the function goes by. See crbug.com/489929. | 102 ->DispatchEventToExtension(FindCastExtension()->id(), event.Pass()); |
| 157 ExecuteJavaScript("backgroundSetup.Qu('user-stop');"); | |
| 158 } | 103 } |
| 159 | 104 |
| 160 void CastConfigDelegateChromeos::LaunchCastOptions() { | 105 void CastConfigDelegateChromeos::LaunchCastOptions() { |
| 161 chrome::NavigateParams params( | 106 chrome::NavigateParams params( |
| 162 ProfileManager::GetActiveUserProfile(), | 107 ProfileManager::GetActiveUserProfile(), |
| 163 FindCastExtension()->GetResourceURL("options.html"), | 108 FindCastExtension()->GetResourceURL("options.html"), |
| 164 ui::PAGE_TRANSITION_LINK); | 109 ui::PAGE_TRANSITION_LINK); |
| 165 params.disposition = NEW_FOREGROUND_TAB; | 110 params.disposition = NEW_FOREGROUND_TAB; |
| 166 params.window_action = chrome::NavigateParams::SHOW_WINDOW; | 111 params.window_action = chrome::NavigateParams::SHOW_WINDOW; |
| 167 chrome::Navigate(¶ms); | 112 chrome::Navigate(¶ms); |
| 168 } | 113 } |
| 169 | 114 |
| 170 } // namespace chromeos | 115 } // namespace chromeos |
| OLD | NEW |