| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/webapks_handler.h" | 5 #include "chrome/browser/ui/webui/webapks_handler.h" |
| 6 | 6 |
| 7 #include <string> |
| 8 |
| 7 #include "base/callback_forward.h" | 9 #include "base/callback_forward.h" |
| 10 #include "base/strings/stringprintf.h" |
| 8 #include "base/values.h" | 11 #include "base/values.h" |
| 9 #include "chrome/browser/android/shortcut_helper.h" | 12 #include "chrome/browser/android/shortcut_helper.h" |
| 10 #include "content/public/browser/web_ui.h" | 13 #include "content/public/browser/web_ui.h" |
| 14 #include "content/public/common/manifest_util.h" |
| 15 #include "ui/gfx/color_utils.h" |
| 16 |
| 17 namespace { |
| 18 // Converts a color from the format documented in content::Manifest to a |
| 19 // rgba() CSS string. |
| 20 std::string ColorToString(int64_t color) { |
| 21 if (color == content::Manifest::kInvalidOrMissingColor) |
| 22 return std::string(); |
| 23 return color_utils::SkColorToRgbaString(reinterpret_cast<uint32_t&>(color)); |
| 24 } |
| 25 } // namespace |
| 11 | 26 |
| 12 WebApksHandler::WebApksHandler() : weak_ptr_factory_(this) {} | 27 WebApksHandler::WebApksHandler() : weak_ptr_factory_(this) {} |
| 13 | 28 |
| 14 WebApksHandler::~WebApksHandler() {} | 29 WebApksHandler::~WebApksHandler() {} |
| 15 | 30 |
| 16 void WebApksHandler::RegisterMessages() { | 31 void WebApksHandler::RegisterMessages() { |
| 17 web_ui()->RegisterMessageCallback( | 32 web_ui()->RegisterMessageCallback( |
| 18 "requestWebApksInfo", | 33 "requestWebApksInfo", |
| 19 base::Bind(&WebApksHandler::HandleRequestWebApksInfo, | 34 base::Bind(&WebApksHandler::HandleRequestWebApksInfo, |
| 20 base::Unretained(this))); | 35 base::Unretained(this))); |
| 21 } | 36 } |
| 22 | 37 |
| 23 void WebApksHandler::HandleRequestWebApksInfo(const base::ListValue* args) { | 38 void WebApksHandler::HandleRequestWebApksInfo(const base::ListValue* args) { |
| 24 AllowJavascript(); | 39 AllowJavascript(); |
| 25 ShortcutHelper::RetrieveWebApks(base::Bind( | 40 ShortcutHelper::RetrieveWebApks(base::Bind( |
| 26 &WebApksHandler::OnWebApkInfoRetrieved, weak_ptr_factory_.GetWeakPtr())); | 41 &WebApksHandler::OnWebApkInfoRetrieved, weak_ptr_factory_.GetWeakPtr())); |
| 27 } | 42 } |
| 28 | 43 |
| 29 void WebApksHandler::OnWebApkInfoRetrieved( | 44 void WebApksHandler::OnWebApkInfoRetrieved( |
| 30 const std::vector<WebApkInfo>& webapks_list) { | 45 const std::vector<WebApkInfo>& webapks_list) { |
| 31 if (!IsJavascriptAllowed()) | 46 if (!IsJavascriptAllowed()) |
| 32 return; | 47 return; |
| 33 base::ListValue list; | 48 base::ListValue list; |
| 34 for (const auto& webapk_info : webapks_list) { | 49 for (const auto& webapk_info : webapks_list) { |
| 35 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 50 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
| 51 result->SetString("name", webapk_info.name); |
| 36 result->SetString("shortName", webapk_info.short_name); | 52 result->SetString("shortName", webapk_info.short_name); |
| 37 result->SetString("packageName", webapk_info.package_name); | 53 result->SetString("packageName", webapk_info.package_name); |
| 38 result->SetInteger("shellApkVersion", webapk_info.shell_apk_version); | 54 result->SetInteger("shellApkVersion", webapk_info.shell_apk_version); |
| 39 result->SetInteger("versionCode", webapk_info.version_code); | 55 result->SetInteger("versionCode", webapk_info.version_code); |
| 56 result->SetString("uri", webapk_info.uri); |
| 57 result->SetString("scope", webapk_info.scope); |
| 58 result->SetString("manifestUrl", webapk_info.manifest_url); |
| 59 result->SetString("manifestStartUrl", webapk_info.manifest_start_url); |
| 60 result->SetString("displayMode", |
| 61 content::WebDisplayModeToString(webapk_info.display)); |
| 62 result->SetString( |
| 63 "orientation", |
| 64 content::WebScreenOrientationLockTypeToString(webapk_info.orientation)); |
| 65 result->SetString("themeColor", ColorToString(webapk_info.theme_color)); |
| 66 result->SetString("backgroundColor", |
| 67 ColorToString(webapk_info.background_color)); |
| 40 list.Append(std::move(result)); | 68 list.Append(std::move(result)); |
| 41 } | 69 } |
| 42 | 70 |
| 43 CallJavascriptFunction("returnWebApksInfo", list); | 71 CallJavascriptFunction("returnWebApksInfo", list); |
| 44 } | 72 } |
| OLD | NEW |