| 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/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_wrapper.h" | 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.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_dispatcher_host.h" | 25 #include "content/public/browser/resource_dispatcher_host.h" |
| 26 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 27 #include "net/url_request/url_request.h" | 27 #include "net/url_request/url_request.h" |
| 28 #include "webkit/plugins/npapi/plugin_group.h" |
| 28 | 29 |
| 29 using content::BrowserThread; | 30 using content::BrowserThread; |
| 30 using content::DownloadItem; | 31 using content::DownloadItem; |
| 31 using content::ResourceDispatcherHost; | 32 using content::ResourceDispatcherHost; |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 void BeginDownload( | 36 void BeginDownload( |
| 36 const GURL& url, | 37 const GURL& url, |
| 37 content::ResourceContext* resource_context, | 38 content::ResourceContext* resource_context, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 52 content::DownloadSaveInfo(), | 53 content::DownloadSaveInfo(), |
| 53 callback); | 54 callback); |
| 54 | 55 |
| 55 if (error != net::OK) { | 56 if (error != net::OK) { |
| 56 BrowserThread::PostTask( | 57 BrowserThread::PostTask( |
| 57 BrowserThread::UI, FROM_HERE, | 58 BrowserThread::UI, FROM_HERE, |
| 58 base::Bind(callback, content::DownloadId::Invalid(), error)); | 59 base::Bind(callback, content::DownloadId::Invalid(), error)); |
| 59 } | 60 } |
| 60 } | 61 } |
| 61 | 62 |
| 62 } | 63 } // namespace |
| 63 | 64 |
| 64 PluginInstaller::PluginInstaller(const std::string& identifier, | 65 PluginInstaller::PluginInstaller(const std::string& identifier, |
| 65 const GURL& plugin_url, | |
| 66 const GURL& help_url, | |
| 67 const string16& name, | 66 const string16& name, |
| 68 bool url_for_display, | 67 bool url_for_display, |
| 69 bool requires_authorization) | 68 const GURL& plugin_url, |
| 70 : state_(kStateIdle), | 69 const GURL& help_url) |
| 71 identifier_(identifier), | 70 : identifier_(identifier), |
| 71 name_(name), |
| 72 url_for_display_(url_for_display), |
| 72 plugin_url_(plugin_url), | 73 plugin_url_(plugin_url), |
| 73 help_url_(help_url), | 74 help_url_(help_url), |
| 74 name_(name), | 75 state_(INSTALLER_STATE_IDLE) { |
| 75 url_for_display_(url_for_display), | |
| 76 requires_authorization_(requires_authorization) { | |
| 77 } | 76 } |
| 78 | 77 |
| 79 PluginInstaller::~PluginInstaller() { | 78 PluginInstaller::~PluginInstaller() { |
| 80 } | 79 } |
| 81 | 80 |
| 81 void PluginInstaller::AddVersion(const Version& version, |
| 82 SecurityStatus status) { |
| 83 DCHECK(versions_.find(version) == versions_.end()); |
| 84 versions_[version] = status; |
| 85 } |
| 86 |
| 87 PluginInstaller::SecurityStatus PluginInstaller::GetSecurityStatus( |
| 88 const webkit::WebPluginInfo& plugin) const { |
| 89 // If there are no versions defined, the plug-in is alway considered |
| 90 // up-to-date. |
| 91 if (versions_.empty()) |
| 92 return SECURITY_STATUS_UP_TO_DATE; |
| 93 |
| 94 scoped_ptr<Version> version( |
| 95 webkit::npapi::PluginGroup::CreateVersionFromString(plugin.version)); |
| 96 if (!version.get()) |
| 97 version.reset(new Version("0")); |
| 98 |
| 99 // |lower_bound| returns the latest version that is not newer than |version|. |
| 100 std::map<Version, SecurityStatus, VersionComparator>::const_iterator it = |
| 101 versions_.lower_bound(*version); |
| 102 // If there is at least one version defined, everything older than the oldest |
| 103 // defined version is considered out-of-date. |
| 104 if (it == versions_.end()) |
| 105 return SECURITY_STATUS_OUT_OF_DATE; |
| 106 |
| 107 return it->second; |
| 108 } |
| 109 |
| 110 bool PluginInstaller::VersionComparator::operator() (const Version& lhs, |
| 111 const Version& rhs) const { |
| 112 // Keep versions ordered by newest (biggest) first. |
| 113 return lhs.CompareTo(rhs) > 0; |
| 114 } |
| 115 |
| 116 // static |
| 117 bool PluginInstaller::ParseSecurityStatus( |
| 118 const std::string& status_str, |
| 119 PluginInstaller::SecurityStatus* status) { |
| 120 if (status_str == "up_to_date") |
| 121 *status = SECURITY_STATUS_UP_TO_DATE; |
| 122 else if (status_str == "out_of_date") |
| 123 *status = SECURITY_STATUS_OUT_OF_DATE; |
| 124 else if (status_str == "requires_authorization") |
| 125 *status = SECURITY_STATUS_REQUIRES_AUTHORIZATION; |
| 126 else |
| 127 return false; |
| 128 |
| 129 return true; |
| 130 } |
| 131 |
| 82 void PluginInstaller::OnDownloadUpdated(DownloadItem* download) { | 132 void PluginInstaller::OnDownloadUpdated(DownloadItem* download) { |
| 83 DownloadItem::DownloadState state = download->GetState(); | 133 DownloadItem::DownloadState state = download->GetState(); |
| 84 switch (state) { | 134 switch (state) { |
| 85 case DownloadItem::IN_PROGRESS: | 135 case DownloadItem::IN_PROGRESS: |
| 86 return; | 136 return; |
| 87 case DownloadItem::COMPLETE: { | 137 case DownloadItem::COMPLETE: { |
| 88 DCHECK_EQ(kStateDownloading, state_); | 138 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); |
| 89 state_ = kStateIdle; | 139 state_ = INSTALLER_STATE_IDLE; |
| 90 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, | 140 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, |
| 91 DownloadFinished()); | 141 DownloadFinished()); |
| 92 break; | 142 break; |
| 93 } | 143 } |
| 94 case DownloadItem::CANCELLED: { | 144 case DownloadItem::CANCELLED: { |
| 95 DownloadCancelled(); | 145 DownloadCancelled(); |
| 96 break; | 146 break; |
| 97 } | 147 } |
| 98 case DownloadItem::REMOVING: { | 148 case DownloadItem::REMOVING: { |
| 99 DCHECK_EQ(kStateDownloading, state_); | 149 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); |
| 100 state_ = kStateIdle; | 150 state_ = INSTALLER_STATE_IDLE; |
| 101 break; | 151 break; |
| 102 } | 152 } |
| 103 case DownloadItem::INTERRUPTED: { | 153 case DownloadItem::INTERRUPTED: { |
| 104 content::DownloadInterruptReason reason = download->GetLastReason(); | 154 content::DownloadInterruptReason reason = download->GetLastReason(); |
| 105 DownloadError(content::InterruptReasonDebugString(reason)); | 155 DownloadError(content::InterruptReasonDebugString(reason)); |
| 106 break; | 156 break; |
| 107 } | 157 } |
| 108 case DownloadItem::MAX_DOWNLOAD_STATE: { | 158 case DownloadItem::MAX_DOWNLOAD_STATE: { |
| 109 NOTREACHED(); | 159 NOTREACHED(); |
| 110 return; | 160 return; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 131 void PluginInstaller::AddWeakObserver(WeakPluginInstallerObserver* observer) { | 181 void PluginInstaller::AddWeakObserver(WeakPluginInstallerObserver* observer) { |
| 132 weak_observers_.AddObserver(observer); | 182 weak_observers_.AddObserver(observer); |
| 133 } | 183 } |
| 134 | 184 |
| 135 void PluginInstaller::RemoveWeakObserver( | 185 void PluginInstaller::RemoveWeakObserver( |
| 136 WeakPluginInstallerObserver* observer) { | 186 WeakPluginInstallerObserver* observer) { |
| 137 weak_observers_.RemoveObserver(observer); | 187 weak_observers_.RemoveObserver(observer); |
| 138 } | 188 } |
| 139 | 189 |
| 140 void PluginInstaller::StartInstalling(TabContentsWrapper* wrapper) { | 190 void PluginInstaller::StartInstalling(TabContentsWrapper* wrapper) { |
| 141 DCHECK(state_ == kStateIdle); | 191 DCHECK_EQ(INSTALLER_STATE_IDLE, state_); |
| 142 DCHECK(!url_for_display_); | 192 DCHECK(!url_for_display_); |
| 143 state_ = kStateDownloading; | 193 state_ = INSTALLER_STATE_DOWNLOADING; |
| 144 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadStarted()); | 194 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadStarted()); |
| 145 content::WebContents* web_contents = wrapper->web_contents(); | 195 content::WebContents* web_contents = wrapper->web_contents(); |
| 146 DownloadService* download_service = | 196 DownloadService* download_service = |
| 147 DownloadServiceFactory::GetForProfile(wrapper->profile()); | 197 DownloadServiceFactory::GetForProfile(wrapper->profile()); |
| 148 download_util::RecordDownloadSource( | 198 download_util::RecordDownloadSource( |
| 149 download_util::INITIATED_BY_PLUGIN_INSTALLER); | 199 download_util::INITIATED_BY_PLUGIN_INSTALLER); |
| 150 BrowserThread::PostTask( | 200 BrowserThread::PostTask( |
| 151 BrowserThread::IO, FROM_HERE, | 201 BrowserThread::IO, FROM_HERE, |
| 152 base::Bind(&BeginDownload, | 202 base::Bind(&BeginDownload, |
| 153 plugin_url_, | 203 plugin_url_, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 170 DownloadError(msg); | 220 DownloadError(msg); |
| 171 return; | 221 return; |
| 172 } | 222 } |
| 173 DownloadItem* download_item = | 223 DownloadItem* download_item = |
| 174 dlm->GetActiveDownloadItem(download_id.local()); | 224 dlm->GetActiveDownloadItem(download_id.local()); |
| 175 download_item->SetOpenWhenComplete(true); | 225 download_item->SetOpenWhenComplete(true); |
| 176 download_item->AddObserver(this); | 226 download_item->AddObserver(this); |
| 177 } | 227 } |
| 178 | 228 |
| 179 void PluginInstaller::OpenDownloadURL(content::WebContents* web_contents) { | 229 void PluginInstaller::OpenDownloadURL(content::WebContents* web_contents) { |
| 180 DCHECK(state_ == kStateIdle); | 230 DCHECK_EQ(INSTALLER_STATE_IDLE, state_); |
| 181 DCHECK(url_for_display_); | 231 DCHECK(url_for_display_); |
| 182 web_contents->OpenURL(content::OpenURLParams( | 232 web_contents->OpenURL(content::OpenURLParams( |
| 183 plugin_url_, | 233 plugin_url_, |
| 184 content::Referrer(web_contents->GetURL(), | 234 content::Referrer(web_contents->GetURL(), |
| 185 WebKit::WebReferrerPolicyDefault), | 235 WebKit::WebReferrerPolicyDefault), |
| 186 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false)); | 236 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, false)); |
| 187 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished()); | 237 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadFinished()); |
| 188 } | 238 } |
| 189 | 239 |
| 190 void PluginInstaller::DownloadError(const std::string& msg) { | 240 void PluginInstaller::DownloadError(const std::string& msg) { |
| 191 DCHECK(state_ == kStateDownloading); | 241 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); |
| 192 state_ = kStateIdle; | 242 state_ = INSTALLER_STATE_IDLE; |
| 193 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg)); | 243 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadError(msg)); |
| 194 } | 244 } |
| 195 | 245 |
| 196 void PluginInstaller::DownloadCancelled() { | 246 void PluginInstaller::DownloadCancelled() { |
| 197 DCHECK(state_ == kStateDownloading); | 247 DCHECK_EQ(INSTALLER_STATE_DOWNLOADING, state_); |
| 198 state_ = kStateIdle; | 248 state_ = INSTALLER_STATE_IDLE; |
| 199 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled()); | 249 FOR_EACH_OBSERVER(PluginInstallerObserver, observers_, DownloadCancelled()); |
| 200 } | 250 } |
| OLD | NEW |