-
Notifications
You must be signed in to change notification settings - Fork 0
/
chromeapp.py
834 lines (712 loc) · 24.2 KB
/
chromeapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# 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 base64
import hashlib
import heapq
import json
import logging
import os
import re
import select
import socket
import subprocess
import sys
import time
import urlparse
import BaseHTTPServer
_unittests_running = False
class ChromeNotFoundException(Exception):
pass
class _PossibleDesktopBrowser(object):
def __init__(self, browser_type, executable):
self.browser_type = browser_type
self.local_executable = executable
def __repr__(self):
return '_PossibleDesktopBrowser(browser_type=%s)' % self.browser_type
def _samefile(a, b):
if sys.platform != 'win32':
return os.path.samefile(a, b)
return os.path.abspath(a) == os.path.abspath(b)
def _FindAllAvailableBrowsers():
"""Finds all the desktop browsers available on this machine."""
browsers = []
has_display = True
if (sys.platform.startswith('linux') and
os.getenv('DISPLAY') == None):
has_display = False
def AddIfFound(browser_type, browser_dir, app_name):
app = os.path.join(browser_dir, app_name)
if os.path.exists(app):
browsers.append(_PossibleDesktopBrowser(browser_type,
app))
return True
return False
# Look for a browser in the standard chrome build locations.
if sys.platform == 'darwin':
chromium_app_name = 'Chromium.app/Contents/MacOS/Chromium'
elif sys.platform.startswith('linux'):
chromium_app_name = 'chrome'
elif sys.platform.startswith('win'):
chromium_app_name = 'chrome.exe'
else:
raise Exception('Platform not recognized')
# Mac-specific options.
if sys.platform == 'darwin':
mac_canary = ('/Applications/Google Chrome Canary.app/'
'Contents/MacOS/Google Chrome Canary')
mac_system = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
if os.path.exists(mac_canary):
browsers.append(_PossibleDesktopBrowser('canary',
mac_canary))
if os.path.exists(mac_system):
browsers.append(_PossibleDesktopBrowser('system',
mac_system))
# Linux specific options.
if sys.platform.startswith('linux'):
# Look for a google-chrome instance.
found = False
try:
with open(os.devnull, 'w') as devnull:
found = subprocess.call(['google-chrome', '--version'],
stdout=devnull, stderr=devnull) == 0
except OSError:
pass
if found:
browsers.append(
_PossibleDesktopBrowser('system',
'google-chrome'))
# Win32-specific options.
if sys.platform.startswith('win'):
system_path = os.path.join('Google', 'Chrome', 'Application')
canary_path = os.path.join('Google', 'Chrome SxS', 'Application')
win_search_paths = [os.getenv('PROGRAMFILES(X86)'),
os.getenv('PROGRAMFILES'),
os.getenv('LOCALAPPDATA')]
for path in win_search_paths:
if not path:
continue
if AddIfFound('canary', os.path.join(path, canary_path),
chromium_app_name):
break
for path in win_search_paths:
if not path:
continue
if AddIfFound('system', os.path.join(path, system_path),
chromium_app_name):
break
if len(browsers) and not has_display:
logging.warning(
'Found (%s), but you do not have a DISPLAY environment set.' %
','.join([b.browser_type for b in browsers]))
return []
return browsers
_ExceptionNames = {
404: 'Not found',
500: 'Internal exception',
}
class _RequestException(Exception):
def __init__(self, number):
super(_RequestException, self).__init__('_RequestException')
self.number = number
class _RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def __init__(self, request, client_address, server):
self._server = server
BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address, server)
def _SendJSON(self, obj, resp_code=200, resp_code_str='OK'):
text = json.dumps(obj)
try:
self.send_response(resp_code, resp_code_str)
self.send_header('Cache-Control', 'no-cache')
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Length', len(text))
self.end_headers()
self.wfile.write(text)
except IOError:
return
def log_message(self, format, *args):
pass
def do_POST(self):
self._HandleRequest('POST')
def do_GET(self):
self._HandleRequest('GET')
def _HandleRequest(self, method):
if 'Content-Length' in self.headers:
cl = int(self.headers['Content-Length'])
text = self.rfile.read(cl).encode('utf8')
try:
if text != '':
content = json.loads(text)
else:
content = ''
except ValueError:
raise Exception('Payload was unparseable: [%s]' % text)
else:
content = None
if self.path == '/ping':
self._SendJSON('pong')
return
try:
res = self._server.HandleRequest(method, self.path, content)
self._SendJSON(res)
except _RequestException, ex:
self.send_response(ex.number, _ExceptionNames[ex.number])
self.send_header('Content-Length', 0)
self.end_headers()
except:
import traceback
traceback.print_exc()
self.send_response(500, 'ServerError')
self.send_header('Content-Length', 0)
self.end_headers()
###########################################################################
class _TimeoutTask(object):
def __init__(self, cb, deadline, args):
self.cb = cb
self.deadline = deadline
self.args = args
def __cmp__(self, that):
return cmp(self.deadline, that.deadline)
# crx id computation
###########################################################################
def _HexToInt(hex_chars):
""" Convert bytes like \xab -> 171 """
val = 0
for i in xrange(len(hex_chars)):
val += pow(256, i) * ord(hex_chars[i])
return val
def _HexToMPDecimal(hex_chars):
""" Convert bytes to an MPDecimal string. Example \x00 -> "aa"
This gives us the AppID for a chrome extension.
"""
result = ''
base = ord('a')
for i in xrange(len(hex_chars)):
value = ord(hex_chars[i])
dig1 = value / 16
dig2 = value % 16
result += chr(dig1 + base)
result += chr(dig2 + base)
return result
def _GetPublicKeyFromPath(filepath):
# Normalize the path for windows to have capital drive letters.
# We intentionally don't check if sys.platform == 'win32' and just
# check if this looks like drive letter so that we can test this
# even on posix systems.
if (len(filepath) >= 2 and
filepath[0].islower() and
filepath[1] == ':'):
return filepath[0].upper() + filepath[1:]
return filepath
def _GetPublicKeyUnpacked(f, filepath):
manifest = json.load(f)
if 'key' not in manifest:
# Use the path as the public key.
# See Extension::GenerateIdForPath in extension.cc
return _GetPublicKeyFromPath(filepath)
else:
return base64.standard_b64decode(manifest['key'])
def _GetPublicKey(filename):
pub_key = ''
if not os.path.isdir(filename):
raise Exception('crx_id for packed extensions not supported');
# Assume it's an unpacked extension
f = open(os.path.join(filename, 'manifest.json'), 'rb')
pub_key = _GetPublicKeyUnpacked(f, filename)
f.close()
return pub_key
def _GetCRXAppID(filename):
pub_key = _GetPublicKey(filename)
pub_key_hash = hashlib.sha256(pub_key).digest()
# AppID is the MPDecimal of only the first 128 bits of the hash.
return _HexToMPDecimal(pub_key_hash[:128/8])
###########################################################################
class _Daemon(BaseHTTPServer.HTTPServer):
def __init__(self, server_address):
BaseHTTPServer.HTTPServer.__init__(self, server_address, _RequestHandler)
self._port = server_address[1]
self._is_running = False
self._pending_timeout_heap = []
@property
def port(self):
return self._port
@property
def is_running(self):
return self._is_running
def AddDelayedTask(self, cb, delay, *args):
deadline = time.time() + delay
to = _TimeoutTask(cb, deadline, args)
heapq.heappush(self._pending_timeout_heap, to)
def serve_forever(self):
self._is_running = True
try:
while self._is_running:
now = time.time()
while True:
if len(self._pending_timeout_heap):
deadline = self._pending_timeout_heap[0].deadline
if now > deadline:
item = heapq.heappop(self._pending_timeout_heap)
item.cb(*item.args)
else:
next_deadline = deadline
break
else:
next_deadline = now + 0.2
break
now = time.time()
delay = max(0.0,next_deadline - now)
delay = min(0.25,delay)
r, w, e = select.select([self], [], [], delay)
if r:
self.handle_request()
finally:
self._is_running = False
def HandleRequest(self, method, path, content):
if self.handler:
return self.handler(method, path, content)
raise _RequestException('404')
def Stop(self):
assert self._is_running
self._is_running = False
def Run(self):
logging.debug('Starting chromeapp._Daemon on port %d', self._port)
self.serve_forever()
logging.debug('Shut down chromeapp._Daemon on port %d', self._port)
class _TimeoutException(Exception):
pass
def _WaitFor(condition,
timeout, poll_interval=0.1,
pass_time_left_to_func=False):
assert isinstance(condition, type(lambda: None)) # is function
start_time = time.time()
while True:
if pass_time_left_to_func:
res = condition(max((start_time + timeout) - time.time(), 0.0))
else:
res = condition()
if res:
break
if time.time() - start_time > timeout:
if condition.__name__ == '<lambda>':
try:
condition_string = inspect.getsource(condition).strip()
except IOError:
condition_string = condition.__name__
else:
condition_string = condition.__name__
raise _TimeoutException('Timed out while waiting %ds for %s.' %
(timeout, condition_string))
time.sleep(poll_interval)
def IsChromeInstalled():
"""Returns whether chromeapp works on this system."""
browsers = _FindAllAvailableBrowsers()
return len(browsers) > 0
def _HexToMPDecimal(hex_chars):
""" Convert bytes to an MPDecimal string. Example \x00 -> "aa"
This gives us the AppID for a chrome extension.
"""
result = ''
base = ord('a')
for i in xrange(len(hex_chars)):
value = ord(hex_chars[i])
dig1 = value / 16
dig2 = value % 16
result += chr(dig1 + base)
result += chr(dig2 + base)
return result
def _GetPublicKeyFromPath(filepath):
# Normalize the path for windows to have capital drive letters.
# We intentionally don't check if sys.platform == 'win32' and just
# check if this looks like drive letter so that we can test this
# even on posix systems.
if (len(filepath) >= 2 and
filepath[0].islower() and
filepath[1] == ':'):
return filepath[0].upper() + filepath[1:]
return filepath
def _GetPublicKeyUnpacked(filepath):
assert os.path.isdir(filepath)
f = open(os.path.join(filepath, 'manifest.json'), 'rb')
manifest = json.load(f)
if 'key' not in manifest:
# Use the path as the public key.
# See Extension::GenerateIdForPath in extension.cc
return _GetPublicKeyFromPath(os.path.abspath(filepath))
else:
return base64.standard_b64decode(manifest['key'])
def _GetCRXAppID(filepath):
pub_key = _GetPublicKeyUnpacked(filepath)
pub_key_hash = hashlib.sha256(pub_key).digest()
# AppID is the MPDecimal of only the first 128 bits of the hash.
return _HexToMPDecimal(pub_key_hash[:128/8])
class AppInstance(object):
def __init__(self, app, args=None):
self._app = app
self._proc = None
self._devnull = None
self._cur_chromeapp_js = None
self._event_listeners = {}
if args:
self._args = args
else:
self._args = []
self._exit_code = None
self._exiting_run_loop = False
def __enter__(self):
if not self.is_started:
self.Start()
return self
def __exit__(self, *args):
if self.is_started:
self._CloseBrowserProcess()
@property
def is_started(self):
return self._proc != None
def Start(self):
tmp = socket.socket()
tmp.bind(('', 0))
port = tmp.getsockname()[1]
tmp.close()
self._daemon = _Daemon(('localhost', port))
self._daemon.handler = self._HandleRequest
browsers = _FindAllAvailableBrowsers()
if len(browsers) == 0:
raise ChromeNotFoundException('Could not find Chrome. Cannot start app.')
browser = browsers[0]
app_id = _GetCRXAppID(self._app.manifest_dirname)
if self._app.debug_mode:
print "chromeapp: app_id is %s" % app_id
browser_args = [browser.local_executable]
browser_args.extend(self._app._GetBrowserStartupArgs())
browser_args.append('--load-and-launch-app=%s' % self._app.manifest_dirname)
logging.info('Launching %s as %s', self._app.stable_app_name, app_id)
self._CreateLaunchJS()
try:
self._Launch(browser_args)
except:
if self.is_started:
self._CloseBrowserProcess()
def _CreateLaunchJS(self):
js_template = """
'use strict';
// DO NOT COMMIT! This template is automatically created and updated by
// py-chrome-ui just before launch, with the necessary
// parameters for communicating back to the hosting python code.
(function() {
var BASE_URL = "__CHROMEAPP_REPLY_URL__"; // Note: chromeapp will set this up during launch.
var DEBUG_MODE = __CHROMEAPP_DEBUG_MODE__; // Note: chromeapp will set this up during launch.lj
function reqAsync(method, path, data, opt_response_cb, opt_err_cb) {
if (path[0] != '/')
throw new Error('Must start with /');
var req = new XMLHttpRequest();
req.open(method, BASE_URL + path, true);
req.addEventListener('load', function() {
if (req.status == 200) {
if (opt_response_cb)
opt_response_cb(JSON.parse(req.responseText));
return;
}
if (opt_err_cb)
opt_err_cb();
else
console.log('reqAsync ' + path, req);
});
req.addEventListener('error', function() {
if (opt_err_cb)
opt_err_cb();
else
console.log('reqAsync ' + path, req);
});
if (data)
req.send(JSON.stringify(data));
else
req.send(null);
}
function Event(type, opt_bubbles, opt_preventable) {
var e = document.createEvent('Event');
e.initEvent(type, !!opt_bubbles, !!opt_preventable);
e.__proto__ = window.Event.prototype;
return e;
};
function dispatchSimpleEvent(target, type, opt_bubbles, opt_cancelable) {
var e = new Event(type, opt_bubbles, opt_cancelable);
return target.dispatchEvent(e);
}
// chromeapp derives from a div in order to get basic dispatchEvent
// capabilities. Lame but effective.
var chromeapp = document.createElement('div');
// Ask the server for startup args.
// TODO(nduca): crbug.com/168085 causes breakpoints to be ineffective
// during the early stages of page load. In debug mode, we delay the launch
// event for a bit so that breakpoints work.
if (!DEBUG_MODE) {
reqAsync('GET', '/launch_args', null, gotLaunchArgs);
} else {
reqAsync('GET', '/launch_args', null, function(args) {
setTimeout(function() {
gotLaunchArgs(args);
}, 1000);
});
}
function gotLaunchArgs(args) {
var e = new Event('launch', false, false);
e.args = args;
chromeapp.launch_args = args;
chromeapp.dispatchEvent(e);
}
var oldOnError = window.onerror;
function onUncaughtError(error, url, line_number) {
reqAsync('POST', '/uncaught_error', {
error: error,
url: url,
line_number: line_number});
if (oldOnError) oldOnError(error, url, line_number);
}
window.onerror = onUncaughtError;
function print() {
var messages = [];
for (var i = 0; i < arguments.length; i++) {
try {
// See if it even stringifies.
JSON.stringify(arguments[i]);
messages.push(arguments[i]);
} catch(ex) {
messages.push('Argument ' + i + ' not convertible to JSON');
}
}
reqAsync('POST', '/print', messages);
}
function sendEvent(event_name, args, opt_callback, opt_err_callback) {
if (args === undefined)
throw new Error('args is required');
reqAsync('POST', '/send_event', {
event_name: event_name,
args: args},
opt_callback,
opt_err_callback);
}
var exiting = false;
function exit(opt_exitCode) {
var exitCode = opt_exitCode;
if (opt_exitCode === undefined)
exitCode = 0;
if (typeof(exitCode) != 'number')
throw new Error('exit code must be a number or undefined');
if (exiting)
throw new Error('chromeapp.exit() was a already called');
exiting = true;
reqAsync('POST', '/exit', {exitCode: exitCode}, function() { });
// Busy wait for a bit to try to give the xhr a chance to hit
// python.
var start = Date.now();
while(Date.now() < start + 150);
}
window.chromeapp = chromeapp;
window.chromeapp.launch_args = undefined;
window.chromeapp.sendEvent = sendEvent;
window.chromeapp.print = print;
window.chromeapp.exit = exit;
})();
"""
assert self._daemon
self._cur_chromeapp_js = os.path.join(
self._app.manifest_dirname,
'chromeapp.js')
js = js_template
js = js.replace('__CHROMEAPP_REPLY_URL__',
'http://localhost:%i' % self._daemon.port)
js = js.replace('__CHROMEAPP_DEBUG_MODE__',
'%s' % json.dumps(self._app.debug_mode))
with open(self._cur_chromeapp_js, 'w') as f:
f.write(js)
def _CleanupLaunchJS(self):
if self._cur_chromeapp_js == None:
return
assert os.path.exists(self._cur_chromeapp_js)
os.unlink(self._cur_chromeapp_js)
self._cur_chromeapp_js = None
def _Launch(self, browser_args):
if not self._app.debug_mode:
self._devnull = open(os.devnull, 'w')
self._proc = subprocess.Popen(
browser_args, stdout=self._devnull, stderr=self._devnull)
else:
self._devnull = None
self._proc = subprocess.Popen(browser_args)
def _StartCheckingForBrowserAliveness(self):
self._daemon.AddDelayedTask(self._CheckForBrowserAliveness, 0.25)
def _CheckForBrowserAliveness(self):
if not self._proc:
return
def IsStopped():
return self._proc.poll() != None
if IsStopped():
if not self._exiting_run_loop:
sys.stderr.write("Browser closed without notifying us. Exiting...\n")
self.ExitRunLoop(1)
return
self._StartCheckingForBrowserAliveness()
def Run(self):
assert self._exit_code == None
assert self.is_started
self._StartCheckingForBrowserAliveness()
try:
self._daemon.Run()
finally:
self._exiting_run_loop = False
exit_code = self._exit_code
self._exit_code = None
return exit_code
def _OnUncaughtError(self, error):
m = re.match('chrome-extension:\/\/(.+)\/(.*)', error['url'], re.DOTALL)
assert m
sys.stderr.write("Uncaught error: %s:%i: %s\n" % (
m.group(2), error['line_number'],
error['error']))
def _OnPrint(self, content):
print "%s" % ' '.join([str(x) for x in content])
def AddListener(self, event_name, callback):
if event_name in self._event_listeners:
raise Exception('Event listener already registered')
self._event_listeners[event_name] = callback
def RemoveListener(self, event_name, callback):
if event_name not in self._event_listeners:
raise Exception("Not found")
del self._event_listeners[event_name]
def HasListener(self, event_name, callback):
return event_name in self._event_listeners
def _OnSendEvent(self, content):
event_name = content["event_name"]
args = content["args"]
listener = self._event_listeners.get(event_name, None)
if not listener:
sys.stderr.write('No listener for %s\n' % event_name)
raise _RequestException(500)
try:
return listener(args)
except:
import traceback
traceback.print_exc()
raise _RequestException(500)
def _HandleRequest(self, method, path, content):
parsed_result = urlparse.urlparse(path)
if path == '/launch_args':
return self._args
if path == '/uncaught_error':
self._OnUncaughtError(content)
return
if path == '/print':
self._OnPrint(content)
return
if path == '/send_event':
return self._OnSendEvent(content)
if path == '/exit':
self.ExitRunLoop(content['exitCode'])
return True
raise _RequestException(404)
def ExitRunLoop(self, exit_code):
"""Forces the app out of its run loop."""
assert self._daemon.is_running
if self._exiting_run_loop:
logging.warning("Multiple calls to exit. First return value will be chosen.")
return
self._exiting_run_loop = True
self._exit_code = exit_code
self._daemon.Stop()
def _CloseBrowserProcess(self):
assert self.is_started
assert not self._daemon.is_running
if self._proc:
def IsStopped():
if not self._proc:
return True
return self._proc.poll() != None
# Try to politely shutdown, first.
if not IsStopped():
try:
self._proc.terminate()
try:
_WaitFor(IsStopped, timeout=5)
self._proc = None
except _TimeoutException:
pass
except:
pass
# Kill it.
if not IsStopped():
self._proc.kill()
try:
_WaitFor(IsStopped, timeout=5)
self._proc = None
except _TimeoutException:
self._proc = None
raise Exception('Could not shutdown the browser.')
if self._devnull:
self._devnull.close()
self._devnull = None
self._CleanupLaunchJS()
class ManifestError(Exception):
pass
class App(object):
def __init__(self, stable_app_name, manifest_filename,
debug_mode=False,
chromeapp_profiles_dir=False):
self._stable_app_name = stable_app_name
self._profile_dir = None
self._manifest_filename = manifest_filename
self._debug_mode = debug_mode
with open(self._manifest_filename, 'r') as f:
manifest_text = f.read()
self._manifest = json.loads(manifest_text)
if 'permissions' not in self._manifest:
raise ManifestError('You need to have permissions: "http://localhost:*/" in your manifest.')
if 'http://localhost:*/' not in self._manifest['permissions']:
raise ManifestError('You need to have permissions: "http://localhost:*/" in your manifest.')
if not chromeapp_profiles_dir:
chromeapp_profiles_dir = os.path.expanduser('~/.chromeapp')
if not os.path.exists(chromeapp_profiles_dir):
os.mkdir(chromeapp_profiles_dir)
self._profile_dir = os.path.join(chromeapp_profiles_dir,
stable_app_name)
def Run(self, args=None):
"""Launches and runs instance of the application. Returns its exit code.
This is shorthand for creating an AppInstance against this app and running it:
with AppInstance(app, args) as instance:
ret_val = instance.Run()
"""
with AppInstance(self, args) as instance:
return instance.Run()
@property
def stable_app_name(self):
return self._stable_app_name
@property
def debug_mode(self):
return self._debug_mode
@property
def manifest_filename(self):
return self._manifest_filename
@property
def manifest_dirname(self):
return os.path.abspath(os.path.dirname(self._manifest_filename))
def _GetBrowserStartupArgs(self):
args = []
args.append('--user-data-dir=%s' % self._profile_dir)
args.append('--no-first-run')
args.append('--noerrdialogs')
args.append('--enable-experimental-extension-apis')
return args
def _ReadPreferences(self):
prefs_file = os.path.join(self._profile_dir,
'Default', 'Preferences')
try:
with open(prefs_file, 'r') as f:
contents = f.read()
except:
contents = """{
"extensions": {
"settings": {
}
}
}"""
return json.loads(contents)