[M120 Migration][MM] Handle live stream duration and currenttime
[platform/framework/web/chromium-efl.git] / tizen_src / downloadable / ewk_api_wrapper_generator.py
1 import glob
2 import sys
3 import os
4 from optparse import OptionParser
5
6 # Text file containing list of EWK APIs
7 filepath = 'API_LIST.txt'
8
9 API_LIST = []
10 FUNC_DEFI = []
11
12 # ewk_interface_main.cc defines few APIs (like ewk_init, ewk_shutdown etc.) to setup
13 # and preinitialize dl library parameters. Those APIs are excluded from auto-generated
14 # code to avoid duplicate symbols.
15 exclude_list = ["ewk_init", "ewk_shutdown", "ewk_set_version_policy", "ewk_wait_chromium_ready", "ewk_check_chromium_ready"]
16
17 # Few ewk headers are directly included in impl, so they are
18 # manually added here
19 main_headers_list = ["ewk_interface_main.h", "dlog_util.h"]
20 headers_list = ["ewk_cookie_parser.h", "ewk_media_playback_info_product.h", "ewk_media_subtitle_info_product.h"]
21 headers_list_inc = ["EWebKit.h", "EWebKit_internal.h", "EWebKit_product.h", "dlfcn.h"]
22
23 basepath = os.path.dirname(os.path.realpath(__file__))
24 path = basepath + "/../ewk/efl_integration/public/ewk*.h"
25
26 files = glob.glob(path)
27 with open(filepath, 'w') as outfile:
28   for file in files:
29     if (file.find("ewk_export.h") != -1):
30       continue;
31     f = open(file, 'r')
32     for line in f:
33       outfile.write(line)
34     f.close()
35
36 # Extract lines containing EXPORT_API
37 api_line = ""
38 with open(filepath, 'r') as f:
39   for line in f:
40     if (line.find('EXPORT_API') == -1):
41       continue
42     else:
43       api_line = api_line + line.strip()
44       while (api_line.find(");") == -1):
45         if sys.version_info < (3, 0):
46           api_line = api_line + " " + f.next().strip()
47         else:
48           api_line = api_line + " " + f.readline().strip()
49
50       # Special case for ewk_view_hit_test_request, because one of its args does not have name
51       if (api_line.find("Ewk_View_Hit_Test_Request_Callback") != -1):
52         api_line = api_line.replace("Callback", "Callback callback")
53
54       # Special case for ewk_context_background_music_set to avoid duplicate
55       if (api_line.find("ewk_context_background_music_set") != -1):
56         api_line = api_line.replace("Ewk_Context *ewkContext", "Ewk_Context* context")
57
58       API_LIST.append(api_line)
59       api_line = ""
60
61 # Parse function declarations extracted in API_LIST.txt
62 for line in API_LIST:
63   start = line.find('EXPORT_API')
64   line = line[start + len('EXPORT_API') + 1:]
65
66   # Parse to extract return type, api name and params
67   split_api_params = line.split("(")
68   api_name = split_api_params[0]
69   index = api_name.rfind(' ')
70   RETURN_TYPE = api_name[:index]
71   api_name = api_name[index+1:]
72   if api_name[0] == '*':
73     API_NAME = api_name[1:]
74     RETURN_TYPE = RETURN_TYPE + '*'
75   else:
76     API_NAME = api_name
77
78   # Since ewk_init and ewk_shutdown have manually written code, exclude them here
79   should_ignore = False
80   for api in exclude_list:
81     if (API_NAME == api):
82       should_ignore = True
83
84   if (should_ignore):
85     continue
86
87   # Generate params list from params string
88   params = split_api_params[1]
89   index = params.rfind(');')
90   PARAMS = params[:index]
91   params_list = PARAMS.split(",")
92
93   # Trim '*' char from param name and append it to return type
94   idx = 0
95   if params_list[0]:
96     for x in params_list:
97       index = x.rfind(' ')
98       x = x[index+1:]
99       if (x[0] == '*'):
100         x = x[1:]
101       if (x == "void"):
102         del params_list[idx]
103       else:
104         params_list[idx] = x
105       idx = idx + 1
106
107   # Generate definition code by concatinating string peices and related macro
108   func_def = RETURN_TYPE + " " + API_NAME + "(" + PARAMS + ")\n{\n\t"
109   func_ptr_decl = "typedef " + RETURN_TYPE + " (*func_ptr_t)(" + PARAMS + ");"
110
111   func_def = func_def + func_ptr_decl + "\n\t"
112   func_def = func_def + "DL_FUNCTION("
113   func_def = func_def + RETURN_TYPE + ", " + API_NAME
114
115   if len(params_list) > 0:
116     for x in params_list:
117       func_def = func_def + ', ' + x
118
119   func_def = func_def + ");\n}\n"
120   FUNC_DEFI.append(func_def)
121
122   # There might duplicate entries because of inclusion of both public and internal APIs,
123   # so make the list unique by creating a set
124   FUNC_DEFI = list(set(FUNC_DEFI))
125
126 output_file = ""
127 parser = OptionParser()
128 parser.add_option('--out-cpp', dest='out_cpp')
129 (options, args) = parser.parse_args()
130 output_file = options.out_cpp
131
132 # Include required headers
133 text_file = open(output_file, "w")
134
135 for header_file in headers_list_inc:
136   str = "#include <" + header_file + ">\n"
137   text_file.write(str)
138 text_file.write("\n")
139
140 # Include main headers
141 for header_file in main_headers_list:
142   str = "#include \"" + header_file + "\"\n"
143   text_file.write(str)
144
145 # Include other ewk headers by checking file existance
146 header_path = basepath + "/../ewk/efl_integration/public/"
147 for header_file in headers_list:
148   print(header_path + header_file)
149   if os.path.isfile(header_path + header_file):
150     str = "#include \"" + header_file + "\"\n"
151     text_file.write(str)
152
153 DL_MACRO = """
154 #define DL_FUNCTION(return_type, func_name, ...)                            \\
155   do {                                                                      \\
156     if (!g_impl_lib_handle && !open_library())                              \\
157       return static_cast<return_type>(0);                                   \\
158     func_ptr_t fp =                                                         \\
159         reinterpret_cast<func_ptr_t>(dlsym(g_impl_lib_handle, #func_name)); \\
160     if (fp)                                                                 \\
161       return fp(__VA_ARGS__);                                               \\
162     return static_cast<return_type>(0);                                     \\
163   } while (0)
164 """
165
166 text_file.write(DL_MACRO + "\n")
167 text_file.write("using namespace ewk_interface;\n\n")
168
169 # Write funtion definition to output file
170 for x in FUNC_DEFI:
171   text_file.write(x + '\n')
172
173 text_file.close()