Index: tools/android/loading/cloud/common/clovis_task.py |
diff --git a/tools/android/loading/cloud/common/clovis_task.py b/tools/android/loading/cloud/common/clovis_task.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8bc3127a34364c279459cd8f576eac396d2c6d69 |
--- /dev/null |
+++ b/tools/android/loading/cloud/common/clovis_task.py |
@@ -0,0 +1,66 @@ |
+# Copyright 2016 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import base64 |
+import json |
+ |
+class ClovisTask(object): |
+ """Generic task, generated by the AppEngine frontend and consumed by the |
+ ComputeEngine backend. |
+ """ |
+ |
+ def __init__(self, action, params, taskqueue_tag): |
+ """Params: |
+ action(str): Action accomplished by this task. |
+ params(dict): Parameters of task. |
+ taskqueue_tag(str): Tag of the task. Optional. |
+ """ |
+ self._action = action |
+ self._params = params |
+ self._taskqueue_tag = taskqueue_tag |
+ |
+ @classmethod |
+ def FromJsonDict(cls, json_dict): |
+ """Loads a ClovisTask from a JSON string. |
+ |
+ Returns: |
+ ClovisTask: The task, or None if the string is invalid. |
+ """ |
+ try: |
+ data = json.loads(json_dict) |
+ action = data['action'] |
+ params = data['params'] |
+ tag = data.get('taskqueue_tag') |
+ # Vaidate the format. |
+ if action == 'trace': |
+ urls = params['urls'] |
+ if (type(urls) is not list) or (len(urls) == 0): |
+ return None |
+ else: |
+ # When more actions are supported, check that they are valid here. |
+ return None |
+ return cls(action, params, tag) |
+ except Exception: |
+ return None |
+ |
+ @classmethod |
+ def FromBase64(cls, base64_string): |
+ """Loads a ClovisTask from a base 64 string.""" |
+ return ClovisTask.FromJsonDict(base64.b64decode(base64_string)) |
+ |
+ def ToJsonDict(self): |
+ """Returns the JSON representation of the task.""" |
+ task_dict = { 'action': self._action, 'params': self._params } |
+ if self._taskqueue_tag: |
+ task_dict['taskqueue_tag'] = self._taskqueue_tag |
+ return json.dumps(task_dict) |
+ |
+ def Action(self): |
+ return self._action |
+ |
+ def Params(self): |
+ return self._params |
+ |
+ def TaskqueueTag(self): |
+ return self._taskqueue_tag |