PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/wrt/src/view/webkit/bundles/wrt-wk2-bundle.cpp

https://review.tizen.org/git/
C++ | 770 lines | 631 code | 105 blank | 34 comment | 96 complexity | 8da0474af47d037555cccdf27687d314 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-3.0, GPL-2.0, MPL-2.0, JSON, WTFPL, CC-BY-SA-4.0, CC-BY-3.0, BSD-3-Clause, LGPL-2.0, MPL-2.0-no-copyleft-exception, AGPL-1.0, 0BSD, Zlib, Unlicense, BSD-2-Clause, Apache-2.0, LGPL-3.0, ISC, MIT, CC-BY-SA-3.0, CC0-1.0, LGPL-2.1
  1. /*
  2. * Copyright (c) 2011 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. * @file wrt-wk2-bundle.cpp
  18. * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
  19. * @brief Implementation file for view logic for Webkit2
  20. */
  21. #include "wrt-wk2-bundle.h"
  22. #include <WKBundle.h>
  23. #include <WKBundleInitialize.h>
  24. #include <WKBundlePage.h>
  25. #include <WKBundleFrame.h>
  26. #include <WKURLRequest.h>
  27. #include <WKString.h>
  28. #include <WKType.h>
  29. #include <WKURL.h>
  30. #include <WKError.h>
  31. #include <string>
  32. #include <cstdio>
  33. #include <sstream>
  34. #include <sys/stat.h>
  35. #include <set>
  36. #include <openssl/sha.h>
  37. #include <openssl/hmac.h>
  38. #include <openssl/evp.h>
  39. #include <openssl/bio.h>
  40. #include <openssl/buffer.h>
  41. #include <dpl/log/log.h>
  42. #include <dpl/foreach.h>
  43. #include <dpl/assert.h>
  44. #include <dpl/wrt-dao-ro/WrtDatabase.h>
  45. #include <dpl/localization/localization_utils.h>
  46. #include <dpl/string.h>
  47. #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
  48. #include <dpl/utils/mime_type_utils.h>
  49. #include <wrt_plugin_module.h>
  50. #include <vconf.h>
  51. #include <appcore-efl.h>
  52. #include "messages_names.h"
  53. namespace {
  54. const char * const willSendMessageName = "will_send_msg";
  55. const char * const uriChangedMessageName = "uri_changed_msg";
  56. const char * const URICHANGE_PLUGIN_STOP_ONLY = "plugin_stop_only";
  57. const char * const URICHANGE_PLUGIN_RESTART = "plugin_restart";
  58. const char * const URICHANGE_PLUGIN_NO_CHANGE = "plugin_no_change";
  59. const char * const URICHANGE_BLOCKED_URL = "null";
  60. const char * const SCHEME_HTTP = "http";
  61. const char * const SCHEME_HTTPS = "https";
  62. const char * const SCHEME_FILE = "file://";
  63. const char * const DATA_STRING = "data:";
  64. const char * const BASE64_STRING = ";base64,";
  65. }
  66. Bundle::Bundle(WKBundleRef bundle) :
  67. m_willSendMessage(0),
  68. m_bundle(bundle),
  69. m_widgetHandle(-1),
  70. m_scale(0),
  71. m_encodedBundle(""),
  72. m_theme(""),
  73. m_resDec(NULL),
  74. m_encrypted(false)
  75. {
  76. WrtDB::WrtDatabase::attachToThreadRO();
  77. m_willSendMessage = WKStringCreateWithUTF8CString(willSendMessageName);
  78. m_uriChangedMessage = WKStringCreateWithUTF8CString(uriChangedMessageName);
  79. LogDebug("message will be named " << toString(m_willSendMessage).c_str());
  80. }
  81. Bundle::~Bundle()
  82. {
  83. WrtDB::WrtDatabase::detachFromThread();
  84. if (!m_pagesList.empty())
  85. {
  86. LogError("There are not closed pages!");
  87. }
  88. WKRelease(m_willSendMessage);
  89. WKRelease(m_uriChangedMessage);
  90. WKRelease(m_bundle);
  91. }
  92. void Bundle::didCreatePageCallback(
  93. WKBundleRef /*bundle*/,
  94. WKBundlePageRef page,
  95. const void* clientInfo)
  96. {
  97. LogDebug("didCreatePageCallback called");
  98. Bundle* This = static_cast<Bundle*>(const_cast<void*>(clientInfo));
  99. This->didCreatePage(page);
  100. }
  101. void Bundle::didReceiveMessageCallback(
  102. WKBundleRef /*bundle*/,
  103. WKStringRef messageName,
  104. WKTypeRef messageBody,
  105. const void *clientInfo)
  106. {
  107. LogDebug("didReceiveMessageCallback called");
  108. Bundle* bundle = static_cast<Bundle*>(const_cast<void*>(clientInfo));
  109. bundle->didReceiveMessage(messageName, messageBody);
  110. }
  111. void Bundle::willDestroyPageCallback(
  112. WKBundleRef /*bundle*/,
  113. WKBundlePageRef page,
  114. const void* clientInfo)
  115. {
  116. LogDebug("willDestroyPageCallback called");
  117. Bundle* This = static_cast<Bundle*>(const_cast<void*>(clientInfo));
  118. This->willDestroyPage(page);
  119. }
  120. void Bundle::didCreatePage(WKBundlePageRef page)
  121. {
  122. auto mainFrame = WKBundlePageGetMainFrame(page);
  123. auto context = WKBundleFrameGetJavaScriptContext(mainFrame);
  124. m_pagesList.push_back(page);
  125. m_pageGlobalContext[page] = context;
  126. LogDebug("created Page : " << page << " created JSContext : " << context);
  127. WKBundlePageResourceLoadClient resourceLoadClient = {
  128. kWKBundlePageResourceLoadClientCurrentVersion, /* version */
  129. this, /* clientinfo */
  130. 0, /* didInitiateLoadForResource */
  131. willSendRequestForFrameCallback, /* willSendRequestForFrame */
  132. 0, /* didReceiveResponseForResource */
  133. 0, /* didReceiveContentLengthForResource */
  134. didFinishLoadForResourceCallback, /* didFinishLoadForResource */
  135. 0, /* didFailLoadForResource */
  136. 0, /* shouldCacheResponse */
  137. 0, /* shouldUseCredentialStorage */
  138. };
  139. WKBundlePageSetResourceLoadClient(page, &resourceLoadClient);
  140. WKBundlePageLoaderClient loaderClient = {
  141. kWKBundlePageLoaderClientCurrentVersion,
  142. this, /* clientinfo */
  143. didStartProvisionalLoadForFrameCallback, /* didStartProvisionalLoadForFrame */
  144. 0, /* didReceiveServerRedirectForProvisionalLoadForFrame */
  145. 0, /* didFailProvisionalLoadWithErrorForFrame */
  146. didCommitLoadForFrameCallback, /* didCommitLoadForFrame */
  147. 0, /* didFinishDocumentLoadForFrame */
  148. 0, /* didFinishLoadForFrame */
  149. 0, /* didFailLoadWithErrorForFrame */
  150. 0, /* didSameDocumentNavigationForFrame */
  151. 0, /* didReceiveTitleForFrame */
  152. 0, /* didFirstLayoutForFrame */
  153. 0, /* didFirstVisuallyNonEmptyLayoutForFrame */
  154. didRemoveFrameFromHierarchyCallback, /* didRemoveFrameFromHierarchy */
  155. 0, /* didDisplayInsecureContentForFrame */
  156. 0, /* didRunInsecureContentForFrame */
  157. 0, /* didClearWindowObjectForFrame */
  158. 0, /* didCancelClientRedirectForFrame */
  159. 0, /* willPerformClientRedirectForFrame */
  160. 0, /* didHandleOnloadEventsForFrame */
  161. 0, /* didLayoutForFrame */
  162. 0, /* didNewFirstVisuallyNonEmptyLayout */
  163. 0, /* didDetectXSSForFrame */
  164. 0, /* shouldGoToBackForwardListItem */
  165. 0, /* globalObjectIsAvailableForFrame */
  166. 0, /* willDisconnectDOMWindowExtensionFromGlobalObject */
  167. 0, /* didReconnectDOMWindowExtensionToGlobalObject */
  168. 0, /* willDestroyGlobalObjectForDOMWindowExtension */
  169. 0, /* didFinishProgress */
  170. 0, /* shouldForceUniversalAccessFromLocalURL */
  171. };
  172. WKBundlePageSetPageLoaderClient(page, &loaderClient);
  173. }
  174. void Bundle::willDestroyPage(WKBundlePageRef page)
  175. {
  176. LogDebug("Destroyed page : " << page);
  177. auto context = m_pageGlobalContext[page];
  178. m_pagesList.remove(page);
  179. m_pageGlobalContext.erase(page);
  180. m_pageContext.erase(page);
  181. PluginModule::stop(context);
  182. }
  183. void Bundle::didReceiveMessage(WKStringRef messageName, WKTypeRef messageBody)
  184. {
  185. LogDebug("got message type: " << toString(messageName).c_str());
  186. if (WKStringIsEqualToUTF8CString(messageName,
  187. BundleMessages::START))
  188. {
  189. if (!messageBody || WKStringGetTypeID() != WKGetTypeID(messageBody)) {
  190. LogError("Wrong message format received, ignoring");
  191. return;
  192. }
  193. auto msgString = toString(static_cast<WKStringRef>(messageBody));
  194. LogDebug("Got message text: " << msgString);
  195. LogDebug("loading Page : " << m_pagesList.back() <<
  196. " loading JSContext : " <<
  197. m_pageGlobalContext[m_pagesList.back()]);
  198. // set information from ui process
  199. std::stringstream ssMsg(msgString);
  200. std::string argScale;
  201. std::string argEncodedBundle;
  202. std::string argTheme;
  203. ssMsg >> m_widgetHandle;
  204. ssMsg >> argScale;
  205. ssMsg >> argEncodedBundle;
  206. ssMsg >> argTheme;
  207. ssMsg >> m_encrypted;
  208. if (argScale != "null" && argScale[0] == '_')
  209. {
  210. argScale.erase(0, 1);
  211. std::stringstream ssScale(argScale);
  212. ssScale >> m_scale;
  213. }
  214. if (argEncodedBundle != "null" && argEncodedBundle[0] == '_')
  215. {
  216. argEncodedBundle.erase(0, 1);
  217. m_encodedBundle = argEncodedBundle;
  218. }
  219. if (argTheme != "null" && argTheme[0] == '_')
  220. {
  221. argTheme.erase(0, 1);
  222. m_theme = argTheme;
  223. }
  224. }
  225. else if (WKStringIsEqualToUTF8CString(messageName,
  226. BundleMessages::SHUTDOWN))
  227. {
  228. LogDebug("shutdown plugins");
  229. if (m_pagesList.empty())
  230. {
  231. PluginModule::shutdown();
  232. }
  233. else
  234. {
  235. LogInfo("PluginModule shutdown ignored, there are still alive pages!");
  236. }
  237. }
  238. else if (WKStringIsEqualToUTF8CString(messageName,
  239. BundleMessages::SET_CUSTOM_PROPERTIES))
  240. {
  241. LogDebug("reset custom properties of window objects");
  242. // set information from ui process
  243. auto msgString = toString(static_cast<WKStringRef>(messageBody));
  244. std::string argScale;
  245. std::string argEncodedBundle;
  246. std::string argTheme;
  247. std::stringstream ssMsg(msgString);
  248. ssMsg >> argScale;
  249. ssMsg >> argEncodedBundle;
  250. ssMsg >> argTheme;
  251. if(argScale != "null" && argScale[0] == '_')
  252. {
  253. argScale.erase(0, 1);
  254. std::stringstream ssScale(argScale);
  255. ssScale >> m_scale;
  256. }
  257. if(argEncodedBundle != "null" && argEncodedBundle[0] == '_')
  258. {
  259. argEncodedBundle.erase(0, 1);
  260. m_encodedBundle = argEncodedBundle;
  261. }
  262. if(argTheme != "null" && argTheme[0] == '_')
  263. {
  264. argTheme.erase(0, 1);
  265. m_theme = argTheme;
  266. }
  267. //apply for each context
  268. PageGlobalContext::iterator it = m_pageGlobalContext.begin();
  269. for (; it != m_pageGlobalContext.end(); ++it)
  270. {
  271. PluginModule::setCustomProperties(it->second,
  272. m_scale,
  273. m_encodedBundle.c_str(),
  274. m_theme.c_str());
  275. }
  276. }
  277. else if (WKStringIsEqualToUTF8CString(
  278. messageName,
  279. BundleMessages::DISPATCH_JAVASCRIPT_EVENT))
  280. {
  281. LogDebug("dispatch javascript event to created frames");
  282. // set information from ui process
  283. auto text = toString(static_cast<WKStringRef>(messageBody));
  284. int eventType;
  285. std::stringstream ss(text);
  286. ss >> eventType;
  287. //apply for each context
  288. PageGlobalContext::iterator it = m_pageGlobalContext.begin();
  289. for (; it != m_pageGlobalContext.end(); ++it)
  290. {
  291. PluginModule::dispatchJavaScriptEvent(
  292. it->second,
  293. static_cast<WrtPlugins::W3C::CustomEventType>(eventType));
  294. }
  295. }
  296. }
  297. WKURLRequestRef Bundle::willSendRequestForFrameCallback(
  298. WKBundlePageRef /*page*/,
  299. WKBundleFrameRef /*frame*/,
  300. uint64_t /*resourceIdentifier*/,
  301. WKURLRequestRef request,
  302. WKURLResponseRef /*response*/,
  303. const void *clientInfo)
  304. {
  305. LogDebug("willSendRequestForFrameCallback called");
  306. Bundle* This = static_cast<Bundle*>(const_cast<void*>(clientInfo));
  307. return This->willSendRequestForFrame(request);
  308. }
  309. void Bundle::didStartProvisionalLoadForFrameCallback(
  310. WKBundlePageRef page,
  311. WKBundleFrameRef frame,
  312. WKTypeRef* /*userData*/,
  313. const void *clientInfo)
  314. {
  315. LogDebug("didStartProvisionalLoadForFrameCallback called");
  316. Bundle* This = static_cast<Bundle*>(const_cast<void*>(clientInfo));
  317. if (This->m_pageGlobalContext.count(page) == 0)
  318. {
  319. return;
  320. }
  321. if (This->m_pageContext.count(page) == 0)
  322. {
  323. return;
  324. }
  325. JSGlobalContextRef context = WKBundleFrameGetJavaScriptContext(frame);
  326. ContextSet::iterator i = This->m_pageContext[page].find(context);
  327. if (i == This->m_pageContext[page].end())
  328. {
  329. LogDebug("Initially attached frame");
  330. return;
  331. }
  332. This->m_pageContext[page].erase(i);
  333. PluginModule::unloadFrame(context);
  334. }
  335. void Bundle::didRemoveFrameFromHierarchyCallback(
  336. WKBundlePageRef page,
  337. WKBundleFrameRef frame,
  338. WKTypeRef* /*userData*/,
  339. const void *clientInfo)
  340. {
  341. LogDebug("didFinishLoadForResourceCallback called");
  342. Bundle* This = static_cast<Bundle*>(const_cast<void*>(clientInfo));
  343. if (This->m_pageGlobalContext.count(page) == 0)
  344. {
  345. return;
  346. }
  347. if (This->m_pageContext.count(page) == 0)
  348. {
  349. return;
  350. }
  351. JSGlobalContextRef context = WKBundleFrameGetJavaScriptContext(frame);
  352. ContextSet::iterator i = This->m_pageContext[page].find(context);
  353. if (i == This->m_pageContext[page].end())
  354. {
  355. LogWarning("Tried to unload frame which has never been loaded");
  356. return;
  357. }
  358. This->m_pageContext[page].erase(i);
  359. PluginModule::unloadFrame(context);
  360. }
  361. void Bundle::didFinishLoadForResourceCallback(
  362. WKBundlePageRef /*page*/,
  363. WKBundleFrameRef frame,
  364. uint64_t /*resourceIdentifier*/,
  365. const void* clientInfo)
  366. {
  367. LogDebug("didFinishLoadForResourceCallback called");
  368. Bundle* This = static_cast<Bundle*>(const_cast<void*>(clientInfo));
  369. WKURLRef url = WKBundleFrameCopyURL(frame);
  370. WKTypeRef retVal = NULL;
  371. // If url is NULL, this url has been already blocked by willsend
  372. // So current page should be moved to previous page
  373. if (url == NULL)
  374. {
  375. if (WKBundleFrameIsMainFrame(frame))
  376. {
  377. LogInfo("url is NULL. Go back to previous view");
  378. WKStringRef blockedURL =
  379. WKStringCreateWithUTF8CString(URICHANGE_BLOCKED_URL);
  380. WKBundlePostSynchronousMessage(This->m_bundle,
  381. This->m_uriChangedMessage,
  382. blockedURL,
  383. &retVal);
  384. }
  385. else
  386. {
  387. LogInfo("Blocked uri in IFrame.");
  388. }
  389. return;
  390. }
  391. WKRelease(url);
  392. }
  393. void Bundle::didCommitLoadForFrameCallback(
  394. WKBundlePageRef page,
  395. WKBundleFrameRef frame,
  396. WKTypeRef* /*userData*/,
  397. const void *clientInfo)
  398. {
  399. LogInfo("didCommitLoadForFrameCallback called");
  400. Bundle* This = static_cast<Bundle*>(const_cast<void*>(clientInfo));
  401. WKURLRef url = WKBundleFrameCopyURL(frame);
  402. WKTypeRef retVal = NULL;
  403. if (url == NULL) {
  404. LogInfo("url is NULL");
  405. return;
  406. }
  407. JSGlobalContextRef context = WKBundleFrameGetJavaScriptContext(frame);
  408. This->m_pageContext[page].insert(context);
  409. if (!WKBundleFrameIsMainFrame(frame))
  410. {
  411. LogInfo("frame isn't main frame");
  412. PluginModule::start(This->m_widgetHandle,
  413. context,
  414. This->m_scale,
  415. This->m_encodedBundle.c_str(),
  416. This->m_theme.c_str());
  417. PluginModule::loadFrame(context);
  418. return;
  419. }
  420. WKStringRef urlStr = WKURLCopyString(url);
  421. WKBundlePostSynchronousMessage(This->m_bundle,
  422. This->m_uriChangedMessage,
  423. urlStr,
  424. &retVal);
  425. WKRelease(url);
  426. WKRelease(urlStr);
  427. std::string result = toString(static_cast<WKStringRef>(retVal));
  428. LogInfo("result from UI process : " << result);
  429. if (result == URICHANGE_PLUGIN_STOP_ONLY)
  430. {
  431. PluginModule::stop(context);
  432. }
  433. else if (result == URICHANGE_PLUGIN_RESTART)
  434. {
  435. PluginModule::stop(context);
  436. This->m_pageGlobalContext[page] = context;
  437. PluginModule::start(This->m_widgetHandle,
  438. context,
  439. This->m_scale,
  440. This->m_encodedBundle.c_str(),
  441. This->m_theme.c_str() );
  442. PluginModule::loadFrame(context);
  443. }
  444. }
  445. WKURLRequestRef Bundle::willSendRequestForFrame(WKURLRequestRef request)
  446. {
  447. LogDebug("willSendReq got " << toString(request).c_str());
  448. WKURLRef url = WKURLRequestCopyURL(request);
  449. WKStringRef urlStr = WKURLCopyString(url);
  450. WKTypeRef retVal = NULL;
  451. WKBundlePostSynchronousMessage(m_bundle,
  452. m_willSendMessage,
  453. urlStr,
  454. &retVal);
  455. WKRelease(urlStr);
  456. if (NULL == retVal) {
  457. LogDebug("uri is blocked");
  458. WKRelease(url);
  459. return NULL;
  460. } else if (retVal && WKStringGetTypeID() == WKGetTypeID(retVal)) {
  461. std::string tmpUrlStr = toString(static_cast<WKStringRef>(retVal));
  462. WKRelease(retVal);
  463. if (tmpUrlStr.empty()) {
  464. LogDebug("uri is blocked");
  465. WKRelease(url);
  466. return NULL;
  467. }
  468. WKURLRef tmpUrl = WKURLCreateWithUTF8CString(tmpUrlStr.c_str());
  469. std::string scheme = toString(WKURLCopyScheme(url));
  470. WKRelease(url);
  471. // Return value must contain details information of input
  472. // WKURLRequestRef. Current webkit2 doesn't support api that
  473. // copy WKURLRequestRef or change url only. Before webkit2
  474. // support api, callback return original WKURLRequestRef in the
  475. // case of external scheme
  476. // external scheme also need to send message to UI process for
  477. // checking roaming and security
  478. if (scheme == SCHEME_HTTP || scheme == SCHEME_HTTPS) {
  479. LogDebug("external scheme return original WKURLRequestRef");
  480. WKRelease(tmpUrl);
  481. WKRetain(request);
  482. return request;
  483. } else {
  484. std::string checkUrl = toString(tmpUrl);
  485. int getFileSize;
  486. if(m_encrypted) {
  487. if(isEncryptedResource(checkUrl, getFileSize)) {
  488. std::string decryptString = DecryptResource(checkUrl,
  489. getFileSize);
  490. if (!decryptString.empty()) {
  491. std::string destString = DATA_STRING;
  492. std::string mimeString =
  493. DPL::ToUTF8String(MimeTypeUtils::identifyFileMimeType(
  494. DPL::FromUTF8String(checkUrl)));
  495. destString += mimeString;
  496. destString += BASE64_STRING;
  497. decryptString.insert(0, destString);
  498. WKURLRef destUrl =
  499. WKURLCreateWithUTF8CString(decryptString.c_str());
  500. WKURLRequestRef req = WKURLRequestCreateWithWKURL(destUrl);
  501. WKRelease(destUrl);
  502. LogDebug("return value " << decryptString << "]]");
  503. return req;
  504. }
  505. }
  506. }
  507. WKURLRequestRef req = WKURLRequestCreateWithWKURL(tmpUrl);
  508. WKRelease(tmpUrl);
  509. LogDebug("return value " << toString(req).c_str());
  510. return req;
  511. }
  512. } else {
  513. Assert(false && "not common case need to check work flow");
  514. }
  515. }
  516. std::string Bundle::toString(WKStringRef str)
  517. {
  518. if (WKStringIsEmpty(str)) {
  519. return std::string();
  520. }
  521. size_t size = WKStringGetMaximumUTF8CStringSize(str);
  522. char buffer[size + 1];
  523. WKStringGetUTF8CString(str, buffer, size + 1);
  524. return buffer;
  525. }
  526. std::string Bundle::toString(WKURLRef url)
  527. {
  528. WKStringRef urlStr = WKURLCopyString(url);
  529. std::string str = toString(urlStr);
  530. WKRelease(urlStr);
  531. return str;
  532. }
  533. std::string Bundle::toString(WKURLRequestRef req)
  534. {
  535. WKURLRef reqUrl = WKURLRequestCopyURL(req);
  536. std::string str = toString(reqUrl);
  537. WKRelease(reqUrl);
  538. return str;
  539. }
  540. std::string Bundle::toString(WKErrorRef err)
  541. {
  542. WKStringRef domErr = WKErrorCopyDomain(err);
  543. WKStringRef desc = WKErrorCopyLocalizedDescription(err);
  544. std::string str = toString(domErr) + "\n" + toString(desc);
  545. WKRelease(domErr);
  546. WKRelease(desc);
  547. return str;
  548. }
  549. bool Bundle::isEncryptedResource(std::string Url, int &size)
  550. {
  551. std::string filePath;
  552. size_t pos = Url.find_first_not_of(SCHEME_FILE);
  553. if ( std::string::npos != pos) {
  554. filePath = Url.substr(pos-1);
  555. }
  556. if (m_encryptedFiles.empty()) {
  557. WrtDB::WidgetDAOReadOnly(m_widgetHandle).
  558. getEncryptedFileList(m_encryptedFiles);
  559. }
  560. WrtDB::EncryptedFileInfo info;
  561. std::set<WrtDB::EncryptedFileInfo>::iterator it;
  562. info.fileName = DPL::FromUTF8String(filePath);
  563. if ((0 == strncmp(Url.c_str(), SCHEME_FILE, strlen(SCHEME_FILE))) &&
  564. (m_encryptedFiles.end() != (it =
  565. m_encryptedFiles.find(info)))) {
  566. LogDebug(" info file name : " << it->fileName);
  567. LogDebug(" info file size : " << it->fileSize);
  568. size = it->fileSize;
  569. return true;
  570. }
  571. return false;
  572. }
  573. std::string Bundle::DecryptResource(std::string resource, int size)
  574. {
  575. std::string filePath;
  576. size_t pos = resource.find_first_not_of(SCHEME_FILE);
  577. if ( std::string::npos != pos) {
  578. filePath = resource.substr(pos-1);
  579. }
  580. struct stat buf;
  581. if (0 == stat(filePath.c_str(), &buf)) {
  582. if (NULL == m_resDec) {
  583. using namespace WrtDB;
  584. DPL::Optional<DPL::String> pkgName =
  585. WidgetDAOReadOnly(m_widgetHandle).getPkgname();
  586. m_resDec = new
  587. WRTDecryptor::ResourceDecryptor(DPL::ToUTF8String(*pkgName));
  588. WrtDB::WidgetDAOReadOnly(m_widgetHandle).
  589. getEncryptedFileList(m_encryptedFiles);
  590. }
  591. size_t bufSize = buf.st_size;
  592. unsigned char contents[bufSize];
  593. memset(contents, 0, bufSize);
  594. FILE* fp = fopen(filePath.c_str(), "rb");
  595. if ( NULL == fp) {
  596. LogDebug("Couldnot open file : " << filePath);
  597. return std::string();
  598. }
  599. fread(contents, sizeof(unsigned char), buf.st_size, fp);
  600. fclose(fp);
  601. LogDebug("resource is encrypted. decrypting....");
  602. unsigned char outDecBuf[bufSize];
  603. memset(outDecBuf, 0, bufSize);
  604. m_resDec->GetDecryptedChunk(contents, outDecBuf, bufSize);
  605. memset(outDecBuf+size, '\n', bufSize - size);
  606. LogDebug("resource need to encoding base64");
  607. BIO *bmem, *b64;
  608. BUF_MEM *bptr;
  609. b64 = BIO_new(BIO_f_base64());
  610. bmem = BIO_new(BIO_s_mem());
  611. b64 = BIO_push(b64, bmem);
  612. BIO_write(b64, outDecBuf, bufSize);
  613. BIO_flush(b64);
  614. BIO_get_mem_ptr(b64, &bptr);
  615. std::string base64Enc((char *)bptr->data, bptr->length-1);
  616. BIO_free_all(b64);
  617. return base64Enc;
  618. }
  619. return std::string();
  620. }
  621. namespace {
  622. static int LanguageChanged(void *)
  623. {
  624. char* lang = vconf_get_str(VCONFKEY_LANGSET);
  625. if (!lang) {
  626. LogError("Cannot get locale settings from vconf");
  627. return 0;
  628. }
  629. LogDebug("Language set to: " << lang);
  630. LanguageTagsList list;
  631. list.push_back(DPL::FromUTF8String(lang));
  632. LocalizationUtils::SetSystemLanguageTags(list);
  633. LogDebug("LanguageChanged to " << lang);
  634. return 0;
  635. }
  636. void Initialize()
  637. {
  638. appcore_set_event_callback(
  639. APPCORE_EVENT_LANG_CHANGE,
  640. &LanguageChanged,
  641. NULL);
  642. LanguageChanged(NULL); // updating language for the first time
  643. }
  644. }
  645. extern "C"
  646. {
  647. WK_EXPORT
  648. void WKBundleInitialize(WKBundleRef bundle,
  649. WKTypeRef)
  650. {
  651. DPL::Log::LogSystemSingleton::Instance().SetTag("WRT-BUNDLE");
  652. Initialize();
  653. LogDebug("Bundle initialized");
  654. static Bundle s_bundle = Bundle(bundle);
  655. WKBundleClient client = {
  656. kWKBundleClientCurrentVersion,
  657. &s_bundle,
  658. &Bundle::didCreatePageCallback,
  659. &Bundle::willDestroyPageCallback,
  660. 0, /* didInitializePageGroup */
  661. &Bundle::didReceiveMessageCallback
  662. };
  663. WKBundleSetClient(bundle, &client);
  664. }
  665. }