Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / net / chrome_network_delegate.cc
index 89bb50a..37683b0 100644 (file)
@@ -16,7 +16,6 @@
 #include "base/prefs/pref_member.h"
 #include "base/prefs/pref_service.h"
 #include "base/strings/string_number_conversions.h"
-#include "base/strings/string_split.h"
 #include "base/time/time.h"
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/content_settings/cookie_settings.h"
 #include "chrome/browser/net/chrome_extensions_network_delegate.h"
 #include "chrome/browser/net/client_hints.h"
 #include "chrome/browser/net/connect_interceptor.h"
+#include "chrome/browser/net/safe_search_util.h"
 #include "chrome/browser/performance_monitor/performance_monitor.h"
 #include "chrome/browser/prerender/prerender_tracker.h"
 #include "chrome/browser/profiles/profile_manager.h"
 #include "chrome/browser/task_manager/task_manager.h"
 #include "chrome/common/pref_names.h"
-#include "chrome/common/url_constants.h"
+#include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler.h"
 #include "components/data_reduction_proxy/browser/data_reduction_proxy_metrics.h"
 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.h"
+#include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.h"
 #include "components/domain_reliability/monitor.h"
-#include "components/google/core/browser/google_util.h"
 #include "content/public/browser/browser_thread.h"
 #include "content/public/browser/render_frame_host.h"
 #include "content/public/browser/render_view_host.h"
 #include "content/public/browser/resource_request_info.h"
-#include "extensions/common/constants.h"
 #include "net/base/host_port_pair.h"
 #include "net/base/net_errors.h"
 #include "net/base/net_log.h"
 #include "net/cookies/cookie_options.h"
 #include "net/http/http_request_headers.h"
 #include "net/http/http_response_headers.h"
+#include "net/proxy/proxy_config.h"
+#include "net/proxy/proxy_info.h"
+#include "net/proxy/proxy_retry_info.h"
+#include "net/proxy/proxy_server.h"
 #include "net/socket_stream/socket_stream.h"
 #include "net/url_request/url_request.h"
 #include "net/url_request/url_request_context.h"
 #include "components/policy/core/browser/url_blacklist_manager.h"
 #endif
 
+#if defined(ENABLE_EXTENSIONS)
+#include "extensions/common/constants.h"
+#endif
+
 using content::BrowserThread;
 using content::RenderViewHost;
 using content::ResourceRequestInfo;
+using content::ResourceType;
 
 // By default we don't allow access to all file:// urls on ChromeOS and
 // Android.
@@ -80,69 +88,16 @@ bool ChromeNetworkDelegate::g_allow_file_access_ = false;
 bool ChromeNetworkDelegate::g_allow_file_access_ = true;
 #endif
 
+#if defined(ENABLE_EXTENSIONS)
 // This remains false unless the --disable-extensions-http-throttling
 // flag is passed to the browser.
 bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
+#endif
 
 namespace {
 
 const char kDNTHeader[] = "DNT";
 
-// Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
-// same key as the the |second_parameter| (e.g. foo=baz). Both parameters
-// must be in key=value form.
-bool HasSameParameterKey(const std::string& first_parameter,
-                         const std::string& second_parameter) {
-  DCHECK(second_parameter.find("=") != std::string::npos);
-  // Prefix for "foo=bar" is "foo=".
-  std::string parameter_prefix = second_parameter.substr(
-      0, second_parameter.find("=") + 1);
-  return StartsWithASCII(first_parameter, parameter_prefix, false);
-}
-
-// Examines the query string containing parameters and adds the necessary ones
-// so that SafeSearch is active. |query| is the string to examine and the
-// return value is the |query| string modified such that SafeSearch is active.
-std::string AddSafeSearchParameters(const std::string& query) {
-  std::vector<std::string> new_parameters;
-  std::string safe_parameter = chrome::kSafeSearchSafeParameter;
-  std::string ssui_parameter = chrome::kSafeSearchSsuiParameter;
-
-  std::vector<std::string> parameters;
-  base::SplitString(query, '&', &parameters);
-
-  std::vector<std::string>::iterator it;
-  for (it = parameters.begin(); it < parameters.end(); ++it) {
-    if (!HasSameParameterKey(*it, safe_parameter) &&
-        !HasSameParameterKey(*it, ssui_parameter)) {
-      new_parameters.push_back(*it);
-    }
-  }
-
-  new_parameters.push_back(safe_parameter);
-  new_parameters.push_back(ssui_parameter);
-  return JoinString(new_parameters, '&');
-}
-
-// If |request| is a request to Google Web Search the function
-// enforces that the SafeSearch query parameters are set to active.
-// Sets the query part of |new_url| with the new value of the parameters.
-void ForceGoogleSafeSearch(net::URLRequest* request,
-                           GURL* new_url) {
-  if (!google_util::IsGoogleSearchUrl(request->url()) &&
-      !google_util::IsGoogleHomePageUrl(request->url()))
-    return;
-
-  std::string query = request->url().query();
-  std::string new_query = AddSafeSearchParameters(query);
-  if (query == new_query)
-    return;
-
-  GURL::Replacements replacements;
-  replacements.SetQueryStr(new_query);
-  *new_url = request->url().ReplaceComponents(replacements);
-}
-
 // Gets called when the extensions finish work on the URL. If the extensions
 // did not do a redirect (so |new_url| is empty) then we enforce the
 // SafeSearch parameters. Otherwise we will get called again after the
@@ -153,7 +108,7 @@ void ForceGoogleSafeSearchCallbackWrapper(
     GURL* new_url,
     int rv) {
   if (rv == net::OK && new_url->is_empty())
-    ForceGoogleSafeSearch(request, new_url);
+    safe_search_util::ForceGoogleSafeSearch(request, new_url);
   callback.Run(rv);
 }
 
@@ -179,7 +134,7 @@ void UpdateContentLengthPrefs(
       profile->IsOffTheRecord()) {
     return;
   }
-#if defined(OS_ANDROID)
+#if defined(OS_ANDROID) && defined(SPDY_PROXY_AUTH_ORIGIN)
   // If Android ever goes multi profile, the profile should be passed so that
   // the browser preference will be taken.
   bool with_data_reduction_proxy_enabled =
@@ -288,6 +243,7 @@ ChromeNetworkDelegate::ChromeNetworkDelegate(
       enable_referrers_(enable_referrers),
       enable_do_not_track_(NULL),
       force_google_safe_search_(NULL),
+      data_reduction_proxy_enabled_(NULL),
 #if defined(ENABLE_CONFIGURATION_POLICY)
       url_blacklist_manager_(NULL),
 #endif
@@ -296,7 +252,9 @@ ChromeNetworkDelegate::ChromeNetworkDelegate(
       original_content_length_(0),
       first_request_(true),
       prerender_tracker_(NULL),
-      data_reduction_proxy_params_(NULL) {
+      data_reduction_proxy_params_(NULL),
+      data_reduction_proxy_usage_stats_(NULL),
+      data_reduction_proxy_auth_request_handler_(NULL) {
   DCHECK(enable_referrers);
   extensions_delegate_.reset(
       ChromeExtensionsNetworkDelegate::Create(event_router));
@@ -331,9 +289,11 @@ void ChromeNetworkDelegate::SetEnableClientHints() {
 }
 
 // static
+#if defined(ENABLE_EXTENSIONS)
 void ChromeNetworkDelegate::NeverThrottleRequests() {
   g_never_throttle_requests_ = true;
 }
+#endif
 
 // static
 void ChromeNetworkDelegate::InitializePrefsOnUIThread(
@@ -460,7 +420,7 @@ int ChromeNetworkDelegate::OnBeforeURLRequest(
       request, wrapped_callback, new_url);
 
   if (force_safe_search && rv == net::OK && new_url->is_empty())
-    ForceGoogleSafeSearch(request, new_url);
+    safe_search_util::ForceGoogleSafeSearch(request, new_url);
 
   if (connect_interceptor_)
     connect_interceptor_->WitnessURLRequest(request);
@@ -468,14 +428,52 @@ int ChromeNetworkDelegate::OnBeforeURLRequest(
   return rv;
 }
 
+void ChromeNetworkDelegate::OnResolveProxy(
+    const GURL& url,
+    int load_flags,
+    const net::ProxyService& proxy_service,
+    net::ProxyInfo* result) {
+  if (!on_resolve_proxy_handler_.is_null() &&
+      !proxy_config_getter_.is_null()) {
+    on_resolve_proxy_handler_.Run(url, load_flags,
+                                  proxy_config_getter_.Run(),
+                                  proxy_service.proxy_retry_info(),
+                                  data_reduction_proxy_params_, result);
+  }
+}
+
+void ChromeNetworkDelegate::OnProxyFallback(const net::ProxyServer& bad_proxy,
+                                            int net_error,
+                                            bool did_fallback) {
+  if (data_reduction_proxy_usage_stats_) {
+    data_reduction_proxy_usage_stats_->RecordBypassEventHistograms(
+        bad_proxy, net_error, did_fallback);
+  }
+}
+
 int ChromeNetworkDelegate::OnBeforeSendHeaders(
     net::URLRequest* request,
     const net::CompletionCallback& callback,
     net::HttpRequestHeaders* headers) {
+  bool force_safe_search = force_google_safe_search_ &&
+                           force_google_safe_search_->GetValue();
+  if (force_safe_search)
+    safe_search_util::ForceYouTubeSafetyMode(request, headers);
+
   TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "SendRequest");
   return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers);
 }
 
+void ChromeNetworkDelegate::OnBeforeSendProxyHeaders(
+    net::URLRequest* request,
+    const net::ProxyInfo& proxy_info,
+    net::HttpRequestHeaders* headers) {
+  if (data_reduction_proxy_auth_request_handler_) {
+    data_reduction_proxy_auth_request_handler_->MaybeAddRequestHeader(
+        request, proxy_info.proxy_server(), headers);
+  }
+}
+
 void ChromeNetworkDelegate::OnSendHeaders(
     net::URLRequest* request,
     const net::HttpRequestHeaders& headers) {
@@ -488,11 +486,15 @@ int ChromeNetworkDelegate::OnHeadersReceived(
     const net::HttpResponseHeaders* original_response_headers,
     scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
     GURL* allowed_unsafe_redirect_url) {
+  data_reduction_proxy::DataReductionProxyBypassType bypass_type;
   if (data_reduction_proxy::MaybeBypassProxyAndPrepareToRetry(
       data_reduction_proxy_params_,
       request,
       original_response_headers,
-      override_response_headers)) {
+      override_response_headers,
+      &bypass_type)) {
+    if (data_reduction_proxy_usage_stats_)
+      data_reduction_proxy_usage_stats_->SetBypassType(bypass_type);
     return net::OK;
   }
 
@@ -534,6 +536,9 @@ void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
 
 void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
                                         bool started) {
+  if (data_reduction_proxy_usage_stats_)
+    data_reduction_proxy_usage_stats_->OnUrlRequestCompleted(request, started);
+
   TRACE_EVENT_ASYNC_END0("net", "URLRequest", request);
   if (request->status().status() == net::URLRequestStatus::SUCCESS) {
     // For better accuracy, we use the actual bytes read instead of the length
@@ -579,6 +584,10 @@ void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request,
       RecordContentLengthHistograms(received_content_length,
                                     original_content_length,
                                     freshness_lifetime);
+      if (data_reduction_proxy_enabled_ && data_reduction_proxy_usage_stats_) {
+        data_reduction_proxy_usage_stats_->RecordBypassedBytesHistograms(
+            *request, *data_reduction_proxy_enabled_);
+      }
       DVLOG(2) << __FUNCTION__
           << " received content length: " << received_content_length
           << " original content length: " << original_content_length
@@ -637,8 +646,8 @@ bool ChromeNetworkDelegate::OnCanGetCookies(
   bool is_for_blocking_resource = false;
   const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(&request);
   if (info && ((!info->IsAsync()) ||
-               info->GetResourceType() == ResourceType::STYLESHEET ||
-               info->GetResourceType() == ResourceType::SCRIPT)) {
+               info->GetResourceType() == content::RESOURCE_TYPE_STYLESHEET ||
+               info->GetResourceType() == content::RESOURCE_TYPE_SCRIPT)) {
     is_for_blocking_resource = true;
   }
 
@@ -761,10 +770,14 @@ bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request,
 
 bool ChromeNetworkDelegate::OnCanThrottleRequest(
     const net::URLRequest& request) const {
+#if defined(ENABLE_EXTENSIONS)
   if (g_never_throttle_requests_)
     return false;
   return request.first_party_for_cookies().scheme() ==
       extensions::kExtensionScheme;
+#else
+  return false;
+#endif
 }
 
 bool ChromeNetworkDelegate::OnCanEnablePrivacyMode(