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/profiles/profile_manager.h" | |
12 #include "chrome/browser/ui/browser_navigator.h" | |
13 #include "content/public/browser/render_view_host.h" | |
14 #include "extensions/browser/extension_host.h" | |
15 #include "extensions/browser/extension_registry.h" | |
16 #include "extensions/browser/process_manager.h" | |
17 #include "extensions/common/extension.h" | |
18 | |
19 namespace chromeos { | |
20 namespace { | |
21 | |
22 using JavaScriptResultCallback = | |
23 content::RenderFrameHost::JavaScriptResultCallback; | |
24 | |
25 // Cast extension ids. | |
26 const char* kExtensionIds[] = { | |
achuithb
2015/05/06 00:45:58
Please file a bug and add a TODO that this needs t
jdufault
2015/05/06 19:02:55
I can create a CL to do this refactoring, but it s
achuithb
2015/05/06 19:08:59
So remove lines 37-39, right? That would make the
jdufault
2015/05/07 20:05:32
Done, please see https://codereview.chromium.org/1
| |
27 // Dev | |
28 "enhhojjnijigcajfphajepfemndkmdlo", | |
29 // Canary | |
30 "hfaagokkkhdbgiakmmlclaapfelnkoah", | |
31 // Beta (internal) | |
32 "fmfcbgogabcbclcofgocippekhfcmgfj", | |
33 // Google Cast Beta | |
34 "dliochdbjfkdbacpmhlcpmleaejidimm", | |
35 // Google Cast Stable | |
36 "boadgeojelhgndaghljhdicfkmllpafd", | |
37 // http://crbug.com/457908 | |
38 "ekpaaapppgpmolpcldedioblbkmijaca", | |
39 "fjhoaacokmgbjemoflkofnenfaiekifl"}; | |
40 | |
41 // Returns the cast extension if it exists. | |
42 const extensions::Extension* FindCastExtension() { | |
43 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
44 const extensions::ExtensionRegistry* extension_registry = | |
45 extensions::ExtensionRegistry::Get(profile); | |
46 const extensions::ExtensionSet& enabled_extensions = | |
47 extension_registry->enabled_extensions(); | |
48 | |
49 for (size_t i = 0; i < arraysize(kExtensionIds); ++i) { | |
50 std::string extension_id(kExtensionIds[i]); | |
achuithb
2015/05/06 00:45:57
const
jdufault
2015/05/06 19:02:55
Done.
| |
51 if (enabled_extensions.Contains(extension_id)) { | |
52 return extension_registry->GetExtensionById( | |
53 extension_id, extensions::ExtensionRegistry::ENABLED); | |
54 } | |
55 } | |
56 return nullptr; | |
57 } | |
58 | |
59 content::RenderViewHost* GetRenderViewHost() { | |
achuithb
2015/05/06 00:45:57
Add fn comment
jdufault
2015/05/06 19:02:55
Done.
| |
60 const extensions::Extension* extension = FindCastExtension(); | |
61 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
62 extensions::ProcessManager* pm = extensions::ProcessManager::Get(profile); | |
63 return pm->GetBackgroundHostForExtension(extension->id())->render_view_host(); | |
64 } | |
65 | |
66 // Executes JavaScript in the context of the cast extension's background page. | |
67 void ExecuteJavaScript(const std::string& javascript) { | |
68 GetRenderViewHost()->GetMainFrame()->ExecuteJavaScript( | |
69 base::UTF8ToUTF16(javascript)); | |
70 } | |
71 | |
72 void ExecuteJavaScriptWithCallback(const std::string& javascript, | |
achuithb
2015/05/06 00:45:58
Fn comment
jdufault
2015/05/06 19:02:56
Done.
| |
73 const JavaScriptResultCallback& callback) { | |
74 GetRenderViewHost()->GetMainFrame()->ExecuteJavaScript( | |
75 base::UTF8ToUTF16(javascript), callback); | |
76 } | |
77 | |
78 // Handler for GetReceiversAndActivities. | |
79 void GetReceiversAndActivitiesCallback( | |
80 const ash::CastConfigDelegate::ReceiversAndActivitesCallback& callback, | |
81 const base::Value* value) { | |
82 ash::CastConfigDelegate::ReceiversAndActivites receiver_activites; | |
83 const base::ListValue* ra_list = nullptr; | |
84 if (value->GetAsList(&ra_list)) { | |
85 for (auto i = ra_list->begin(); i != ra_list->end(); ++i) { | |
86 const base::DictionaryValue* ra_dict = nullptr; | |
87 if ((*i)->GetAsDictionary(&ra_dict)) { | |
88 const base::DictionaryValue* receiver_dict = nullptr, | |
89 * activity_dict = nullptr; | |
achuithb
2015/05/06 00:45:57
This looks weird to me, maybe
const base::Diction
jdufault
2015/05/06 19:02:56
Done.
| |
90 ash::CastConfigDelegate::ReceiverAndActivity receiver_activity; | |
91 if (ra_dict->GetDictionary("receiver", &receiver_dict)) { | |
92 receiver_dict->GetString("name", &receiver_activity.receiver.name); | |
93 receiver_dict->GetString("id", &receiver_activity.receiver.id); | |
94 } | |
95 if (ra_dict->GetDictionary("activity", &activity_dict) && | |
96 !activity_dict->empty()) { | |
97 activity_dict->GetString("id", &receiver_activity.activity.id); | |
98 activity_dict->GetString("title", &receiver_activity.activity.title); | |
99 activity_dict->GetString("activityType", | |
100 &receiver_activity.activity.activity_type); | |
101 activity_dict->GetBoolean("allowStop", | |
102 &receiver_activity.activity.allow_stop); | |
103 activity_dict->GetInteger("tabId", | |
104 &receiver_activity.activity.tab_id); | |
105 } | |
106 receiver_activites[receiver_activity.receiver.id] = receiver_activity; | |
107 } | |
108 } | |
109 } | |
110 callback.Run(receiver_activites); | |
111 } | |
112 | |
113 } // namespace | |
114 | |
115 CastConfigDelegateChromeos::CastConfigDelegateChromeos() { | |
116 } | |
117 | |
118 CastConfigDelegateChromeos::~CastConfigDelegateChromeos() { | |
119 } | |
120 | |
121 bool CastConfigDelegateChromeos::HasCastExtension() { | |
122 return FindCastExtension() != nullptr; | |
123 } | |
124 | |
125 void CastConfigDelegateChromeos::GetReceiversAndActivities( | |
126 const ReceiversAndActivitesCallback& callback) { | |
127 // TODO(jdufault): why is getMirrorCapableReceiversAndActivites() exported | |
achuithb
2015/05/06 00:45:57
What's this TODO?
jdufault
2015/05/06 19:02:56
Just a note to myself, removed.
| |
128 // in the JavaScript? | |
129 ExecuteJavaScriptWithCallback( | |
130 "backgroundSetup.getMirrorCapableReceiversAndActivities();", | |
131 base::Bind(&GetReceiversAndActivitiesCallback, callback)); | |
132 } | |
133 | |
134 void CastConfigDelegateChromeos::CastToReceiver( | |
135 const std::string& receiver_id) { | |
136 ExecuteJavaScript("backgroundSetup.launchDesktopMirroring('" + receiver_id + | |
137 "');"); | |
138 } | |
139 | |
140 void CastConfigDelegateChromeos::StopCasting(const std::string& activity_id) { | |
141 ExecuteJavaScript("backgroundSetup.stopCastMirroring('user-stop');"); | |
142 } | |
143 | |
144 void CastConfigDelegateChromeos::LaunchCastOptions() { | |
145 chrome::NavigateParams params( | |
146 ProfileManager::GetActiveUserProfile(), | |
147 FindCastExtension()->GetResourceURL("options.html"), | |
148 ui::PAGE_TRANSITION_LINK); | |
149 params.disposition = NEW_FOREGROUND_TAB; | |
150 params.window_action = chrome::NavigateParams::SHOW_WINDOW; | |
151 chrome::Navigate(¶ms); | |
152 } | |
153 | |
154 } // namespace chromeos | |
OLD | NEW |