1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 #include "Log.h"
18 
19 #include "task/TaskAll.h"
20 
21 
TaskGeneric(TaskType type)22 TaskGeneric::TaskGeneric(TaskType type):
23     mType(type),
24     mParent(NULL)
25 {
26 
27 }
28 
deleteChildInstance(TaskGeneric * child,void *)29 bool deleteChildInstance(TaskGeneric* child, void* /*data*/)
30 {
31     delete child;
32     return true;
33 }
34 
~TaskGeneric()35 TaskGeneric::~TaskGeneric()
36 {
37     forEachChild(deleteChildInstance, NULL);
38     //mChildren.clear();
39 }
40 
addChild(TaskGeneric * child)41 bool TaskGeneric::addChild(TaskGeneric* child)
42 {
43     mChildren.push_back(child);
44     child->setParent(this);
45     return true;
46 }
47 
forEachChild(bool (* runForEachChild)(TaskGeneric * child,void * data),void * data)48 bool TaskGeneric::forEachChild(bool (*runForEachChild)(TaskGeneric* child, void* data), void* data)
49 {
50     std::list<TaskGeneric*>::iterator i = mChildren.begin();
51     std::list<TaskGeneric*>::iterator end = mChildren.end();
52     for (; i != end; i++) {
53         if (!(*runForEachChild)(*i, data)) {
54             return false;
55         }
56     }
57     return true;
58 }
59 
getParent()60 TaskGeneric* TaskGeneric::getParent()
61 {
62     return mParent;
63 }
64 
getTestCase()65 TaskCase* TaskGeneric::getTestCase()
66 {
67     TaskGeneric* task = this;
68 
69     while (task != NULL) {
70         if (task->getType() == ETaskCase) {
71             // do not use dynamic_cast intentionally
72             return reinterpret_cast<TaskCase*>(task);
73         }
74         task = task->getParent();
75     }
76     LOGE("TaskGeneric::getTestCase no TaskCase found!");
77     return NULL;
78 }
79 
setParent(TaskGeneric * parent)80 void TaskGeneric::setParent(TaskGeneric* parent)
81 {
82     LOGD("TaskGeneric::setParent self %x, parent %x", this, parent);
83     mParent = parent;
84 }
85 
runChild(TaskGeneric * child,void * data)86 bool runChild(TaskGeneric* child, void* data)
87 {
88     TaskGeneric::ExecutionResult* result = reinterpret_cast<TaskGeneric::ExecutionResult*>(data);
89     *result = child->run();
90     if (*result != TaskGeneric::EResultOK) {
91         LOGE("child type %d returned %d", child->getType(), *result);
92         return false;
93     }
94     return true;
95 }
96 
run()97 TaskGeneric::ExecutionResult TaskGeneric::run()
98 {
99     ExecutionResult result = EResultOK;
100     forEachChild(runChild, &result);
101     return result;
102 }
103 
parseAttribute(const android::String8 & name,const android::String8 & value)104 bool TaskGeneric::parseAttribute(const android::String8& name, const android::String8& value)
105 {
106     // default implementation only handles registered string attributes
107     if (!addStringAttribute(name, value)) {
108         LOGE("parseAttribute unknown attribute %s %s for type %d",
109                 name.string(), value.string(), getType());
110         return false;
111     }
112     return true;
113 }
114 
115 
registerSupportedStringAttributes(const android::String8 * keys[])116 void TaskGeneric::registerSupportedStringAttributes(const android::String8* keys[])
117 {
118     int i = 0;
119     while (keys[i] != NULL) {
120         mAllowedStringAttributes.insert(*keys[i]);
121         i++;
122     }
123 }
124 
addStringAttribute(const android::String8 & key,const android::String8 & value)125 bool TaskGeneric::addStringAttribute(const android::String8& key, const android::String8& value)
126 {
127     std::set<android::String8, android::String8>::iterator it = mAllowedStringAttributes.find(key);
128     if (it == mAllowedStringAttributes.end()) {
129         return false; // not allowed
130     }
131     mStringAttributes[key] = value;
132     return true;
133 }
134 
findStringAttribute(const android::String8 & key,android::String8 & value) const135 bool TaskGeneric::findStringAttribute(const android::String8& key, android::String8& value) const
136 {
137     std::map<android::String8, android::String8>::const_iterator it = mStringAttributes.find(key);
138     if (it == mStringAttributes.end()) {
139         return false; // not found
140     }
141     value = it->second;
142     return true;
143 }
144 
145