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
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_LOOKUP_REQUEST_H_
6#define CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_LOOKUP_REQUEST_H_
7
8#include "base/callback.h"
9#include "base/memory/scoped_ptr.h"
10#include "net/base/address_list.h"
11#include "net/base/net_errors.h"
12#include "net/dns/host_resolver.h"
13#include "net/dns/single_request_host_resolver.h"
14
15namespace content {
16
17template <class T>
18class PepperLookupRequest {
19 public:
20  typedef base::Callback<void(int, const net::AddressList&, const T&)>
21      LookupRequestCallback;
22
23  // Takes ownership over |bound_info|. |bound_info| will be passed to
24  // callback, when lookup will finish.
25  PepperLookupRequest(net::HostResolver* resolver,
26                      const net::HostResolver::RequestInfo& request_info,
27                      net::RequestPriority priority,
28                      T* bound_info,
29                      const LookupRequestCallback& callback)
30      : resolver_(resolver),
31        request_info_(request_info),
32        priority_(priority),
33        bound_info_(bound_info),
34        callback_(callback) {}
35
36  void Start() {
37    int result =
38        resolver_.Resolve(request_info_,
39                          priority_,
40                          &addresses_,
41                          base::Bind(&PepperLookupRequest<T>::OnLookupFinished,
42                                     base::Unretained(this)),
43                          net::BoundNetLog());
44    if (result != net::ERR_IO_PENDING)
45      OnLookupFinished(result);
46  }
47
48 private:
49  void OnLookupFinished(int result) {
50    callback_.Run(result, addresses_, *bound_info_);
51    delete this;
52  }
53
54  net::SingleRequestHostResolver resolver_;
55  net::HostResolver::RequestInfo request_info_;
56  net::RequestPriority priority_;
57  scoped_ptr<T> bound_info_;
58  LookupRequestCallback callback_;
59
60  net::AddressList addresses_;
61
62  DISALLOW_COPY_AND_ASSIGN(PepperLookupRequest);
63};
64
65}  // namespace content
66
67#endif  // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_LOOKUP_REQUEST_H_
68