Chromium Code Reviews| Index: tools/chrome_remote_control/chrome_remote_control/tab.py |
| diff --git a/tools/chrome_remote_control/chrome_remote_control/tab.py b/tools/chrome_remote_control/chrome_remote_control/tab.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cea0a54bb6b27c395984d43ca278b60ccf1ae6eb |
| --- /dev/null |
| +++ b/tools/chrome_remote_control/chrome_remote_control/tab.py |
| @@ -0,0 +1,90 @@ |
| +# Copyright (c) 2012 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 json |
| +import time |
| +import websocket |
| +import socket |
| + |
| +class EvaluateException(Exception): |
| + pass |
| + |
| +class TabRuntime(object): |
| + def __init__(self, inspector_backend): |
| + self._inspector_backend = inspector_backend |
| + self._inspector_backend.RegisterDomain( |
| + 'Runtime', |
| + self._OnNotification, |
| + self._OnClose) |
| + |
| + def _OnNotification(self, msg): |
| + pass |
| + |
| + def _OnClose(self): |
| + pass |
| + |
| + def Evaluate(self, expr): |
| + request = { |
| + "method": "Runtime.evaluate", |
| + "params": { |
| + "expression": expr, |
| + "returnByValue": True |
| + } |
| + } |
| + res = self._inspector_backend.SyncRequest(request) |
| + if res["result"]["wasThrown"]: |
| + # TODO(nduca): propagate stacks from javascript up to the python |
| + # exception. |
| + raise EvaluateException(res["result"]["result"]["description"]) |
| + if res["result"]["result"]["type"] == 'undefined': |
| + return None |
| + return res["result"]["result"]["value"] |
| + |
| +class Tab(object): |
| + def __init__(self, inspector_backend): |
| + self._inspector_backend = inspector_backend |
| + self.runtime = TabRuntime(self._inspector_backend) |
| + |
| + def Close(self): |
| + self._inspector_backend.Close() |
| + self._inspector_backend = None |
| + |
| + def __enter__(self): |
| + return self |
| + |
| + def __exit__(self, *args): |
| + if self._inspector_backend: |
| + self.Close() |
|
dtu
2012/08/27 22:27:27
Makes more sense in __del__, so it'll take effect
|
| + |
| + def BeginToLoadURL(self, url): |
| + # In order to tell when the document has actually changed, |
| + # we go to about:blank first and wait. When that has happened, we |
| + # to go the new URL and detect the document being non-about:blank as |
| + # indication that the new document is loading. |
| + self.runtime.Evaluate("document.location = 'about:blank';") |
| + while True: |
|
dtu
2012/08/27 22:27:27
while self.runtime.Evaluate("document.location.hre
|
| + hr = self.runtime.Evaluate("document.location.href") |
| + if hr == "about:blank": |
| + break |
| + time.sleep(0.01) |
| + |
| + self.runtime.Evaluate("document.location = '%s';" % url) |
| + while True: |
|
dtu
2012/08/27 22:27:27
while self.runtime.Evaluate("document.location.hre
|
| + hr = self.runtime.Evaluate("document.location.href") |
| + if hr != "about:blank": |
| + break |
| + time.sleep(0.01) |
| + |
| + def WaitForDocumentReadyStateToBeComplete(self): |
| + while True: |
|
dtu
2012/08/27 22:27:27
while self.runtime.Evaluate("document.readyState")
|
| + rs = self.runtime.Evaluate("document.readyState") |
| + if rs == 'complete': |
| + break |
| + time.sleep(0.01) |
| + |
| + def WaitForDocumentReadyStateToBeInteractiveOrBetter(self): |
| + while True: |
| + rs = self.runtime.Evaluate("document.readyState") |
| + if rs == 'complete' or rs == 'interactive': |
| + break |
| + time.sleep(0.01) |