OLD | NEW |
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/common/json_response_fetcher.h" | 5 #include "chrome/browser/ui/app_list/search/common/json_response_fetcher.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" |
10 #include "base/values.h" | 11 #include "base/values.h" |
11 #include "components/safe_json/safe_json_parser.h" | 12 #include "components/safe_json/safe_json_parser.h" |
12 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
13 #include "net/url_request/url_fetcher.h" | 14 #include "net/url_request/url_fetcher.h" |
14 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
16 | 17 |
17 namespace app_list { | 18 namespace app_list { |
18 | 19 |
19 const char kBadResponse[] = "Bad Web Service search response"; | 20 const char kBadResponse[] = "Bad Web Service search response"; |
(...skipping 18 matching lines...) Expand all Loading... |
38 net::LOAD_DISABLE_CACHE); | 39 net::LOAD_DISABLE_CACHE); |
39 fetcher_->Start(); | 40 fetcher_->Start(); |
40 } | 41 } |
41 | 42 |
42 void JSONResponseFetcher::Stop() { | 43 void JSONResponseFetcher::Stop() { |
43 fetcher_.reset(); | 44 fetcher_.reset(); |
44 weak_factory_.InvalidateWeakPtrs(); | 45 weak_factory_.InvalidateWeakPtrs(); |
45 } | 46 } |
46 | 47 |
47 void JSONResponseFetcher::OnJsonParseSuccess( | 48 void JSONResponseFetcher::OnJsonParseSuccess( |
48 scoped_ptr<base::Value> parsed_json) { | 49 std::unique_ptr<base::Value> parsed_json) { |
49 if (!parsed_json->IsType(base::Value::TYPE_DICTIONARY)) { | 50 if (!parsed_json->IsType(base::Value::TYPE_DICTIONARY)) { |
50 OnJsonParseError(kBadResponse); | 51 OnJsonParseError(kBadResponse); |
51 return; | 52 return; |
52 } | 53 } |
53 | 54 |
54 callback_.Run(make_scoped_ptr( | 55 callback_.Run(base::WrapUnique( |
55 static_cast<base::DictionaryValue*>(parsed_json.release()))); | 56 static_cast<base::DictionaryValue*>(parsed_json.release()))); |
56 } | 57 } |
57 | 58 |
58 void JSONResponseFetcher::OnJsonParseError(const std::string& error) { | 59 void JSONResponseFetcher::OnJsonParseError(const std::string& error) { |
59 callback_.Run(scoped_ptr<base::DictionaryValue>()); | 60 callback_.Run(std::unique_ptr<base::DictionaryValue>()); |
60 } | 61 } |
61 | 62 |
62 void JSONResponseFetcher::OnURLFetchComplete( | 63 void JSONResponseFetcher::OnURLFetchComplete( |
63 const net::URLFetcher* source) { | 64 const net::URLFetcher* source) { |
64 CHECK_EQ(fetcher_.get(), source); | 65 CHECK_EQ(fetcher_.get(), source); |
65 | 66 |
66 scoped_ptr<net::URLFetcher> fetcher(std::move(fetcher_)); | 67 std::unique_ptr<net::URLFetcher> fetcher(std::move(fetcher_)); |
67 | 68 |
68 if (!fetcher->GetStatus().is_success() || | 69 if (!fetcher->GetStatus().is_success() || |
69 fetcher->GetResponseCode() != 200) { | 70 fetcher->GetResponseCode() != 200) { |
70 OnJsonParseError(kBadResponse); | 71 OnJsonParseError(kBadResponse); |
71 return; | 72 return; |
72 } | 73 } |
73 | 74 |
74 std::string json_data; | 75 std::string json_data; |
75 fetcher->GetResponseAsString(&json_data); | 76 fetcher->GetResponseAsString(&json_data); |
76 | 77 |
77 // The parser will call us back via one of the callbacks. | 78 // The parser will call us back via one of the callbacks. |
78 safe_json::SafeJsonParser::Parse( | 79 safe_json::SafeJsonParser::Parse( |
79 json_data, base::Bind(&JSONResponseFetcher::OnJsonParseSuccess, | 80 json_data, base::Bind(&JSONResponseFetcher::OnJsonParseSuccess, |
80 weak_factory_.GetWeakPtr()), | 81 weak_factory_.GetWeakPtr()), |
81 base::Bind(&JSONResponseFetcher::OnJsonParseError, | 82 base::Bind(&JSONResponseFetcher::OnJsonParseError, |
82 weak_factory_.GetWeakPtr())); | 83 weak_factory_.GetWeakPtr())); |
83 } | 84 } |
84 | 85 |
85 } // namespace app_list | 86 } // namespace app_list |
OLD | NEW |