| 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/plugins/plugin_installer.h" | 5 #include "chrome/browser/plugins/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/plugins/plugin_installer_observer.h" | 15 #include "chrome/browser/plugins/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  Loading... | 
| 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 (versions_.empty()) { |  | 
| 99 #if defined(OS_LINUX) |  | 
| 100     // On Linux, unknown plugins require authorization. |  | 
| 101     return SECURITY_STATUS_REQUIRES_AUTHORIZATION; |  | 
| 102 #else |  | 
| 103     return SECURITY_STATUS_UP_TO_DATE; |  | 
| 104 #endif |  | 
| 105   } |  | 
| 106 |  | 
| 107   Version version; |  | 
| 108   webkit::npapi::CreateVersionFromString(plugin.version, &version); |  | 
| 109   if (!version.IsValid()) |  | 
| 110     version = Version("0"); |  | 
| 111 |  | 
| 112   // |lower_bound| returns the latest version that is not newer than |version|. |  | 
| 113   std::map<Version, SecurityStatus, VersionComparator>::const_iterator it = |  | 
| 114       versions_.lower_bound(version); |  | 
| 115   // If there is at least one version defined, everything older than the oldest |  | 
| 116   // defined version is considered out-of-date. |  | 
| 117   if (it == versions_.end()) |  | 
| 118     return SECURITY_STATUS_OUT_OF_DATE; |  | 
| 119 |  | 
| 120   return it->second; |  | 
| 121 } |  | 
| 122 |  | 
| 123 bool PluginInstaller::VersionComparator::operator() (const Version& lhs, |  | 
| 124                                                      const Version& rhs) const { |  | 
| 125   // Keep versions ordered by newest (biggest) first. |  | 
| 126   return lhs.CompareTo(rhs) > 0; |  | 
| 127 } |  | 
| 128 |  | 
| 129 // static |  | 
| 130 bool PluginInstaller::ParseSecurityStatus( |  | 
| 131     const std::string& status_str, |  | 
| 132     PluginInstaller::SecurityStatus* status) { |  | 
| 133   if (status_str == "up_to_date") |  | 
| 134     *status = SECURITY_STATUS_UP_TO_DATE; |  | 
| 135   else if (status_str == "out_of_date") |  | 
| 136     *status = SECURITY_STATUS_OUT_OF_DATE; |  | 
| 137   else if (status_str == "requires_authorization") |  | 
| 138     *status = SECURITY_STATUS_REQUIRES_AUTHORIZATION; |  | 
| 139   else |  | 
| 140     return false; |  | 
| 141 |  | 
| 142   return true; |  | 
| 143 } |  | 
| 144 |  | 
| 145 void PluginInstaller::OnDownloadUpdated(DownloadItem* download) { | 78 void PluginInstaller::OnDownloadUpdated(DownloadItem* download) { | 
| 146   DownloadItem::DownloadState state = download->GetState(); | 79   DownloadItem::DownloadState state = download->GetState(); | 
| 147   switch (state) { | 80   switch (state) { | 
| 148     case DownloadItem::IN_PROGRESS: | 81     case DownloadItem::IN_PROGRESS: | 
| 149       return; | 82       return; | 
| 150     case DownloadItem::COMPLETE: { | 83     case DownloadItem::COMPLETE: { | 
| 151       DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); | 84       DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); | 
| 152       state_ = INSTALLER_STATE_IDLE; | 85       state_ = INSTALLER_STATE_IDLE; | 
| 153       FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, | 86       FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, | 
| 154                         DownloadFinished()); | 87                         DownloadFinished()); | 
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 193   weak_observers_.AddObserver(observer); | 126   weak_observers_.AddObserver(observer); | 
| 194 } | 127 } | 
| 195 | 128 | 
| 196 void PluginInstaller::RemoveWeakObserver( | 129 void PluginInstaller::RemoveWeakObserver( | 
| 197     WeakPluginInstallerObserver* observer) { | 130     WeakPluginInstallerObserver* observer) { | 
| 198   weak_observers_.RemoveObserver(observer); | 131   weak_observers_.RemoveObserver(observer); | 
| 199 } | 132 } | 
| 200 | 133 | 
| 201 void PluginInstaller::StartInstalling(TabContents* tab_contents) { | 134 void PluginInstaller::StartInstalling(TabContents* tab_contents) { | 
| 202   DCHECK_EQ(INSTALLER_STATE_IDLE, state_); | 135   DCHECK_EQ(INSTALLER_STATE_IDLE, state_); | 
| 203   DCHECK(!url_for_display_); | 136   DCHECK(!plugin_->url_for_display()); | 
| 204   state_ = INSTALLER_STATE_DOWNLOADING; | 137   state_ = INSTALLER_STATE_DOWNLOADING; | 
| 205   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadStarted()); | 138   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadStarted()); | 
| 206   content::WebContents* web_contents = tab_contents->web_contents(); | 139   content::WebContents* web_contents = tab_contents->web_contents(); | 
| 207   DownloadManager* download_manager = | 140   DownloadManager* download_manager = | 
| 208       BrowserContext::GetDownloadManager(tab_contents->profile()); | 141       BrowserContext::GetDownloadManager(tab_contents->profile()); | 
| 209   download_util::RecordDownloadSource( | 142   download_util::RecordDownloadSource( | 
| 210       download_util::INITIATED_BY_PLUGIN_INSTALLER); | 143       download_util::INITIATED_BY_PLUGIN_INSTALLER); | 
| 211   BrowserThread::PostTask( | 144   BrowserThread::PostTask( | 
| 212       BrowserThread::IO, FROM_HERE, | 145       BrowserThread::IO, FROM_HERE, | 
| 213       base::Bind(&BeginDownload, | 146       base::Bind(&BeginDownload, | 
| 214                  plugin_url_, | 147                  plugin_->plugin_url(), | 
| 215                  tab_contents->profile()->GetResourceContext(), | 148                  tab_contents->profile()->GetResourceContext(), | 
| 216                  web_contents->GetRenderProcessHost()->GetID(), | 149                  web_contents->GetRenderProcessHost()->GetID(), | 
| 217                  web_contents->GetRenderViewHost()->GetRoutingID(), | 150                  web_contents->GetRenderViewHost()->GetRoutingID(), | 
| 218                  base::Bind(&PluginInstaller::DownloadStarted, | 151                  base::Bind(&PluginInstaller::DownloadStarted, | 
| 219                             base::Unretained(this), | 152                             base::Unretained(this), | 
| 220                             make_scoped_refptr(download_manager)))); | 153                             make_scoped_refptr(download_manager)))); | 
| 221 } | 154 } | 
| 222 | 155 | 
| 223 void PluginInstaller::DownloadStarted( | 156 void PluginInstaller::DownloadStarted( | 
| 224     scoped_refptr<content::DownloadManager> dlm, | 157     scoped_refptr<content::DownloadManager> dlm, | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
| 236   if (!download_item) { | 169   if (!download_item) { | 
| 237     DownloadError("Download not found"); | 170     DownloadError("Download not found"); | 
| 238     return; | 171     return; | 
| 239   } | 172   } | 
| 240   download_item->SetOpenWhenComplete(true); | 173   download_item->SetOpenWhenComplete(true); | 
| 241   download_item->AddObserver(this); | 174   download_item->AddObserver(this); | 
| 242 } | 175 } | 
| 243 | 176 | 
| 244 void PluginInstaller::OpenDownloadURL(content::WebContents* web_contents) { | 177 void PluginInstaller::OpenDownloadURL(content::WebContents* web_contents) { | 
| 245   DCHECK_EQ(INSTALLER_STATE_IDLE, state_); | 178   DCHECK_EQ(INSTALLER_STATE_IDLE, state_); | 
| 246   DCHECK(url_for_display_); | 179   DCHECK(plugin_->url_for_display()); | 
| 247   web_contents->OpenURL(content::OpenURLParams( | 180   web_contents->OpenURL(content::OpenURLParams( | 
| 248       plugin_url_, | 181       plugin_->plugin_url(), | 
| 249       content::Referrer(web_contents->GetURL(), | 182       content::Referrer(web_contents->GetURL(), | 
| 250                         WebKit::WebReferrerPolicyDefault), | 183                         WebKit::WebReferrerPolicyDefault), | 
| 251       NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false)); | 184       NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false)); | 
| 252   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished()); | 185   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished()); | 
| 253 } | 186 } | 
| 254 | 187 | 
| 255 void PluginInstaller::DownloadError(const std::string& msg) { | 188 void PluginInstaller::DownloadError(const std::string& msg) { | 
| 256   DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); | 189   DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); | 
| 257   state_ = INSTALLER_STATE_IDLE; | 190   state_ = INSTALLER_STATE_IDLE; | 
| 258   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg)); | 191   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg)); | 
| 259 } | 192 } | 
| 260 | 193 | 
| 261 void PluginInstaller::DownloadCancelled() { | 194 void PluginInstaller::DownloadCancelled() { | 
| 262   DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); | 195   DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); | 
| 263   state_ = INSTALLER_STATE_IDLE; | 196   state_ = INSTALLER_STATE_IDLE; | 
| 264   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled()); | 197   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled()); | 
| 265 } | 198 } | 
| 266 |  | 
| 267 bool PluginInstaller::MatchesPlugin(const webkit::WebPluginInfo& plugin) { |  | 
| 268   return plugin.name.find(group_name_matcher_) != string16::npos; |  | 
| 269 } |  | 
| OLD | NEW | 
|---|