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/ui/ash/cast_config_delegate_chromeos.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/extensions/api/tab_capture/tab_capture_api.h" |
| 12 #include "chrome/browser/profiles/profile_manager.h" |
| 13 #include "chrome/browser/ui/browser_navigator.h" |
| 14 #include "content/public/browser/browser_context.h" |
| 15 #include "content/public/browser/render_frame_host.h" |
| 16 #include "content/public/browser/render_view_host.h" |
| 17 #include "extensions/browser/extension_host.h" |
| 18 #include "extensions/browser/extension_registry.h" |
| 19 #include "extensions/browser/process_manager.h" |
| 20 #include "extensions/common/extension.h" |
| 21 |
| 22 namespace chromeos { |
| 23 namespace { |
| 24 |
| 25 using JavaScriptResultCallback = |
| 26 content::RenderFrameHost::JavaScriptResultCallback; |
| 27 |
| 28 // Returns the cast extension if it exists. |
| 29 const extensions::Extension* FindCastExtension() { |
| 30 // TODO(jdufault): Figure out how to correctly handle multiprofile mode. |
| 31 // See crbug.com/488751 |
| 32 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 33 const extensions::ExtensionRegistry* extension_registry = |
| 34 extensions::ExtensionRegistry::Get(profile); |
| 35 const extensions::ExtensionSet& enabled_extensions = |
| 36 extension_registry->enabled_extensions(); |
| 37 |
| 38 for (size_t i = 0; i < arraysize(extensions::kChromecastExtensionIds); ++i) { |
| 39 const std::string extension_id(extensions::kChromecastExtensionIds[i]); |
| 40 if (enabled_extensions.Contains(extension_id)) { |
| 41 return extension_registry->GetExtensionById( |
| 42 extension_id, extensions::ExtensionRegistry::ENABLED); |
| 43 } |
| 44 } |
| 45 return nullptr; |
| 46 } |
| 47 |
| 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 |
| 117 |
| 118 CastConfigDelegateChromeos::CastConfigDelegateChromeos() { |
| 119 } |
| 120 |
| 121 CastConfigDelegateChromeos::~CastConfigDelegateChromeos() { |
| 122 } |
| 123 |
| 124 bool CastConfigDelegateChromeos::HasCastExtension() const { |
| 125 return FindCastExtension() != nullptr; |
| 126 } |
| 127 |
| 128 void CastConfigDelegateChromeos::GetReceiversAndActivities( |
| 129 const ReceiversAndActivitesCallback& callback) { |
| 130 ExecuteJavaScriptWithCallback( |
| 131 "backgroundSetup.getMirrorCapableReceiversAndActivities();", |
| 132 base::Bind(&GetReceiversAndActivitiesCallback, callback)); |
| 133 } |
| 134 |
| 135 void CastConfigDelegateChromeos::CastToReceiver( |
| 136 const std::string& receiver_id) { |
| 137 ExecuteJavaScript("backgroundSetup.launchDesktopMirroring('" + receiver_id + |
| 138 "');"); |
| 139 } |
| 140 |
| 141 void CastConfigDelegateChromeos::StopCasting(const std::string& activity_id) { |
| 142 ExecuteJavaScript("backgroundSetup.stopCastMirroring('user-stop');"); |
| 143 } |
| 144 |
| 145 void CastConfigDelegateChromeos::LaunchCastOptions() { |
| 146 chrome::NavigateParams params( |
| 147 ProfileManager::GetActiveUserProfile(), |
| 148 FindCastExtension()->GetResourceURL("options.html"), |
| 149 ui::PAGE_TRANSITION_LINK); |
| 150 params.disposition = NEW_FOREGROUND_TAB; |
| 151 params.window_action = chrome::NavigateParams::SHOW_WINDOW; |
| 152 chrome::Navigate(¶ms); |
| 153 } |
| 154 |
| 155 } // namespace chromeos |
OLD | NEW |