OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """PyAuto: Python Interface to Chromium's Automation Proxy. | 6 """PyAuto: Python Interface to Chromium's Automation Proxy. |
7 | 7 |
8 PyAuto uses swig to expose Automation Proxy interfaces to Python. | 8 PyAuto uses swig to expose Automation Proxy interfaces to Python. |
9 For complete documentation on the functionality available, | 9 For complete documentation on the functionality available, |
10 run pydoc on this file. | 10 run pydoc on this file. |
(...skipping 4726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4737 # this method. | 4737 # this method. |
4738 return False | 4738 return False |
4739 | 4739 |
4740 # The hidden AP's will always be on, thus we will assume it is ready to | 4740 # The hidden AP's will always be on, thus we will assume it is ready to |
4741 # connect to. | 4741 # connect to. |
4742 if is_hidden: | 4742 if is_hidden: |
4743 return bool(_GotWifiNetwork()) | 4743 return bool(_GotWifiNetwork()) |
4744 | 4744 |
4745 return self.WaitUntil(_GotWifiNetwork, timeout=timeout, retry_sleep=1) | 4745 return self.WaitUntil(_GotWifiNetwork, timeout=timeout, retry_sleep=1) |
4746 | 4746 |
4747 def GetProxyTypeName(self, proxy_type): | |
4748 values = { self.PROXY_TYPE_DIRECT: 'Direct Internet connection', | |
4749 self.PROXY_TYPE_MANUAL: 'Manual proxy configuration', | |
4750 self.PROXY_TYPE_PAC: 'Automatic proxy configuration' } | |
4751 return values[proxy_type] | |
4752 | |
4753 def GetProxySettingsOnChromeOS(self): | |
4754 """Get current proxy settings on Chrome OS. | |
4755 | |
4756 Returns: | |
4757 A dictionary. See SetProxySetting() below | |
4758 for the full list of possible dictionary keys. | |
4759 | |
4760 Samples: | |
4761 { u'ignorelist': [], | |
4762 u'single': False, | |
4763 u'type': 1} | |
4764 | |
4765 { u'ignorelist': [u'www.example.com', u'www.example2.com'], | |
4766 u'single': True, | |
4767 u'singlehttp': u'24.27.78.152', | |
4768 u'singlehttpport': 1728, | |
4769 u'type': 2} | |
4770 | |
4771 { u'ignorelist': [], | |
4772 u'pacurl': u'http://example.com/config.pac', | |
4773 u'single': False, | |
4774 u'type': 3} | |
4775 | |
4776 Raises: | |
4777 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
4778 """ | |
4779 cmd_dict = { 'command': 'GetProxySettings' } | |
4780 return self._GetResultFromJSONRequest(cmd_dict, windex=None) | |
4781 | |
4782 def _FindNamedNetwork(self, network_dict, name): | |
4783 """Finds a network by name. | |
4784 | |
4785 Args: | |
4786 network_dict: network settings as returned by GetNetworkInfo. | |
4787 name: name of network we want to set proxy settings on. | |
4788 | |
4789 Returns: | |
4790 A dictionary with service_path and network_type of the | |
4791 named network, when given a dictionary with all system | |
4792 network information as returned by GetNetworkInfo. | |
4793 | |
4794 See GetNetworkInfo for a description of the input dictionary. | |
4795 | |
4796 Samples: | |
4797 { u'network_type': 'wifi_networks', | |
4798 u'service_path': '/service/700'} | |
4799 """ | |
4800 for (key, value) in network_dict.iteritems(): | |
4801 if isinstance(value, dict): | |
4802 if 'name' in value: | |
4803 if value['name'] == name: | |
4804 network_info = {'service_path': key} | |
4805 return network_info | |
4806 else: | |
4807 # if key is a dict but it doesnt have a 'name' entry, go deeper | |
4808 network_info = self._FindNamedNetwork(value, name) | |
4809 # if only service path set, set type from networking dictionary | |
4810 if network_info != None and 'network_type' not in network_info: | |
4811 network_info['network_type'] = value['network_type'] | |
4812 return network_info | |
4813 return None | |
4814 | |
4815 def _GetNamedNetworkInfo(self, network_name): | |
4816 """Gets settings needed to enable shared proxies for the named network. | |
4817 | |
4818 Args: | |
4819 network_name: name of network we want to set proxy settings on. | |
4820 | |
4821 Returns: | |
4822 A dictionary with network_type and service_path. | |
4823 Samples: | |
4824 { u'network_type': '1', | |
4825 u'service_path': '/service/0'} | |
4826 | |
4827 Raises: | |
4828 AutomationCommandFail if network name isn't found. | |
4829 """ | |
4830 net = self.GetNetworkInfo() | |
4831 if network_name == 'NAME_UNKNOWN': | |
4832 if net.get('ethernet_available'): | |
4833 service_path = net.get('connected_ethernet') | |
4834 network_type = str(pyautolib.TYPE_ETHERNET) | |
4835 elif net.get('wifi_available'): | |
4836 service_path = net.get('connected_wifi') | |
4837 network_type = str(pyautolib.TYPE_WIFI) | |
4838 elif net.get('cellular_available'): | |
4839 service_path = net.get('connected_cellular') | |
4840 network_type = str(pyautolib.TYPE_CELLULAR) | |
4841 else: | |
4842 raise AutomationCommandFail('No network available.') | |
4843 else: | |
4844 named_network_info = self._FindNamedNetwork(net, network_name) | |
4845 if named_network_info == None: | |
4846 raise AutomationCommandFail('%s not found.' % network_name) | |
4847 service_path = named_network_info['service_path'] | |
4848 network_type = named_network_info['network_type'] | |
4849 | |
4850 if not network_type: | |
4851 raise AutomationCommandFail('network type not found.') | |
4852 if not service_path: | |
4853 raise AutomationCommandFail('service path not found.') | |
4854 network_info = {'network type': network_type, 'service path': service_path} | |
4855 return network_info | |
4856 | |
4857 def SetProxySettingOnChromeOS(self, proxy_dict): | |
4858 """Public wrapper around _SetProxySettingOnChromeOSCore, performs | |
4859 state setup and error checking. | |
4860 | |
4861 Args: | |
4862 proxy_dict: dictionary of proxy settings, valid entries of which are | |
4863 what one would supply _SetProxySettingOnChromeOSCore | |
4864 | |
4865 Raises: | |
4866 AutomationCommandFail if a necessary dictionary entries aren't found. | |
4867 """ | |
4868 url_path = proxy_dict.get('url_path') | |
4869 proxy_url = proxy_dict.get('proxy_url') | |
4870 port_path = proxy_dict.get('port_path') | |
4871 proxy_port = proxy_dict.get('proxy_port') | |
4872 | |
4873 if proxy_url is not None: | |
4874 if url_path is None: | |
4875 raise AutomationCommandFail('url_path needed to set proxy_url.') | |
4876 return | |
4877 self.SetSharedProxies(True) | |
4878 self.RefreshInternetDetails() | |
4879 self._SetProxySettingOnChromeOSCore('type', self.PROXY_TYPE_MANUAL) | |
4880 self._SetProxySettingOnChromeOSCore(url_path, proxy_url) | |
4881 | |
4882 if proxy_port is not None: | |
4883 if port_path is None: | |
4884 raise AutomationCommandFail('port_path needed to set proxy_port.') | |
4885 return | |
4886 self._SetProxySettingOnChromeOSCore(port_path, proxy_port) | |
4887 | |
4888 def ResetProxySettingsOnChromeOS(self): | 4747 def ResetProxySettingsOnChromeOS(self): |
4889 """Public wrapper around proxysettings teardown functions.""" | 4748 """Public wrapper around proxysettings teardown functions.""" |
4890 self.SetSharedProxies(False) | 4749 self.SetSharedProxies(False) |
4891 self.RefreshInternetDetails() | 4750 proxy_dict = { |
4892 self._SetProxySettingOnChromeOSCore('type', self.PROXY_TYPE_DIRECT) | 4751 'mode': 'direct' |
| 4752 } |
| 4753 self.SetProxySettingOnChromeOS(proxy_dict) |
4893 | 4754 |
4894 def _SetProxySettingOnChromeOSCore(self, key, value): | 4755 def SetProxySettingOnChromeOS(self, proxy_config): |
4895 """Set a proxy setting. | 4756 """Set the proxy config of the current network. |
4896 | 4757 |
4897 Owner must be logged in for these to persist. | 4758 Owner must be logged in for these to persist. |
4898 If user is not logged in or is logged in as non-owner or guest, | 4759 If user is not logged in or is logged in as non-owner or guest, |
4899 proxy settings do not persist across browser restarts or login/logout. | 4760 proxy settings do not persist across browser restarts or login/logout. |
4900 | 4761 |
4901 Args: | 4762 Args: |
4902 key: string describing type of proxy preference. | 4763 proxy_config: A dictionary following the format described in |
4903 value: value of proxy preference. | 4764 prefs/proxy_config_dictionary.h. |
4904 | |
4905 Valid settings are: | |
4906 'type': int - Type of proxy. Should be one of: | |
4907 PROXY_TYPE_DIRECT, PROXY_TYPE_MANUAL, PROXY_TYPE_PAC. | |
4908 'ignorelist': list - The list of hosts and domains to ignore. | |
4909 | |
4910 These settings set 'type' to PROXY_TYPE_MANUAL: | |
4911 'single': boolean - Whether to use the same proxy for all protocols. | |
4912 | |
4913 These settings set 'single' to True: | |
4914 'singlehttp': string - If single is true, the proxy address to use. | |
4915 'singlehttpport': int - If single is true, the proxy port to use. | |
4916 | |
4917 These settings set 'single' to False: | |
4918 'httpurl': string - HTTP proxy address. | |
4919 'httpport': int - HTTP proxy port. | |
4920 'httpsurl': string - Secure HTTP proxy address. | |
4921 'httpsport': int - Secure HTTP proxy port. | |
4922 'ftpurl': string - FTP proxy address. | |
4923 'ftpport': int - FTP proxy port. | |
4924 'socks': string - SOCKS host address. | |
4925 'socksport': int - SOCKS host port. | |
4926 | |
4927 This setting sets 'type' to PROXY_TYPE_PAC: | |
4928 'pacurl': string - Autoconfiguration URL. | |
4929 | |
4930 Examples: | |
4931 # Sets direct internet connection, no proxy. | |
4932 self.SetProxySettingOnChromeOS('type', self.PROXY_TYPE_DIRECT) | |
4933 | |
4934 # Sets manual proxy configuration, same proxy for all protocols. | |
4935 self.SetProxySettingOnChromeOS('singlehttp', '24.27.78.152') | |
4936 self.SetProxySettingOnChromeOS('singlehttpport', 1728) | |
4937 self.SetProxySettingOnChromeOS('ignorelist', | |
4938 ['www.example.com', 'example2.com']) | |
4939 | |
4940 # Sets automatic proxy configuration with the specified PAC url. | |
4941 self.SetProxySettingOnChromeOS('pacurl', 'http://example.com/config.pac') | |
4942 | |
4943 # Sets httpproxy with specified url | |
4944 self.SetProxySettingOnChromeOS('httpurl', 10.10.10) | |
4945 | 4765 |
4946 Raises: | 4766 Raises: |
4947 pyauto_errors.JSONInterfaceError if the automation call returns an error. | 4767 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
4948 """ | 4768 """ |
4949 cmd_dict = { | 4769 cmd_dict = { |
4950 'command': 'SetProxySettings', | 4770 'command': 'SetProxySettings', |
4951 'key': key, | 4771 'proxy_config': json.dumps(proxy_config) |
4952 'value': value, | |
4953 } | 4772 } |
4954 return self._GetResultFromJSONRequest(cmd_dict, windex=None) | 4773 return self._GetResultFromJSONRequest(cmd_dict, windex=None) |
4955 | 4774 |
4956 def SetSharedProxies(self, value): | 4775 def SetSharedProxies(self, value): |
4957 """Allows shared proxies on the named network. | 4776 """Allows proxies on the shared networks. |
4958 | 4777 |
4959 Args: | 4778 Args: |
4960 value: True/False to set and clear respectively. | 4779 value: True/False to set and clear respectively. |
4961 | 4780 |
4962 Raises: | 4781 Raises: |
4963 pyauto_errors.JSONInterfaceError if the automation call returns an error. | 4782 pyauto_errors.JSONInterfaceError if the automation call returns an error. |
4964 """ | 4783 """ |
4965 cmd_dict = { | 4784 cmd_dict = { |
4966 'command': 'SetSharedProxies', | 4785 'command': 'SetSharedProxies', |
4967 'value': value, | 4786 'value': value, |
4968 } | 4787 } |
4969 return self._GetResultFromJSONRequest(cmd_dict, windex=None) | 4788 return self._GetResultFromJSONRequest(cmd_dict, windex=None) |
4970 | 4789 |
4971 def RefreshInternetDetails(self, network_name='NAME_UNKNOWN'): | |
4972 """Updates network information | |
4973 | |
4974 Args: | |
4975 network_name: name of the network we want to refresh settings for. | |
4976 | |
4977 Raises: | |
4978 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
4979 """ | |
4980 network_info = self._GetNamedNetworkInfo(network_name) | |
4981 cmd_dict = { | |
4982 'command': 'RefreshInternetDetails', | |
4983 'service path': network_info.get('service path'), | |
4984 } | |
4985 return self._GetResultFromJSONRequest(cmd_dict, None) | |
4986 | |
4987 def ForgetAllRememberedNetworks(self): | 4790 def ForgetAllRememberedNetworks(self): |
4988 """Forgets all networks that the device has marked as remembered.""" | 4791 """Forgets all networks that the device has marked as remembered.""" |
4989 for service in self.GetNetworkInfo()['remembered_wifi']: | 4792 for service in self.GetNetworkInfo()['remembered_wifi']: |
4990 self.ForgetWifiNetwork(service) | 4793 self.ForgetWifiNetwork(service) |
4991 | 4794 |
4992 def ForgetWifiNetwork(self, service_path): | 4795 def ForgetWifiNetwork(self, service_path): |
4993 """Forget a remembered network by its service path. | 4796 """Forget a remembered network by its service path. |
4994 | 4797 |
4995 This function is equivalent to clicking the 'Forget Network' button in the | 4798 This function is equivalent to clicking the 'Forget Network' button in the |
4996 chrome://settings/internet page. This function does not indicate whether | 4799 chrome://settings/internet page. This function does not indicate whether |
(...skipping 1213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6210 successful = result.wasSuccessful() | 6013 successful = result.wasSuccessful() |
6211 if not successful: | 6014 if not successful: |
6212 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) | 6015 pyauto_tests_file = os.path.join(self.TestsDir(), self._tests_filename) |
6213 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ | 6016 print >>sys.stderr, 'Tests can be disabled by editing %s. ' \ |
6214 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) | 6017 'Ref: %s' % (pyauto_tests_file, _PYAUTO_DOC_URL) |
6215 sys.exit(not successful) | 6018 sys.exit(not successful) |
6216 | 6019 |
6217 | 6020 |
6218 if __name__ == '__main__': | 6021 if __name__ == '__main__': |
6219 Main() | 6022 Main() |
OLD | NEW |