| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/webstore_install_helper.h" | 5 #include "chrome/browser/extensions/webstore_install_helper.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | 7 #include "base/bind.h" |
| 10 #include "base/thread_task_runner_handle.h" | |
| 11 #include "base/values.h" | 8 #include "base/values.h" |
| 12 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" | 9 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" |
| 13 #include "chrome/common/chrome_utility_messages.h" | 10 #include "chrome/browser/safe_json_parser.h" |
| 14 #include "chrome/common/extensions/chrome_utility_extensions_messages.h" | |
| 15 #include "chrome/grit/generated_resources.h" | |
| 16 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/utility_process_host.h" | |
| 18 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
| 19 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | |
| 21 | 14 |
| 22 using content::BrowserThread; | 15 using content::BrowserThread; |
| 23 using content::UtilityProcessHost; | |
| 24 | 16 |
| 25 namespace { | 17 namespace { |
| 26 | 18 |
| 27 const char kImageDecodeError[] = "Image decode failed"; | 19 const char kImageDecodeError[] = "Image decode failed"; |
| 28 | 20 |
| 29 } // namespace | 21 } // namespace |
| 30 | 22 |
| 31 namespace extensions { | 23 namespace extensions { |
| 32 | 24 |
| 33 WebstoreInstallHelper::WebstoreInstallHelper( | 25 WebstoreInstallHelper::WebstoreInstallHelper( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 44 icon_decode_complete_(false), | 36 icon_decode_complete_(false), |
| 45 manifest_parse_complete_(false), | 37 manifest_parse_complete_(false), |
| 46 parse_error_(Delegate::UNKNOWN_ERROR) { | 38 parse_error_(Delegate::UNKNOWN_ERROR) { |
| 47 } | 39 } |
| 48 | 40 |
| 49 WebstoreInstallHelper::~WebstoreInstallHelper() {} | 41 WebstoreInstallHelper::~WebstoreInstallHelper() {} |
| 50 | 42 |
| 51 void WebstoreInstallHelper::Start() { | 43 void WebstoreInstallHelper::Start() { |
| 52 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 53 | 45 |
| 46 json_parser_ = new SafeJsonParser( |
| 47 manifest_, |
| 48 base::Bind(&WebstoreInstallHelper::OnJSONParseSucceeded, this), |
| 49 base::Bind(&WebstoreInstallHelper::OnJSONParseFailed, this)); |
| 50 json_parser_->Start(); |
| 51 |
| 54 if (icon_url_.is_empty()) { | 52 if (icon_url_.is_empty()) { |
| 55 icon_decode_complete_ = true; | 53 icon_decode_complete_ = true; |
| 56 } else { | 54 } else { |
| 57 // No existing |icon_fetcher_| to avoid unbalanced AddRef(). | 55 // No existing |icon_fetcher_| to avoid unbalanced AddRef(). |
| 58 CHECK(!icon_fetcher_.get()); | 56 CHECK(!icon_fetcher_.get()); |
| 59 AddRef(); // Balanced in OnFetchComplete(). | 57 AddRef(); // Balanced in OnFetchComplete(). |
| 60 icon_fetcher_.reset(new chrome::BitmapFetcher(icon_url_, this)); | 58 icon_fetcher_.reset(new chrome::BitmapFetcher(icon_url_, this)); |
| 61 icon_fetcher_->Start( | 59 icon_fetcher_->Start( |
| 62 context_getter_, std::string(), | 60 context_getter_, std::string(), |
| 63 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | 61 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 64 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES); | 62 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES); |
| 65 } | 63 } |
| 66 | |
| 67 BrowserThread::PostTask( | |
| 68 BrowserThread::IO, | |
| 69 FROM_HERE, | |
| 70 base::Bind(&WebstoreInstallHelper::StartWorkOnIOThread, this)); | |
| 71 } | |
| 72 | |
| 73 void WebstoreInstallHelper::StartWorkOnIOThread() { | |
| 74 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 75 utility_host_ = UtilityProcessHost::Create( | |
| 76 this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr(); | |
| 77 utility_host_->SetName(l10n_util::GetStringUTF16( | |
| 78 IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); | |
| 79 utility_host_->StartBatchMode(); | |
| 80 | |
| 81 utility_host_->Send(new ChromeUtilityMsg_ParseJSON(manifest_)); | |
| 82 } | |
| 83 | |
| 84 bool WebstoreInstallHelper::OnMessageReceived(const IPC::Message& message) { | |
| 85 bool handled = true; | |
| 86 IPC_BEGIN_MESSAGE_MAP(WebstoreInstallHelper, message) | |
| 87 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Succeeded, | |
| 88 OnJSONParseSucceeded) | |
| 89 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ParseJSON_Failed, | |
| 90 OnJSONParseFailed) | |
| 91 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 92 IPC_END_MESSAGE_MAP() | |
| 93 return handled; | |
| 94 } | 64 } |
| 95 | 65 |
| 96 void WebstoreInstallHelper::OnFetchComplete(const GURL& url, | 66 void WebstoreInstallHelper::OnFetchComplete(const GURL& url, |
| 97 const SkBitmap* image) { | 67 const SkBitmap* image) { |
| 98 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 68 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 99 // OnFetchComplete should only be called as icon_fetcher_ delegate to avoid | 69 // OnFetchComplete should only be called as icon_fetcher_ delegate to avoid |
| 100 // unbalanced Release(). | 70 // unbalanced Release(). |
| 101 CHECK(icon_fetcher_.get()); | 71 CHECK(icon_fetcher_.get()); |
| 102 | 72 |
| 103 if (image) | 73 if (image) |
| 104 icon_ = *image; | 74 icon_ = *image; |
| 105 icon_decode_complete_ = true; | 75 icon_decode_complete_ = true; |
| 106 if (icon_.empty()) { | 76 if (icon_.empty()) { |
| 107 error_ = kImageDecodeError; | 77 error_ = kImageDecodeError; |
| 108 parse_error_ = Delegate::ICON_ERROR; | 78 parse_error_ = Delegate::ICON_ERROR; |
| 109 } | 79 } |
| 110 icon_fetcher_.reset(); | 80 icon_fetcher_.reset(); |
| 111 BrowserThread::PostTask( | 81 |
| 112 BrowserThread::IO, | 82 ReportResultsIfComplete(); |
| 113 FROM_HERE, | |
| 114 base::Bind(&WebstoreInstallHelper::ReportResultsIfComplete, this)); | |
| 115 Release(); // Balanced in Start(). | 83 Release(); // Balanced in Start(). |
| 116 } | 84 } |
| 117 | 85 |
| 118 void WebstoreInstallHelper::OnJSONParseSucceeded( | 86 void WebstoreInstallHelper::OnJSONParseSucceeded( |
| 119 const base::ListValue& wrapper) { | 87 scoped_ptr<base::Value> result) { |
| 120 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 88 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 121 manifest_parse_complete_ = true; | 89 manifest_parse_complete_ = true; |
| 122 const base::Value* value = NULL; | 90 const base::DictionaryValue* value; |
| 123 CHECK(wrapper.Get(0, &value)); | 91 if (result->GetAsDictionary(&value)) |
| 124 if (value->IsType(base::Value::TYPE_DICTIONARY)) { | 92 parsed_manifest_.reset(value->DeepCopy()); |
| 125 parsed_manifest_.reset( | 93 else |
| 126 static_cast<const base::DictionaryValue*>(value)->DeepCopy()); | |
| 127 } else { | |
| 128 parse_error_ = Delegate::MANIFEST_ERROR; | 94 parse_error_ = Delegate::MANIFEST_ERROR; |
| 129 } | 95 |
| 130 ReportResultsIfComplete(); | 96 ReportResultsIfComplete(); |
| 131 } | 97 } |
| 132 | 98 |
| 133 void WebstoreInstallHelper::OnJSONParseFailed( | 99 void WebstoreInstallHelper::OnJSONParseFailed( |
| 134 const std::string& error_message) { | 100 const std::string& error_message) { |
| 135 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 101 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 136 manifest_parse_complete_ = true; | 102 manifest_parse_complete_ = true; |
| 137 error_ = error_message; | 103 error_ = error_message; |
| 138 parse_error_ = Delegate::MANIFEST_ERROR; | 104 parse_error_ = Delegate::MANIFEST_ERROR; |
| 139 ReportResultsIfComplete(); | 105 ReportResultsIfComplete(); |
| 140 } | 106 } |
| 141 | 107 |
| 142 void WebstoreInstallHelper::ReportResultsIfComplete() { | 108 void WebstoreInstallHelper::ReportResultsIfComplete() { |
| 143 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 109 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 144 | 110 |
| 145 if (!icon_decode_complete_ || !manifest_parse_complete_) | 111 if (!icon_decode_complete_ || !manifest_parse_complete_) |
| 146 return; | 112 return; |
| 147 | 113 |
| 148 // The utility_host_ will take care of deleting itself after this call. | |
| 149 if (utility_host_.get()) { | |
| 150 utility_host_->EndBatchMode(); | |
| 151 utility_host_.reset(); | |
| 152 } | |
| 153 | |
| 154 BrowserThread::PostTask( | |
| 155 BrowserThread::UI, | |
| 156 FROM_HERE, | |
| 157 base::Bind(&WebstoreInstallHelper::ReportResultFromUIThread, this)); | |
| 158 } | |
| 159 | |
| 160 void WebstoreInstallHelper::ReportResultFromUIThread() { | |
| 161 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 162 if (error_.empty() && parsed_manifest_) | 114 if (error_.empty() && parsed_manifest_) |
| 163 delegate_->OnWebstoreParseSuccess(id_, icon_, parsed_manifest_.release()); | 115 delegate_->OnWebstoreParseSuccess(id_, icon_, parsed_manifest_.release()); |
| 164 else | 116 else |
| 165 delegate_->OnWebstoreParseFailure(id_, parse_error_, error_); | 117 delegate_->OnWebstoreParseFailure(id_, parse_error_, error_); |
| 166 } | 118 } |
| 167 | 119 |
| 168 } // namespace extensions | 120 } // namespace extensions |
| OLD | NEW |