Home | History | Annotate | Download | only in extensions
      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 #include "chrome/browser/extensions/webstore_data_fetcher.h"
      6 
      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"
     15 
     16 namespace {
     17 
     18 const char kInvalidWebstoreResponseError[] = "Invalid Chrome Web Store reponse";
     19 
     20 }  // namespace
     21 
     22 namespace extensions {
     23 
     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) {
     33 }
     34 
     35 WebstoreDataFetcher::~WebstoreDataFetcher() {}
     36 
     37 void WebstoreDataFetcher::Start() {
     38   GURL webstore_data_url(extension_urls::GetWebstoreItemJsonDataURL(id_));
     39 
     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();
     47 }
     48 
     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;
     54   }
     55 
     56   delegate_->OnWebstoreResponseParseSuccess(scoped_ptr<base::DictionaryValue>(
     57       static_cast<base::DictionaryValue*>(parsed_json.release())));
     58 }
     59 
     60 void WebstoreDataFetcher::OnJsonParseFailure(
     61     const std::string& error) {
     62   delegate_->OnWebstoreResponseParseFailure(error);
     63 }
     64 
     65 void WebstoreDataFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
     66   CHECK_EQ(webstore_data_url_fetcher_.get(), source);
     67 
     68   scoped_ptr<net::URLFetcher> fetcher(webstore_data_url_fetcher_.Pass());
     69 
     70   if (!fetcher->GetStatus().is_success() ||
     71       fetcher->GetResponseCode() != 200) {
     72     delegate_->OnWebstoreRequestFailure();
     73     return;
     74   }
     75 
     76   std::string webstore_json_data;
     77   fetcher->GetResponseAsString(&webstore_json_data);
     78 
     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();
     87 }
     88 
     89 }  // namespace extensions
     90