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_EXTENSIONS_BLACKLIST_STATE_FETCHER_H_
6#define CHROME_BROWSER_EXTENSIONS_BLACKLIST_STATE_FETCHER_H_
7
8#include <set>
9#include <string>
10
11#include "base/callback.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "chrome/browser/safe_browsing/protocol_manager_helper.h"
15#include "extensions/browser/blacklist_state.h"
16#include "net/url_request/url_fetcher.h"
17#include "net/url_request/url_fetcher_delegate.h"
18
19namespace net {
20class URLRequestContextGetter;
21}
22
23namespace extensions {
24
25class BlacklistStateFetcher : public net::URLFetcherDelegate {
26 public:
27  typedef base::Callback<void(BlacklistState)> RequestCallback;
28
29  BlacklistStateFetcher();
30
31  virtual ~BlacklistStateFetcher();
32
33  virtual void Request(const std::string& id, const RequestCallback& callback);
34
35  void SetSafeBrowsingConfig(const SafeBrowsingProtocolConfig& config);
36
37  void SetURLRequestContextForTest(
38      net::URLRequestContextGetter* parent_request_context);
39
40 protected:
41  // net::URLFetcherDelegate interface.
42  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
43
44 private:
45  typedef std::multimap<std::string, RequestCallback> CallbackMultiMap;
46
47  GURL RequestUrl() const;
48
49  void SaveRequestContext(
50    const std::string& id,
51    scoped_refptr<net::URLRequestContextGetter> request_context_getter);
52
53  void SendRequest(const std::string& id);
54
55  // ID for URLFetchers for testing.
56  int url_fetcher_id_;
57
58  scoped_ptr<SafeBrowsingProtocolConfig> safe_browsing_config_;
59  scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
60  scoped_refptr<net::URLRequestContextGetter> parent_request_context_for_test_;
61
62  // Extension id by URLFetcher.
63  std::map<const net::URLFetcher*, std::string> requests_;
64
65  // Callbacks by extension ID.
66  CallbackMultiMap callbacks_;
67
68  base::WeakPtrFactory<BlacklistStateFetcher> weak_ptr_factory_;
69
70  DISALLOW_COPY_AND_ASSIGN(BlacklistStateFetcher);
71};
72
73}  // namespace extensions
74
75#endif  // CHROME_BROWSER_EXTENSIONS_BLACKLIST_STATE_FETCHER_H_
76
77