Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/webui/settings/chromeos/android_apps_handler.h" | |
| 6 | |
| 7 #include "base/values.h" | |
| 8 #include "chrome/browser/chromeos/arc/arc_session_manager.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #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.
| |
| 11 #include "chrome/browser/ui/app_list/arc/arc_app_utils.h" // kSettingsAppId | |
| 12 | |
| 13 namespace chromeos { | |
| 14 namespace settings { | |
| 15 | |
| 16 AndroidAppsHandler::AndroidAppsHandler(Profile* profile) | |
| 17 : arc_prefs_observer_(this), profile_(profile), weak_ptr_factory_(this) {} | |
| 18 | |
| 19 AndroidAppsHandler::~AndroidAppsHandler() {} | |
| 20 | |
| 21 void AndroidAppsHandler::RegisterMessages() { | |
| 22 web_ui()->RegisterMessageCallback( | |
| 23 "getAndroidAppsInfo", | |
| 24 base::Bind(&AndroidAppsHandler::HandleGetAndroidAppsInfo, | |
| 25 weak_ptr_factory_.GetWeakPtr())); | |
| 26 web_ui()->RegisterMessageCallback( | |
| 27 "showAndroidAppsSettings", | |
| 28 base::Bind(&AndroidAppsHandler::ShowAndroidAppsSettings, | |
| 29 weak_ptr_factory_.GetWeakPtr())); | |
| 30 } | |
| 31 | |
| 32 void AndroidAppsHandler::OnJavascriptAllowed() { | |
| 33 ArcAppListPrefs* arc_prefs = ArcAppListPrefs::Get(profile_); | |
| 34 if (arc_prefs) | |
| 35 arc_prefs_observer_.Add(arc_prefs); | |
| 36 } | |
| 37 | |
| 38 void AndroidAppsHandler::OnJavascriptDisallowed() { | |
| 39 arc_prefs_observer_.RemoveAll(); | |
| 40 } | |
| 41 | |
| 42 void AndroidAppsHandler::OnAppRegistered( | |
| 43 const std::string& app_id, | |
| 44 const ArcAppListPrefs::AppInfo& app_info) { | |
| 45 OnAppChanged(app_id); | |
| 46 } | |
| 47 | |
| 48 void AndroidAppsHandler::OnAppRemoved(const std::string& app_id) { | |
| 49 OnAppChanged(app_id); | |
| 50 } | |
| 51 | |
| 52 void AndroidAppsHandler::OnAppReadyChanged(const std::string& app_id, | |
| 53 bool ready) { | |
| 54 OnAppChanged(app_id); | |
| 55 } | |
| 56 | |
| 57 void AndroidAppsHandler::OnAppChanged(const std::string& app_id) { | |
| 58 if (app_id != arc::kSettingsAppId) | |
| 59 return; | |
| 60 SendAndroidAppsInfo(); | |
| 61 } | |
| 62 | |
| 63 std::unique_ptr<base::DictionaryValue> | |
| 64 AndroidAppsHandler::BuildAndroidAppsInfo() { | |
| 65 std::unique_ptr<base::DictionaryValue> info(new base::DictionaryValue); | |
| 66 bool app_ready = false; | |
| 67 if (arc::ArcSessionManager::Get()->IsArcEnabled()) { | |
| 68 std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = | |
| 69 ArcAppListPrefs::Get(profile_)->GetApp(arc::kSettingsAppId); | |
| 70 app_ready = app_info && app_info->ready; | |
| 71 } | |
| 72 info->SetBoolean("appReady", app_ready); | |
| 73 return info; | |
| 74 } | |
| 75 | |
| 76 void AndroidAppsHandler::HandleGetAndroidAppsInfo(const base::ListValue* args) { | |
| 77 CHECK_EQ(1U, args->GetSize()); | |
| 78 const base::Value* callback_id = nullptr; | |
| 79 args->Get(0, &callback_id); | |
| 80 CHECK(callback_id); | |
| 81 | |
| 82 AllowJavascript(); | |
| 83 std::unique_ptr<base::DictionaryValue> info = BuildAndroidAppsInfo(); | |
| 84 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
| |
| 85 } | |
| 86 | |
| 87 void AndroidAppsHandler::SendAndroidAppsInfo() { | |
| 88 AllowJavascript(); | |
| 89 std::unique_ptr<base::DictionaryValue> info = BuildAndroidAppsInfo(); | |
| 90 CallJavascriptFunction("cr.webUIListenerCallback", | |
| 91 base::StringValue("android-apps-info-update"), *info); | |
| 92 } | |
| 93 | |
| 94 void AndroidAppsHandler::ShowAndroidAppsSettings(const base::ListValue* args) { | |
| 95 // Settings in secondary profile cannot access ARC. | |
| 96 CHECK(arc::ArcSessionManager::IsAllowedForProfile(profile_)); | |
| 97 arc::LaunchAndroidSettingsApp(profile_); | |
| 98 } | |
| 99 | |
| 100 } // namespace settings | |
| 101 } // namespace chromeos | |
| OLD | NEW |