| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/web_request/web_request_api.h" | 5 #include "chrome/browser/extensions/api/web_request/web_request_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 2211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2222 | 2222 |
| 2223 helpers::ClearCacheOnNavigation(); | 2223 helpers::ClearCacheOnNavigation(); |
| 2224 | 2224 |
| 2225 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | 2225 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
| 2226 &helpers::NotifyWebRequestAPIUsed, | 2226 &helpers::NotifyWebRequestAPIUsed, |
| 2227 profile_id(), make_scoped_refptr(GetExtension()))); | 2227 profile_id(), make_scoped_refptr(GetExtension()))); |
| 2228 | 2228 |
| 2229 return true; | 2229 return true; |
| 2230 } | 2230 } |
| 2231 | 2231 |
| 2232 void WebRequestEventHandled::CancelWithError( | 2232 void WebRequestEventHandled::RespondWithError( |
| 2233 const std::string& event_name, | 2233 const std::string& event_name, |
| 2234 const std::string& sub_event_name, | 2234 const std::string& sub_event_name, |
| 2235 uint64 request_id, | 2235 uint64 request_id, |
| 2236 scoped_ptr<ExtensionWebRequestEventRouter::EventResponse> response, | 2236 scoped_ptr<ExtensionWebRequestEventRouter::EventResponse> response, |
| 2237 const std::string& error) { | 2237 const std::string& error) { |
| 2238 error_ = error; | 2238 error_ = error; |
| 2239 response->cancel = true; | |
| 2240 ExtensionWebRequestEventRouter::GetInstance()->OnEventHandled( | 2239 ExtensionWebRequestEventRouter::GetInstance()->OnEventHandled( |
| 2241 profile_id(), | 2240 profile_id(), |
| 2242 extension_id(), | 2241 extension_id(), |
| 2243 event_name, | 2242 event_name, |
| 2244 sub_event_name, | 2243 sub_event_name, |
| 2245 request_id, | 2244 request_id, |
| 2246 response.release()); | 2245 response.release()); |
| 2247 } | 2246 } |
| 2248 | 2247 |
| 2249 bool WebRequestEventHandled::RunImpl() { | 2248 bool WebRequestEventHandled::RunImpl() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 2267 if (!value->empty()) { | 2266 if (!value->empty()) { |
| 2268 base::Time install_time = | 2267 base::Time install_time = |
| 2269 extension_info_map()->GetInstallTime(extension_id()); | 2268 extension_info_map()->GetInstallTime(extension_id()); |
| 2270 response.reset(new ExtensionWebRequestEventRouter::EventResponse( | 2269 response.reset(new ExtensionWebRequestEventRouter::EventResponse( |
| 2271 extension_id(), install_time)); | 2270 extension_id(), install_time)); |
| 2272 } | 2271 } |
| 2273 | 2272 |
| 2274 if (value->HasKey("cancel")) { | 2273 if (value->HasKey("cancel")) { |
| 2275 // Don't allow cancel mixed with other keys. | 2274 // Don't allow cancel mixed with other keys. |
| 2276 if (value->size() != 1) { | 2275 if (value->size() != 1) { |
| 2277 CancelWithError(event_name, | 2276 RespondWithError(event_name, |
| 2278 sub_event_name, | 2277 sub_event_name, |
| 2279 request_id, | 2278 request_id, |
| 2280 response.Pass(), | 2279 response.Pass(), |
| 2281 keys::kInvalidBlockingResponse); | 2280 keys::kInvalidBlockingResponse); |
| 2282 return false; | 2281 return false; |
| 2283 } | 2282 } |
| 2284 | 2283 |
| 2285 bool cancel = false; | 2284 bool cancel = false; |
| 2286 EXTENSION_FUNCTION_VALIDATE(value->GetBoolean("cancel", &cancel)); | 2285 EXTENSION_FUNCTION_VALIDATE(value->GetBoolean("cancel", &cancel)); |
| 2287 response->cancel = cancel; | 2286 response->cancel = cancel; |
| 2288 } | 2287 } |
| 2289 | 2288 |
| 2290 if (value->HasKey("redirectUrl")) { | 2289 if (value->HasKey("redirectUrl")) { |
| 2291 std::string new_url_str; | 2290 std::string new_url_str; |
| 2292 EXTENSION_FUNCTION_VALIDATE(value->GetString("redirectUrl", | 2291 EXTENSION_FUNCTION_VALIDATE(value->GetString("redirectUrl", |
| 2293 &new_url_str)); | 2292 &new_url_str)); |
| 2294 response->new_url = GURL(new_url_str); | 2293 response->new_url = GURL(new_url_str); |
| 2295 if (!response->new_url.is_valid()) { | 2294 if (!response->new_url.is_valid()) { |
| 2296 CancelWithError(event_name, | 2295 RespondWithError(event_name, |
| 2297 sub_event_name, | 2296 sub_event_name, |
| 2298 request_id, | 2297 request_id, |
| 2299 response.Pass(), | 2298 response.Pass(), |
| 2300 ErrorUtils::FormatErrorMessage( | 2299 ErrorUtils::FormatErrorMessage( |
| 2301 keys::kInvalidRedirectUrl, new_url_str)); | 2300 keys::kInvalidRedirectUrl, new_url_str)); |
| 2302 return false; | 2301 return false; |
| 2303 } | 2302 } |
| 2304 } | 2303 } |
| 2305 | 2304 |
| 2306 const bool hasRequestHeaders = value->HasKey("requestHeaders"); | 2305 const bool hasRequestHeaders = value->HasKey("requestHeaders"); |
| 2307 const bool hasResponseHeaders = value->HasKey("responseHeaders"); | 2306 const bool hasResponseHeaders = value->HasKey("responseHeaders"); |
| 2308 if (hasRequestHeaders || hasResponseHeaders) { | 2307 if (hasRequestHeaders || hasResponseHeaders) { |
| 2309 if (hasRequestHeaders && hasResponseHeaders) { | 2308 if (hasRequestHeaders && hasResponseHeaders) { |
| 2310 // Allow only one of the keys, not both. | 2309 // Allow only one of the keys, not both. |
| 2311 CancelWithError(event_name, | 2310 RespondWithError(event_name, |
| 2312 sub_event_name, | 2311 sub_event_name, |
| 2313 request_id, | 2312 request_id, |
| 2314 response.Pass(), | 2313 response.Pass(), |
| 2315 keys::kInvalidHeaderKeyCombination); | 2314 keys::kInvalidHeaderKeyCombination); |
| 2316 return false; | 2315 return false; |
| 2317 } | 2316 } |
| 2318 | 2317 |
| 2319 base::ListValue* headers_value = NULL; | 2318 base::ListValue* headers_value = NULL; |
| 2319 scoped_ptr<net::HttpRequestHeaders> request_headers; |
| 2320 scoped_ptr<helpers::ResponseHeaders> response_headers; |
| 2320 if (hasRequestHeaders) { | 2321 if (hasRequestHeaders) { |
| 2321 response->request_headers.reset(new net::HttpRequestHeaders()); | 2322 request_headers.reset(new net::HttpRequestHeaders()); |
| 2322 EXTENSION_FUNCTION_VALIDATE(value->GetList(keys::kRequestHeadersKey, | 2323 EXTENSION_FUNCTION_VALIDATE(value->GetList(keys::kRequestHeadersKey, |
| 2323 &headers_value)); | 2324 &headers_value)); |
| 2324 } else { | 2325 } else { |
| 2325 response->response_headers.reset(new helpers::ResponseHeaders()); | 2326 response_headers.reset(new helpers::ResponseHeaders()); |
| 2326 EXTENSION_FUNCTION_VALIDATE(value->GetList(keys::kResponseHeadersKey, | 2327 EXTENSION_FUNCTION_VALIDATE(value->GetList(keys::kResponseHeadersKey, |
| 2327 &headers_value)); | 2328 &headers_value)); |
| 2328 } | 2329 } |
| 2329 | 2330 |
| 2330 for (size_t i = 0; i < headers_value->GetSize(); ++i) { | 2331 for (size_t i = 0; i < headers_value->GetSize(); ++i) { |
| 2331 base::DictionaryValue* header_value = NULL; | 2332 base::DictionaryValue* header_value = NULL; |
| 2332 std::string name; | 2333 std::string name; |
| 2333 std::string value; | 2334 std::string value; |
| 2334 EXTENSION_FUNCTION_VALIDATE( | 2335 EXTENSION_FUNCTION_VALIDATE( |
| 2335 headers_value->GetDictionary(i, &header_value)); | 2336 headers_value->GetDictionary(i, &header_value)); |
| 2336 if (!FromHeaderDictionary(header_value, &name, &value)) { | 2337 if (!FromHeaderDictionary(header_value, &name, &value)) { |
| 2337 std::string serialized_header; | 2338 std::string serialized_header; |
| 2338 base::JSONWriter::Write(header_value, &serialized_header); | 2339 base::JSONWriter::Write(header_value, &serialized_header); |
| 2339 CancelWithError(event_name, | 2340 RespondWithError(event_name, |
| 2340 sub_event_name, | 2341 sub_event_name, |
| 2341 request_id, | 2342 request_id, |
| 2342 response.Pass(), | 2343 response.Pass(), |
| 2343 ErrorUtils::FormatErrorMessage(keys::kInvalidHeader, | 2344 ErrorUtils::FormatErrorMessage(keys::kInvalidHeader, |
| 2344 serialized_header)); | 2345 serialized_header)); |
| 2345 return false; | 2346 return false; |
| 2346 } | 2347 } |
| 2347 if (!helpers::IsValidHeaderName(name)) { | 2348 if (!helpers::IsValidHeaderName(name)) { |
| 2348 CancelWithError(event_name, | 2349 RespondWithError(event_name, |
| 2349 sub_event_name, | 2350 sub_event_name, |
| 2350 request_id, | 2351 request_id, |
| 2351 response.Pass(), | 2352 response.Pass(), |
| 2352 keys::kInvalidHeaderName); | 2353 keys::kInvalidHeaderName); |
| 2353 return false; | 2354 return false; |
| 2354 } | 2355 } |
| 2355 if (!helpers::IsValidHeaderValue(value)) { | 2356 if (!helpers::IsValidHeaderValue(value)) { |
| 2356 CancelWithError(event_name, | 2357 RespondWithError(event_name, |
| 2357 sub_event_name, | 2358 sub_event_name, |
| 2358 request_id, | 2359 request_id, |
| 2359 response.Pass(), | 2360 response.Pass(), |
| 2360 ErrorUtils::FormatErrorMessage( | 2361 ErrorUtils::FormatErrorMessage( |
| 2361 keys::kInvalidHeaderValue, name)); | 2362 keys::kInvalidHeaderValue, name)); |
| 2362 return false; | 2363 return false; |
| 2363 } | 2364 } |
| 2364 if (hasRequestHeaders) | 2365 if (hasRequestHeaders) |
| 2365 response->request_headers->SetHeader(name, value); | 2366 request_headers->SetHeader(name, value); |
| 2366 else | 2367 else |
| 2367 response->response_headers->push_back(helpers::ResponseHeader(name, | 2368 response_headers->push_back(helpers::ResponseHeader(name, value)); |
| 2368 value)); | |
| 2369 } | 2369 } |
| 2370 if (hasRequestHeaders) |
| 2371 response->request_headers.reset(request_headers.release()); |
| 2372 else |
| 2373 response->response_headers.reset(response_headers.release()); |
| 2370 } | 2374 } |
| 2371 | 2375 |
| 2372 if (value->HasKey(keys::kAuthCredentialsKey)) { | 2376 if (value->HasKey(keys::kAuthCredentialsKey)) { |
| 2373 base::DictionaryValue* credentials_value = NULL; | 2377 base::DictionaryValue* credentials_value = NULL; |
| 2374 EXTENSION_FUNCTION_VALIDATE(value->GetDictionary( | 2378 EXTENSION_FUNCTION_VALIDATE(value->GetDictionary( |
| 2375 keys::kAuthCredentialsKey, | 2379 keys::kAuthCredentialsKey, |
| 2376 &credentials_value)); | 2380 &credentials_value)); |
| 2377 base::string16 username; | 2381 base::string16 username; |
| 2378 base::string16 password; | 2382 base::string16 password; |
| 2379 EXTENSION_FUNCTION_VALIDATE( | 2383 EXTENSION_FUNCTION_VALIDATE( |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2446 } else if ((*it)->name().find("AdBlock") != std::string::npos) { | 2450 } else if ((*it)->name().find("AdBlock") != std::string::npos) { |
| 2447 adblock = true; | 2451 adblock = true; |
| 2448 } else { | 2452 } else { |
| 2449 other = true; | 2453 other = true; |
| 2450 } | 2454 } |
| 2451 } | 2455 } |
| 2452 } | 2456 } |
| 2453 | 2457 |
| 2454 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); | 2458 host->Send(new ExtensionMsg_UsingWebRequestAPI(adblock, adblock_plus, other)); |
| 2455 } | 2459 } |
| OLD | NEW |