|
1 /* |
|
2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Describes a factory to return a handle to the Catalogs engine |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <e32base.h> |
|
20 #include <e32property.h> |
|
21 #include <ecom/ecom.h> |
|
22 |
|
23 #include "catalogsengine.h" |
|
24 #include "catalogsdebug.h" |
|
25 #include "catalogserrors.h" |
|
26 #include "catalogsconstants.h" |
|
27 |
|
28 #ifndef CATALOGS_ECOM |
|
29 |
|
30 static void CleanupMutex( TAny* aMutexPtr ) |
|
31 { |
|
32 DLTRACEIN(("")); |
|
33 RMutex* mutex = static_cast< RMutex* >( aMutexPtr ); |
|
34 DLINFO(( "Signalling and closing engine mutex" )); |
|
35 mutex->Signal(); |
|
36 mutex->Close(); |
|
37 } |
|
38 |
|
39 |
|
40 static void GetMutexLC( RMutex& aMutex ) |
|
41 { |
|
42 DLTRACEIN(("")); |
|
43 |
|
44 // First try to open the global mutex. |
|
45 DLINFO(( "Trying to open engine mutex" )); |
|
46 |
|
47 TInt mutexOpenErr = aMutex.OpenGlobal( KCatalogsEngineMutex ); |
|
48 if( mutexOpenErr == KErrNotFound ) |
|
49 { |
|
50 DLINFO(( "Mutex was not found, trying to create." )); |
|
51 mutexOpenErr = aMutex.CreateGlobal( KCatalogsEngineMutex ); |
|
52 if( mutexOpenErr == KErrAlreadyExists ) |
|
53 { |
|
54 // Someone beat us to it. Try opening again. |
|
55 DLINFO(( "Mutex was just created. Trying to open." )); |
|
56 mutexOpenErr = aMutex.OpenGlobal( KCatalogsEngineMutex ); |
|
57 } |
|
58 } |
|
59 |
|
60 if( mutexOpenErr != KErrNone ) |
|
61 { |
|
62 DLERROR(( "Mutex open failed with %d", mutexOpenErr )); |
|
63 DLTRACEOUT(( "LEAVE %d", mutexOpenErr )); |
|
64 User::Leave( mutexOpenErr ); |
|
65 } |
|
66 |
|
67 DLINFO(( "Waiting for engine mutex..." )); |
|
68 aMutex.Wait(); |
|
69 DLINFO(( "Got property mutex" )); |
|
70 |
|
71 // Push a cleanup item for signalling and closing the mutex. |
|
72 CleanupStack::PushL( TCleanupItem( CleanupMutex, &aMutex ) ); |
|
73 |
|
74 DLTRACEOUT(("")); |
|
75 } |
|
76 |
|
77 static TBool IsMaintenanceLockOnL() |
|
78 { |
|
79 DLTRACEIN(("")); |
|
80 |
|
81 // Get Catalogs Engine maintenance P&S variable |
|
82 DLINFO(( "Reading maintenance lock value" )); |
|
83 |
|
84 TInt value; |
|
85 TInt err = RProperty::Get( |
|
86 KCatalogsEnginePropertyCategory, |
|
87 KCatalogsEnginePropertyKeyMaintenanceLock, |
|
88 value ); |
|
89 |
|
90 if( err == KErrNone ) |
|
91 { |
|
92 DLINFO(( "Maintenance lock value %08x", value )); |
|
93 |
|
94 DLTRACEOUT(( "%d", value != 0 )); |
|
95 return value != 0; |
|
96 } |
|
97 |
|
98 else if( err != KErrNotFound ) |
|
99 { |
|
100 DLERROR(( "Maintenance lock read failed with %d", err )); |
|
101 DLTRACEOUT(( "LEAVE %d", err )); |
|
102 User::Leave( err ); |
|
103 } |
|
104 |
|
105 // Maintenance P&S variable not set, not in maintenance mode. |
|
106 DLTRACEOUT(( "EFalse" )); |
|
107 return EFalse; |
|
108 } |
|
109 |
|
110 |
|
111 static void SetMaintenanceLockL( TBool aLock ) |
|
112 { |
|
113 DLTRACEIN(( "Writing %d to maintenance lock P&S", aLock )); |
|
114 |
|
115 // Write maintenance lock property. |
|
116 DLINFO(( "Writing maintenance lock property" )); |
|
117 TInt err = RProperty::Set( |
|
118 KCatalogsEnginePropertyCategory, |
|
119 KCatalogsEnginePropertyKeyMaintenanceLock, |
|
120 aLock ); |
|
121 |
|
122 if( err == KErrNotFound ) |
|
123 { |
|
124 DLINFO(( "Maintenance lock property not found, invoking server to define it" )); |
|
125 |
|
126 // P&S variable is defined in the server process. Connect to server to |
|
127 // accomplish this. |
|
128 MCatalogsEngineObserver* nullObserver = NULL; |
|
129 CCatalogsEngine* engine = CCatalogsEngine::NewLC( *nullObserver ); |
|
130 |
|
131 err = engine->Connect( TUid::Uid( 0 ) ); |
|
132 |
|
133 // Engine no longer needed, close, release and delete. |
|
134 DLINFO(( "Connect returned %d. Closing engine object.", err )); |
|
135 engine->Close(); |
|
136 |
|
137 DLINFO(( "Deleting engine object" )); |
|
138 CleanupStack::PopAndDestroy( engine ); |
|
139 |
|
140 if( err == KErrNone ) |
|
141 { |
|
142 // The maintenance lock property should be defined now, try again. |
|
143 DLINFO(( "Writing maintenance lock property" )); |
|
144 err = RProperty::Set( |
|
145 KCatalogsEnginePropertyCategory, |
|
146 KCatalogsEnginePropertyKeyMaintenanceLock, |
|
147 aLock ); |
|
148 } |
|
149 } |
|
150 |
|
151 if( err != KErrNone ) |
|
152 { |
|
153 DLERROR(( "Maintenance lock set failed, error %d", err )); |
|
154 DLTRACEOUT(( "LEAVE %d", err )); |
|
155 User::Leave( err ); |
|
156 } |
|
157 |
|
158 DLTRACEOUT(("")); |
|
159 } |
|
160 |
|
161 |
|
162 EXPORT_C CCatalogsEngine::TState CCatalogsEngine::StateL() |
|
163 { |
|
164 DLTRACEIN(("")); |
|
165 |
|
166 RMutex mutex; |
|
167 GetMutexLC( mutex ); |
|
168 |
|
169 TState result = EStateNormal; |
|
170 |
|
171 if( IsMaintenanceLockOnL() ) |
|
172 { |
|
173 // Either EStatePrepareMaintenance or EStateMaintenance. |
|
174 DLINFO(( "Maintenance lock is on." )); |
|
175 |
|
176 // Check if engine instances exist. This can be determined by mutex usage: |
|
177 // all existing instances hold a handle to the mutex. If there are handles |
|
178 // in addition to the one held here, other instances exist. |
|
179 THandleInfo mutexHandleInfo; |
|
180 mutex.HandleInfo( &mutexHandleInfo ); |
|
181 |
|
182 DLINFO(( "Mutex handle in-process count %d, process count %d", |
|
183 mutexHandleInfo.iNumOpenInProcess, mutexHandleInfo.iNumProcesses )); |
|
184 |
|
185 if( mutexHandleInfo.iNumOpenInProcess > 1 || |
|
186 mutexHandleInfo.iNumProcesses > 1 ) |
|
187 { |
|
188 result = EStatePrepareMaintenance; |
|
189 } |
|
190 else |
|
191 { |
|
192 result = EStateMaintenance; |
|
193 } |
|
194 } |
|
195 |
|
196 CleanupStack::PopAndDestroy(); // mutex Signal() and Close() |
|
197 DLTRACEOUT(( "%d", result )); |
|
198 return result; |
|
199 } |
|
200 |
|
201 |
|
202 /** |
|
203 * Instantiates the catalogs engine, using the maintenance lock to determine |
|
204 * if instantiation is allowed. |
|
205 */ |
|
206 EXPORT_C CCatalogsEngine* CCatalogsEngine::NewLC( MCatalogsEngineObserver& aObserver ) |
|
207 { |
|
208 DLTRACEIN(( "aObserver=%08x", &aObserver )); |
|
209 |
|
210 // Get/create Catalogs Engine maintenance lock mutex |
|
211 RMutex mutex; |
|
212 GetMutexLC( mutex ); |
|
213 |
|
214 if( IsMaintenanceLockOnL() ) |
|
215 { |
|
216 DLWARNING(( "Catalogs Engine in maintenance mode, cannot create engine instance" )); |
|
217 DLTRACEOUT(( "LEAVE KCatalogsErrorMaintenanceMode" )); |
|
218 User::Leave( KCatalogsErrorMaintenanceMode ); |
|
219 } |
|
220 |
|
221 // Not in maintenance mode, ok to instantiate engine class. Do this before releasing the |
|
222 // mutex, as this may define the maintenance lock P&S. |
|
223 DLINFO(( "Creating ECom object with UID %08x", KCCatalogsEngineImplUid.iUid )); |
|
224 CCatalogsEngine* object = 0; |
|
225 TRAPD( err, |
|
226 { |
|
227 object = reinterpret_cast< CCatalogsEngine* >( |
|
228 REComSession::CreateImplementationL( |
|
229 KCCatalogsEngineImplUid, _FOFF(CCatalogsEngine,iDtor_ID_Key), &aObserver ) ); |
|
230 }); //TRAPD |
|
231 DLINFO(("ECom creation err=%d",err)); |
|
232 User::LeaveIfError( err ); |
|
233 |
|
234 DLINFO(( "ECom returned pointer %08x", object )); |
|
235 DASSERT( object != NULL ); |
|
236 |
|
237 CleanupStack::PopAndDestroy(); // mutex signal & close |
|
238 CleanupStack::PushL( object ); |
|
239 |
|
240 DLTRACEOUT(( "%08x", object )); |
|
241 return object; |
|
242 } |
|
243 |
|
244 |
|
245 EXPORT_C CCatalogsEngine* CCatalogsEngine::NewL( MCatalogsEngineObserver& aObserver ) |
|
246 { |
|
247 DLTRACEIN(( "aObserver=%08x", &aObserver )); |
|
248 CCatalogsEngine* engine = CCatalogsEngine::NewLC( aObserver ); |
|
249 CleanupStack::Pop(); |
|
250 DLTRACEOUT(( "%08x", engine )); |
|
251 return engine; |
|
252 } |
|
253 |
|
254 EXPORT_C void CCatalogsEngine::StartMaintenanceL() |
|
255 { |
|
256 DLTRACEIN(("")); |
|
257 |
|
258 // Get Catalogs Engine maintenance lock mutex |
|
259 RMutex mutex; |
|
260 GetMutexLC( mutex ); |
|
261 |
|
262 if( IsMaintenanceLockOnL() ) |
|
263 { |
|
264 DLERROR(( "Engine already locked for maintenance mode" )); |
|
265 DLTRACEOUT(( "LEAVE KErrAlreadyExists" )); |
|
266 User::Leave( KErrAlreadyExists ); |
|
267 } |
|
268 |
|
269 // Set the maintenance lock. This will send a P&S event to any existing |
|
270 // engine instances and create a callback for the clients. |
|
271 SetMaintenanceLockL( ETrue ); |
|
272 |
|
273 CleanupStack::PopAndDestroy(); // mutex signal & close |
|
274 } |
|
275 |
|
276 EXPORT_C void CCatalogsEngine::EndMaintenanceL() |
|
277 { |
|
278 // Get Catalogs Engine maintenance lock mutex |
|
279 DLTRACEIN(("")); |
|
280 |
|
281 RMutex mutex; |
|
282 GetMutexLC( mutex ); |
|
283 |
|
284 if( !IsMaintenanceLockOnL() ) |
|
285 { |
|
286 DLERROR(( "Maintenance lock not on" )); |
|
287 DLTRACEOUT(( "LEAVE KCatalogsErrorMaintenanceNotStarted" )); |
|
288 User::Leave( KCatalogsErrorMaintenanceNotStarted ); |
|
289 } |
|
290 |
|
291 SetMaintenanceLockL( EFalse ); |
|
292 |
|
293 CleanupStack::PopAndDestroy(); // mutex signal & close |
|
294 } |
|
295 |
|
296 #endif // CATALOGS_ECOM |
|
297 |
|
298 /** |
|
299 * Destructor |
|
300 */ |
|
301 CCatalogsEngine::~CCatalogsEngine() |
|
302 { |
|
303 DLTRACEIN(( "this=%08x", this )); |
|
304 REComSession::DestroyedImplementation( iDtor_ID_Key ); |
|
305 // Is it ok to say FinalClose() even if engine object instances exist? Should be, hope so. |
|
306 // FinalClose not okay, from help: "It must never be called from within a plugin class destructor." |
|
307 //DLTRACE(("FinalClose()")); |
|
308 |
|
309 //REComSession::FinalClose(); |
|
310 DLTRACEOUT(( "this=%08x", this )); |
|
311 } |
|
312 |
|
313 /** |
|
314 * Constructor |
|
315 */ |
|
316 CCatalogsEngine::CCatalogsEngine() |
|
317 { |
|
318 } |