Upstream version 9.38.198.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 "xwalk/application/common/tizen/package_path.h"
16
17 #include "third_party/re2/re2/re2.h"
18 #endif
19
20 namespace xwalk {
21 namespace application {
22 namespace {
23 #if defined(OS_TIZEN)
24 const char kWGTAppIdPattern[] = "\\A([0-9a-zA-Z]{10})[.][0-9a-zA-Z]{1,52}\\z";
25 const char kXPKAppIdPattern[] = "\\Axwalk[.]([a-p]{32})\\z";
26 const char kPkgIdPattern[] = "\\A([0-9a-zA-Z]{10,})\\z";
27 const std::string kAppIdPrefix("xwalk.");
28 #endif
29 const size_t kIdSize = 16;
30 }  // namespace
31
32 // Converts a normal hexadecimal string into the alphabet used by applications.
33 // We use the characters 'a'-'p' instead of '0'-'f' to avoid ever having a
34 // completely numeric host, since some software interprets that as an IP
35 // address.
36 static void ConvertHexadecimalToIDAlphabet(std::string* id) {
37   for (size_t i = 0; i < id->size(); ++i) {
38     int val;
39     if (base::HexStringToInt(base::StringPiece(
40             id->begin() + i, id->begin() + i + 1), &val)) {
41       (*id)[i] = val + 'a';
42     } else {
43       (*id)[i] = 'a';
44     }
45   }
46 }
47
48 std::string GenerateId(const std::string& input) {
49   uint8 hash[kIdSize];
50   crypto::SHA256HashString(input, hash, sizeof(hash));
51   std::string output =
52       base::StringToLowerASCII(base::HexEncode(hash, sizeof(hash)));
53   ConvertHexadecimalToIDAlphabet(&output);
54
55 #if defined(OS_TIZEN)
56   return kAppIdPrefix + output;
57 #else
58   return output;
59 #endif
60 }
61
62 std::string GenerateIdForPath(const base::FilePath& path) {
63   std::string path_bytes =
64       std::string(reinterpret_cast<const char*>(path.value().data()),
65                   path.value().size() * sizeof(base::FilePath::CharType));
66   return GenerateId(path_bytes);
67 }
68
69 #if defined(OS_TIZEN)
70 bool IsValidWGTID(const std::string& id) {
71   return RE2::FullMatch(id, kWGTAppIdPattern);
72 }
73
74 bool IsValidXPKID(const std::string& id) {
75   return RE2::FullMatch(id, kXPKAppIdPattern);
76 }
77
78 bool IsValidPkgID(const std::string& id) {
79   return RE2::FullMatch(id, kPkgIdPattern);
80 }
81
82 std::string AppIdToPkgId(const std::string& id) {
83   std::string package_id;
84   if (!RE2::FullMatch(id, kWGTAppIdPattern, &package_id) &&
85       !RE2::FullMatch(id, kXPKAppIdPattern, &package_id)) {
86     LOG(ERROR) << "Cannot get package_id from invalid app id";
87     return std::string();
88   }
89   return package_id;
90 }
91
92 std::string PkgIdToAppId(const std::string& id) {
93   base::FilePath app_path = GetPackagePath(id);
94   if (app_path.empty())
95     return std::string();
96
97   return app_path.BaseName().value();
98 }
99
100 #endif
101
102 bool IsValidApplicationID(const std::string& id) {
103 #if defined(OS_TIZEN)
104   if (IsValidWGTID(id) ||
105       IsValidXPKID(id))
106     return true;
107   return false;
108 #endif
109
110   std::string temp = base::StringToLowerASCII(id);
111   // Verify that the id is legal.
112   if (temp.size() != (kIdSize * 2))
113     return false;
114
115   // We only support lowercase IDs, because IDs can be used as URL components
116   // (where GURL will lowercase it).
117   for (size_t i = 0; i < temp.size(); ++i)
118     if (temp[i] < 'a' || temp[i] > 'p')
119       return false;
120
121   return true;
122 }
123
124 }  // namespace application
125 }  // namespace xwalk