Chromium Code Reviews| 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" | |
| 11 | 15 |
| 12 WebApksHandler::WebApksHandler() : weak_ptr_factory_(this) {} | 16 WebApksHandler::WebApksHandler() : weak_ptr_factory_(this) {} |
| 13 | 17 |
| 14 WebApksHandler::~WebApksHandler() {} | 18 WebApksHandler::~WebApksHandler() {} |
| 15 | 19 |
| 16 void WebApksHandler::RegisterMessages() { | 20 void WebApksHandler::RegisterMessages() { |
| 17 web_ui()->RegisterMessageCallback( | 21 web_ui()->RegisterMessageCallback( |
| 18 "requestWebApksInfo", | 22 "requestWebApksInfo", |
| 19 base::Bind(&WebApksHandler::HandleRequestWebApksInfo, | 23 base::Bind(&WebApksHandler::HandleRequestWebApksInfo, |
| 20 base::Unretained(this))); | 24 base::Unretained(this))); |
| 21 } | 25 } |
| 22 | 26 |
| 23 void WebApksHandler::HandleRequestWebApksInfo(const base::ListValue* args) { | 27 void WebApksHandler::HandleRequestWebApksInfo(const base::ListValue* args) { |
| 24 AllowJavascript(); | 28 AllowJavascript(); |
| 25 ShortcutHelper::RetrieveWebApks(base::Bind( | 29 ShortcutHelper::RetrieveWebApks(base::Bind( |
| 26 &WebApksHandler::OnWebApkInfoRetrieved, weak_ptr_factory_.GetWeakPtr())); | 30 &WebApksHandler::OnWebApkInfoRetrieved, weak_ptr_factory_.GetWeakPtr())); |
| 27 } | 31 } |
| 28 | 32 |
| 33 // Converts a color from the format specified in content::Manifest to a CSS | |
| 34 // string. | |
| 35 std::string ColorToString(int64_t color) { | |
| 36 if (color == content::Manifest::kInvalidOrMissingColor) | |
| 37 return ""; | |
| 38 | |
| 39 SkColor sk_color = reinterpret_cast<uint32_t&>(color); | |
| 40 int r = SkColorGetR(sk_color); | |
| 41 int g = SkColorGetG(sk_color); | |
| 42 int b = SkColorGetB(sk_color); | |
| 43 double a = SkColorGetA(sk_color) / 255.0; | |
| 44 return base::StringPrintf("rgba(%d,%d,%d,%.2f)", r, g, b, a); | |
| 45 } | |
|
Dan Beam
2017/02/24 18:36:16
can this share code with
https://cs.chromium.org/
gonzalon
2017/02/24 22:30:52
Yes, we probably can. https://cs.chromium.org/chro
Dan Beam
2017/02/24 22:33:52
i think they both share chrome/browser/ui/webui as
| |
| 46 | |
| 29 void WebApksHandler::OnWebApkInfoRetrieved( | 47 void WebApksHandler::OnWebApkInfoRetrieved( |
| 30 const std::vector<WebApkInfo>& webapks_list) { | 48 const std::vector<WebApkInfo>& webapks_list) { |
| 31 if (!IsJavascriptAllowed()) | 49 if (!IsJavascriptAllowed()) |
| 32 return; | 50 return; |
| 33 base::ListValue list; | 51 base::ListValue list; |
| 34 for (const auto& webapk_info : webapks_list) { | 52 for (const auto& webapk_info : webapks_list) { |
| 35 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 53 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
| 54 result->SetString("name", webapk_info.name); | |
| 36 result->SetString("shortName", webapk_info.short_name); | 55 result->SetString("shortName", webapk_info.short_name); |
| 37 result->SetString("packageName", webapk_info.package_name); | 56 result->SetString("packageName", webapk_info.package_name); |
| 38 result->SetInteger("shellApkVersion", webapk_info.shell_apk_version); | 57 result->SetInteger("shellApkVersion", webapk_info.shell_apk_version); |
| 39 result->SetInteger("versionCode", webapk_info.version_code); | 58 result->SetInteger("versionCode", webapk_info.version_code); |
| 59 result->SetString("uri", webapk_info.uri); | |
| 60 result->SetString("scope", webapk_info.scope); | |
| 61 result->SetString("manifestUrl", webapk_info.manifest_url); | |
| 62 result->SetString("manifestStartUrl", webapk_info.manifest_start_url); | |
| 63 result->SetString("displayMode", | |
| 64 content::WebDisplayModeToString(webapk_info.display)); | |
| 65 result->SetString( | |
| 66 "orientation", | |
| 67 content::WebScreenOrientationLockTypeToString(webapk_info.orientation)); | |
| 68 result->SetString("themeColor", ColorToString(webapk_info.theme_color)); | |
| 69 result->SetString("backgroundColor", | |
| 70 ColorToString(webapk_info.background_color)); | |
| 40 list.Append(std::move(result)); | 71 list.Append(std::move(result)); |
| 41 } | 72 } |
| 42 | 73 |
| 43 CallJavascriptFunction("returnWebApksInfo", list); | 74 CallJavascriptFunction("returnWebApksInfo", list); |
| 44 } | 75 } |
| OLD | NEW |