NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / webstore_data_fetcher.cc
blob89d523b8135dc93f80fecf59adce350bffb011d3
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.
5 #include "chrome/browser/extensions/webstore_data_fetcher.h"
7 #include "base/bind.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/webstore_data_fetcher_delegate.h"
10 #include "chrome/browser/safe_json_parser.h"
11 #include "chrome/common/extensions/extension_constants.h"
12 #include "net/base/load_flags.h"
13 #include "net/url_request/url_fetcher.h"
14 #include "net/url_request/url_request_status.h"
16 namespace {
18 const char kInvalidWebstoreResponseError[] = "Invalid Chrome Web Store reponse";
20 } // namespace
22 namespace extensions {
24 WebstoreDataFetcher::WebstoreDataFetcher(
25 WebstoreDataFetcherDelegate* delegate,
26 net::URLRequestContextGetter* request_context,
27 const GURL& referrer_url,
28 const std::string webstore_item_id)
29 : delegate_(delegate),
30 request_context_(request_context),
31 referrer_url_(referrer_url),
32 id_(webstore_item_id) {
35 WebstoreDataFetcher::~WebstoreDataFetcher() {}
37 void WebstoreDataFetcher::Start() {
38 GURL webstore_data_url(extension_urls::GetWebstoreItemJsonDataURL(id_));
40 webstore_data_url_fetcher_.reset(net::URLFetcher::Create(
41 webstore_data_url, net::URLFetcher::GET, this));
42 webstore_data_url_fetcher_->SetRequestContext(request_context_);
43 webstore_data_url_fetcher_->SetReferrer(referrer_url_.spec());
44 webstore_data_url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
45 net::LOAD_DISABLE_CACHE);
46 webstore_data_url_fetcher_->Start();
49 void WebstoreDataFetcher::OnJsonParseSuccess(
50 scoped_ptr<base::Value> parsed_json) {
51 if (!parsed_json->IsType(base::Value::TYPE_DICTIONARY)) {
52 OnJsonParseFailure(kInvalidWebstoreResponseError);
53 return;
56 delegate_->OnWebstoreResponseParseSuccess(scoped_ptr<base::DictionaryValue>(
57 static_cast<base::DictionaryValue*>(parsed_json.release())));
60 void WebstoreDataFetcher::OnJsonParseFailure(
61 const std::string& error) {
62 delegate_->OnWebstoreResponseParseFailure(error);
65 void WebstoreDataFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
66 CHECK_EQ(webstore_data_url_fetcher_.get(), source);
68 scoped_ptr<net::URLFetcher> fetcher(webstore_data_url_fetcher_.Pass());
70 if (!fetcher->GetStatus().is_success() ||
71 fetcher->GetResponseCode() != 200) {
72 delegate_->OnWebstoreRequestFailure();
73 return;
76 std::string webstore_json_data;
77 fetcher->GetResponseAsString(&webstore_json_data);
79 scoped_refptr<SafeJsonParser> parser =
80 new SafeJsonParser(webstore_json_data,
81 base::Bind(&WebstoreDataFetcher::OnJsonParseSuccess,
82 AsWeakPtr()),
83 base::Bind(&WebstoreDataFetcher::OnJsonParseFailure,
84 AsWeakPtr()));
85 // The parser will call us back via one of the callbacks.
86 parser->Start();
89 } // namespace extensions