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#include "net/proxy/network_delegate_error_observer.h"
6
7#include "base/bind.h"
8#include "base/location.h"
9#include "base/message_loop/message_loop_proxy.h"
10#include "net/base/net_errors.h"
11#include "net/base/network_delegate.h"
12
13namespace net {
14
15// NetworkDelegateErrorObserver::Core -----------------------------------------
16
17class NetworkDelegateErrorObserver::Core
18    : public base::RefCountedThreadSafe<NetworkDelegateErrorObserver::Core> {
19 public:
20  Core(NetworkDelegate* network_delegate, base::MessageLoopProxy* origin_loop);
21
22  void NotifyPACScriptError(int line_number, const base::string16& error);
23
24  void Shutdown();
25
26 private:
27  friend class base::RefCountedThreadSafe<NetworkDelegateErrorObserver::Core>;
28
29  virtual ~Core();
30
31  NetworkDelegate* network_delegate_;
32  scoped_refptr<base::MessageLoopProxy> origin_loop_;
33
34  DISALLOW_COPY_AND_ASSIGN(Core);
35};
36
37NetworkDelegateErrorObserver::Core::Core(NetworkDelegate* network_delegate,
38                                         base::MessageLoopProxy* origin_loop)
39  : network_delegate_(network_delegate),
40    origin_loop_(origin_loop) {
41  DCHECK(origin_loop);
42}
43
44NetworkDelegateErrorObserver::Core::~Core() {}
45
46
47void NetworkDelegateErrorObserver::Core::NotifyPACScriptError(
48    int line_number,
49    const base::string16& error) {
50  if (!origin_loop_->BelongsToCurrentThread()) {
51    origin_loop_->PostTask(
52        FROM_HERE,
53        base::Bind(&Core::NotifyPACScriptError, this, line_number, error));
54    return;
55  }
56  if (network_delegate_)
57    network_delegate_->NotifyPACScriptError(line_number, error);
58}
59
60void NetworkDelegateErrorObserver::Core::Shutdown() {
61  CHECK(origin_loop_->BelongsToCurrentThread());
62  network_delegate_ = NULL;
63}
64
65// NetworkDelegateErrorObserver -----------------------------------------------
66
67NetworkDelegateErrorObserver::NetworkDelegateErrorObserver(
68    NetworkDelegate* network_delegate,
69    base::MessageLoopProxy* origin_loop)
70    : core_(new Core(network_delegate, origin_loop)) {}
71
72NetworkDelegateErrorObserver::~NetworkDelegateErrorObserver() {
73  core_->Shutdown();
74}
75
76void NetworkDelegateErrorObserver::OnPACScriptError(
77    int line_number,
78    const base::string16& error) {
79  core_->NotifyPACScriptError(line_number, error);
80}
81
82}  // namespace net
83