Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Side by Side Diff: chrome/browser/extensions/extension_proxy_api.cc

Issue 9690006: Move Proxy Settings API from chrome/browser/extensions to chrome/browser/eextensions/api/proxy (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 // Implementation of the Chrome Extensions Proxy Settings API.
6
7 #include "chrome/browser/extensions/extension_proxy_api.h"
8
9 #include "base/json/json_writer.h"
10 #include "base/stringprintf.h"
11 #include "base/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/extensions/extension_event_router_forwarder.h"
14 #include "chrome/browser/extensions/extension_proxy_api_constants.h"
15 #include "chrome/browser/extensions/extension_proxy_api_helpers.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/prefs/proxy_config_dictionary.h"
18 #include "net/base/net_errors.h"
19
20 namespace helpers = extension_proxy_api_helpers;
21 namespace keys = extension_proxy_api_constants;
22
23 // static
24 ExtensionProxyEventRouter* ExtensionProxyEventRouter::GetInstance() {
25 return Singleton<ExtensionProxyEventRouter>::get();
26 }
27
28 ExtensionProxyEventRouter::ExtensionProxyEventRouter() {
29 }
30
31 ExtensionProxyEventRouter::~ExtensionProxyEventRouter() {
32 }
33
34 void ExtensionProxyEventRouter::OnProxyError(
35 ExtensionEventRouterForwarder* event_router,
36 void* profile,
37 int error_code) {
38 ListValue args;
39 DictionaryValue* dict = new DictionaryValue();
40 dict->SetBoolean(keys::kProxyEventFatal, true);
41 dict->SetString(keys::kProxyEventError, net::ErrorToString(error_code));
42 dict->SetString(keys::kProxyEventDetails, "");
43 args.Append(dict);
44
45 std::string json_args;
46 base::JSONWriter::Write(&args, false, &json_args);
47
48 if (profile) {
49 event_router->DispatchEventToRenderers(
50 keys::kProxyEventOnProxyError, json_args, profile, true, GURL());
51 } else {
52 event_router->BroadcastEventToRenderers(
53 keys::kProxyEventOnProxyError, json_args, GURL());
54 }
55 }
56
57 void ExtensionProxyEventRouter::OnPACScriptError(
58 ExtensionEventRouterForwarder* event_router,
59 void* profile,
60 int line_number,
61 const string16& error) {
62 ListValue args;
63 DictionaryValue* dict = new DictionaryValue();
64 dict->SetBoolean(keys::kProxyEventFatal, false);
65 dict->SetString(keys::kProxyEventError,
66 net::ErrorToString(net::ERR_PAC_SCRIPT_FAILED));
67 std::string error_msg;
68 if (line_number != -1) {
69 base::SStringPrintf(
70 &error_msg, "line: %d: %s", line_number, UTF16ToUTF8(error).c_str());
71 } else {
72 error_msg = UTF16ToUTF8(error);
73 }
74 dict->SetString(keys::kProxyEventDetails, error_msg);
75 args.Append(dict);
76
77 std::string json_args;
78 base::JSONWriter::Write(&args, false, &json_args);
79
80 if (profile) {
81 event_router->DispatchEventToRenderers(
82 keys::kProxyEventOnProxyError, json_args, profile, true, GURL());
83 } else {
84 event_router->BroadcastEventToRenderers(
85 keys::kProxyEventOnProxyError, json_args, GURL());
86 }
87 }
88
89 ProxyPrefTransformer::ProxyPrefTransformer() {
90 }
91
92 ProxyPrefTransformer::~ProxyPrefTransformer() {
93 }
94
95 Value* ProxyPrefTransformer::ExtensionToBrowserPref(const Value* extension_pref,
96 std::string* error,
97 bool* bad_message) {
98 // When ExtensionToBrowserPref is called, the format of |extension_pref|
99 // has been verified already by the extension API to match the schema
100 // defined in the extension API JSON.
101 CHECK(extension_pref->IsType(Value::TYPE_DICTIONARY));
102 const DictionaryValue* config =
103 static_cast<const DictionaryValue*>(extension_pref);
104
105 // Extract the various pieces of information passed to
106 // chrome.proxy.settings.set(). Several of these strings will
107 // remain blank no respective values have been passed to set().
108 // If a values has been passed to set but could not be parsed, we bail
109 // out and return NULL.
110 ProxyPrefs::ProxyMode mode_enum;
111 bool pac_mandatory;
112 std::string pac_url;
113 std::string pac_data;
114 std::string proxy_rules_string;
115 std::string bypass_list;
116 if (!helpers::GetProxyModeFromExtensionPref(
117 config, &mode_enum, error, bad_message) ||
118 !helpers::GetPacMandatoryFromExtensionPref(
119 config, &pac_mandatory, error, bad_message) ||
120 !helpers::GetPacUrlFromExtensionPref(
121 config, &pac_url, error, bad_message) ||
122 !helpers::GetPacDataFromExtensionPref(
123 config, &pac_data, error, bad_message) ||
124 !helpers::GetProxyRulesStringFromExtensionPref(
125 config, &proxy_rules_string, error, bad_message) ||
126 !helpers::GetBypassListFromExtensionPref(
127 config, &bypass_list, error, bad_message)) {
128 return NULL;
129 }
130
131 return helpers::CreateProxyConfigDict(
132 mode_enum, pac_mandatory, pac_url, pac_data, proxy_rules_string,
133 bypass_list, error);
134 }
135
136 Value* ProxyPrefTransformer::BrowserToExtensionPref(const Value* browser_pref) {
137 CHECK(browser_pref->IsType(Value::TYPE_DICTIONARY));
138
139 // This is a dictionary wrapper that exposes the proxy configuration stored in
140 // the browser preferences.
141 ProxyConfigDictionary config(
142 static_cast<const DictionaryValue*>(browser_pref));
143
144 ProxyPrefs::ProxyMode mode;
145 if (!config.GetMode(&mode)) {
146 LOG(ERROR) << "Cannot determine proxy mode.";
147 return NULL;
148 }
149
150 // Build a new ProxyConfig instance as defined in the extension API.
151 scoped_ptr<DictionaryValue> extension_pref(new DictionaryValue);
152
153 extension_pref->SetString(keys::kProxyConfigMode,
154 ProxyPrefs::ProxyModeToString(mode));
155
156 switch (mode) {
157 case ProxyPrefs::MODE_DIRECT:
158 case ProxyPrefs::MODE_AUTO_DETECT:
159 case ProxyPrefs::MODE_SYSTEM:
160 // These modes have no further parameters.
161 break;
162 case ProxyPrefs::MODE_PAC_SCRIPT: {
163 // A PAC URL either point to a PAC script or contain a base64 encoded
164 // PAC script. In either case we build a PacScript dictionary as defined
165 // in the extension API.
166 DictionaryValue* pac_dict = helpers::CreatePacScriptDict(config);
167 if (!pac_dict)
168 return NULL;
169 extension_pref->Set(keys::kProxyConfigPacScript, pac_dict);
170 break;
171 }
172 case ProxyPrefs::MODE_FIXED_SERVERS: {
173 // Build ProxyRules dictionary according to the extension API.
174 DictionaryValue* proxy_rules_dict = helpers::CreateProxyRulesDict(config);
175 if (!proxy_rules_dict)
176 return NULL;
177 extension_pref->Set(keys::kProxyConfigRules, proxy_rules_dict);
178 break;
179 }
180 case ProxyPrefs::kModeCount:
181 NOTREACHED();
182 }
183 return extension_pref.release();
184 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_proxy_api.h ('k') | chrome/browser/extensions/extension_proxy_api_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698