privet_local_printer_lister.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1// Copyright 2013 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_LOCAL_DISCOVERY_PRIVET_LOCAL_PRINTER_LISTER_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_LOCAL_PRINTER_LISTER_H_
7
8#include <map>
9#include <string>
10
11#include "base/memory/linked_ptr.h"
12#include "chrome/browser/local_discovery/privet_device_lister.h"
13#include "chrome/browser/local_discovery/privet_http.h"
14#include "chrome/browser/local_discovery/privet_http_asynchronous_factory.h"
15#include "chrome/common/local_discovery/service_discovery_client.h"
16#include "net/url_request/url_request_context.h"
17
18namespace local_discovery {
19
20// This is an adapter to PrivetDeviceLister that finds printers and checks if
21// they support Privet local printing.
22class PrivetLocalPrinterLister : PrivetDeviceLister::Delegate,
23                                 PrivetInfoOperation::Delegate {
24 public:
25  class Delegate {
26   public:
27    virtual ~Delegate() {}
28    virtual void LocalPrinterChanged(bool added,
29                                     const std::string& name,
30                                     bool has_local_printing,
31                                     const DeviceDescription& description) = 0;
32    virtual void LocalPrinterRemoved(const std::string& name) = 0;
33    virtual void LocalPrinterCacheFlushed() = 0;
34  };
35
36  PrivetLocalPrinterLister(ServiceDiscoveryClient* service_discovery_client,
37                           net::URLRequestContextGetter* request_context,
38                           Delegate* delegate);
39  virtual ~PrivetLocalPrinterLister();
40
41  void Start();
42
43  // Stops listening/listing, keeps the data.
44  void Stop();
45
46  const DeviceDescription* GetDeviceDescription(const std::string& name);
47
48  // PrivetDeviceLister::Delegate implementation.
49  virtual void DeviceChanged(bool added,
50                             const std::string& name,
51                             const DeviceDescription& description) OVERRIDE;
52  virtual void DeviceRemoved(const std::string& name) OVERRIDE;
53  virtual void DeviceCacheFlushed() OVERRIDE;
54
55  // PrivetInfoOperation::Delegate implementation.
56  virtual void OnPrivetInfoDone(
57      PrivetInfoOperation* operation,
58      int http_code,
59      const base::DictionaryValue* json_value) OVERRIDE;
60
61 private:
62  struct DeviceContext;
63
64  void OnPrivetResolved(scoped_ptr<PrivetHTTPClient> http_client);
65
66  typedef std::map<std::string, linked_ptr<DeviceContext> > DeviceContextMap;
67
68  scoped_ptr<PrivetHTTPAsynchronousFactory> privet_http_factory_;
69  DeviceContextMap device_contexts_;
70  Delegate* delegate_;
71
72  scoped_ptr<PrivetDeviceLister> privet_lister_;
73};
74
75}  // namespace local_discovery
76
77#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_LOCAL_PRINTER_LISTER_H_
78