Chromium Code Reviews| Index: chrome/browser/ui/webui/settings/chromeos/device_stylus_handler.cc |
| diff --git a/chrome/browser/ui/webui/settings/chromeos/device_stylus_handler.cc b/chrome/browser/ui/webui/settings/chromeos/device_stylus_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c96047d7ddc162272ec3abd795236bb16ddeb99a |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/settings/chromeos/device_stylus_handler.cc |
| @@ -0,0 +1,95 @@ |
| +// Copyright 2017 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/device_stylus_handler.h" |
| + |
| +#include "chrome/browser/profiles/profile.h" |
| + |
| +namespace chromeos { |
| +namespace settings { |
| + |
| +namespace { |
| + |
| +// Keys in objects passed to onNoteTakingAppsUpdated. |
| +constexpr char kAppNameKey[] = "name"; |
| +constexpr char kAppIdKey[] = "value"; |
| +constexpr char kAppPreferredKey[] = "preferred"; |
| + |
| +} // namespace |
| + |
| +StylusHandler::StylusHandler() { |
| + NoteTakingHelper::Get()->AddObserver(this); |
| +} |
| + |
| +StylusHandler::~StylusHandler() { |
| + NoteTakingHelper::Get()->RemoveObserver(this); |
| +} |
| + |
| +void StylusHandler::RegisterMessages() { |
| + DCHECK(web_ui()); |
| + |
| + web_ui()->RegisterMessageCallback( |
| + "requestNoteTakingApps", |
| + base::Bind(&StylusHandler::RequestApps, base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback( |
| + "setPreferredNoteTakingApp", |
| + base::Bind(&StylusHandler::SetPreferredNoteTakingApp, |
| + base::Unretained(this))); |
| +} |
| + |
| +void StylusHandler::OnAvailableNoteTakingAppsUpdated() { |
| + UpdateNoteTakingApps(); |
| +} |
| + |
| +void StylusHandler::UpdateNoteTakingApps() { |
| + bool waiting_for_android = false; |
| + note_taking_app_ids_.clear(); |
| + base::ListValue apps_list; |
| + |
| + NoteTakingHelper* helper = NoteTakingHelper::Get(); |
| + if (helper->android_enabled() && !helper->android_apps_received()) { |
| + // If Android is enabled but not ready yet, let the JS know so it can |
| + // disable the menu and display an explanatory message. |
| + waiting_for_android = true; |
| + } else { |
| + for (const NoteTakingAppInfo& info : |
| + NoteTakingHelper::Get()->GetAvailableApps( |
| + Profile::FromWebUI(web_ui()))) { |
|
stevenjb
2017/01/19 17:48:53
nit: This might be a little more clear with:
auto
jdufault
2017/01/25 00:52:22
Done.
|
| + auto dict = base::MakeUnique<base::DictionaryValue>(); |
| + dict->SetString(kAppNameKey, info.name); |
| + dict->SetString(kAppIdKey, info.app_id); |
| + dict->SetBoolean(kAppPreferredKey, info.preferred); |
| + apps_list.Append(std::move(dict)); |
| + |
| + note_taking_app_ids_.insert(info.app_id); |
| + } |
| + } |
| + |
| + CallJavascriptFunction( |
| + "cr.webUIListenerCallback", base::StringValue("onNoteTakingAppsUpdated"), |
| + apps_list, base::FundamentalValue(waiting_for_android)); |
| +} |
| + |
| +void StylusHandler::RequestApps(const base::ListValue* unused_args) { |
| + AllowJavascript(); |
| + UpdateNoteTakingApps(); |
| +} |
| + |
| +void StylusHandler::SetPreferredNoteTakingApp(const base::ListValue* args) { |
| + std::string app_id; |
| + CHECK(args->GetString(0, &app_id)); |
| + |
| + // Sanity check: make sure that the ID we got back from WebUI is in the |
| + // currently-available set. |
| + if (!note_taking_app_ids_.count(app_id)) { |
| + LOG(ERROR) << "Got unknown note-taking-app ID \"" << app_id << "\""; |
| + return; |
| + } |
| + |
| + NoteTakingHelper::Get()->SetPreferredApp(Profile::FromWebUI(web_ui()), |
|
Daniel Erat
2017/01/19 00:50:05
since we discussed it before: it ended up making s
jdufault
2017/01/25 00:52:22
Yea, given the nature of how preferred values are
|
| + app_id); |
| +} |
| + |
| +} // namespace settings |
| +} // namespace chromeos |