|
1 /* |
|
2 * Copyright (c) 2002-2008 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: CCFBasicScriptRoot class implementation. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // Define this to get capability strings in to use |
|
20 #define __INCLUDE_CAPABILITY_NAMES__ |
|
21 |
|
22 // INCLUDE FILES |
|
23 #include "cfbasicscriptroot.h" |
|
24 #include "cfifclause.h" |
|
25 #include "cfelseifclause.h" |
|
26 #include "cfelseclause.h" |
|
27 #include "cfdelay.h" |
|
28 #include "cfbasicoptrace.h" |
|
29 |
|
30 #include <gmxmlelement.h> |
|
31 #include <gmxmltext.h> |
|
32 #include <e32capability.h> |
|
33 |
|
34 // CONSTANTS |
|
35 _LIT( KScriptRootName, "script" ); |
|
36 _LIT( KScriptEvaluationDelayAttribute, "evaluationDelay" ); |
|
37 _LIT( KScriptIfName, "if" ); |
|
38 _LIT( KScriptElseIfName, "elseIf" ); |
|
39 _LIT( KScriptElseName, "else" ); |
|
40 _LIT( KScriptUpgradeName, "upgradeSecurity" ); |
|
41 _LIT( KScriptCapabilityName, "capability" ); |
|
42 |
|
43 const TInt KMillisecondsToMicroMultiplier = 1000; |
|
44 |
|
45 // ============================ MEMBER FUNCTIONS =============================== |
|
46 |
|
47 // ----------------------------------------------------------------------------- |
|
48 // CCFBasicScriptRoot::CCFBasicScriptRoot |
|
49 // C++ default constructor can NOT contain any code, that might leave. |
|
50 // ----------------------------------------------------------------------------- |
|
51 // |
|
52 CCFBasicScriptRoot::CCFBasicScriptRoot( MCFOperationServices& aServices, |
|
53 CCFOperationNode* aParent ): |
|
54 CCFScriptRoot( aServices, aParent ), |
|
55 iCapabilitySet( ECapabilityAllFiles ) |
|
56 { |
|
57 FUNC_LOG; |
|
58 } |
|
59 |
|
60 // ----------------------------------------------------------------------------- |
|
61 // CCFBasicScriptRoot::ConstructL |
|
62 // Symbian 2nd phase constructor can leave. |
|
63 // ----------------------------------------------------------------------------- |
|
64 // |
|
65 void CCFBasicScriptRoot::ConstructL() |
|
66 { |
|
67 FUNC_LOG; |
|
68 } |
|
69 |
|
70 // ----------------------------------------------------------------------------- |
|
71 // CCFBasicScriptRoot::NewL |
|
72 // Two-phased constructor. |
|
73 // ----------------------------------------------------------------------------- |
|
74 // |
|
75 CCFBasicScriptRoot* CCFBasicScriptRoot::NewL( MCFOperationServices& aServices, |
|
76 CCFOperationNode* aParent ) |
|
77 { |
|
78 FUNC_LOG; |
|
79 |
|
80 CCFBasicScriptRoot* self = NewLC( aServices, aParent ); |
|
81 CleanupStack::Pop( self ); |
|
82 return self; |
|
83 } |
|
84 |
|
85 // ----------------------------------------------------------------------------- |
|
86 // CCFBasicScriptRoot::NewLC |
|
87 // Two-phased constructor. |
|
88 // ----------------------------------------------------------------------------- |
|
89 // |
|
90 CCFBasicScriptRoot* CCFBasicScriptRoot::NewLC( MCFOperationServices& aServices, |
|
91 CCFOperationNode* aParent ) |
|
92 { |
|
93 FUNC_LOG; |
|
94 |
|
95 CCFBasicScriptRoot* self |
|
96 = new( ELeave ) CCFBasicScriptRoot( aServices, aParent ); |
|
97 CleanupStack::PushL( self ); |
|
98 self->ConstructL(); |
|
99 return self; |
|
100 } |
|
101 |
|
102 // ----------------------------------------------------------------------------- |
|
103 // CCFBasicScriptRoot::ParseL |
|
104 // Construction with parsing from a DOM node. |
|
105 // ----------------------------------------------------------------------------- |
|
106 // |
|
107 CCFBasicScriptRoot* CCFBasicScriptRoot::ParseL( MCFOperationServices& aServices, |
|
108 CCFOperationNode* aParent, |
|
109 CMDXMLNode& aNode ) |
|
110 { |
|
111 if ( aNode.NodeName().CompareF( KScriptRootName ) != 0 |
|
112 || aNode.NodeType() != CMDXMLNode::EElementNode ) |
|
113 { |
|
114 return NULL; // Cannot create script root from the given node. |
|
115 } |
|
116 |
|
117 CCFBasicScriptRoot* self = NewLC( aServices, aParent ); // CLEANUP<< self |
|
118 |
|
119 // Try getting evaluation delay. |
|
120 CMDXMLElement& element = static_cast< CMDXMLElement& >( aNode ); |
|
121 TPtrC evaluationDelay; |
|
122 if ( KErrNone == element.GetAttribute( KScriptEvaluationDelayAttribute, |
|
123 evaluationDelay ) ) |
|
124 { |
|
125 if ( evaluationDelay.Length() ) |
|
126 { |
|
127 // Convert delay to integer. |
|
128 TLex parseInt( evaluationDelay ); |
|
129 parseInt.SkipSpace(); |
|
130 TInt err = parseInt.Val( self->iScriptLevelDelay ); |
|
131 if ( err != KErrNone ) |
|
132 { |
|
133 ERROR_1( err, "CCFBasicScriptRoot::ParseL - Converting script level evaluation delay failed for [%S]", |
|
134 &evaluationDelay ); |
|
135 User::LeaveIfError( err ); |
|
136 } |
|
137 self->iDelayActionFiring = CCFDelay::NewL( *self ); |
|
138 } |
|
139 } |
|
140 |
|
141 TBool ifFound( EFalse ); |
|
142 TBool elseFound( EFalse ); |
|
143 TBool unsupportedNode( EFalse ); |
|
144 // Parse clause nodes (children). |
|
145 CMDXMLNode* child = aNode.FirstChild(); |
|
146 while ( child ) |
|
147 { |
|
148 TPtrC nodeName( child->NodeName() ); |
|
149 if ( child->NodeType() == CMDXMLNode::EElementNode ) |
|
150 { |
|
151 if ( !ifFound ) |
|
152 { |
|
153 // <upgradeSecurity> |
|
154 if ( nodeName.CompareF( KScriptUpgradeName ) == 0 ) |
|
155 { |
|
156 ResolveSecurity( *self, *child ); |
|
157 } |
|
158 // <if> |
|
159 else |
|
160 { |
|
161 if ( nodeName.CompareF( KScriptIfName ) != 0 ) |
|
162 { |
|
163 TPtrC nodeName( child->NodeName() ); |
|
164 ERROR_GEN_1( "CCFBasicScriptRoot::ParseL - Expecting if, encountered %S", |
|
165 &nodeName ); |
|
166 unsupportedNode = ETrue; |
|
167 break; |
|
168 } |
|
169 ifFound = ETrue; |
|
170 CCFClauseNode* clause |
|
171 = CCFIfClause::ParseL( aServices, self, *child ); |
|
172 if ( clause ) |
|
173 { |
|
174 self->iChildren.AppendL( clause ); |
|
175 } |
|
176 else |
|
177 { |
|
178 ERROR_GEN( "CCFBasicScriptRoot::ParseL - Unknown if clause" ); |
|
179 unsupportedNode = ETrue; |
|
180 break; |
|
181 } |
|
182 } |
|
183 } |
|
184 else |
|
185 { |
|
186 // <elseIf> |
|
187 if ( nodeName.CompareF( KScriptElseIfName ) == 0 ) |
|
188 { |
|
189 if ( elseFound ) |
|
190 { |
|
191 ERROR_GEN( "CCFBasicScriptRoot::ParseL - Clause sequence error, elseIf defined after else clause" ); |
|
192 unsupportedNode = ETrue; |
|
193 break; |
|
194 } |
|
195 CCFClauseNode* clause |
|
196 = CCFElseIfClause::ParseL( aServices, self, *child ); |
|
197 if ( clause ) |
|
198 { |
|
199 self->iChildren.AppendL( clause ); |
|
200 } |
|
201 else |
|
202 { |
|
203 ERROR_GEN( "CCFBasicScriptRoot::ParseL - Unknown elseIf clause" ); |
|
204 unsupportedNode = ETrue; |
|
205 break; |
|
206 } |
|
207 } |
|
208 // <else> |
|
209 else if ( nodeName.CompareF( KScriptElseName ) == 0 ) |
|
210 { |
|
211 if ( elseFound ) |
|
212 { |
|
213 ERROR_GEN( "CCFBasicScriptRoot::ParseL - Only one else clause supported" ); |
|
214 unsupportedNode = ETrue; |
|
215 break; |
|
216 } |
|
217 elseFound = ETrue; |
|
218 CCFClauseNode* clause |
|
219 = CCFElseClause::ParseL( aServices, self, *child ); |
|
220 if ( clause ) |
|
221 { |
|
222 self->iChildren.AppendL( clause ); |
|
223 } |
|
224 else |
|
225 { |
|
226 ERROR_GEN( "CCFBasicScriptRoot::ParseL - Unknown else clause" ); |
|
227 unsupportedNode = ETrue; |
|
228 break; |
|
229 } |
|
230 } |
|
231 else |
|
232 { |
|
233 ERROR_GEN_1( "CCFBasicScriptRoot::ParseL - Expecting elseIf or else, encountered %S", |
|
234 &nodeName ); |
|
235 unsupportedNode = ETrue; |
|
236 break; |
|
237 } |
|
238 } |
|
239 } |
|
240 else if ( child->NodeType() != CMDXMLNode::ECommentNode ) |
|
241 { |
|
242 ERROR_GEN_1( "CCFBasicScriptRoot::ParseL - Unsupported node [%S]", |
|
243 &nodeName ); |
|
244 unsupportedNode = ETrue; |
|
245 break; |
|
246 } |
|
247 child = child->NextSibling(); |
|
248 } |
|
249 |
|
250 CleanupStack::Pop( self ); // CLEANUP>> self |
|
251 if ( unsupportedNode ) |
|
252 { |
|
253 delete self; |
|
254 self = NULL; // Signal inability to parse. |
|
255 } |
|
256 |
|
257 CREATE_DOM_INFO( self, aNode ); |
|
258 |
|
259 return self; |
|
260 } |
|
261 |
|
262 |
|
263 // Destructor |
|
264 CCFBasicScriptRoot::~CCFBasicScriptRoot() |
|
265 { |
|
266 FUNC_LOG; |
|
267 |
|
268 delete iDelayActionFiring; |
|
269 iChildren.ResetAndDestroy(); |
|
270 } |
|
271 |
|
272 |
|
273 // ----------------------------------------------------------------------------- |
|
274 // CCFBasicScriptRoot::ContextEvaluatedL |
|
275 // ----------------------------------------------------------------------------- |
|
276 // |
|
277 void CCFBasicScriptRoot::ContextEvaluatedL( TInt aContextLevelDelay ) |
|
278 { |
|
279 FUNC_LOG; |
|
280 |
|
281 INFO_1( "CCFBasicScriptRoot::ContextEvaluatedL - Root value is (-1=undefined, 0=false, 1=true): %d", iValue ); |
|
282 |
|
283 // Delay if required |
|
284 if ( iScriptLevelDelay || aContextLevelDelay ) |
|
285 { |
|
286 TTimeIntervalMicroSeconds32 delay( 0 ); |
|
287 if ( aContextLevelDelay ) |
|
288 { |
|
289 delay = aContextLevelDelay * KMillisecondsToMicroMultiplier; |
|
290 } |
|
291 else |
|
292 { |
|
293 delay = iScriptLevelDelay * KMillisecondsToMicroMultiplier; |
|
294 } |
|
295 |
|
296 if ( !iDelayActionFiring ) |
|
297 { |
|
298 iDelayActionFiring = CCFDelay::NewL( *this ); |
|
299 } |
|
300 iDelayActionFiring->Delay( delay ); |
|
301 |
|
302 INFO( "CCFBasicScriptRoot::ContextEvaluatedL - Action firing delayed" ); |
|
303 } |
|
304 else if ( Value() == CCFOperationNode::ECFConditionTrue |
|
305 && ( !iDelayActionFiring || !( iDelayActionFiring->IsActive() ) ) ) |
|
306 { |
|
307 FireActionsL(); |
|
308 } |
|
309 } |
|
310 |
|
311 // ----------------------------------------------------------------------------- |
|
312 // CCFBasicScriptRoot::EvaluatedL |
|
313 // Cancel evaluation delay if firing actions. |
|
314 // ----------------------------------------------------------------------------- |
|
315 // |
|
316 void CCFBasicScriptRoot::EvaluatedL() |
|
317 { |
|
318 FUNC_LOG; |
|
319 |
|
320 INFO_1( "CCFBasicScriptRoot::EvaluatedL - Root value is (-1=undefined, 0=false, 1=true): %d", iValue ); |
|
321 |
|
322 if ( Value() == ECFConditionTrue ) |
|
323 { |
|
324 if ( iDelayActionFiring ) |
|
325 { |
|
326 iDelayActionFiring->Cancel(); |
|
327 } |
|
328 FireActionsL(); |
|
329 } |
|
330 } |
|
331 |
|
332 // ----------------------------------------------------------------------------- |
|
333 // CCFBasicScriptRoot::UpgradeSecurity |
|
334 // ----------------------------------------------------------------------------- |
|
335 // |
|
336 const TCapabilitySet& CCFBasicScriptRoot::UpgradeSecurity() const |
|
337 { |
|
338 FUNC_LOG; |
|
339 |
|
340 return iCapabilitySet; |
|
341 } |
|
342 |
|
343 // --------------------------------------------------------------------------- |
|
344 // CCFBasicScriptRoot::ActivateL |
|
345 // --------------------------------------------------------------------------- |
|
346 // |
|
347 void CCFBasicScriptRoot::ActivateL() |
|
348 { |
|
349 FUNC_LOG; |
|
350 |
|
351 for ( TInt i = 0; i < iChildren.Count(); ++i ) |
|
352 { |
|
353 iChildren[ i ]->ActivateL(); |
|
354 } |
|
355 } |
|
356 |
|
357 // --------------------------------------------------------------------------- |
|
358 // CCFBasicScriptRoot::Deactivate |
|
359 // --------------------------------------------------------------------------- |
|
360 // |
|
361 void CCFBasicScriptRoot::Deactivate() |
|
362 { |
|
363 FUNC_LOG; |
|
364 |
|
365 for ( TInt i = 0; i < iChildren.Count(); ++i ) |
|
366 { |
|
367 iChildren[ i ]->Deactivate(); |
|
368 } |
|
369 } |
|
370 |
|
371 // --------------------------------------------------------------------------- |
|
372 // CCFBasicScriptRoot::CheckSecurity |
|
373 // --------------------------------------------------------------------------- |
|
374 // |
|
375 TInt CCFBasicScriptRoot::CheckSecurity() |
|
376 { |
|
377 FUNC_LOG; |
|
378 |
|
379 for ( TInt i = 0; i < iChildren.Count(); ++i ) |
|
380 { |
|
381 TInt err = iChildren[ i ]->CheckSecurity(); |
|
382 if ( err != KErrNone ) |
|
383 { |
|
384 return err; |
|
385 } |
|
386 } |
|
387 |
|
388 return KErrNone; |
|
389 } |
|
390 |
|
391 // ----------------------------------------------------------------------------- |
|
392 // CCFBasicScriptRoot::Cleanup |
|
393 // ----------------------------------------------------------------------------- |
|
394 // |
|
395 void CCFBasicScriptRoot::Cleanup() |
|
396 { |
|
397 FUNC_LOG; |
|
398 |
|
399 for ( TInt i = 0; i < iChildren.Count(); ++i ) |
|
400 { |
|
401 iChildren[ i ]->Cleanup(); |
|
402 } |
|
403 } |
|
404 |
|
405 // ----------------------------------------------------------------------------- |
|
406 // CCFBasicScriptRoot::ExpiredL |
|
407 // ----------------------------------------------------------------------------- |
|
408 // |
|
409 void CCFBasicScriptRoot::ExpiredL() |
|
410 { |
|
411 FUNC_LOG; |
|
412 |
|
413 INFO_1( "CCFBasicScriptRoot::ExpiredL - Action firing delay expired, root value is (-1=undefined, 0=false, 1=true): %d", iValue ); |
|
414 |
|
415 if ( Value() == ECFConditionTrue ) |
|
416 { |
|
417 FireActionsL(); |
|
418 } |
|
419 } |
|
420 |
|
421 |
|
422 // ----------------------------------------------------------------------------- |
|
423 // CCFBasicScriptRoot::Evaluate |
|
424 // ----------------------------------------------------------------------------- |
|
425 // |
|
426 void CCFBasicScriptRoot::Evaluate() |
|
427 { |
|
428 FUNC_LOG; |
|
429 |
|
430 DOM_INFO( "CCFBasicScriptRoot::Evaluate" ); |
|
431 |
|
432 TCFConditionValue newValue = Value(); |
|
433 TBool branchUndefined = EFalse; |
|
434 |
|
435 // New value is determined like for the OR operation. |
|
436 for ( TInt i = 0; i < iChildren.Count(); ++i ) |
|
437 { |
|
438 newValue = iChildren[ i ]->Value(); |
|
439 if ( newValue == ECFConditionTrue ) |
|
440 { |
|
441 break; |
|
442 } |
|
443 else if ( newValue == ECFConditionUndefined ) |
|
444 { |
|
445 branchUndefined = ETrue; |
|
446 } |
|
447 } |
|
448 |
|
449 if ( branchUndefined && newValue == ECFConditionFalse ) |
|
450 { |
|
451 newValue = ECFConditionUndefined; |
|
452 } |
|
453 |
|
454 if ( newValue != Value() ) |
|
455 { |
|
456 INFO_1( "CCFBasicScriptRoot::Evaluate - Value changed to (-1=undefined, 0=false, 1=true): %d", newValue ); |
|
457 |
|
458 iValue = newValue; |
|
459 if ( iParent ) |
|
460 { |
|
461 iParent->Evaluate(); // Script root should not have parents. |
|
462 } |
|
463 } |
|
464 else |
|
465 { |
|
466 INFO_1( "CCFBasicScriptRoot::Evaluate - Value still (-1=undefined, 0=false, 1=true): %d", newValue ); |
|
467 } |
|
468 } |
|
469 |
|
470 // ----------------------------------------------------------------------------- |
|
471 // CCFBasicScriptRoot::FireActionsL |
|
472 // Fires actions of the first clause node having true value. |
|
473 // ----------------------------------------------------------------------------- |
|
474 // |
|
475 void CCFBasicScriptRoot::FireActionsL() |
|
476 { |
|
477 FUNC_LOG; |
|
478 |
|
479 // Fires actions of the first clause having true value. |
|
480 for ( TInt i = 0; i < iChildren.Count(); ++i ) |
|
481 { |
|
482 if ( iChildren[ i ]->Value() == ECFConditionTrue ) |
|
483 { |
|
484 iChildren[ i ]->FireActionsL(); |
|
485 break; |
|
486 } |
|
487 } |
|
488 } |
|
489 |
|
490 // ----------------------------------------------------------------------------- |
|
491 // CCFBasicScriptRoot::SetCapabilitites |
|
492 // ----------------------------------------------------------------------------- |
|
493 // |
|
494 void CCFBasicScriptRoot::SetCapabilitites( TCapabilitySet& aSet ) |
|
495 { |
|
496 FUNC_LOG; |
|
497 |
|
498 iCapabilitySet = aSet; |
|
499 } |
|
500 |
|
501 // ----------------------------------------------------------------------------- |
|
502 // CCFBasicScriptRoot::CapabilityFromDesC |
|
503 // ----------------------------------------------------------------------------- |
|
504 // |
|
505 TCapability CCFBasicScriptRoot::CapabilityFromDesC( |
|
506 const TDesC8& aCapDesc ) |
|
507 { |
|
508 FUNC_LOG; |
|
509 |
|
510 TCapability capability = ECapability_None; |
|
511 for( TInt i = 0; i < ECapability_Limit; i++ ) |
|
512 { |
|
513 const char* capName = CapabilityNames[i]; |
|
514 TPtrC8 capabilityPtr( ( TUint8* )capName ); |
|
515 if( capabilityPtr.CompareF( aCapDesc ) == KErrNone ) |
|
516 { |
|
517 // Found capability |
|
518 capability = ( TCapability )i; |
|
519 break; |
|
520 } |
|
521 } |
|
522 |
|
523 return capability; |
|
524 } |
|
525 |
|
526 // ----------------------------------------------------------------------------- |
|
527 // CCFBasicScriptRoot::ResolveSecurity |
|
528 // ----------------------------------------------------------------------------- |
|
529 // |
|
530 void CCFBasicScriptRoot::ResolveSecurity( CCFBasicScriptRoot& aRoot, |
|
531 CMDXMLNode& aNode ) |
|
532 { |
|
533 FUNC_LOG; |
|
534 |
|
535 TBool capabilitiesFound = EFalse; |
|
536 |
|
537 TCapabilitySet capabilitySet; |
|
538 capabilitySet.SetEmpty(); |
|
539 |
|
540 // Get capabilities (security level) |
|
541 HBufC8* textNodeData = HBufC8::New( KMaxFileName ); |
|
542 if( textNodeData ) |
|
543 { |
|
544 TPtr8 textNodeDataPtr( textNodeData->Des() ); |
|
545 CMDXMLNode* capabilityNode = aNode.FirstChild(); |
|
546 while( capabilityNode ) |
|
547 { |
|
548 // <capability> |
|
549 if( capabilityNode->NodeName().CompareF( |
|
550 KScriptCapabilityName ) == KErrNone ) |
|
551 { |
|
552 if( capabilityNode->NodeType() == CMDXMLNode::EElementNode ) |
|
553 { |
|
554 CMDXMLNode* child = capabilityNode->FirstChild(); |
|
555 if( child->NodeType() == CMDXMLNode::ETextNode ) |
|
556 { |
|
557 CMDXMLText* textNode = static_cast<CMDXMLText*>( child ); |
|
558 textNodeDataPtr.Copy( textNode->Data() ); |
|
559 capabilitySet.AddCapability( CapabilityFromDesC( |
|
560 textNodeDataPtr ) ); |
|
561 capabilitiesFound = ETrue; |
|
562 } |
|
563 } |
|
564 } |
|
565 capabilityNode = capabilityNode->NextSibling(); |
|
566 } |
|
567 |
|
568 delete textNodeData; |
|
569 textNodeData = NULL; |
|
570 } |
|
571 |
|
572 // Check if the script does not have an upgrade security level set |
|
573 if( !capabilitiesFound ) |
|
574 { |
|
575 // Set AllFiles capability |
|
576 capabilitySet.Set( ECapabilityAllFiles ); |
|
577 } |
|
578 aRoot.SetCapabilitites( capabilitySet ); |
|
579 } |
|
580 |
|
581 // End of file |