1# Copyright (c) 2011 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"""Python representation for Chromium Plugins info.
6
7This is the info available at about:plugins.
8Obtain one of these from PyUITestSuite::GetPluginsInfo() call.
9
10Example:
11class MyTest(pyauto.PyUITest):
12  def testBasic(self):
13    info = self.GetPluginsInfo()  # fetch plugins snapshot
14    print info.Plugins()
15
16See more examples in chrome/test/functional/plugins.py.
17"""
18
19import simplejson as json
20
21from pyauto_errors import JSONInterfaceError
22
23
24class PluginsInfo(object):
25  """Represent info for Chromium plugins.
26
27  The info is represented as a list of dictionaries, one for each plugin.
28  """
29  def __init__(self, plugins_dict):
30    """Initialize a PluginsInfo from a json string.
31
32    Args:
33      plugins_dict: a dictionary returned by the automation command
34          'GetPluginsInfo'.
35
36    Raises:
37      pyauto_errors.JSONInterfaceError if the automation call returns an error.
38    """
39    # JSON string prepared in GetPluginsInfo() in automation_provider.cc
40    self.pluginsdict = plugins_dict
41    if self.pluginsdict.has_key('error'):
42      raise JSONInterfaceError(self.pluginsdict['error'])
43
44  def Plugins(self):
45    """Get plugins.
46
47    Returns:
48      a list of plugins info
49      Sample:
50      [ { u'desc': u'Shockwave Flash 10.0 r45',
51          u'enabled': True,
52          u'mimeTypes': [ { u'description': u'Shockwave Flash',
53                            u'fileExtensions': [u'swf'],
54                            u'mimeType': u'application/x-shockwave-flash'},
55                          { u'description': u'FutureSplash Player',
56                            u'fileExtensions': [u'spl'],
57                            u'mimeType': u'application/futuresplash'}],
58          u'name': u'Shockwave Flash',
59          u'path': u'/Library/Internet Plug-Ins/Flash Player.plugin',
60          u'version': u'10.0.45.2'},
61        { u'desc': u'Version 1.1.2.9282',
62          u'enabled': True,
63          u'mimeTypes': [ { u'description': u'Google voice and video chat',
64                            u'fileExtensions': [u'googletalk'],
65                            u'mimeType': u'application/googletalk'}],
66          u'name': u'Google Talk NPAPI Plugin',
67          u'path': u'/Library/Internet Plug-Ins/googletalkbrowserplugin.plugin',
68          u'version': u'1.1.2.9282'},
69        ...,
70        ...,
71      ]
72    """
73    return self.pluginsdict.get('plugins', [])
74
75  def PluginForPath(self, path):
76    """Get plugin info for the given plugin path.
77
78    Returns:
79      a dictionary of info for the plugin.
80    """
81    got = filter(lambda x: x['path'] == path, self.Plugins())
82    if not got: return None
83    return got[0]
84
85  def PluginForName(self, name):
86    """Get plugin info for the given name.
87
88    There might be several plugins with the same name.
89
90    Args:
91      name: the name for which to look for.
92
93    Returns:
94      a list of info dictionaries for each plugin found with the given name.
95    """
96    return filter(lambda x: x['name'] == name, self.Plugins())
97
98  def FirstPluginForName(self, name):
99    """Get plugin info for the first plugin with the given name.
100
101    This is useful in case there are multiple plugins for a name.
102
103    Args:
104      name: the name for which to look for.
105
106    Returns:
107      a plugin info dictionary
108      None, if not found
109    """
110    all = self.PluginForName(name)
111    if not all: return None
112    return all[0]
113