Temporary fix for asking geolocation permission popup crash.
[platform/framework/web/wrt.git] / src / view / common / view_logic_storage_support.cpp
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    view_logic_storage_support.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @brief   Implementation file of StorageSupport API used by ViewLogic
20  */
21 #include "view_logic_storage_support.h"
22
23 #include <string>
24 #include <ftw.h>
25
26 #include <dpl/assert.h>
27 #include <dpl/exception.h>
28 #include <dpl/log/log.h>
29 #include <dpl/string.h>
30 #include <dpl/utils/wrt_utility.h>
31 #include <widget_model.h>
32
33 namespace ViewModule {
34 namespace StorageSupport {
35 namespace { //anonymous
36 const mode_t TEMPORARY_STORAGE_MODE = 0700;
37 static bool rootDirectory = true;
38
39 static int removeFile(const char* path, const struct stat* /*sb*/, int tflag)
40 {
41     if (path == NULL) {
42         LogError("Wrong input path");
43         return 0;
44     }
45     LogDebug(path);
46     std::string inputPath = path;
47
48     if (rootDirectory) {
49         LogDebug("Skip root directory");
50         rootDirectory = false;
51         return 0;
52     }
53
54     if (tflag == FTW_F || tflag == FTW_D) {
55         if (!WrtUtilRemove(inputPath)) {
56             LogError("Fail to remove");
57         }
58     } else if (tflag == FTW_DNR) {
59         LogError("This is directory which can't be read");
60     } else if (tflag == FTW_NS) {
61         LogError("Unknow error");
62     }
63
64     return 0;
65 }
66
67 bool removeDirectory(const char* path)
68 {
69     rootDirectory = true;
70     if (ftw(path, removeFile, 20) != 0) {
71         return false;
72     }
73     return true;
74 }
75 }
76
77 void initializeStorage(WidgetModel *widgetModel)
78 {
79     LogDebug("initializeStorage");
80     Assert(widgetModel && "Passed widgetModel is NULL!");
81
82     // create temporary storage
83     std::string path =
84         DPL::ToUTF8String(widgetModel->TemporaryStoragePath.Get());
85     if (!WrtUtilDirExists(path)) {
86         if (!WrtUtilMakeDir(path, TEMPORARY_STORAGE_MODE)) {
87             ThrowMsg(DPL::CommonException::InternalError,
88                      "Fail to initialize temporary storage");
89         }
90     }
91 }
92
93 void deinitializeStorage(WidgetModel *widgetModel)
94 {
95     LogDebug("deinitializeStorage");
96     Assert(widgetModel && "Passed widgetModel is NULL!");
97
98     // clean-up temporary storage
99     std::string path =
100         DPL::ToUTF8String(widgetModel->TemporaryStoragePath.Get());
101     if (!removeDirectory(path.c_str())) {
102         LogError("Fail to deinitialize temporary storage");
103     }
104 }
105 } // namespace StorageSupport
106 } // namespace ViewModule