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