OLD | NEW |
(Empty) | |
| 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/values.h" |
| 8 #include "chrome/browser/extensions/webstore_data_fetcher_delegate.h" |
| 9 #include "chrome/common/chrome_utility_messages.h" |
| 10 #include "chrome/common/extensions/extension_constants.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/utility_process_host.h" |
| 13 #include "content/public/browser/utility_process_host_client.h" |
| 14 #include "net/base/load_flags.h" |
| 15 #include "net/url_request/url_fetcher.h" |
| 16 #include "net/url_request/url_request_status.h" |
| 17 |
| 18 using content::BrowserThread; |
| 19 using content::UtilityProcessHost; |
| 20 using content::UtilityProcessHostClient; |
| 21 |
| 22 namespace { |
| 23 |
| 24 const char kInvalidWebstoreResponseError[] = "Invalid Chrome Web Store reponse"; |
| 25 |
| 26 } // namespace |
| 27 |
| 28 namespace extensions { |
| 29 |
| 30 class WebstoreDataFetcher::SafeWebstoreResponseParser |
| 31 : public UtilityProcessHostClient { |
| 32 public: |
| 33 SafeWebstoreResponseParser(const base::WeakPtr<WebstoreDataFetcher>& client, |
| 34 const std::string& webstore_data) |
| 35 : client_(client), |
| 36 webstore_data_(webstore_data) {} |
| 37 |
| 38 void Start() { |
| 39 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 40 BrowserThread::PostTask( |
| 41 BrowserThread::IO, |
| 42 FROM_HERE, |
| 43 base::Bind(&SafeWebstoreResponseParser::StartWorkOnIOThread, this)); |
| 44 } |
| 45 |
| 46 void StartWorkOnIOThread() { |
| 47 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 48 UtilityProcessHost* host = |
| 49 UtilityProcessHost::Create( |
| 50 this, base::MessageLoopProxy::current()); |
| 51 host->EnableZygote(); |
| 52 host->Send(new ChromeUtilityMsg_ParseJSON(webstore_data_)); |
| 53 } |
| 54 |
| 55 // Implementing pieces of the UtilityProcessHostClient interface. |
| 56 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| 57 bool handled = true; |
| 58 IPC_BEGIN_MESSAGE_MAP(SafeWebstoreResponseParser, message) |
| 59 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Succeeded, |
| 60 OnJSONParseSucceeded) |
| 61 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Failed, |
| 62 OnJSONParseFailed) |
| 63 IPC_MESSAGE_UNHANDLED(handled = false) |
| 64 IPC_END_MESSAGE_MAP() |
| 65 return handled; |
| 66 } |
| 67 |
| 68 void OnJSONParseSucceeded(const base::ListValue& wrapper) { |
| 69 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 70 const Value* value = NULL; |
| 71 CHECK(wrapper.Get(0, &value)); |
| 72 if (value->IsType(Value::TYPE_DICTIONARY)) { |
| 73 parsed_webstore_data_.reset( |
| 74 static_cast<const DictionaryValue*>(value)->DeepCopy()); |
| 75 } else { |
| 76 error_ = kInvalidWebstoreResponseError; |
| 77 } |
| 78 |
| 79 ReportResults(); |
| 80 } |
| 81 |
| 82 virtual void OnJSONParseFailed(const std::string& error_message) { |
| 83 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 84 error_ = error_message; |
| 85 ReportResults(); |
| 86 } |
| 87 |
| 88 void ReportResults() { |
| 89 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 90 |
| 91 BrowserThread::PostTask( |
| 92 BrowserThread::UI, |
| 93 FROM_HERE, |
| 94 base::Bind(&SafeWebstoreResponseParser::ReportResultOnUIThread, this)); |
| 95 } |
| 96 |
| 97 void ReportResultOnUIThread() { |
| 98 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 99 if (!client_) |
| 100 return; |
| 101 |
| 102 if (error_.empty() && parsed_webstore_data_.get()) { |
| 103 client_->OnWebstoreResponseParseSuccess(parsed_webstore_data_.release()); |
| 104 } else { |
| 105 client_->OnWebstoreResponseParseFailure(error_); |
| 106 } |
| 107 } |
| 108 |
| 109 private: |
| 110 virtual ~SafeWebstoreResponseParser() {} |
| 111 |
| 112 base::WeakPtr<WebstoreDataFetcher> client_; |
| 113 |
| 114 std::string webstore_data_; |
| 115 std::string error_; |
| 116 scoped_ptr<DictionaryValue> parsed_webstore_data_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(SafeWebstoreResponseParser); |
| 119 }; |
| 120 |
| 121 WebstoreDataFetcher::WebstoreDataFetcher( |
| 122 WebstoreDataFetcherDelegate* delegate, |
| 123 net::URLRequestContextGetter* request_context, |
| 124 const GURL& referrer_url, |
| 125 const std::string webstore_item_id) |
| 126 : delegate_(delegate), |
| 127 request_context_(request_context), |
| 128 referrer_url_(referrer_url), |
| 129 id_(webstore_item_id) { |
| 130 } |
| 131 |
| 132 WebstoreDataFetcher::~WebstoreDataFetcher() {} |
| 133 |
| 134 void WebstoreDataFetcher::Start() { |
| 135 GURL webstore_data_url(extension_urls::GetWebstoreItemJsonDataURL(id_)); |
| 136 |
| 137 webstore_data_url_fetcher_.reset(net::URLFetcher::Create( |
| 138 webstore_data_url, net::URLFetcher::GET, this)); |
| 139 webstore_data_url_fetcher_->SetRequestContext(request_context_); |
| 140 webstore_data_url_fetcher_->SetReferrer(referrer_url_.spec()); |
| 141 webstore_data_url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
| 142 net::LOAD_DISABLE_CACHE); |
| 143 webstore_data_url_fetcher_->Start(); |
| 144 } |
| 145 |
| 146 void WebstoreDataFetcher::OnWebstoreResponseParseSuccess( |
| 147 base::DictionaryValue* webstore_data) { |
| 148 delegate_->OnWebstoreResponseParseSuccess(webstore_data); |
| 149 } |
| 150 |
| 151 void WebstoreDataFetcher::OnWebstoreResponseParseFailure( |
| 152 const std::string& error) { |
| 153 delegate_->OnWebstoreResponseParseFailure(error); |
| 154 } |
| 155 |
| 156 void WebstoreDataFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 157 CHECK_EQ(webstore_data_url_fetcher_.get(), source); |
| 158 |
| 159 if (!webstore_data_url_fetcher_->GetStatus().is_success() || |
| 160 webstore_data_url_fetcher_->GetResponseCode() != 200) { |
| 161 delegate_->OnWebstoreRequestFailure(); |
| 162 return; |
| 163 } |
| 164 |
| 165 std::string webstore_json_data; |
| 166 webstore_data_url_fetcher_->GetResponseAsString(&webstore_json_data); |
| 167 webstore_data_url_fetcher_.reset(); |
| 168 |
| 169 scoped_refptr<SafeWebstoreResponseParser> parser = |
| 170 new SafeWebstoreResponseParser(AsWeakPtr(), webstore_json_data); |
| 171 // The parser will call us back via OnWebstoreResponseParseSucces or |
| 172 // OnWebstoreResponseParseFailure. |
| 173 parser->Start(); |
| 174 } |
| 175 |
| 176 } // namespace extensions |
OLD | NEW |