Chromium Code Reviews| Index: chrome/browser/ui/webui/webapks_handler.cc |
| diff --git a/chrome/browser/ui/webui/webapks_handler.cc b/chrome/browser/ui/webui/webapks_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6a07aa7d2e4d5741dd1db212f03b287446b4ac41 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/webapks_handler.cc |
| @@ -0,0 +1,48 @@ |
| +// Copyright (c) 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/webapks_handler.h" |
| + |
| +#include "base/callback_forward.h" |
| +#include "chrome/browser/android/shortcut_helper.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/grit/generated_resources.h" |
| +#include "components/strings/grit/components_strings.h" |
| +#include "components/webapks_ui/webapks_ui_constants.h" |
| +#include "content/public/browser/web_ui.h" |
| + |
| +WebApksHandler::WebApksHandler() : weak_ptr_factory_(this) {} |
| + |
| +WebApksHandler::~WebApksHandler() {} |
| + |
| +void WebApksHandler::RegisterMessages() { |
| + web_ui()->RegisterMessageCallback( |
| + webapks_ui::kRequestWebApksInfo, |
|
pkotwicz
2017/01/18 02:08:21
Nit: You can inline the JS method name here the wa
gonzalon
2017/01/18 15:29:32
Done.
|
| + base::Bind(&WebApksHandler::HandleRequestWebApksInfo, |
| + base::Unretained(this))); |
| +} |
| + |
| +void WebApksHandler::HandleRequestWebApksInfo(const base::ListValue* args) { |
| + // The WebApkInfoCallback will delete itself after it is done. |
| + base::Callback<void(std::vector<WebApkInfo*>)> callback = base::Bind( |
| + &WebApksHandler::OnWebApkInfoReceived, weak_ptr_factory_.GetWeakPtr()); |
| + ShortcutHelper::ListWebApks(base::android::AttachCurrentThread(), callback); |
| +} |
| + |
| +void WebApksHandler::OnWebApkInfoReceived( |
| + std::vector<WebApkInfo*> webapks_list) { |
| + base::ListValue* list = new base::ListValue(); |
|
pkotwicz
2017/01/18 02:08:21
Can you create the value on the stack instead? In
gonzalon
2017/01/18 15:29:32
Yeah, not sure why I used so many pointers, it's b
|
| + for (size_t i = 0; i < webapks_list.size(); ++i) { |
| + const WebApkInfo* webapk_info = webapks_list[i]; |
| + std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
|
pkotwicz
2017/01/18 02:08:21
Awesome! Creating base::DictionaryValue on the hea
|
| + result->SetString("shortName", webapk_info->short_name); |
| + result->SetString("packageName", webapk_info->package_name); |
| + result->SetInteger("shellApkVersion", webapk_info->shell_apk_version); |
| + result->SetInteger("versionCode", webapk_info->version_code); |
| + delete webapk_info; |
| + list->Append(std::move(result)); |
| + } |
| + |
| + web_ui()->CallJavascriptFunctionUnsafe(webapks_ui::kReturnWebApksInfo, *list); |
| +} |