Chromium Code Reviews| Index: chrome/browser/ui/app_list/search/webstore_search_fetcher.cc |
| diff --git a/chrome/browser/ui/app_list/search/webstore_search_fetcher.cc b/chrome/browser/ui/app_list/search/webstore_search_fetcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6933d4c4d2b2e88cf10c90095f1905872dce9312 |
| --- /dev/null |
| +++ b/chrome/browser/ui/app_list/search/webstore_search_fetcher.cc |
| @@ -0,0 +1,88 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/app_list/search/webstore_search_fetcher.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/safe_json_parser.h" |
| +#include "chrome/common/extensions/extension_constants.h" |
| +#include "net/base/load_flags.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_request_status.h" |
| + |
| +namespace app_list { |
| + |
| +const char kBadResponse[] = "Bad Web Store search response"; |
| + |
| +WebstoreSearchFetcher::WebstoreSearchFetcher( |
| + const Callback& callback, |
| + net::URLRequestContextGetter* context_getter) |
| + : callback_(callback), |
| + context_getter_(context_getter), |
| + json_parser_factory_(this) { |
|
koz (OOO until 15th September)
2013/05/20 01:14:16
nit: same comment as above.
xiyuan
2013/05/20 16:56:31
Done.
|
| + DCHECK(!callback_.is_null()); |
| +} |
| + |
| +WebstoreSearchFetcher::~WebstoreSearchFetcher() {} |
| + |
| +void WebstoreSearchFetcher::Start(const std::string& query, |
| + const std::string& hl) { |
| + Stop(); |
| + |
| + fetcher_.reset(net::URLFetcher::Create( |
| + extension_urls::GetWebstoreJsonSearchUrl(query, hl), |
| + net::URLFetcher::GET, |
| + this)); |
| + fetcher_->SetRequestContext(context_getter_); |
| + fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
| + net::LOAD_DISABLE_CACHE); |
| + fetcher_->Start(); |
| +} |
| + |
| +void WebstoreSearchFetcher::Stop() { |
| + fetcher_.reset(); |
| + json_parser_factory_.InvalidateWeakPtrs(); |
| +} |
| + |
| +void WebstoreSearchFetcher::OnJsonParseSuccess( |
| + scoped_ptr<base::Value> parsed_json) { |
| + if (!parsed_json->IsType(base::Value::TYPE_DICTIONARY)) { |
| + OnJsonParseError(kBadResponse); |
| + return; |
| + } |
| + |
| + callback_.Run(make_scoped_ptr( |
| + static_cast<base::DictionaryValue*>(parsed_json.release()))); |
| +} |
| + |
| +void WebstoreSearchFetcher::OnJsonParseError(const std::string& error) { |
| + callback_.Run(scoped_ptr<base::DictionaryValue>()); |
| +} |
| + |
| +void WebstoreSearchFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| + CHECK_EQ(fetcher_.get(), source); |
| + |
| + scoped_ptr<net::URLFetcher> fetcher(fetcher_.Pass()); |
| + |
| + if (!fetcher->GetStatus().is_success() || |
| + fetcher->GetResponseCode() != 200) { |
| + OnJsonParseError(kBadResponse); |
|
James Cook
2013/05/20 14:39:34
Is kBadResponse used? OnJsonParseError seems to ig
xiyuan
2013/05/20 16:56:31
OnJsonParseError is a callback for SafeJsonParser.
|
| + return; |
| + } |
| + |
| + std::string webstore_json_data; |
| + fetcher->GetResponseAsString(&webstore_json_data); |
| + |
| + scoped_refptr<SafeJsonParser> parser = |
| + new SafeJsonParser(webstore_json_data, |
| + base::Bind(&WebstoreSearchFetcher::OnJsonParseSuccess, |
| + json_parser_factory_.GetWeakPtr()), |
| + base::Bind(&WebstoreSearchFetcher::OnJsonParseError, |
| + json_parser_factory_.GetWeakPtr())); |
| + // The parser will call us back via one of the callbacks. |
| + parser->Start(); |
| +} |
| + |
| +} // namespace app_list |