json_response_fetcher.h revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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_UI_APP_LIST_SEARCH_COMMON_JSON_RESPONSE_FETCHER_H_
6#define CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_JSON_RESPONSE_FETCHER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/callback.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "net/url_request/url_fetcher_delegate.h"
15
16class GURL;
17
18namespace base {
19class DictionaryValue;
20class Value;
21}
22
23namespace net {
24class URLFetcher;
25class URLRequestContextGetter;
26}
27
28namespace app_list {
29
30// A class that fetches a JSON formatted response from a server and uses a
31// sandboxed utility process to parse it to a DictionaryValue.
32class JSONResponseFetcher : public net::URLFetcherDelegate {
33 public:
34  // Callback to pass back the parsed json dictionary returned from the server.
35  // Invoked with NULL if there is an error.
36  typedef base::Callback<void(scoped_ptr<base::DictionaryValue>)> Callback;
37
38  JSONResponseFetcher(const Callback& callback,
39                      net::URLRequestContextGetter* context_getter);
40  virtual ~JSONResponseFetcher();
41
42  // Starts to fetch results for the given |query_url|.
43  void Start(const GURL& query_url);
44  void Stop();
45
46 private:
47  // Callbacks for SafeJsonParser.
48  void OnJsonParseSuccess(scoped_ptr<base::Value> parsed_json);
49  void OnJsonParseError(const std::string& error);
50
51  // net::URLFetcherDelegate overrides:
52  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
53
54  Callback callback_;
55  net::URLRequestContextGetter* context_getter_;
56
57  scoped_ptr<net::URLFetcher> fetcher_;
58  base::WeakPtrFactory<JSONResponseFetcher> weak_factory_;
59
60  DISALLOW_COPY_AND_ASSIGN(JSONResponseFetcher);
61};
62
63}  // namespace app_list
64
65#endif  // CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_JSON_RESPONSE_FETCHER_H_
66