Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: chrome/browser/ui/app_list/search/common/json_response_fetcher.cc

Issue 23874015: Implement people search. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/app_list/search/webstore_search_fetcher.h" 5 #include "chrome/browser/ui/app_list/search/common/json_response_fetcher.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/browser/safe_json_parser.h" 9 #include "chrome/browser/safe_json_parser.h"
10 #include "chrome/common/extensions/extension_constants.h"
11 #include "net/base/load_flags.h" 10 #include "net/base/load_flags.h"
12 #include "net/url_request/url_fetcher.h" 11 #include "net/url_request/url_fetcher.h"
13 #include "net/url_request/url_request_status.h" 12 #include "net/url_request/url_request_status.h"
13 #include "url/gurl.h"
14 14
15 namespace app_list { 15 namespace app_list {
16 16
17 const char kBadResponse[] = "Bad Web Store search response"; 17 const char kBadResponse[] = "Bad Web Service search response";
18 18
19 WebstoreSearchFetcher::WebstoreSearchFetcher( 19 JSONResponseFetcher::JSONResponseFetcher(
20 const Callback& callback, 20 const Callback& callback,
21 net::URLRequestContextGetter* context_getter) 21 net::URLRequestContextGetter* context_getter)
22 : callback_(callback), 22 : callback_(callback),
23 context_getter_(context_getter), 23 context_getter_(context_getter),
24 weak_factory_(this) { 24 weak_factory_(this) {
25 DCHECK(!callback_.is_null()); 25 DCHECK(!callback_.is_null());
26 } 26 }
27 27
28 WebstoreSearchFetcher::~WebstoreSearchFetcher() {} 28 JSONResponseFetcher::~JSONResponseFetcher() {}
29 29
30 void WebstoreSearchFetcher::Start(const std::string& query, 30 void JSONResponseFetcher::Start(const GURL& query_url) {
31 const std::string& hl) {
32 Stop(); 31 Stop();
33 32
34 fetcher_.reset(net::URLFetcher::Create( 33 fetcher_.reset(net::URLFetcher::Create(
35 extension_urls::GetWebstoreJsonSearchUrl(query, hl), 34 query_url,
36 net::URLFetcher::GET, 35 net::URLFetcher::GET,
37 this)); 36 this));
38 fetcher_->SetRequestContext(context_getter_); 37 fetcher_->SetRequestContext(context_getter_);
39 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | 38 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
40 net::LOAD_DISABLE_CACHE); 39 net::LOAD_DISABLE_CACHE);
41 fetcher_->Start(); 40 fetcher_->Start();
42 } 41 }
43 42
44 void WebstoreSearchFetcher::Stop() { 43 void JSONResponseFetcher::Stop() {
45 fetcher_.reset(); 44 fetcher_.reset();
46 weak_factory_.InvalidateWeakPtrs(); 45 weak_factory_.InvalidateWeakPtrs();
47 } 46 }
48 47
49 void WebstoreSearchFetcher::OnJsonParseSuccess( 48 void JSONResponseFetcher::OnJsonParseSuccess(
50 scoped_ptr<base::Value> parsed_json) { 49 scoped_ptr<base::Value> parsed_json) {
51 if (!parsed_json->IsType(base::Value::TYPE_DICTIONARY)) { 50 if (!parsed_json->IsType(base::Value::TYPE_DICTIONARY)) {
52 OnJsonParseError(kBadResponse); 51 OnJsonParseError(kBadResponse);
53 return; 52 return;
54 } 53 }
55 54
56 callback_.Run(make_scoped_ptr( 55 callback_.Run(make_scoped_ptr(
57 static_cast<base::DictionaryValue*>(parsed_json.release()))); 56 static_cast<base::DictionaryValue*>(parsed_json.release())));
58 } 57 }
59 58
60 void WebstoreSearchFetcher::OnJsonParseError(const std::string& error) { 59 void JSONResponseFetcher::OnJsonParseError(const std::string& error) {
61 callback_.Run(scoped_ptr<base::DictionaryValue>()); 60 callback_.Run(scoped_ptr<base::DictionaryValue>());
62 } 61 }
63 62
64 void WebstoreSearchFetcher::OnURLFetchComplete(const net::URLFetcher* source) { 63 void JSONResponseFetcher::OnURLFetchComplete(
64 const net::URLFetcher* source) {
65 CHECK_EQ(fetcher_.get(), source); 65 CHECK_EQ(fetcher_.get(), source);
66 66
67 scoped_ptr<net::URLFetcher> fetcher(fetcher_.Pass()); 67 scoped_ptr<net::URLFetcher> fetcher(fetcher_.Pass());
68 68
69 if (!fetcher->GetStatus().is_success() || 69 if (!fetcher->GetStatus().is_success() ||
70 fetcher->GetResponseCode() != 200) { 70 fetcher->GetResponseCode() != 200) {
71 OnJsonParseError(kBadResponse); 71 OnJsonParseError(kBadResponse);
72 return; 72 return;
73 } 73 }
74 74
75 std::string webstore_json_data; 75 std::string json_data;
76 fetcher->GetResponseAsString(&webstore_json_data); 76 fetcher->GetResponseAsString(&json_data);
77 77
78 scoped_refptr<SafeJsonParser> parser = 78 scoped_refptr<SafeJsonParser> parser =
79 new SafeJsonParser(webstore_json_data, 79 new SafeJsonParser(json_data,
80 base::Bind(&WebstoreSearchFetcher::OnJsonParseSuccess, 80 base::Bind(
81 weak_factory_.GetWeakPtr()), 81 &JSONResponseFetcher::OnJsonParseSuccess,
82 base::Bind(&WebstoreSearchFetcher::OnJsonParseError, 82 weak_factory_.GetWeakPtr()),
83 weak_factory_.GetWeakPtr())); 83 base::Bind(
84 &JSONResponseFetcher::OnJsonParseError,
85 weak_factory_.GetWeakPtr()));
84 // The parser will call us back via one of the callbacks. 86 // The parser will call us back via one of the callbacks.
85 parser->Start(); 87 parser->Start();
86 } 88 }
87 89
88 } // namespace app_list 90 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698