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