2014-02-14 15:18:10 -08:00
|
|
|
# vim: set sw=4 et:
|
|
|
|
|
2014-05-05 11:58:55 -07:00
|
|
|
import json
|
2014-05-05 12:26:39 -07:00
|
|
|
import itertools
|
2014-05-05 11:58:55 -07:00
|
|
|
import os
|
|
|
|
import re
|
2014-02-14 15:18:10 -08:00
|
|
|
import logging
|
2014-05-04 21:33:13 -07:00
|
|
|
import time
|
2014-05-05 11:58:55 -07:00
|
|
|
import sys
|
2014-05-04 21:33:13 -07:00
|
|
|
|
|
|
|
class Behavior:
|
2014-05-29 20:43:00 -07:00
|
|
|
logger = logging.getLogger(__module__ + "." + __qualname__)
|
2014-05-04 21:33:13 -07:00
|
|
|
|
|
|
|
_behaviors = None
|
2014-05-05 11:58:55 -07:00
|
|
|
_default_behavior = None
|
2014-05-04 21:33:13 -07:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def behaviors():
|
|
|
|
if Behavior._behaviors is None:
|
|
|
|
behaviors_directory = os.path.sep.join(__file__.split(os.path.sep)[:-1] + ['behaviors.d'])
|
2014-05-05 12:26:39 -07:00
|
|
|
behavior_files = itertools.chain(*[[os.path.join(dir, file) for file in files if file.endswith('.js') and file != 'default.js'] for dir, dirs, files in os.walk(behaviors_directory)])
|
2014-05-04 21:33:13 -07:00
|
|
|
Behavior._behaviors = []
|
|
|
|
for file_name in behavior_files:
|
|
|
|
Behavior.logger.debug("reading behavior file {}".format(file_name))
|
2014-05-05 11:58:55 -07:00
|
|
|
script = open(file_name, encoding='utf-8').read()
|
|
|
|
first_line = script[:script.find('\n')]
|
|
|
|
behavior = json.loads(first_line[2:].strip())
|
|
|
|
behavior['script'] = script
|
|
|
|
behavior['file'] = file_name
|
|
|
|
Behavior._behaviors.append(behavior)
|
|
|
|
Behavior.logger.info("will run behaviors from {} on urls matching {}".format(file_name, behavior['url_regex']))
|
2014-05-04 21:33:13 -07:00
|
|
|
|
|
|
|
return Behavior._behaviors
|
|
|
|
|
|
|
|
@staticmethod
|
2014-05-05 11:58:55 -07:00
|
|
|
def default_behavior():
|
|
|
|
if Behavior._default_behavior is None:
|
2014-05-04 21:33:13 -07:00
|
|
|
behaviors_directory = os.path.sep.join(__file__.split(os.path.sep)[:-1] + ['behaviors.d'])
|
|
|
|
file_name = os.path.join(behaviors_directory, 'default.js')
|
|
|
|
Behavior.logger.debug("reading default behavior file {}".format(file_name))
|
2014-05-05 11:58:55 -07:00
|
|
|
script = open(file_name, encoding='utf-8').read()
|
|
|
|
first_line = script[:script.find('\n')]
|
|
|
|
behavior = json.loads(first_line[2:].strip())
|
|
|
|
behavior['script'] = script
|
|
|
|
behavior['file'] = file_name
|
|
|
|
Behavior._default_behavior = behavior
|
|
|
|
return Behavior._default_behavior
|
2014-05-04 21:33:13 -07:00
|
|
|
|
2014-05-05 12:26:39 -07:00
|
|
|
def __init__(self, url, umbra_worker):
|
2014-05-04 21:33:13 -07:00
|
|
|
self.url = url
|
2014-05-05 12:26:39 -07:00
|
|
|
self.umbra_worker = umbra_worker
|
2014-05-04 21:33:13 -07:00
|
|
|
|
|
|
|
self.script_finished = False
|
|
|
|
self.waiting_result_msg_ids = []
|
2014-05-05 11:58:55 -07:00
|
|
|
self.active_behavior = None
|
|
|
|
self.last_activity = time.time()
|
2014-05-04 21:33:13 -07:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
for behavior in Behavior.behaviors():
|
|
|
|
if re.match(behavior['url_regex'], self.url):
|
2014-05-05 11:58:55 -07:00
|
|
|
self.active_behavior = behavior
|
2014-05-04 21:33:13 -07:00
|
|
|
break
|
|
|
|
|
2014-05-05 11:58:55 -07:00
|
|
|
if self.active_behavior is None:
|
|
|
|
self.active_behavior = Behavior.default_behavior()
|
|
|
|
|
2014-05-05 12:26:39 -07:00
|
|
|
self.umbra_worker.send_to_chrome(method="Runtime.evaluate", params={"expression": self.active_behavior['script']})
|
2014-05-05 11:58:55 -07:00
|
|
|
self.notify_of_activity()
|
2014-05-04 21:33:13 -07:00
|
|
|
|
|
|
|
def is_finished(self):
|
2014-08-01 16:22:45 -07:00
|
|
|
msg_id = self.umbra_worker.send_to_chrome(method="Runtime.evaluate",
|
|
|
|
suppress_logging=True, params={"expression":"umbraBehaviorFinished()"})
|
2014-05-04 21:33:13 -07:00
|
|
|
self.waiting_result_msg_ids.append(msg_id)
|
|
|
|
|
2014-05-05 11:58:55 -07:00
|
|
|
request_idle_timeout_sec = 30
|
|
|
|
if self.active_behavior and 'request_idle_timeout_sec' in self.active_behavior:
|
|
|
|
request_idle_timeout_sec = self.active_behavior['request_idle_timeout_sec']
|
|
|
|
idle_time = time.time() - self.last_activity
|
|
|
|
|
|
|
|
return self.script_finished and idle_time > request_idle_timeout_sec
|
2014-05-04 21:33:13 -07:00
|
|
|
|
|
|
|
def is_waiting_on_result(self, msg_id):
|
|
|
|
return msg_id in self.waiting_result_msg_ids
|
|
|
|
|
|
|
|
def notify_of_result(self, chrome_message):
|
|
|
|
# {'id': 59, 'result': {'result': {'type': 'boolean', 'value': True}, 'wasThrown': False}}
|
2014-05-05 19:58:41 -07:00
|
|
|
# {'id': 59, 'result': {'result': {'type': 'boolean', 'value': False}}
|
2014-05-04 21:33:13 -07:00
|
|
|
self.waiting_result_msg_ids.remove(chrome_message['id'])
|
|
|
|
if ('result' in chrome_message
|
2014-05-05 19:58:41 -07:00
|
|
|
and not ('wasThrown' in chrome_message['result'] and chrome_message['result']['wasThrown'])
|
2014-05-04 21:33:13 -07:00
|
|
|
and 'result' in chrome_message['result']
|
|
|
|
and type(chrome_message['result']['result']['value']) == bool):
|
|
|
|
self.script_finished = chrome_message['result']['result']['value']
|
|
|
|
else:
|
|
|
|
self.logger.error("chrome message doesn't look like a boolean result! {}".format(chrome_message))
|
|
|
|
|
|
|
|
def notify_of_activity(self):
|
|
|
|
self.last_activity = time.time()
|
|
|
|
|
2014-05-05 11:58:55 -07:00
|
|
|
if __name__ == "__main__":
|
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG,
|
|
|
|
format='%(asctime)s %(process)d %(levelname)s %(threadName)s %(name)s.%(funcName)s(%(filename)s:%(lineno)d) %(message)s')
|
|
|
|
logger = logging.getLogger('umbra.behaviors')
|
|
|
|
logger.info("custom behaviors: {}".format(Behavior.behaviors()))
|
|
|
|
logger.info("default behavior: {}".format(Behavior.default_behavior()))
|
|
|
|
|
2014-02-13 01:00:39 -05:00
|
|
|
|