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

Side by Side Diff: chrome/browser/plugin_installer.cc

Issue 10910168: Separate plugin_metadata from plugin_installer, thread-safe plugin_finder (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: . Created 8 years, 3 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
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/plugin_installer.h" 5 #include "chrome/browser/plugin_installer.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/process.h" 10 #include "base/process.h"
11 #include "chrome/browser/download/download_service.h" 11 #include "chrome/browser/download/download_service.h"
12 #include "chrome/browser/download/download_service_factory.h" 12 #include "chrome/browser/download/download_service_factory.h"
13 #include "chrome/browser/download/download_util.h" 13 #include "chrome/browser/download/download_util.h"
14 #include "chrome/browser/platform_util.h" 14 #include "chrome/browser/platform_util.h"
15 #include "chrome/browser/plugin_installer_observer.h" 15 #include "chrome/browser/plugin_installer_observer.h"
16 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/tab_contents/tab_contents.h" 17 #include "chrome/browser/ui/tab_contents/tab_contents.h"
18 #include "content/public/browser/browser_context.h" 18 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/download_id.h" 19 #include "content/public/browser/download_id.h"
20 #include "content/public/browser/download_item.h" 20 #include "content/public/browser/download_item.h"
21 #include "content/public/browser/download_manager.h" 21 #include "content/public/browser/download_manager.h"
22 #include "content/public/browser/download_save_info.h" 22 #include "content/public/browser/download_save_info.h"
23 #include "content/public/browser/render_process_host.h" 23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/browser/render_view_host.h" 24 #include "content/public/browser/render_view_host.h"
25 #include "content/public/browser/resource_context.h" 25 #include "content/public/browser/resource_context.h"
26 #include "content/public/browser/resource_dispatcher_host.h" 26 #include "content/public/browser/resource_dispatcher_host.h"
27 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
28 #include "net/url_request/url_request.h" 28 #include "net/url_request/url_request.h"
29 #include "net/url_request/url_request_context.h" 29 #include "net/url_request/url_request_context.h"
30 #include "webkit/plugins/npapi/plugin_group.h"
31 #include "webkit/plugins/npapi/plugin_utils.h"
32 #include "webkit/plugins/webplugininfo.h"
33 30
34 using content::BrowserContext; 31 using content::BrowserContext;
35 using content::BrowserThread; 32 using content::BrowserThread;
36 using content::DownloadItem; 33 using content::DownloadItem;
37 using content::DownloadManager; 34 using content::DownloadManager;
38 using content::ResourceDispatcherHost; 35 using content::ResourceDispatcherHost;
39 36
40 namespace { 37 namespace {
41 38
42 void BeginDownload( 39 void BeginDownload(
(...skipping 19 matching lines...) Expand all
62 59
63 if (error != net::OK) { 60 if (error != net::OK) {
64 BrowserThread::PostTask( 61 BrowserThread::PostTask(
65 BrowserThread::UI, FROM_HERE, 62 BrowserThread::UI, FROM_HERE,
66 base::Bind(callback, content::DownloadId::Invalid(), error)); 63 base::Bind(callback, content::DownloadId::Invalid(), error));
67 } 64 }
68 } 65 }
69 66
70 } // namespace 67 } // namespace
71 68
72 PluginInstaller::PluginInstaller(const std::string& identifier, 69 PluginInstaller::PluginInstaller(PluginMetadata* plugin)
73 const string16& name, 70 : plugin_(plugin),
74 bool url_for_display,
75 const GURL& plugin_url,
76 const GURL& help_url,
77 const string16& group_name_matcher)
78 : identifier_(identifier),
79 name_(name),
80 group_name_matcher_(group_name_matcher),
81 url_for_display_(url_for_display),
82 plugin_url_(plugin_url),
83 help_url_(help_url),
84 state_(INSTALLER_STATE_IDLE) { 71 state_(INSTALLER_STATE_IDLE) {
72 DCHECK(plugin_);
85 } 73 }
86 74
87 PluginInstaller::~PluginInstaller() { 75 PluginInstaller::~PluginInstaller() {
88 } 76 }
89 77
90 void PluginInstaller::AddVersion(const Version& version,
91 SecurityStatus status) {
92 DCHECK(versions_.find(version) == versions_.end());
93 versions_[version] = status;
94 }
95
96 PluginInstaller::SecurityStatus PluginInstaller::GetSecurityStatus(
97 const webkit::WebPluginInfo& plugin) const {
98 // If there are no versions defined, the plug-in should require authorization.
99 if (versions_.empty())
100 return SECURITY_STATUS_REQUIRES_AUTHORIZATION;
101
102 Version version;
103 webkit::npapi::CreateVersionFromString(plugin.version, &version);
104 if (!version.IsValid())
105 version = Version("0");
106
107 // |lower_bound| returns the latest version that is not newer than |version|.
108 std::map<Version, SecurityStatus, VersionComparator>::const_iterator it =
109 versions_.lower_bound(version);
110 // If there is at least one version defined, everything older than the oldest
111 // defined version is considered out-of-date.
112 if (it == versions_.end())
113 return SECURITY_STATUS_OUT_OF_DATE;
114
115 return it->second;
116 }
117
118 bool PluginInstaller::VersionComparator::operator() (const Version& lhs,
119 const Version& rhs) const {
120 // Keep versions ordered by newest (biggest) first.
121 return lhs.CompareTo(rhs) > 0;
122 }
123
124 // static
125 bool PluginInstaller::ParseSecurityStatus(
126 const std::string& status_str,
127 PluginInstaller::SecurityStatus* status) {
128 if (status_str == "up_to_date")
129 *status = SECURITY_STATUS_UP_TO_DATE;
130 else if (status_str == "out_of_date")
131 *status = SECURITY_STATUS_OUT_OF_DATE;
132 else if (status_str == "requires_authorization")
133 *status = SECURITY_STATUS_REQUIRES_AUTHORIZATION;
134 else
135 return false;
136
137 return true;
138 }
139
140 void PluginInstaller::OnDownloadUpdated(DownloadItem* download) { 78 void PluginInstaller::OnDownloadUpdated(DownloadItem* download) {
141 DownloadItem::DownloadState state = download->GetState(); 79 DownloadItem::DownloadState state = download->GetState();
142 switch (state) { 80 switch (state) {
143 case DownloadItem::IN_PROGRESS: 81 case DownloadItem::IN_PROGRESS:
144 return; 82 return;
145 case DownloadItem::COMPLETE: { 83 case DownloadItem::COMPLETE: {
146 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); 84 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_);
147 state_ = INSTALLER_STATE_IDLE; 85 state_ = INSTALLER_STATE_IDLE;
148 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, 86 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_,
149 DownloadFinished()); 87 DownloadFinished());
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 weak_observers_.AddObserver(observer); 126 weak_observers_.AddObserver(observer);
189 } 127 }
190 128
191 void PluginInstaller::RemoveWeakObserver( 129 void PluginInstaller::RemoveWeakObserver(
192 WeakPluginInstallerObserver* observer) { 130 WeakPluginInstallerObserver* observer) {
193 weak_observers_.RemoveObserver(observer); 131 weak_observers_.RemoveObserver(observer);
194 } 132 }
195 133
196 void PluginInstaller::StartInstalling(TabContents* tab_contents) { 134 void PluginInstaller::StartInstalling(TabContents* tab_contents) {
197 DCHECK_EQ(INSTALLER_STATE_IDLE, state_); 135 DCHECK_EQ(INSTALLER_STATE_IDLE, state_);
198 DCHECK(!url_for_display_); 136 DCHECK(!plugin_->url_for_display());
199 state_ = INSTALLER_STATE_DOWNLOADING; 137 state_ = INSTALLER_STATE_DOWNLOADING;
200 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadStarted()); 138 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadStarted());
201 content::WebContents* web_contents = tab_contents->web_contents(); 139 content::WebContents* web_contents = tab_contents->web_contents();
202 DownloadManager* download_manager = 140 DownloadManager* download_manager =
203 BrowserContext::GetDownloadManager(tab_contents->profile()); 141 BrowserContext::GetDownloadManager(tab_contents->profile());
204 download_util::RecordDownloadSource( 142 download_util::RecordDownloadSource(
205 download_util::INITIATED_BY_PLUGIN_INSTALLER); 143 download_util::INITIATED_BY_PLUGIN_INSTALLER);
206 BrowserThread::PostTask( 144 BrowserThread::PostTask(
207 BrowserThread::IO, FROM_HERE, 145 BrowserThread::IO, FROM_HERE,
208 base::Bind(&BeginDownload, 146 base::Bind(&BeginDownload,
209 plugin_url_, 147 plugin_->plugin_url(),
210 tab_contents->profile()->GetResourceContext(), 148 tab_contents->profile()->GetResourceContext(),
211 web_contents->GetRenderProcessHost()->GetID(), 149 web_contents->GetRenderProcessHost()->GetID(),
212 web_contents->GetRenderViewHost()->GetRoutingID(), 150 web_contents->GetRenderViewHost()->GetRoutingID(),
213 base::Bind(&PluginInstaller::DownloadStarted, 151 base::Bind(&PluginInstaller::DownloadStarted,
214 base::Unretained(this), 152 base::Unretained(this),
215 make_scoped_refptr(download_manager)))); 153 make_scoped_refptr(download_manager))));
216 } 154 }
217 155
218 void PluginInstaller::DownloadStarted( 156 void PluginInstaller::DownloadStarted(
219 scoped_refptr<content::DownloadManager> dlm, 157 scoped_refptr<content::DownloadManager> dlm,
220 content::DownloadId download_id, 158 content::DownloadId download_id,
221 net::Error error) { 159 net::Error error) {
222 if (error != net::OK) { 160 if (error != net::OK) {
223 std::string msg = 161 std::string msg =
224 base::StringPrintf("Error %d: %s", error, net::ErrorToString(error)); 162 base::StringPrintf("Error %d: %s", error, net::ErrorToString(error));
225 DownloadError(msg); 163 DownloadError(msg);
226 return; 164 return;
227 } 165 }
228 DownloadItem* download_item = 166 DownloadItem* download_item =
229 dlm->GetActiveDownloadItem(download_id.local()); 167 dlm->GetActiveDownloadItem(download_id.local());
230 download_item->SetOpenWhenComplete(true); 168 download_item->SetOpenWhenComplete(true);
231 download_item->AddObserver(this); 169 download_item->AddObserver(this);
232 } 170 }
233 171
234 void PluginInstaller::OpenDownloadURL(content::WebContents* web_contents) { 172 void PluginInstaller::OpenDownloadURL(content::WebContents* web_contents) {
235 DCHECK_EQ(INSTALLER_STATE_IDLE, state_); 173 DCHECK_EQ(INSTALLER_STATE_IDLE, state_);
236 DCHECK(url_for_display_); 174 DCHECK(plugin_->url_for_display());
237 web_contents->OpenURL(content::OpenURLParams( 175 web_contents->OpenURL(content::OpenURLParams(
238 plugin_url_, 176 plugin_->plugin_url(),
239 content::Referrer(web_contents->GetURL(), 177 content::Referrer(web_contents->GetURL(),
240 WebKit::WebReferrerPolicyDefault), 178 WebKit::WebReferrerPolicyDefault),
241 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false)); 179 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false));
242 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished()); 180 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished());
243 } 181 }
244 182
245 void PluginInstaller::DownloadError(const std::string& msg) { 183 void PluginInstaller::DownloadError(const std::string& msg) {
246 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); 184 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_);
247 state_ = INSTALLER_STATE_IDLE; 185 state_ = INSTALLER_STATE_IDLE;
248 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg)); 186 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg));
249 } 187 }
250 188
251 void PluginInstaller::DownloadCancelled() { 189 void PluginInstaller::DownloadCancelled() {
252 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); 190 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_);
253 state_ = INSTALLER_STATE_IDLE; 191 state_ = INSTALLER_STATE_IDLE;
254 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled()); 192 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled());
255 } 193 }
256
257 bool PluginInstaller::MatchesPlugin(const webkit::WebPluginInfo& plugin) {
258 return plugin.name.find(group_name_matcher_) != string16::npos;
259 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698