OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/device_stylus_handler.h" |
| 6 |
| 7 #include "ash/common/system/chromeos/palette/palette_utils.h" |
| 8 #include "base/bind.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "ui/events/devices/input_device_manager.h" |
| 11 |
| 12 namespace chromeos { |
| 13 namespace settings { |
| 14 |
| 15 namespace { |
| 16 |
| 17 // Keys in objects passed to onNoteTakingAppsUpdated. |
| 18 constexpr char kAppNameKey[] = "name"; |
| 19 constexpr char kAppIdKey[] = "value"; |
| 20 constexpr char kAppPreferredKey[] = "preferred"; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 StylusHandler::StylusHandler() { |
| 25 NoteTakingHelper::Get()->AddObserver(this); |
| 26 ui::InputDeviceManager::GetInstance()->AddObserver(this); |
| 27 } |
| 28 |
| 29 StylusHandler::~StylusHandler() { |
| 30 ui::InputDeviceManager::GetInstance()->RemoveObserver(this); |
| 31 NoteTakingHelper::Get()->RemoveObserver(this); |
| 32 } |
| 33 |
| 34 void StylusHandler::RegisterMessages() { |
| 35 DCHECK(web_ui()); |
| 36 |
| 37 web_ui()->RegisterMessageCallback( |
| 38 "initializeStylusSettings", |
| 39 base::Bind(&StylusHandler::HandleInitialize, base::Unretained(this))); |
| 40 web_ui()->RegisterMessageCallback( |
| 41 "requestNoteTakingApps", |
| 42 base::Bind(&StylusHandler::RequestApps, base::Unretained(this))); |
| 43 web_ui()->RegisterMessageCallback( |
| 44 "setPreferredNoteTakingApp", |
| 45 base::Bind(&StylusHandler::SetPreferredNoteTakingApp, |
| 46 base::Unretained(this))); |
| 47 } |
| 48 |
| 49 void StylusHandler::OnAvailableNoteTakingAppsUpdated() { |
| 50 UpdateNoteTakingApps(); |
| 51 } |
| 52 |
| 53 void StylusHandler::OnDeviceListsComplete() { |
| 54 SendHasStylus(); |
| 55 } |
| 56 |
| 57 void StylusHandler::UpdateNoteTakingApps() { |
| 58 bool waiting_for_android = false; |
| 59 note_taking_app_ids_.clear(); |
| 60 base::ListValue apps_list; |
| 61 |
| 62 NoteTakingHelper* helper = NoteTakingHelper::Get(); |
| 63 if (helper->android_enabled() && !helper->android_apps_received()) { |
| 64 // If Android is enabled but not ready yet, let the JS know so it can |
| 65 // disable the menu and display an explanatory message. |
| 66 waiting_for_android = true; |
| 67 } else { |
| 68 std::vector<NoteTakingAppInfo> available_apps = |
| 69 NoteTakingHelper::Get()->GetAvailableApps(Profile::FromWebUI(web_ui())); |
| 70 for (const NoteTakingAppInfo& info : available_apps) { |
| 71 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 72 dict->SetString(kAppNameKey, info.name); |
| 73 dict->SetString(kAppIdKey, info.app_id); |
| 74 dict->SetBoolean(kAppPreferredKey, info.preferred); |
| 75 apps_list.Append(std::move(dict)); |
| 76 |
| 77 note_taking_app_ids_.insert(info.app_id); |
| 78 } |
| 79 } |
| 80 |
| 81 AllowJavascript(); |
| 82 CallJavascriptFunction( |
| 83 "cr.webUIListenerCallback", base::StringValue("onNoteTakingAppsUpdated"), |
| 84 apps_list, base::FundamentalValue(waiting_for_android)); |
| 85 } |
| 86 |
| 87 void StylusHandler::RequestApps(const base::ListValue* unused_args) { |
| 88 UpdateNoteTakingApps(); |
| 89 } |
| 90 |
| 91 void StylusHandler::SetPreferredNoteTakingApp(const base::ListValue* args) { |
| 92 std::string app_id; |
| 93 CHECK(args->GetString(0, &app_id)); |
| 94 |
| 95 // Sanity check: make sure that the ID we got back from WebUI is in the |
| 96 // currently-available set. |
| 97 if (!note_taking_app_ids_.count(app_id)) { |
| 98 LOG(ERROR) << "Got unknown note-taking-app ID \"" << app_id << "\""; |
| 99 return; |
| 100 } |
| 101 |
| 102 NoteTakingHelper::Get()->SetPreferredApp(Profile::FromWebUI(web_ui()), |
| 103 app_id); |
| 104 } |
| 105 |
| 106 void StylusHandler::HandleInitialize(const base::ListValue* args) { |
| 107 if (ui::InputDeviceManager::GetInstance()->AreDeviceListsComplete()) |
| 108 SendHasStylus(); |
| 109 } |
| 110 |
| 111 void StylusHandler::SendHasStylus() { |
| 112 DCHECK(ui::InputDeviceManager::GetInstance()->AreDeviceListsComplete()); |
| 113 AllowJavascript(); |
| 114 CallJavascriptFunction( |
| 115 "cr.webUIListenerCallback", base::StringValue("has-stylus-changed"), |
| 116 base::FundamentalValue(ash::palette_utils::HasStylusInput())); |
| 117 } |
| 118 |
| 119 } // namespace settings |
| 120 } // namespace chromeos |
OLD | NEW |