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