1 // Copyright (c) 2012 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_install_helper.h"
10 #include "base/values.h"
11 #include "chrome/common/chrome_utility_messages.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/utility_process_host.h"
14 #include "net/base/load_flags.h"
15 #include "net/url_request/url_fetcher.h"
16 #include "net/url_request/url_request_context_getter.h"
17 #include "net/url_request/url_request_status.h"
19 using content::BrowserThread;
20 using content::UtilityProcessHost;
24 const char kImageDecodeError[] = "Image decode failed";
28 namespace extensions {
30 WebstoreInstallHelper::WebstoreInstallHelper(
32 const std::string& id,
33 const std::string& manifest,
34 const std::string& icon_data,
36 net::URLRequestContextGetter* context_getter)
37 : delegate_(delegate),
40 icon_base64_data_(icon_data),
42 context_getter_(context_getter),
43 icon_decode_complete_(false),
44 manifest_parse_complete_(false),
45 parse_error_(Delegate::UNKNOWN_ERROR) {}
47 WebstoreInstallHelper::~WebstoreInstallHelper() {}
49 void WebstoreInstallHelper::Start() {
50 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
51 CHECK(icon_base64_data_.empty() || icon_url_.is_empty());
53 if (icon_base64_data_.empty() && icon_url_.is_empty())
54 icon_decode_complete_ = true;
56 BrowserThread::PostTask(
59 base::Bind(&WebstoreInstallHelper::StartWorkOnIOThread, this));
61 if (!icon_url_.is_empty()) {
62 CHECK(context_getter_);
63 url_fetcher_.reset(net::URLFetcher::Create(
64 icon_url_, net::URLFetcher::GET, this));
65 url_fetcher_->SetRequestContext(context_getter_);
66 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
67 net::LOAD_DO_NOT_SEND_COOKIES);
69 url_fetcher_->Start();
70 // We'll get called back in OnURLFetchComplete.
74 void WebstoreInstallHelper::StartWorkOnIOThread() {
75 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
76 utility_host_ = UtilityProcessHost::Create(
77 this, base::MessageLoopProxy::current().get())->AsWeakPtr();
78 utility_host_->EnableZygote();
79 utility_host_->StartBatchMode();
81 if (!icon_base64_data_.empty())
83 new ChromeUtilityMsg_DecodeImageBase64(icon_base64_data_));
85 utility_host_->Send(new ChromeUtilityMsg_ParseJSON(manifest_));
88 void WebstoreInstallHelper::OnURLFetchComplete(
89 const net::URLFetcher* source) {
90 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
91 CHECK(source == url_fetcher_.get());
92 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS ||
93 source->GetResponseCode() != 200) {
94 BrowserThread::PostTask(
97 base::Bind(&WebstoreInstallHelper::OnDecodeImageFailed, this));
99 std::string response_data;
100 source->GetResponseAsString(&response_data);
101 fetched_icon_data_.insert(fetched_icon_data_.begin(),
102 response_data.begin(),
103 response_data.end());
104 BrowserThread::PostTask(
107 base::Bind(&WebstoreInstallHelper::StartFetchedImageDecode, this));
109 url_fetcher_.reset();
112 void WebstoreInstallHelper::StartFetchedImageDecode() {
113 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
114 CHECK(utility_host_.get());
115 utility_host_->Send(new ChromeUtilityMsg_DecodeImage(fetched_icon_data_));
119 bool WebstoreInstallHelper::OnMessageReceived(const IPC::Message& message) {
121 IPC_BEGIN_MESSAGE_MAP(WebstoreInstallHelper, message)
122 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded,
123 OnDecodeImageSucceeded)
124 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed,
126 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Succeeded,
127 OnJSONParseSucceeded)
128 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Failed,
130 IPC_MESSAGE_UNHANDLED(handled = false)
131 IPC_END_MESSAGE_MAP()
136 void WebstoreInstallHelper::OnDecodeImageSucceeded(
137 const SkBitmap& decoded_image) {
138 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
139 icon_ = decoded_image;
140 icon_decode_complete_ = true;
141 ReportResultsIfComplete();
144 void WebstoreInstallHelper::OnDecodeImageFailed() {
145 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
146 icon_decode_complete_ = true;
147 error_ = kImageDecodeError;
148 parse_error_ = Delegate::ICON_ERROR;
149 ReportResultsIfComplete();
152 void WebstoreInstallHelper::OnJSONParseSucceeded(
153 const base::ListValue& wrapper) {
154 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
155 manifest_parse_complete_ = true;
156 const Value* value = NULL;
157 CHECK(wrapper.Get(0, &value));
158 if (value->IsType(Value::TYPE_DICTIONARY)) {
159 parsed_manifest_.reset(
160 static_cast<const DictionaryValue*>(value)->DeepCopy());
162 parse_error_ = Delegate::MANIFEST_ERROR;
164 ReportResultsIfComplete();
167 void WebstoreInstallHelper::OnJSONParseFailed(
168 const std::string& error_message) {
169 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
170 manifest_parse_complete_ = true;
171 error_ = error_message;
172 parse_error_ = Delegate::MANIFEST_ERROR;
173 ReportResultsIfComplete();
176 void WebstoreInstallHelper::ReportResultsIfComplete() {
177 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
179 if (!icon_decode_complete_ || !manifest_parse_complete_)
182 // The utility_host_ will take care of deleting itself after this call.
183 if (utility_host_.get()) {
184 utility_host_->EndBatchMode();
185 utility_host_.reset();
188 BrowserThread::PostTask(
191 base::Bind(&WebstoreInstallHelper::ReportResultFromUIThread, this));
194 void WebstoreInstallHelper::ReportResultFromUIThread() {
195 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
196 if (error_.empty() && parsed_manifest_)
197 delegate_->OnWebstoreParseSuccess(id_, icon_, parsed_manifest_.release());
199 delegate_->OnWebstoreParseFailure(id_, parse_error_, error_);
202 } // namespace extensions