Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / xwalk / app / tools / android / handle_permissions.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2014 Intel Corporation. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 """
8 Provide the function interface of mapping the permissions
9 from manifest.json to AndroidManifest.xml.
10 It suppports the mapping of permission fields both defined in Crosswalk
11 and supported by Android Manifest specification.
12
13 Sample usage from shell script:
14 python handle_permissions.py --jsonfile=/path/to/manifest.json
15 --manifest=/path/to/AndroidManifest.xml
16 """
17
18 import optparse
19 import os
20 import sys
21
22 from handle_xml import AddElementAttribute
23 from manifest_json_parser import ManifestJsonParser
24 from xml.dom import minidom
25 from xml.parsers.expat import ExpatError
26
27 # The global permission mapping table.
28 # TODO: update the permission mapping table for added permission.
29 permission_mapping_table = {
30     'contacts': ['android.permission.READ_CONTACTS',
31                  'android.permission.WRITE_CONTACTS'],
32     'geolocation': ['android.permission.ACCESS_FINE_LOCATION'],
33     'messaging': ['android.permission.READ_SMS',
34                   'android.permission.READ_PHONE_STATE',
35                   'android.permission.RECEIVE_SMS',
36                   'android.permission.SEND_SMS',
37                   'android.permission.WRITE_SMS'],
38     'devicecapabilities': [],
39     'fullscreen': [],
40     'iap': ['com.android.vending.BILLING'],
41     'nfc': ['android.permission.NFC'],
42     'presentation': [],
43     'rawsockets': [],
44     'screenorientation': [],
45     'vibration': ['android.permission.VIBRATE']
46 }
47
48
49 def HandlePermissions(permissions, xmldoc):
50   """ Implement the mapping of permission list to the AndroidManifest.xml file.
51   Args:
52     permissions: the permissions defined in Crosswalk and Android Manifest
53         specification.
54     xmldoc: the parsed xmldoc of the AndroidManifest.xml file, used for
55         reading and writing.
56   """
57   if permissions:
58     existing_permission_list = []
59     used_permissions = xmldoc.getElementsByTagName("uses-permission")
60     for item in used_permissions:
61       existing_permission_list.append(item.getAttribute("android:name"))
62
63     for permission in permissions.split(':'):
64       if permission.lower() not in list(permission_mapping_table.keys()):
65         print('Error: permission \'%s\' related API is not supported.'
66               % permission)
67         sys.exit(1)
68       permission_item = permission_mapping_table.get(permission.lower())
69       if permission_item:
70         for android_permission in permission_item:
71           if android_permission not in existing_permission_list:
72             existing_permission_list.append(android_permission)
73             AddElementAttribute(xmldoc, 'uses-permission',
74                                 'android:name', android_permission)
75
76
77 def main(argv):
78   """Respond to command mode of the mapping permission list."""
79   parser = optparse.OptionParser()
80   info = ('The input json-format file name. Such as: '
81           '--jsonfile=manifest.json')
82   parser.add_option('-j', '--jsonfile', action='store', dest='jsonfile',
83                     help=info)
84   info = ('The destination android manifest file name. Such as: '
85           '--manifest=AndroidManifest.xml')
86   parser.add_option('-m', '--manifest', action='store', dest='manifest',
87                     help=info)
88   options, _ = parser.parse_args()
89   if len(argv) == 1:
90     parser.print_help()
91     return 0
92   if not options.jsonfile:
93     print('Please set the manifest.json file.')
94     return 1
95   if not options.manifest:
96     print('Please set the AndroidManifest.xml file.')
97     return 1
98   try:
99     json_parser = ManifestJsonParser(os.path.expanduser(options.jsonfile))
100     if json_parser.GetPermissions():
101       options.permissions = json_parser.GetPermissions()
102   except SystemExit as ec:
103     print('Exiting with error code: %d' % ec.code)
104     return ec.code
105   try:
106     xmldoc = minidom.parse(options.manifest)
107     HandlePermissions(options.permissions, xmldoc)
108     file_handle = open(options.manifest, 'wb')
109     xmldoc.writexml(file_handle)
110     file_handle.close()
111   except (ExpatError, IOError):
112     print('There is an error in AndroidManifest.xml.')
113     return 1
114   return 0
115
116
117 if __name__ == '__main__':
118   sys.exit(main(sys.argv))