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_utils.h" // kSettingsAppId |
| 11 #include "ui/events/event_constants.h" |
| 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 "requestAndroidAppsInfo", |
| 24 base::Bind(&AndroidAppsHandler::HandleRequestAndroidAppsInfo, |
| 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::HandleRequestAndroidAppsInfo( |
| 77 const base::ListValue* args) { |
| 78 SendAndroidAppsInfo(); |
| 79 } |
| 80 |
| 81 void AndroidAppsHandler::SendAndroidAppsInfo() { |
| 82 AllowJavascript(); |
| 83 std::unique_ptr<base::DictionaryValue> info = BuildAndroidAppsInfo(); |
| 84 CallJavascriptFunction("cr.webUIListenerCallback", |
| 85 base::StringValue("android-apps-info-update"), *info); |
| 86 } |
| 87 |
| 88 void AndroidAppsHandler::ShowAndroidAppsSettings(const base::ListValue* args) { |
| 89 CHECK_EQ(1U, args->GetSize()); |
| 90 bool activated_from_keyboard = false; |
| 91 args->GetBoolean(0, &activated_from_keyboard); |
| 92 int flags = activated_from_keyboard ? ui::EF_NONE : ui::EF_LEFT_MOUSE_BUTTON; |
| 93 |
| 94 // Settings in secondary profile cannot access ARC. |
| 95 CHECK(arc::ArcSessionManager::IsAllowedForProfile(profile_)); |
| 96 arc::LaunchAndroidSettingsApp(profile_, flags); |
| 97 } |
| 98 |
| 99 } // namespace settings |
| 100 } // namespace chromeos |
OLD | NEW |