pepper_lookup_request.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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                      T* bound_info,
28                      const LookupRequestCallback& callback)
29      : resolver_(resolver),
30        request_info_(request_info),
31        bound_info_(bound_info),
32        callback_(callback) {
33  }
34
35  void Start() {
36    int result = resolver_.Resolve(
37        request_info_, &addresses_,
38        base::Bind(&PepperLookupRequest<T>::OnLookupFinished,
39                   base::Unretained(this)),
40        net::BoundNetLog());
41    if (result != net::ERR_IO_PENDING)
42      OnLookupFinished(result);
43  }
44
45 private:
46  void OnLookupFinished(int result) {
47    callback_.Run(result, addresses_, *bound_info_);
48    delete this;
49  }
50
51  net::SingleRequestHostResolver resolver_;
52  net::HostResolver::RequestInfo request_info_;
53  scoped_ptr<T> bound_info_;
54  LookupRequestCallback callback_;
55
56  net::AddressList addresses_;
57
58  DISALLOW_COPY_AND_ASSIGN(PepperLookupRequest);
59};
60
61}  // namespace content
62
63#endif  // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_LOOKUP_REQUEST_H_
64