d8f91b470679dffa35f22df7354c72d80ca10879
[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   'presentation' : [],
41   'rawsockets' : [],
42   'screenorientation' : [],
43   'vibration' : ['android.permission.VIBRATE']
44 }
45
46
47 def HandlePermissions(permissions, xmldoc):
48   """ Implement the mapping of permission list to the AndroidManifest.xml file.
49   Args:
50     permissions: the permissions defined in Crosswalk and Android Manifest
51         specification.
52     xmldoc: the parsed xmldoc of the AndroidManifest.xml file, used for
53         reading and writing.
54   """
55   if permissions:
56     existing_permission_list = []
57     used_permissions = xmldoc.getElementsByTagName("uses-permission")
58     for item in used_permissions:
59       existing_permission_list.append(item.getAttribute("android:name"))
60
61     for permission in permissions.split(':'):
62       if permission.lower() not in list(permission_mapping_table.keys()):
63         print('Error: permission \'%s\' related API is not supported.'
64               % permission)
65         sys.exit(1)
66       permission_item = permission_mapping_table.get(permission.lower())
67       if permission_item:
68         for android_permission in permission_item:
69           if android_permission not in existing_permission_list:
70             existing_permission_list.append(android_permission)
71             AddElementAttribute(xmldoc, 'uses-permission',
72                                 'android:name', android_permission)
73
74
75 def main(argv):
76   """Respond to command mode of the mapping permission list."""
77   parser = optparse.OptionParser()
78   info = ('The input json-format file name. Such as: '
79           '--jsonfile=manifest.json')
80   parser.add_option('-j', '--jsonfile', action='store', dest='jsonfile',
81                     help=info)
82   info = ('The destination android manifest file name. Such as: '
83           '--manifest=AndroidManifest.xml')
84   parser.add_option('-m', '--manifest', action='store', dest='manifest',
85                     help=info)
86   options, _ = parser.parse_args()
87   if len(argv) == 1:
88     parser.print_help()
89     return 0
90   if not options.jsonfile:
91     print('Please set the manifest.json file.')
92     return 1
93   if not options.manifest:
94     print('Please set the AndroidManifest.xml file.')
95     return 1
96   try:
97     json_parser = ManifestJsonParser(os.path.expanduser(options.jsonfile))
98     if json_parser.GetPermissions():
99       options.permissions = json_parser.GetPermissions()
100   except SystemExit as ec:
101     print('Exiting with error code: %d' % ec.code)
102     return ec.code
103   try:
104     xmldoc = minidom.parse(options.manifest)
105     HandlePermissions(options.permissions, xmldoc)
106     file_handle = open(options.manifest, 'wb')
107     xmldoc.writexml(file_handle)
108     file_handle.close()
109   except (ExpatError, IOError):
110     print('There is an error in AndroidManifest.xml.')
111     return 1
112   return 0
113
114
115 if __name__ == '__main__':
116   sys.exit(main(sys.argv))