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 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 31 const extensions::ExtensionRegistry* extension_registry = |
| 32 extensions::ExtensionRegistry::Get(profile); |
| 33 const extensions::ExtensionSet& enabled_extensions = |
| 34 extension_registry->enabled_extensions(); |
| 35 |
| 36 for (size_t i = 0; i < arraysize(extensions::kChromecastExtensionIds); ++i) { |
| 37 const std::string extension_id(extensions::kChromecastExtensionIds[i]); |
| 38 if (enabled_extensions.Contains(extension_id)) { |
| 39 return extension_registry->GetExtensionById( |
| 40 extension_id, extensions::ExtensionRegistry::ENABLED); |
| 41 } |
| 42 } |
| 43 return nullptr; |
| 44 } |
| 45 |
| 46 // Utility method that returns the currently active RenderViewHost. |
| 47 content::RenderViewHost* GetRenderViewHost() { |
| 48 const extensions::Extension* extension = FindCastExtension(); |
| 49 if (!extension) |
| 50 return nullptr; |
| 51 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 52 if (!profile) |
| 53 return nullptr; |
| 54 extensions::ProcessManager* pm = extensions::ProcessManager::Get(profile); |
| 55 return pm->GetBackgroundHostForExtension(extension->id())->render_view_host(); |
| 56 } |
| 57 |
| 58 // Executes JavaScript in the context of the cast extension's background page. |
| 59 void ExecuteJavaScript(const std::string& javascript) { |
| 60 auto rvh = GetRenderViewHost(); |
| 61 if (!rvh) |
| 62 return; |
| 63 rvh->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(javascript)); |
| 64 } |
| 65 |
| 66 // Executes JavaScript in the context of the cast extension's background page. |
| 67 // Invokes |callback| with the return value of the invoked javascript. |
| 68 void ExecuteJavaScriptWithCallback(const std::string& javascript, |
| 69 const JavaScriptResultCallback& callback) { |
| 70 auto rvh = GetRenderViewHost(); |
| 71 if (!rvh) |
| 72 return; |
| 73 rvh->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(javascript), |
| 74 callback); |
| 75 } |
| 76 |
| 77 // Handler for GetReceiversAndActivities. |
| 78 void GetReceiversAndActivitiesCallback( |
| 79 const ash::CastConfigDelegate::ReceiversAndActivitesCallback& callback, |
| 80 const base::Value* value) { |
| 81 ash::CastConfigDelegate::ReceiversAndActivites receiver_activites; |
| 82 const base::ListValue* ra_list = nullptr; |
| 83 if (value->GetAsList(&ra_list)) { |
| 84 for (auto i = ra_list->begin(); i != ra_list->end(); ++i) { |
| 85 const base::DictionaryValue* ra_dict = nullptr; |
| 86 if ((*i)->GetAsDictionary(&ra_dict)) { |
| 87 const base::DictionaryValue* receiver_dict(nullptr), |
| 88 *activity_dict(nullptr); |
| 89 ash::CastConfigDelegate::ReceiverAndActivity receiver_activity; |
| 90 if (ra_dict->GetDictionary("receiver", &receiver_dict)) { |
| 91 receiver_dict->GetString("name", &receiver_activity.receiver.name); |
| 92 receiver_dict->GetString("id", &receiver_activity.receiver.id); |
| 93 } |
| 94 if (ra_dict->GetDictionary("activity", &activity_dict) && |
| 95 !activity_dict->empty()) { |
| 96 activity_dict->GetString("id", &receiver_activity.activity.id); |
| 97 activity_dict->GetString("title", &receiver_activity.activity.title); |
| 98 activity_dict->GetString("activityType", |
| 99 &receiver_activity.activity.activity_type); |
| 100 activity_dict->GetBoolean("allowStop", |
| 101 &receiver_activity.activity.allow_stop); |
| 102 activity_dict->GetInteger("tabId", |
| 103 &receiver_activity.activity.tab_id); |
| 104 } |
| 105 receiver_activites[receiver_activity.receiver.id] = receiver_activity; |
| 106 } |
| 107 } |
| 108 } |
| 109 callback.Run(receiver_activites); |
| 110 } |
| 111 |
| 112 } // namespace |
| 113 |
| 114 CastConfigDelegateChromeos::CastConfigDelegateChromeos() { |
| 115 } |
| 116 |
| 117 CastConfigDelegateChromeos::~CastConfigDelegateChromeos() { |
| 118 } |
| 119 |
| 120 bool CastConfigDelegateChromeos::HasCastExtension() { |
| 121 return FindCastExtension() != nullptr; |
| 122 } |
| 123 |
| 124 void CastConfigDelegateChromeos::GetReceiversAndActivities( |
| 125 const ReceiversAndActivitesCallback& callback) { |
| 126 ExecuteJavaScriptWithCallback( |
| 127 "backgroundSetup.getMirrorCapableReceiversAndActivities();", |
| 128 base::Bind(&GetReceiversAndActivitiesCallback, callback)); |
| 129 } |
| 130 |
| 131 void CastConfigDelegateChromeos::CastToReceiver( |
| 132 const std::string& receiver_id) { |
| 133 ExecuteJavaScript("backgroundSetup.launchDesktopMirroring('" + receiver_id + |
| 134 "');"); |
| 135 } |
| 136 |
| 137 void CastConfigDelegateChromeos::StopCasting(const std::string& activity_id) { |
| 138 ExecuteJavaScript("backgroundSetup.stopCastMirroring('user-stop');"); |
| 139 } |
| 140 |
| 141 void CastConfigDelegateChromeos::LaunchCastOptions() { |
| 142 chrome::NavigateParams params( |
| 143 ProfileManager::GetActiveUserProfile(), |
| 144 FindCastExtension()->GetResourceURL("options.html"), |
| 145 ui::PAGE_TRANSITION_LINK); |
| 146 params.disposition = NEW_FOREGROUND_TAB; |
| 147 params.window_action = chrome::NavigateParams::SHOW_WINDOW; |
| 148 chrome::Navigate(¶ms); |
| 149 } |
| 150 |
| 151 } // namespace chromeos |
OLD | NEW |