| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" | 9 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" |
| 10 #include "components/safe_json/safe_json_parser.h" | 10 #include "components/safe_json/safe_json_parser.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 WebstoreInstallHelper::WebstoreInstallHelper( | 25 WebstoreInstallHelper::WebstoreInstallHelper( |
| 26 Delegate* delegate, | 26 Delegate* delegate, |
| 27 const std::string& id, | 27 const std::string& id, |
| 28 const std::string& manifest, | 28 const std::string& manifest, |
| 29 const GURL& icon_url, | 29 const GURL& icon_url, |
| 30 net::URLRequestContextGetter* context_getter) | 30 net::URLRequestContextGetter* context_getter) |
| 31 : delegate_(delegate), | 31 : delegate_(delegate), |
| 32 id_(id), | 32 id_(id), |
| 33 manifest_(manifest), | 33 manifest_(manifest), |
| 34 json_parser_(nullptr), |
| 34 icon_url_(icon_url), | 35 icon_url_(icon_url), |
| 35 context_getter_(context_getter), | 36 context_getter_(context_getter), |
| 36 icon_decode_complete_(false), | 37 icon_decode_complete_(false), |
| 37 manifest_parse_complete_(false), | 38 manifest_parse_complete_(false), |
| 38 parse_error_(Delegate::UNKNOWN_ERROR) { | 39 parse_error_(Delegate::UNKNOWN_ERROR) { |
| 39 } | 40 } |
| 40 | 41 |
| 41 WebstoreInstallHelper::~WebstoreInstallHelper() {} | 42 WebstoreInstallHelper::~WebstoreInstallHelper() {} |
| 42 | 43 |
| 43 void WebstoreInstallHelper::Start() { | 44 void WebstoreInstallHelper::Start() { |
| 44 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 45 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 45 | 46 |
| 46 // No existing |json_parser_| to avoid unbalanced AddRef(). | 47 CHECK(!json_parser_); |
| 47 CHECK(!json_parser_.get()); | 48 |
| 48 AddRef(); // Balanced in OnJSONParseSucceeded()/OnJSONParseFailed(). | 49 json_parser_ = safe_json::SafeJsonParser::Create( |
| 49 // Use base::Unretained so that base::Bind won't AddRef() on us. Otherwise, | 50 manifest_, base::Bind(&WebstoreInstallHelper::OnJSONParseSucceeded, this), |
| 50 // we'll have two callbacks holding references to us, only one of which will | 51 base::Bind(&WebstoreInstallHelper::OnJSONParseFailed, this)); |
| 51 // ever be called. | |
| 52 json_parser_ = new safe_json::SafeJsonParser( | |
| 53 manifest_, | |
| 54 base::Bind(&WebstoreInstallHelper::OnJSONParseSucceeded, | |
| 55 base::Unretained(this)), | |
| 56 base::Bind(&WebstoreInstallHelper::OnJSONParseFailed, | |
| 57 base::Unretained(this))); | |
| 58 json_parser_->Start(); | 52 json_parser_->Start(); |
| 59 | 53 |
| 60 if (icon_url_.is_empty()) { | 54 if (icon_url_.is_empty()) { |
| 61 icon_decode_complete_ = true; | 55 icon_decode_complete_ = true; |
| 62 } else { | 56 } else { |
| 63 // No existing |icon_fetcher_| to avoid unbalanced AddRef(). | 57 // No existing |icon_fetcher_| to avoid unbalanced AddRef(). |
| 64 CHECK(!icon_fetcher_.get()); | 58 CHECK(!icon_fetcher_.get()); |
| 65 AddRef(); // Balanced in OnFetchComplete(). | 59 AddRef(); // Balanced in OnFetchComplete(). |
| 66 icon_fetcher_.reset(new chrome::BitmapFetcher(icon_url_, this)); | 60 icon_fetcher_.reset(new chrome::BitmapFetcher(icon_url_, this)); |
| 67 icon_fetcher_->Init( | 61 icon_fetcher_->Init( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 96 scoped_ptr<base::Value> result) { | 90 scoped_ptr<base::Value> result) { |
| 97 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 91 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 98 manifest_parse_complete_ = true; | 92 manifest_parse_complete_ = true; |
| 99 const base::DictionaryValue* value; | 93 const base::DictionaryValue* value; |
| 100 if (result->GetAsDictionary(&value)) | 94 if (result->GetAsDictionary(&value)) |
| 101 parsed_manifest_.reset(value->DeepCopy()); | 95 parsed_manifest_.reset(value->DeepCopy()); |
| 102 else | 96 else |
| 103 parse_error_ = Delegate::MANIFEST_ERROR; | 97 parse_error_ = Delegate::MANIFEST_ERROR; |
| 104 | 98 |
| 105 ReportResultsIfComplete(); | 99 ReportResultsIfComplete(); |
| 106 Release(); // Balanced in Start(). | |
| 107 } | 100 } |
| 108 | 101 |
| 109 void WebstoreInstallHelper::OnJSONParseFailed( | 102 void WebstoreInstallHelper::OnJSONParseFailed( |
| 110 const std::string& error_message) { | 103 const std::string& error_message) { |
| 111 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 104 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 112 manifest_parse_complete_ = true; | 105 manifest_parse_complete_ = true; |
| 113 error_ = error_message; | 106 error_ = error_message; |
| 114 parse_error_ = Delegate::MANIFEST_ERROR; | 107 parse_error_ = Delegate::MANIFEST_ERROR; |
| 115 ReportResultsIfComplete(); | 108 ReportResultsIfComplete(); |
| 116 Release(); // Balanced in Start(). | |
| 117 } | 109 } |
| 118 | 110 |
| 119 void WebstoreInstallHelper::ReportResultsIfComplete() { | 111 void WebstoreInstallHelper::ReportResultsIfComplete() { |
| 120 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 112 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 121 | 113 |
| 122 if (!icon_decode_complete_ || !manifest_parse_complete_) | 114 if (!icon_decode_complete_ || !manifest_parse_complete_) |
| 123 return; | 115 return; |
| 124 | 116 |
| 125 if (error_.empty() && parsed_manifest_) | 117 if (error_.empty() && parsed_manifest_) |
| 126 delegate_->OnWebstoreParseSuccess(id_, icon_, parsed_manifest_.release()); | 118 delegate_->OnWebstoreParseSuccess(id_, icon_, parsed_manifest_.release()); |
| 127 else | 119 else |
| 128 delegate_->OnWebstoreParseFailure(id_, parse_error_, error_); | 120 delegate_->OnWebstoreParseFailure(id_, parse_error_, error_); |
| 129 } | 121 } |
| 130 | 122 |
| 131 } // namespace extensions | 123 } // namespace extensions |
| OLD | NEW |