15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_LOOKUP_REQUEST_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_LOOKUP_REQUEST_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/address_list.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/dns/host_resolver.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/dns/single_request_host_resolver.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochtemplate <class T>
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PepperLookupRequest {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef base::Callback<void(int, const net::AddressList&, const T&)>
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LookupRequestCallback;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Takes ownership over |bound_info|. |bound_info| will be passed to
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // callback, when lookup will finish.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PepperLookupRequest(net::HostResolver* resolver,
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      const net::HostResolver::RequestInfo& request_info,
273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                      net::RequestPriority priority,
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      T* bound_info,
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      const LookupRequestCallback& callback)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : resolver_(resolver),
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        request_info_(request_info),
323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        priority_(priority),
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        bound_info_(bound_info),
343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        callback_(callback) {}
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Start() {
373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    int result =
383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        resolver_.Resolve(request_info_,
393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                          priority_,
403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                          &addresses_,
413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                          base::Bind(&PepperLookupRequest<T>::OnLookupFinished,
423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                     base::Unretained(this)),
433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                          net::BoundNetLog());
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (result != net::ERR_IO_PENDING)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      OnLookupFinished(result);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnLookupFinished(int result) {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    callback_.Run(result, addresses_, *bound_info_);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete this;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::SingleRequestHostResolver resolver_;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::HostResolver::RequestInfo request_info_;
563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  net::RequestPriority priority_;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<T> bound_info_;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LookupRequestCallback callback_;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::AddressList addresses_;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PepperLookupRequest);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_LOOKUP_REQUEST_H_
68