|
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <f32file.h> |
|
17 #include "ssmswppolicyframe.h" |
|
18 #include "ssmswppolicyresolver.h" |
|
19 #include "ssmdebug.h" |
|
20 #include "ssmserverpanic.h" |
|
21 #include "ssmswppolicymap.h" |
|
22 |
|
23 _LIT(KRomDriveLetter, "Z:"); |
|
24 _LIT(KDllFilenamePostfix, ".dll"); |
|
25 |
|
26 /** |
|
27 Empty constructor |
|
28 */ |
|
29 CSsmSwpPolicyResolver::CSsmSwpPolicyResolver() |
|
30 { |
|
31 } |
|
32 |
|
33 /** |
|
34 Destructor cleans up internal resources |
|
35 */ |
|
36 CSsmSwpPolicyResolver::~CSsmSwpPolicyResolver() |
|
37 { |
|
38 delete iSwpPolicyMap; |
|
39 } |
|
40 |
|
41 /** |
|
42 Static Factory |
|
43 */ |
|
44 CSsmSwpPolicyResolver* CSsmSwpPolicyResolver::NewL() |
|
45 { |
|
46 CSsmSwpPolicyResolver* self = new (ELeave) CSsmSwpPolicyResolver(); |
|
47 CleanupStack::PushL(self); |
|
48 self->ConstructL(); |
|
49 CleanupStack::Pop(self); |
|
50 return self; |
|
51 } |
|
52 |
|
53 /** |
|
54 Construct policy map object |
|
55 */ |
|
56 void CSsmSwpPolicyResolver::ConstructL() |
|
57 { |
|
58 iSwpPolicyMap = CSsmSwpPolicyMap::NewL(); |
|
59 } |
|
60 |
|
61 |
|
62 /** |
|
63 Register an Swp and its policy file with the map |
|
64 |
|
65 @param aSwp the swp value to register |
|
66 @param aFile The file name to associate with the swp value |
|
67 @leave KErrAlreadyExists if the swp value already exists |
|
68 @leave KErrArgument if aFilename is larger than KMaxFileName |
|
69 */ |
|
70 void CSsmSwpPolicyResolver::RegisterSwpMappingL(TUint aSwpKey, const TDesC& aFilename) |
|
71 { |
|
72 SSMLOGLEAVEIFNULL(iSwpPolicyMap); |
|
73 DEBUGPRINT3(_L("CSsmSwpPolicyResolver::RegisterSwpMappingL swp %d file %S"), aSwpKey, &aFilename); |
|
74 if(aFilename.Length() > KMaxFileName) |
|
75 { |
|
76 DEBUGPRINT1(_L("Filename too long")); |
|
77 SSMLOGLEAVE(KErrArgument); //lint !e527 Suppress Unreachable. Lint is just confused by macro, warning goes away when code in macro gets expanded |
|
78 } |
|
79 iSwpPolicyMap->AddL(aSwpKey, aFilename); |
|
80 } //lint !e1746 Suppress parameter 'aSwpKey' could be made const reference |
|
81 |
|
82 /** |
|
83 * Used only for testing purposes |
|
84 * DeRegister an Swp and its policy file with the map |
|
85 |
|
86 * @param aSwpKey the swp value to deregister |
|
87 * @param aFilename The file name to associate with the swp value |
|
88 * @leave KErrNotFound if the swp value doesn't exists |
|
89 * @leave KErrArgument if aFilename is longer than KMaxFileName |
|
90 */ |
|
91 #ifdef _DEBUG |
|
92 void CSsmSwpPolicyResolver::DeRegisterSwpMappingL(TUint aSwpKey, const TDesC& aFilename) |
|
93 { |
|
94 SSMLOGLEAVEIFNULL(iSwpPolicyMap); |
|
95 DEBUGPRINT3(_L("CSsmSwpPolicyResolver::DeRegisterSwpMappingL swp %d file %S"), aSwpKey, &aFilename); |
|
96 if(aFilename.Length() > KMaxFileName) |
|
97 { |
|
98 DEBUGPRINT1(_L("Filename too long")); |
|
99 SSMLOGLEAVE(KErrArgument); //lint !e527 Suppress Unreachable. Lint is just confused by macro, warning goes away when code in macro gets expanded |
|
100 } |
|
101 iSwpPolicyMap->DeleteSwpMapL(aSwpKey); |
|
102 } |
|
103 #endif |
|
104 |
|
105 /** |
|
106 Construct a policy frame object based on the SWP value supplied |
|
107 |
|
108 @param aSwp The swp to resolve |
|
109 @return A policy frame containing a valid policy for the swp value |
|
110 @leave KErrNotFound If the swp is not registered |
|
111 @leave Any error returned from LoadLibraryLC |
|
112 */ |
|
113 CSsmSwpPolicyFrame* CSsmSwpPolicyResolver::GetSwpPolicyL(const TSsmSwp& aSwp) |
|
114 { |
|
115 TBuf<KMaxFileName + 2> libraryFilename; //KMaxFileName + KRomDriveLetter().Length() |
|
116 GetFileNameForSwpL(aSwp, libraryFilename); |
|
117 |
|
118 RLibrary library; |
|
119 LoadLibraryLC(library, libraryFilename); |
|
120 |
|
121 // make a process-owned copy of the handle as it has to be used by SsmSwpPolicyServer, |
|
122 // which runs in a separate thread |
|
123 User::LeaveIfError(library.Duplicate(RThread(), EOwnerProcess)); |
|
124 |
|
125 CSsmSwpPolicyFrame* policy = CreatePolicyLC(library); |
|
126 CleanupStack::Pop(policy); |
|
127 policy->SetLibrary(library); // takes ownership of open library handle |
|
128 CleanupStack::Pop(&library); |
|
129 policy->SetSwpKey(aSwp.Key()); |
|
130 |
|
131 DEBUGPRINT2(_L("Created Swp Policy DLL %S"), &libraryFilename); |
|
132 |
|
133 return policy; |
|
134 } |
|
135 |
|
136 /** |
|
137 Construct a descriptor containing the composite file name from the DLL to load |
|
138 |
|
139 @param aSwp The swp to create the file name for |
|
140 @param aLibraryFilename reference to object in which file name is returned |
|
141 @leave KErrNotFound If the swp is not registered |
|
142 */ |
|
143 void CSsmSwpPolicyResolver::GetFileNameForSwpL(const TSsmSwp& aSwp, TDes& aLibraryFilename) const |
|
144 { |
|
145 aLibraryFilename.Zero(); |
|
146 aLibraryFilename.Append(KRomDriveLetter); |
|
147 aLibraryFilename.Append(iSwpPolicyMap->FilenameL(aSwp.Key())); |
|
148 const TInt postfixLength = KDllFilenamePostfix().Length(); |
|
149 if(KDllFilenamePostfix().CompareF(aLibraryFilename.Right(postfixLength))) |
|
150 { |
|
151 aLibraryFilename.Append(KDllFilenamePostfix); |
|
152 } |
|
153 } |
|
154 |
|
155 /** |
|
156 Attempt to load the library with the supplied name |
|
157 |
|
158 @param aLibrary Library object used to load the DLL |
|
159 @param aLibraryFilename file name of library to load |
|
160 @leave KErrNotSupported If UID2 of the DLL is not KStatePolicyDllTypeUidValue |
|
161 @leave KErrNotFound If the requested policy DLL file is missing. |
|
162 @leave KErrCorrupt Or any other system wide error code that the fileserver can raise. |
|
163 */ |
|
164 void CSsmSwpPolicyResolver::LoadLibraryLC(RLibrary& aLibrary, const TDesC& aLibraryFilename) const |
|
165 { |
|
166 CleanupClosePushL(aLibrary); |
|
167 const TInt fileErr = aLibrary.Load(aLibraryFilename); |
|
168 if (fileErr != KErrNone) |
|
169 { |
|
170 DEBUGPRINT3(_L("Failed to load library file %S, file error-code: %d"), &aLibraryFilename, fileErr); |
|
171 User::Leave(fileErr); |
|
172 } |
|
173 if (aLibrary.Type()[1] != KSsmSwpPolicyDllTypeUid) |
|
174 { |
|
175 DEBUGPRINT4(_L("Wrong type (uid2) in swp policy library dll %S. Expected %x found %x"), |
|
176 &aLibraryFilename, KSsmSwpPolicyDllTypeUid, aLibrary.Type()[1]); |
|
177 User::Leave(KErrNotSupported); |
|
178 } |
|
179 } |
|
180 |
|
181 /** |
|
182 Create a policy frame that encapsulates the policy dll represented by the library |
|
183 Retrieves the DLLs factory method which is passed to the frame in oder to create the policy object |
|
184 |
|
185 @param aLibrary The library object that references the DLL |
|
186 @return A policy frame containing the loaded policy |
|
187 @leave Any system error |
|
188 */ |
|
189 CSsmSwpPolicyFrame* CSsmSwpPolicyResolver::CreatePolicyLC(const RLibrary& aLibrary) const |
|
190 { |
|
191 __ASSERT_DEBUG( KNullHandle != aLibrary.Handle(), PanicNow(KPanicSysStateMgr, ESwpPolicyResolverLibraryNotLoaded)); |
|
192 |
|
193 CSsmSwpPolicyFrame* frame = NULL; |
|
194 TRAPD(err, frame = CSsmSwpPolicyFrame::NewL(aLibrary.Lookup(1))); |
|
195 CleanupStack::PushL(frame); |
|
196 #ifdef _DEBUG |
|
197 if(KErrNone != err) |
|
198 { |
|
199 TFileName name = aLibrary.FileName(); |
|
200 DEBUGPRINT3(_L("Error %d when calling first function in State Policy DLL %S."), err, &name); |
|
201 } |
|
202 #endif |
|
203 SSMLOGLEAVEIFERROR(err); |
|
204 return frame; |
|
205 } |
|
206 |
|
207 |