Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webdatabase / DatabaseAuthorizer.h
1 /*
2  * Copyright (C) 2007 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #ifndef DatabaseAuthorizer_h
29 #define DatabaseAuthorizer_h
30
31 #include "platform/heap/Handle.h"
32 #include "wtf/Forward.h"
33 #include "wtf/HashSet.h"
34 #include "wtf/ThreadSafeRefCounted.h"
35 #include "wtf/text/StringHash.h"
36 #include "wtf/text/WTFString.h"
37
38 namespace blink {
39
40 extern const int SQLAuthAllow;
41 extern const int SQLAuthDeny;
42
43 class DatabaseAuthorizer : public ThreadSafeRefCountedWillBeGarbageCollectedFinalized<DatabaseAuthorizer> {
44 public:
45
46     enum Permissions {
47         ReadWriteMask = 0,
48         ReadOnlyMask = 1 << 1,
49         NoAccessMask = 1 << 2
50     };
51
52     static PassRefPtrWillBeRawPtr<DatabaseAuthorizer> create(const String& databaseInfoTableName);
53     void trace(Visitor*) { }
54
55     int createTable(const String& tableName);
56     int createTempTable(const String& tableName);
57     int dropTable(const String& tableName);
58     int dropTempTable(const String& tableName);
59     int allowAlterTable(const String& databaseName, const String& tableName);
60
61     int createIndex(const String& indexName, const String& tableName);
62     int createTempIndex(const String& indexName, const String& tableName);
63     int dropIndex(const String& indexName, const String& tableName);
64     int dropTempIndex(const String& indexName, const String& tableName);
65
66     int createTrigger(const String& triggerName, const String& tableName);
67     int createTempTrigger(const String& triggerName, const String& tableName);
68     int dropTrigger(const String& triggerName, const String& tableName);
69     int dropTempTrigger(const String& triggerName, const String& tableName);
70
71     int createView(const String& viewName);
72     int createTempView(const String& viewName);
73     int dropView(const String& viewName);
74     int dropTempView(const String& viewName);
75
76     int createVTable(const String& tableName, const String& moduleName);
77     int dropVTable(const String& tableName, const String& moduleName);
78
79     int allowDelete(const String& tableName);
80     int allowInsert(const String& tableName);
81     int allowUpdate(const String& tableName, const String& columnName);
82     int allowTransaction();
83
84     int allowSelect() { return SQLAuthAllow; }
85     int allowRead(const String& tableName, const String& columnName);
86
87     int allowReindex(const String& indexName);
88     int allowAnalyze(const String& tableName);
89     int allowFunction(const String& functionName);
90     int allowPragma(const String& pragmaName, const String& firstArgument);
91
92     int allowAttach(const String& filename);
93     int allowDetach(const String& databaseName);
94
95     void disable();
96     void enable();
97     void setPermissions(int permissions);
98
99     void reset();
100     void resetDeletes();
101
102     bool lastActionWasInsert() const { return m_lastActionWasInsert; }
103     bool lastActionChangedDatabase() const { return m_lastActionChangedDatabase; }
104     bool hadDeletes() const { return m_hadDeletes; }
105
106 private:
107     explicit DatabaseAuthorizer(const String& databaseInfoTableName);
108     void addWhitelistedFunctions();
109     int denyBasedOnTableName(const String&) const;
110     int updateDeletesBasedOnTableName(const String&);
111     bool allowWrite();
112
113     int m_permissions;
114     bool m_securityEnabled : 1;
115     bool m_lastActionWasInsert : 1;
116     bool m_lastActionChangedDatabase : 1;
117     bool m_hadDeletes : 1;
118
119     const String m_databaseInfoTableName;
120
121     HashSet<String, CaseFoldingHash> m_whitelistedFunctions;
122 };
123
124 } // namespace blink
125
126 #endif // DatabaseAuthorizer_h