Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / build / android / pylib / content_settings.py
1 # Copyright 2014 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
6 class ContentSettings(dict):
7
8   """A dict interface to interact with device content settings.
9
10   System properties are key/value pairs as exposed by adb shell content.
11   """
12
13   def __init__(self, table, adb):
14     super(ContentSettings, self).__init__()
15     try:
16       sdk_version = int(adb.system_properties['ro.build.version.sdk'])
17       assert sdk_version >= 16, (
18           'ContentSettings supported only on SDK 16 and later')
19     except ValueError:
20       assert False, ('Unknown SDK version %s' %
21           adb.system_properties['ro.build.version.sdk'])
22     self._table = table
23     self._adb = adb
24
25   @staticmethod
26   def _GetTypeBinding(value):
27     if isinstance(value, bool):
28       return 'b'
29     if isinstance(value, float):
30       return 'f'
31     if isinstance(value, int):
32       return 'i'
33     if isinstance(value, long):
34       return 'l'
35     if isinstance(value, str):
36       return 's'
37     raise ValueError('Unsupported type %s' % type(value))
38
39   def iteritems(self):
40     # Example row:
41     # 'Row: 0 _id=13, name=logging_id2, value=-1fccbaa546705b05'
42     for row in self._adb.RunShellCommandWithSU(
43         'content query --uri content://%s' % self._table):
44       fields = row.split(', ')
45       key = None
46       value = None
47       for field in fields:
48         k, _, v = field.partition('=')
49         if k == 'name':
50           key = v
51         elif k == 'value':
52           value = v
53       assert key, value
54       yield key, value
55
56   def __getitem__(self, key):
57     return self._adb.RunShellCommandWithSU(
58         'content query --uri content://%s --where "name=\'%s\'" '
59         '--projection value' % (self._table, key)).strip()
60
61   def __setitem__(self, key, value):
62     if key in self:
63       self._adb.RunShellCommandWithSU(
64           'content update --uri content://%s '
65           '--bind value:%s:%s --where "name=\'%s\'"' % (
66               self._table,
67               self._GetTypeBinding(value), value, key))
68     else:
69       self._adb.RunShellCommandWithSU(
70           'content insert --uri content://%s '
71           '--bind name:%s:%s --bind value:%s:%s' % (
72               self._table,
73               self._GetTypeBinding(key), key,
74               self._GetTypeBinding(value), value))
75
76   def __delitem__(self, key):
77     self._adb.RunShellCommandWithSU(
78         'content delete --uri content://%s '
79         '--bind name:%s:%s' % (
80             self._table,
81             self._GetTypeBinding(key), key))