| 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, | 
|  225     content::DownloadId download_id, |  158     content::DownloadId download_id, | 
|  226     net::Error error) { |  159     net::Error error) { | 
|  227   if (error != net::OK) { |  160   if (error != net::OK) { | 
|  228     std::string msg = |  161     std::string msg = | 
|  229         base::StringPrintf("Error %d: %s", error, net::ErrorToString(error)); |  162         base::StringPrintf("Error %d: %s", error, net::ErrorToString(error)); | 
|  230     DownloadError(msg); |  163     DownloadError(msg); | 
|  231     return; |  164     return; | 
|  232   } |  165   } | 
|  233   DownloadItem* download_item = |  166   DownloadItem* download_item = | 
|  234       dlm->GetActiveDownloadItem(download_id.local()); |  167       dlm->GetActiveDownloadItem(download_id.local()); | 
|  235   download_item->SetOpenWhenComplete(true); |  168   download_item->SetOpenWhenComplete(true); | 
|  236   download_item->AddObserver(this); |  169   download_item->AddObserver(this); | 
|  237 } |  170 } | 
|  238  |  171  | 
|  239 void PluginInstaller::OpenDownloadURL(content::WebContents* web_contents) { |  172 void PluginInstaller::OpenDownloadURL(content::WebContents* web_contents) { | 
|  240   DCHECK_EQ(INSTALLER_STATE_IDLE, state_); |  173   DCHECK_EQ(INSTALLER_STATE_IDLE, state_); | 
|  241   DCHECK(url_for_display_); |  174   DCHECK(plugin_->url_for_display()); | 
|  242   web_contents->OpenURL(content::OpenURLParams( |  175   web_contents->OpenURL(content::OpenURLParams( | 
|  243       plugin_url_, |  176       plugin_->plugin_url(), | 
|  244       content::Referrer(web_contents->GetURL(), |  177       content::Referrer(web_contents->GetURL(), | 
|  245                         WebKit::WebReferrerPolicyDefault), |  178                         WebKit::WebReferrerPolicyDefault), | 
|  246       NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false)); |  179       NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false)); | 
|  247   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished()); |  180   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished()); | 
|  248 } |  181 } | 
|  249  |  182  | 
|  250 void PluginInstaller::DownloadError(const std::string& msg) { |  183 void PluginInstaller::DownloadError(const std::string& msg) { | 
|  251   DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); |  184   DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); | 
|  252   state_ = INSTALLER_STATE_IDLE; |  185   state_ = INSTALLER_STATE_IDLE; | 
|  253   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg)); |  186   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg)); | 
|  254 } |  187 } | 
|  255  |  188  | 
|  256 void PluginInstaller::DownloadCancelled() { |  189 void PluginInstaller::DownloadCancelled() { | 
|  257   DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); |  190   DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); | 
|  258   state_ = INSTALLER_STATE_IDLE; |  191   state_ = INSTALLER_STATE_IDLE; | 
|  259   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled()); |  192   FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled()); | 
|  260 } |  193 } | 
|  261  |  | 
|  262 bool PluginInstaller::MatchesPlugin(const webkit::WebPluginInfo& plugin) { |  | 
|  263   return plugin.name.find(group_name_matcher_) != string16::npos; |  | 
|  264 } |  | 
| OLD | NEW |