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#ifndef CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_
6#define CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/memory/ref_counted.h"
11#include "chrome/browser/profiles/profile.h"
12#include "net/base/network_delegate.h"
13
14class ExtensionEventRouterForwarder;
15template<class T> class PrefMember;
16class ProtocolHandlerRegistry;
17
18typedef PrefMember<bool> BooleanPrefMember;
19
20// ChromeNetworkDelegate is the central point from within the chrome code to
21// add hooks into the network stack.
22class ChromeNetworkDelegate : public net::NetworkDelegate {
23 public:
24  // If |profile_id| is the invalid profile, events will be broadcasted to all
25  // profiles, otherwise, they will only be sent to the specified profile.
26  // |enable_referrers| should be initialized on the UI thread (see below)
27  // beforehand. This object's owner is responsible for cleaning it up
28  // at shutdown.
29  ChromeNetworkDelegate(
30      ExtensionEventRouterForwarder* event_router,
31      ProfileId profile_id,
32      BooleanPrefMember* enable_referrers,
33      ProtocolHandlerRegistry* protocol_handler_registry);
34  virtual ~ChromeNetworkDelegate();
35
36  // Binds |enable_referrers| to |pref_service| and moves it to the IO thread.
37  // This method should be called on the UI thread.
38  static void InitializeReferrersEnabled(BooleanPrefMember* enable_referrers,
39                                         PrefService* pref_service);
40 private:
41  // NetworkDelegate methods:
42  virtual int OnBeforeURLRequest(net::URLRequest* request,
43                                 net::CompletionCallback* callback,
44                                 GURL* new_url);
45  virtual int OnBeforeSendHeaders(uint64 request_id,
46                                  net::CompletionCallback* callback,
47                                  net::HttpRequestHeaders* headers);
48  virtual void OnResponseStarted(net::URLRequest* request);
49  virtual void OnReadCompleted(net::URLRequest* request, int bytes_read);
50  virtual void OnURLRequestDestroyed(net::URLRequest* request);
51  virtual net::URLRequestJob* OnMaybeCreateURLRequestJob(
52      net::URLRequest* request);
53
54  scoped_refptr<ExtensionEventRouterForwarder> event_router_;
55  const ProfileId profile_id_;
56
57  // Weak, owned by our owner.
58  BooleanPrefMember* enable_referrers_;
59  scoped_refptr<ProtocolHandlerRegistry> protocol_handler_registry_;
60  DISALLOW_COPY_AND_ASSIGN(ChromeNetworkDelegate);
61};
62
63#endif  // CHROME_BROWSER_NET_CHROME_NETWORK_DELEGATE_H_
64