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 // Definition of helper functions for the Chrome Extensions Proxy Settings API. 6 7 #ifndef CHROME_BROWSER_EXTENSIONS_API_PROXY_PROXY_API_HELPERS_H_ 8 #define CHROME_BROWSER_EXTENSIONS_API_PROXY_PROXY_API_HELPERS_H_ 9 10 #include <string> 11 12 #include "chrome/browser/prefs/proxy_prefs.h" 13 #include "net/proxy/proxy_config.h" 14 15 class ProxyConfigDictionary; 16 17 namespace base { 18 class DictionaryValue; 19 class ListValue; 20 } 21 22 namespace extensions { 23 namespace proxy_api_helpers { 24 25 // Conversion between PAC scripts and data-encoding URLs containing these 26 // PAC scripts. Data-encoding URLs consist of a data:// prefix, a mime-type and 27 // base64 encoded text. The functions return true in case of success. 28 // CreatePACScriptFromDataURL should only be called on data-encoding urls 29 // created with CreateDataURLFromPACScript. 30 bool CreateDataURLFromPACScript(const std::string& pac_script, 31 std::string* pac_script_url_base64_encoded); 32 bool CreatePACScriptFromDataURL( 33 const std::string& pac_script_url_base64_encoded, 34 std::string* pac_script); 35 36 // Helper functions for extension->browser pref transformation: 37 38 // The following functions extract one piece of data from the |proxy_config| 39 // each. |proxy_config| is a ProxyConfig dictionary as defined in the 40 // extension API. All output values conform to the format expected by a 41 // ProxyConfigDictionary. 42 // 43 // - If there are NO entries for the respective pieces of data, the functions 44 // return true. 45 // - If there ARE entries and they could be parsed, the functions set |out| 46 // and return true. 47 // - If there are entries that could not be parsed, the functions set |error| 48 // and return false. 49 // 50 // The parameter |bad_message| is passed to simulate the behavior of 51 // EXTENSION_FUNCTION_VALIDATE. It is never NULL. 52 bool GetProxyModeFromExtensionPref(const base::DictionaryValue* proxy_config, 53 ProxyPrefs::ProxyMode* out, 54 std::string* error, 55 bool* bad_message); 56 bool GetPacMandatoryFromExtensionPref(const base::DictionaryValue* proxy_config, 57 bool* out, 58 std::string* error, 59 bool* bad_message); 60 bool GetPacUrlFromExtensionPref(const base::DictionaryValue* proxy_config, 61 std::string* out, 62 std::string* error, 63 bool* bad_message); 64 bool GetPacDataFromExtensionPref(const base::DictionaryValue* proxy_config, 65 std::string* out, 66 std::string* error, 67 bool* bad_message); 68 bool GetProxyRulesStringFromExtensionPref( 69 const base::DictionaryValue* proxy_config, 70 std::string* out, 71 std::string* error, 72 bool* bad_message); 73 bool GetBypassListFromExtensionPref(const base::DictionaryValue* proxy_config, 74 std::string* out, 75 std::string* error, 76 bool* bad_message); 77 78 // Creates and returns a ProxyConfig dictionary (as defined in the extension 79 // API) from the given parameters. Ownership is passed to the caller. 80 // Depending on the value of |mode_enum|, several of the strings may be empty. 81 base::DictionaryValue* CreateProxyConfigDict( 82 ProxyPrefs::ProxyMode mode_enum, 83 bool pac_mandatory, 84 const std::string& pac_url, 85 const std::string& pac_data, 86 const std::string& proxy_rules_string, 87 const std::string& bypass_list, 88 std::string* error); 89 90 // Converts a ProxyServer dictionary instance (as defined in the extension API) 91 // |proxy_server| to a net::ProxyServer. 92 // |default_scheme| is the default scheme that is filled in, in case the 93 // caller did not pass one. 94 // Returns true if successful and sets |error| otherwise. 95 bool GetProxyServer(const base::DictionaryValue* proxy_server, 96 net::ProxyServer::Scheme default_scheme, 97 net::ProxyServer* out, 98 std::string* error, 99 bool* bad_message); 100 101 // Joins a list of URLs (stored as StringValues) in |list| with |joiner| 102 // to |out|. Returns true if successful and sets |error| otherwise. 103 bool JoinUrlList(const base::ListValue* list, 104 const std::string& joiner, 105 std::string* out, 106 std::string* error, 107 bool* bad_message); 108 109 110 // Helper functions for browser->extension pref transformation: 111 112 // Creates and returns a ProxyRules dictionary as defined in the extension API 113 // with the values of a ProxyConfigDictionary configured for fixed proxy 114 // servers. Returns NULL in case of failures. Ownership is passed to the caller. 115 base::DictionaryValue* CreateProxyRulesDict( 116 const ProxyConfigDictionary& proxy_config); 117 118 // Creates and returns a ProxyServer dictionary as defined in the extension API 119 // with values from a net::ProxyServer object. Never returns NULL. Ownership is 120 // passed to the caller. 121 base::DictionaryValue* CreateProxyServerDict(const net::ProxyServer& proxy); 122 123 // Creates and returns a PacScript dictionary as defined in the extension API 124 // with the values of a ProxyconfigDictionary configured for pac scripts. 125 // Returns NULL in case of failures. Ownership is passed to the caller. 126 base::DictionaryValue* CreatePacScriptDict( 127 const ProxyConfigDictionary& proxy_config); 128 129 // Tokenizes the |in| at delimiters |delims| and returns a new ListValue with 130 // StringValues created from the tokens. Ownership is passed to the caller. 131 base::ListValue* TokenizeToStringList(const std::string& in, 132 const std::string& delims); 133 134 } // namespace proxy_api_helpers 135 } // namespace extensions 136 137 #endif // CHROME_BROWSER_EXTENSIONS_API_PROXY_PROXY_API_HELPERS_H_ 138