Chromium Code Reviews| Index: chrome/browser/ui/ash/cast_config_delegate_chromeos.cc |
| diff --git a/chrome/browser/ui/ash/cast_config_delegate_chromeos.cc b/chrome/browser/ui/ash/cast_config_delegate_chromeos.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5b3b3b0caaef84e6935c97c72bd50608f7ed1a1e |
| --- /dev/null |
| +++ b/chrome/browser/ui/ash/cast_config_delegate_chromeos.cc |
| @@ -0,0 +1,154 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/ash/cast_config_delegate_chromeos.h" |
| + |
| +#include <string> |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/ui/browser_navigator.h" |
| +#include "content/public/browser/render_view_host.h" |
| +#include "extensions/browser/extension_host.h" |
| +#include "extensions/browser/extension_registry.h" |
| +#include "extensions/browser/process_manager.h" |
| +#include "extensions/common/extension.h" |
| + |
| +namespace chromeos { |
| +namespace { |
| + |
| +using JavaScriptResultCallback = |
| + content::RenderFrameHost::JavaScriptResultCallback; |
| + |
| +// Cast extension ids. |
| +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
|
| + // Dev |
| + "enhhojjnijigcajfphajepfemndkmdlo", |
| + // Canary |
| + "hfaagokkkhdbgiakmmlclaapfelnkoah", |
| + // Beta (internal) |
| + "fmfcbgogabcbclcofgocippekhfcmgfj", |
| + // Google Cast Beta |
| + "dliochdbjfkdbacpmhlcpmleaejidimm", |
| + // Google Cast Stable |
| + "boadgeojelhgndaghljhdicfkmllpafd", |
| + // http://crbug.com/457908 |
| + "ekpaaapppgpmolpcldedioblbkmijaca", |
| + "fjhoaacokmgbjemoflkofnenfaiekifl"}; |
| + |
| +// Returns the cast extension if it exists. |
| +const extensions::Extension* FindCastExtension() { |
| + Profile* profile = ProfileManager::GetActiveUserProfile(); |
| + const extensions::ExtensionRegistry* extension_registry = |
| + extensions::ExtensionRegistry::Get(profile); |
| + const extensions::ExtensionSet& enabled_extensions = |
| + extension_registry->enabled_extensions(); |
| + |
| + for (size_t i = 0; i < arraysize(kExtensionIds); ++i) { |
| + std::string extension_id(kExtensionIds[i]); |
|
achuithb
2015/05/06 00:45:57
const
jdufault
2015/05/06 19:02:55
Done.
|
| + if (enabled_extensions.Contains(extension_id)) { |
| + return extension_registry->GetExtensionById( |
| + extension_id, extensions::ExtensionRegistry::ENABLED); |
| + } |
| + } |
| + return nullptr; |
| +} |
| + |
| +content::RenderViewHost* GetRenderViewHost() { |
|
achuithb
2015/05/06 00:45:57
Add fn comment
jdufault
2015/05/06 19:02:55
Done.
|
| + const extensions::Extension* extension = FindCastExtension(); |
| + Profile* profile = ProfileManager::GetActiveUserProfile(); |
| + extensions::ProcessManager* pm = extensions::ProcessManager::Get(profile); |
| + return pm->GetBackgroundHostForExtension(extension->id())->render_view_host(); |
| +} |
| + |
| +// Executes JavaScript in the context of the cast extension's background page. |
| +void ExecuteJavaScript(const std::string& javascript) { |
| + GetRenderViewHost()->GetMainFrame()->ExecuteJavaScript( |
| + base::UTF8ToUTF16(javascript)); |
| +} |
| + |
| +void ExecuteJavaScriptWithCallback(const std::string& javascript, |
|
achuithb
2015/05/06 00:45:58
Fn comment
jdufault
2015/05/06 19:02:56
Done.
|
| + const JavaScriptResultCallback& callback) { |
| + GetRenderViewHost()->GetMainFrame()->ExecuteJavaScript( |
| + base::UTF8ToUTF16(javascript), callback); |
| +} |
| + |
| +// Handler for GetReceiversAndActivities. |
| +void GetReceiversAndActivitiesCallback( |
| + const ash::CastConfigDelegate::ReceiversAndActivitesCallback& callback, |
| + const base::Value* value) { |
| + ash::CastConfigDelegate::ReceiversAndActivites receiver_activites; |
| + const base::ListValue* ra_list = nullptr; |
| + if (value->GetAsList(&ra_list)) { |
| + for (auto i = ra_list->begin(); i != ra_list->end(); ++i) { |
| + const base::DictionaryValue* ra_dict = nullptr; |
| + if ((*i)->GetAsDictionary(&ra_dict)) { |
| + const base::DictionaryValue* receiver_dict = nullptr, |
| + * 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.
|
| + ash::CastConfigDelegate::ReceiverAndActivity receiver_activity; |
| + if (ra_dict->GetDictionary("receiver", &receiver_dict)) { |
| + receiver_dict->GetString("name", &receiver_activity.receiver.name); |
| + receiver_dict->GetString("id", &receiver_activity.receiver.id); |
| + } |
| + if (ra_dict->GetDictionary("activity", &activity_dict) && |
| + !activity_dict->empty()) { |
| + activity_dict->GetString("id", &receiver_activity.activity.id); |
| + activity_dict->GetString("title", &receiver_activity.activity.title); |
| + activity_dict->GetString("activityType", |
| + &receiver_activity.activity.activity_type); |
| + activity_dict->GetBoolean("allowStop", |
| + &receiver_activity.activity.allow_stop); |
| + activity_dict->GetInteger("tabId", |
| + &receiver_activity.activity.tab_id); |
| + } |
| + receiver_activites[receiver_activity.receiver.id] = receiver_activity; |
| + } |
| + } |
| + } |
| + callback.Run(receiver_activites); |
| +} |
| + |
| +} // namespace |
| + |
| +CastConfigDelegateChromeos::CastConfigDelegateChromeos() { |
| +} |
| + |
| +CastConfigDelegateChromeos::~CastConfigDelegateChromeos() { |
| +} |
| + |
| +bool CastConfigDelegateChromeos::HasCastExtension() { |
| + return FindCastExtension() != nullptr; |
| +} |
| + |
| +void CastConfigDelegateChromeos::GetReceiversAndActivities( |
| + const ReceiversAndActivitesCallback& callback) { |
| + // 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.
|
| + // in the JavaScript? |
| + ExecuteJavaScriptWithCallback( |
| + "backgroundSetup.getMirrorCapableReceiversAndActivities();", |
| + base::Bind(&GetReceiversAndActivitiesCallback, callback)); |
| +} |
| + |
| +void CastConfigDelegateChromeos::CastToReceiver( |
| + const std::string& receiver_id) { |
| + ExecuteJavaScript("backgroundSetup.launchDesktopMirroring('" + receiver_id + |
| + "');"); |
| +} |
| + |
| +void CastConfigDelegateChromeos::StopCasting(const std::string& activity_id) { |
| + ExecuteJavaScript("backgroundSetup.stopCastMirroring('user-stop');"); |
| +} |
| + |
| +void CastConfigDelegateChromeos::LaunchCastOptions() { |
| + chrome::NavigateParams params( |
| + ProfileManager::GetActiveUserProfile(), |
| + FindCastExtension()->GetResourceURL("options.html"), |
| + ui::PAGE_TRANSITION_LINK); |
| + params.disposition = NEW_FOREGROUND_TAB; |
| + params.window_action = chrome::NavigateParams::SHOW_WINDOW; |
| + chrome::Navigate(¶ms); |
| +} |
| + |
| +} // namespace chromeos |