Chromium Code Reviews| Index: chrome/browser/ui/webui/settings/chromeos/android_apps_handler.cc |
| diff --git a/chrome/browser/ui/webui/settings/chromeos/android_apps_handler.cc b/chrome/browser/ui/webui/settings/chromeos/android_apps_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c217fadb4d549b90efc4ba2e246a1d989f1a0058 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/settings/chromeos/android_apps_handler.cc |
| @@ -0,0 +1,101 @@ |
| +// Copyright 2016 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/webui/settings/chromeos/android_apps_handler.h" |
| + |
| +#include "base/values.h" |
| +#include "chrome/browser/chromeos/arc/arc_session_manager.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" |
|
michaelpg
2016/12/01 20:15:34
duplicate include
stevenjb
2016/12/02 00:39:19
Done.
|
| +#include "chrome/browser/ui/app_list/arc/arc_app_utils.h" // kSettingsAppId |
| + |
| +namespace chromeos { |
| +namespace settings { |
| + |
| +AndroidAppsHandler::AndroidAppsHandler(Profile* profile) |
| + : arc_prefs_observer_(this), profile_(profile), weak_ptr_factory_(this) {} |
| + |
| +AndroidAppsHandler::~AndroidAppsHandler() {} |
| + |
| +void AndroidAppsHandler::RegisterMessages() { |
| + web_ui()->RegisterMessageCallback( |
| + "getAndroidAppsInfo", |
| + base::Bind(&AndroidAppsHandler::HandleGetAndroidAppsInfo, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + web_ui()->RegisterMessageCallback( |
| + "showAndroidAppsSettings", |
| + base::Bind(&AndroidAppsHandler::ShowAndroidAppsSettings, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void AndroidAppsHandler::OnJavascriptAllowed() { |
| + ArcAppListPrefs* arc_prefs = ArcAppListPrefs::Get(profile_); |
| + if (arc_prefs) |
| + arc_prefs_observer_.Add(arc_prefs); |
| +} |
| + |
| +void AndroidAppsHandler::OnJavascriptDisallowed() { |
| + arc_prefs_observer_.RemoveAll(); |
| +} |
| + |
| +void AndroidAppsHandler::OnAppRegistered( |
| + const std::string& app_id, |
| + const ArcAppListPrefs::AppInfo& app_info) { |
| + OnAppChanged(app_id); |
| +} |
| + |
| +void AndroidAppsHandler::OnAppRemoved(const std::string& app_id) { |
| + OnAppChanged(app_id); |
| +} |
| + |
| +void AndroidAppsHandler::OnAppReadyChanged(const std::string& app_id, |
| + bool ready) { |
| + OnAppChanged(app_id); |
| +} |
| + |
| +void AndroidAppsHandler::OnAppChanged(const std::string& app_id) { |
| + if (app_id != arc::kSettingsAppId) |
| + return; |
| + SendAndroidAppsInfo(); |
| +} |
| + |
| +std::unique_ptr<base::DictionaryValue> |
| +AndroidAppsHandler::BuildAndroidAppsInfo() { |
| + std::unique_ptr<base::DictionaryValue> info(new base::DictionaryValue); |
| + bool app_ready = false; |
| + if (arc::ArcSessionManager::Get()->IsArcEnabled()) { |
| + std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = |
| + ArcAppListPrefs::Get(profile_)->GetApp(arc::kSettingsAppId); |
| + app_ready = app_info && app_info->ready; |
| + } |
| + info->SetBoolean("appReady", app_ready); |
| + return info; |
| +} |
| + |
| +void AndroidAppsHandler::HandleGetAndroidAppsInfo(const base::ListValue* args) { |
| + CHECK_EQ(1U, args->GetSize()); |
| + const base::Value* callback_id = nullptr; |
| + args->Get(0, &callback_id); |
| + CHECK(callback_id); |
| + |
| + AllowJavascript(); |
| + std::unique_ptr<base::DictionaryValue> info = BuildAndroidAppsInfo(); |
| + ResolveJavascriptCallback(*callback_id, *info); |
|
michaelpg
2016/12/01 20:15:34
this function seems a bit redundant. since JS alre
stevenjb
2016/12/02 00:39:19
Sure, I can make this a 'request' instead of a 'ge
|
| +} |
| + |
| +void AndroidAppsHandler::SendAndroidAppsInfo() { |
| + AllowJavascript(); |
| + std::unique_ptr<base::DictionaryValue> info = BuildAndroidAppsInfo(); |
| + CallJavascriptFunction("cr.webUIListenerCallback", |
| + base::StringValue("android-apps-info-update"), *info); |
| +} |
| + |
| +void AndroidAppsHandler::ShowAndroidAppsSettings(const base::ListValue* args) { |
| + // Settings in secondary profile cannot access ARC. |
| + CHECK(arc::ArcSessionManager::IsAllowedForProfile(profile_)); |
| + arc::LaunchAndroidSettingsApp(profile_); |
| +} |
| + |
| +} // namespace settings |
| +} // namespace chromeos |