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