OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import json |
| 6 import logging |
| 7 import urlparse |
| 8 |
| 9 SOURCE_WHITELIST = [ |
| 10 'https://commondatastorage.googleapis.com/nativeclient-mirror/nacl/nacl_sdk', |
| 11 ] |
| 12 |
| 13 def IsSourceValid(url): |
| 14 scheme, host, path, _, _, _ = urlparse.urlparse(url) |
| 15 for allowed_url in SOURCE_WHITELIST: |
| 16 allowed_scheme, allowed_host, allowed_path_prefix, _, _, _ = \ |
| 17 urlparse.urlparse(allowed_url) |
| 18 if (scheme == allowed_scheme and host == allowed_host and |
| 19 path.startswith(allowed_path_prefix)): |
| 20 return True |
| 21 return False |
| 22 |
| 23 |
| 24 class Config(dict): |
| 25 def __init__(self, data=None): |
| 26 dict.__init__(self) |
| 27 if data: |
| 28 self.update(data) |
| 29 else: |
| 30 self.sources = [] |
| 31 |
| 32 def ToJson(self): |
| 33 return json.dumps(self, sort_keys=False, indent=2) |
| 34 |
| 35 def __getattr__(self, name): |
| 36 return self.__getitem__(name) |
| 37 |
| 38 def __setattr__(self, name, value): |
| 39 return self.__setitem__(name, value) |
| 40 |
| 41 def AddSource(self, source): |
| 42 if not IsSourceValid(source): |
| 43 logging.warn('Only whitelisted sources are allowed. Ignoring \"%s\".' % ( |
| 44 source,)) |
| 45 return |
| 46 |
| 47 if source in self.sources: |
| 48 logging.info('Source \"%s\" already in Config.' % (source,)) |
| 49 return |
| 50 self.sources.append(source) |
| 51 |
| 52 def RemoveSource(self, source): |
| 53 if source not in self.sources: |
| 54 logging.warn('Source \"%s\" not in Config.' % (source,)) |
| 55 return |
| 56 self.sources.remove(source) |
| 57 |
| 58 def RemoveAllSources(self): |
| 59 if not self.sources: |
| 60 logging.info('No sources to remove.') |
| 61 return |
| 62 self.sources = [] |
OLD | NEW |