Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: chrome/browser/extensions/webstore_install_helper.cc

Issue 1153143002: Fix race condition in WebstoreInstallHelper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix memleak Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/extensions/webstore_install_helper.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // No existing |json_parser_| to avoid unbalanced AddRef().
47 CHECK(!json_parser_.get());
48 AddRef(); // Balanced in OnJSONParseSucceeded()/OnJSONParseFailed().
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 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
54 if (icon_url_.is_empty()) { 60 if (icon_url_.is_empty()) {
55 icon_decode_complete_ = true; 61 icon_decode_complete_ = true;
56 } else { 62 } else {
57 // No existing |icon_fetcher_| to avoid unbalanced AddRef(). 63 // No existing |icon_fetcher_| to avoid unbalanced AddRef().
58 CHECK(!icon_fetcher_.get()); 64 CHECK(!icon_fetcher_.get());
59 AddRef(); // Balanced in OnFetchComplete(). 65 AddRef(); // Balanced in OnFetchComplete().
60 icon_fetcher_.reset(new chrome::BitmapFetcher(icon_url_, this)); 66 icon_fetcher_.reset(new chrome::BitmapFetcher(icon_url_, this));
61 icon_fetcher_->Start( 67 icon_fetcher_->Start(
62 context_getter_, std::string(), 68 context_getter_, std::string(),
63 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, 69 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
64 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES); 70 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES);
65 } 71 }
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 } 72 }
95 73
96 void WebstoreInstallHelper::OnFetchComplete(const GURL& url, 74 void WebstoreInstallHelper::OnFetchComplete(const GURL& url,
97 const SkBitmap* image) { 75 const SkBitmap* image) {
98 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 76 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
99 // OnFetchComplete should only be called as icon_fetcher_ delegate to avoid 77 // OnFetchComplete should only be called as icon_fetcher_ delegate to avoid
100 // unbalanced Release(). 78 // unbalanced Release().
101 CHECK(icon_fetcher_.get()); 79 CHECK(icon_fetcher_.get());
102 80
103 if (image) 81 if (image)
104 icon_ = *image; 82 icon_ = *image;
105 icon_decode_complete_ = true; 83 icon_decode_complete_ = true;
106 if (icon_.empty()) { 84 if (icon_.empty()) {
107 error_ = kImageDecodeError; 85 error_ = kImageDecodeError;
108 parse_error_ = Delegate::ICON_ERROR; 86 parse_error_ = Delegate::ICON_ERROR;
109 } 87 }
110 icon_fetcher_.reset(); 88 icon_fetcher_.reset();
111 BrowserThread::PostTask( 89
112 BrowserThread::IO, 90 ReportResultsIfComplete();
113 FROM_HERE,
114 base::Bind(&WebstoreInstallHelper::ReportResultsIfComplete, this));
115 Release(); // Balanced in Start(). 91 Release(); // Balanced in Start().
116 } 92 }
117 93
118 void WebstoreInstallHelper::OnJSONParseSucceeded( 94 void WebstoreInstallHelper::OnJSONParseSucceeded(
119 const base::ListValue& wrapper) { 95 scoped_ptr<base::Value> result) {
120 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 96 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
121 manifest_parse_complete_ = true; 97 manifest_parse_complete_ = true;
122 const base::Value* value = NULL; 98 const base::DictionaryValue* value;
123 CHECK(wrapper.Get(0, &value)); 99 if (result->GetAsDictionary(&value))
124 if (value->IsType(base::Value::TYPE_DICTIONARY)) { 100 parsed_manifest_.reset(value->DeepCopy());
125 parsed_manifest_.reset( 101 else
126 static_cast<const base::DictionaryValue*>(value)->DeepCopy());
127 } else {
128 parse_error_ = Delegate::MANIFEST_ERROR; 102 parse_error_ = Delegate::MANIFEST_ERROR;
129 } 103
130 ReportResultsIfComplete(); 104 ReportResultsIfComplete();
105 Release(); // Balanced in Start().
131 } 106 }
132 107
133 void WebstoreInstallHelper::OnJSONParseFailed( 108 void WebstoreInstallHelper::OnJSONParseFailed(
134 const std::string& error_message) { 109 const std::string& error_message) {
135 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 110 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
136 manifest_parse_complete_ = true; 111 manifest_parse_complete_ = true;
137 error_ = error_message; 112 error_ = error_message;
138 parse_error_ = Delegate::MANIFEST_ERROR; 113 parse_error_ = Delegate::MANIFEST_ERROR;
139 ReportResultsIfComplete(); 114 ReportResultsIfComplete();
115 Release(); // Balanced in Start().
140 } 116 }
141 117
142 void WebstoreInstallHelper::ReportResultsIfComplete() { 118 void WebstoreInstallHelper::ReportResultsIfComplete() {
143 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 119 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
144 120
145 if (!icon_decode_complete_ || !manifest_parse_complete_) 121 if (!icon_decode_complete_ || !manifest_parse_complete_)
146 return; 122 return;
147 123
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_) 124 if (error_.empty() && parsed_manifest_)
163 delegate_->OnWebstoreParseSuccess(id_, icon_, parsed_manifest_.release()); 125 delegate_->OnWebstoreParseSuccess(id_, icon_, parsed_manifest_.release());
164 else 126 else
165 delegate_->OnWebstoreParseFailure(id_, parse_error_, error_); 127 delegate_->OnWebstoreParseFailure(id_, parse_error_, error_);
166 } 128 }
167 129
168 } // namespace extensions 130 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/webstore_install_helper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698