Retry read() when getting EAGAIN
[platform/framework/web/download-provider.git] / agent / download-agent-plugin-drm.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <string.h>
18
19 #include "drm_client.h"
20 #include "drm_client_types.h"
21 #include "drm_trusted_client.h"
22 #include "drm_trusted_client_types.h"
23
24 #include "download-agent-debug.h"
25 #include "download-agent-plugin-drm.h"
26
27
28 void __EDRM_clean_up()
29 {
30         int ret = 0;
31         ret = drm_trusted_handle_request(DRM_TRUSTED_REQ_TYPE_CLIENT_CLEAN_UP, NULL, NULL);
32         if (DRM_RETURN_SUCCESS == ret)
33                 DA_LOGD("Clean up successfull");
34         else
35                 DA_LOGE("ret[%0x%x]", ret);
36 }
37
38 da_bool_t EDRM_convert(const char *in_file_path, char **out_file_path)
39 {
40         drm_trusted_conv_info_s input;
41         drm_trusted_conv_resp_info_s output;
42         size_t len = 0;
43         int ret = 0;
44
45         memset(&input, 0x0, sizeof(drm_trusted_conv_info_s));
46         memset(&output, 0x0, sizeof(drm_trusted_conv_resp_info_s));
47
48         len = strlen(in_file_path);
49         if (len >= sizeof(input.filePath))
50                 len = sizeof(input.filePath) - 1;
51         memcpy(input.filePath, in_file_path, len);
52
53         ret = drm_trusted_convert_dm(&input, &output);
54
55         if (DRM_TRUSTED_RETURN_SUCCESS != ret) {
56                 DA_LOGE("ret[%0x%x]", ret);
57                 __EDRM_clean_up();
58                 return DA_FALSE;
59         } else {
60                 DA_SECURE_LOGD("Returned filePath[%s]", output.filePath);
61                 *out_file_path = strdup(output.filePath);
62         }
63         __EDRM_clean_up();
64         return DA_TRUE;
65 }
66
67 da_ret_t EDRM_wm_get_license(char *rights_url, char **out_content_url)
68 {
69         int ret = 0;
70         int len = 0;
71         drm_initiator_info_s init_info;
72         drm_web_server_resp_data_s resp_data;
73
74         if (rights_url == NULL)
75                 return DA_ERR_DRM_FAIL;
76
77         memset(&init_info, 0, sizeof(init_info));
78         memset(&resp_data, 0, sizeof(resp_data));
79         strncpy(init_info.initiator_url, rights_url,
80                         DRM_MAX_LEN_INITIATOR_URL - 1);
81         len = strlen(rights_url);
82         if (len > DRM_MAX_LEN_INITIATOR_URL - 1)
83                 init_info.initiator_url_len = (unsigned int)len;
84         else
85                 init_info.initiator_url_len = DRM_MAX_LEN_INITIATOR_URL;
86         ret = drm_process_request(DRM_REQUEST_TYPE_SUBMIT_INITIATOR_URL,
87                         &init_info, &resp_data);
88         if (DRM_RETURN_SUCCESS == ret) {
89                 DA_SECURE_LOGD("resp_data.content_url = %s", resp_data.content_url);
90                 /* Rights or Domain Certificate are installed successfully */
91                 /* Check for contentURL */
92                 if (strlen(resp_data.content_url) > 0) {
93                         char *content_url = NULL;
94                         size_t content_url_len = 0;
95                         content_url_len = strlen(resp_data.content_url);
96                         content_url = (char *)calloc(1, content_url_len + 1);
97                         if (content_url) {
98                                 strncpy(content_url, resp_data.content_url,
99                                         content_url_len);
100                                 *out_content_url = content_url;
101                                 DA_SECURE_LOGD("drm sumitted initiator url "
102                                                 "succeeded with [%s]", *out_content_url);
103                                 __EDRM_clean_up();
104                                 return DA_RESULT_OK;
105                         } else {
106                                 DA_LOGE("DA_ERR_FAIL_TO_MEMALLOC");
107                                 __EDRM_clean_up();
108                                 return DA_ERR_FAIL_TO_MEMALLOC;
109                         }
110                 } else {
111                         DA_LOGV("content_url is NULL.\
112                                         Join/Leave Domain, Metering case.");
113                         *out_content_url = DA_NULL;
114                         __EDRM_clean_up();
115                         return DA_RESULT_OK;
116                 }
117         } else {
118                 DA_LOGE("drm_process_request() failed");
119                 __EDRM_clean_up();
120                 return DA_ERR_DRM_FAIL;
121         }
122 }
123