Upstream version 8.37.183.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / id_util.cc
1 // Copyright (c) 2013 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 "xwalk/application/common/id_util.h"
6
7 #include "base/files/file_path.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "crypto/sha2.h"
11 #include "xwalk/application/common/application_manifest_constants.h"
12 #include "xwalk/application/common/manifest_handlers/tizen_application_handler.h"
13
14 #if defined(OS_TIZEN)
15 #include "third_party/re2/re2/re2.h"
16 #endif
17
18 namespace xwalk {
19 namespace application {
20 namespace {
21 #if defined(OS_TIZEN)
22 const char kTizenAppIdPattern[] = "\\A[0-9a-zA-Z]{10}[.][0-9a-zA-Z]{1,52}\\z";
23 const std::string kAppIdPrefix("xwalk.");
24 #endif
25 }  // namespace
26
27 // Converts a normal hexadecimal string into the alphabet used by applications.
28 // We use the characters 'a'-'p' instead of '0'-'f' to avoid ever having a
29 // completely numeric host, since some software interprets that as an IP
30 // address.
31 static void ConvertHexadecimalToIDAlphabet(std::string* id) {
32   for (size_t i = 0; i < id->size(); ++i) {
33     int val;
34     if (base::HexStringToInt(base::StringPiece(
35             id->begin() + i, id->begin() + i + 1), &val)) {
36       (*id)[i] = val + 'a';
37     } else {
38       (*id)[i] = 'a';
39     }
40   }
41 }
42
43 // First 16 bytes of SHA256 hashed public key.
44 const size_t kIdSize = 16;
45
46 #if defined(OS_TIZEN)
47 const size_t kLegacyTizenIdSize = 10;
48 #endif
49
50 std::string GenerateId(const std::string& input) {
51   uint8 hash[kIdSize];
52   crypto::SHA256HashString(input, hash, sizeof(hash));
53   std::string output = StringToLowerASCII(base::HexEncode(hash, sizeof(hash)));
54   ConvertHexadecimalToIDAlphabet(&output);
55
56   return output;
57 }
58
59 std::string GenerateIdForPath(const base::FilePath& path) {
60   std::string path_bytes =
61       std::string(reinterpret_cast<const char*>(path.value().data()),
62                   path.value().size() * sizeof(base::FilePath::CharType));
63   return GenerateId(path_bytes);
64 }
65
66 #if defined(OS_TIZEN)
67 std::string RawAppIdToCrosswalkAppId(const std::string& id) {
68   if (RE2::PartialMatch(id, kTizenAppIdPattern))
69     return GenerateId(id);
70   return id;
71 }
72
73 std::string RawAppIdToAppIdForTizenPkgmgrDB(const std::string& id) {
74   if (RE2::PartialMatch(id, kTizenAppIdPattern))
75     return id;
76   return kAppIdPrefix + id;
77 }
78
79 std::string TizenPkgmgrDBAppIdToRawAppId(const std::string& id) {
80   std::string raw_id;
81   if (RE2::FullMatch(id, "xwalk.(\\w+)", &raw_id))
82     return raw_id;
83   return id;
84 }
85
86 std::string GetTizenAppId(ApplicationData* application) {
87   if (application->GetPackageType() == xwalk::application::Package::XPK)
88     return application->ID();
89
90   const TizenApplicationInfo* tizen_app_info =
91       static_cast<TizenApplicationInfo*>(application->GetManifestData(
92           application_widget_keys::kTizenApplicationKey));
93   return tizen_app_info->id();
94 }
95 #endif
96
97 }  // namespace application
98 }  // namespace xwalk