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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/webstore_installer.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/files/file_util.h"
14 #include "base/metrics/field_trial.h"
15 #include "base/metrics/histogram.h"
16 #include "base/metrics/sparse_histogram.h"
17 #include "base/path_service.h"
18 #include "base/rand_util.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/time/time.h"
24 #include "chrome/browser/chrome_notification_types.h"
25 #include "chrome/browser/download/download_crx_util.h"
26 #include "chrome/browser/download/download_prefs.h"
27 #include "chrome/browser/download/download_stats.h"
28 #include "chrome/browser/extensions/crx_installer.h"
29 #include "chrome/browser/extensions/install_tracker.h"
30 #include "chrome/browser/extensions/install_tracker_factory.h"
31 #include "chrome/browser/extensions/install_verifier.h"
32 #include "chrome/browser/extensions/shared_module_service.h"
33 #include "chrome/browser/profiles/profile.h"
34 #include "chrome/common/chrome_paths.h"
35 #include "chrome/common/chrome_switches.h"
36 #include "components/crx_file/id_util.h"
37 #include "components/update_client/update_query_params.h"
38 #include "content/public/browser/browser_thread.h"
39 #include "content/public/browser/download_manager.h"
40 #include "content/public/browser/download_save_info.h"
41 #include "content/public/browser/download_url_parameters.h"
42 #include "content/public/browser/navigation_controller.h"
43 #include "content/public/browser/navigation_entry.h"
44 #include "content/public/browser/notification_details.h"
45 #include "content/public/browser/notification_service.h"
46 #include "content/public/browser/notification_source.h"
47 #include "content/public/browser/render_process_host.h"
48 #include "content/public/browser/render_view_host.h"
49 #include "content/public/browser/web_contents.h"
50 #include "extensions/browser/extension_registry.h"
51 #include "extensions/browser/extension_system.h"
52 #include "extensions/browser/install/crx_install_error.h"
53 #include "extensions/common/extension.h"
54 #include "extensions/common/extension_urls.h"
55 #include "extensions/common/manifest_constants.h"
56 #include "extensions/common/manifest_handlers/shared_module_info.h"
57 #include "net/base/escape.h"
60 #if defined(OS_CHROMEOS)
61 #include "chrome/browser/chromeos/drive/file_system_util.h"
64 using content::BrowserContext
;
65 using content::BrowserThread
;
66 using content::DownloadItem
;
67 using content::DownloadManager
;
68 using content::NavigationController
;
69 using content::DownloadUrlParameters
;
73 // Key used to attach the Approval to the DownloadItem.
74 const char kApprovalKey
[] = "extensions.webstore_installer";
76 const char kInvalidIdError
[] = "Invalid id";
77 const char kDownloadDirectoryError
[] = "Could not create download directory";
78 const char kDownloadCanceledError
[] = "Download canceled";
79 const char kDownloadInterruptedError
[] = "Download interrupted";
80 const char kInvalidDownloadError
[] =
81 "Download was not a valid extension or user script";
82 const char kDependencyNotFoundError
[] = "Dependency not found";
83 const char kDependencyNotSharedModuleError
[] =
84 "Dependency is not shared module";
85 const char kInlineInstallSource
[] = "inline";
86 const char kDefaultInstallSource
[] = "ondemand";
87 const char kAppLauncherInstallSource
[] = "applauncher";
89 // TODO(rockot): Share this duplicated constant with the extension updater.
90 // See http://crbug.com/371398.
91 const char kAuthUserQueryKey
[] = "authuser";
93 const size_t kTimeRemainingMinutesThreshold
= 1u;
95 // Folder for downloading crx files from the webstore. This is used so that the
96 // crx files don't go via the usual downloads folder.
97 const base::FilePath::CharType kWebstoreDownloadFolder
[] =
98 FILE_PATH_LITERAL("Webstore Downloads");
100 base::FilePath
* g_download_directory_for_tests
= NULL
;
102 // Must be executed on the FILE thread.
103 void GetDownloadFilePath(
104 const base::FilePath
& download_directory
,
105 const std::string
& id
,
106 const base::Callback
<void(const base::FilePath
&)>& callback
) {
107 // Ensure the download directory exists. TODO(asargent) - make this use
108 // common code from the downloads system.
109 if (!base::DirectoryExists(download_directory
)) {
110 if (!base::CreateDirectory(download_directory
)) {
111 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
112 base::Bind(callback
, base::FilePath()));
117 // This is to help avoid a race condition between when we generate this
118 // filename and when the download starts writing to it (think concurrently
119 // running sharded browser tests installing the same test file, for
121 std::string random_number
=
122 base::Uint64ToString(base::RandGenerator(kuint16max
));
124 base::FilePath file
=
125 download_directory
.AppendASCII(id
+ "_" + random_number
+ ".crx");
128 base::GetUniquePathNumber(file
, base::FilePath::StringType());
129 if (uniquifier
> 0) {
130 file
= file
.InsertBeforeExtensionASCII(
131 base::StringPrintf(" (%d)", uniquifier
));
134 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
135 base::Bind(callback
, file
));
138 void MaybeAppendAuthUserParameter(const std::string
& authuser
, GURL
* url
) {
139 if (authuser
.empty())
141 std::string old_query
= url
->query();
142 url::Component
query(0, old_query
.length());
143 url::Component key
, value
;
144 // Ensure that the URL doesn't already specify an authuser parameter.
145 while (url::ExtractQueryKeyValue(
146 old_query
.c_str(), &query
, &key
, &value
)) {
147 std::string key_string
= old_query
.substr(key
.begin
, key
.len
);
148 if (key_string
== kAuthUserQueryKey
) {
152 if (!old_query
.empty()) {
155 std::string authuser_param
= base::StringPrintf(
160 // TODO(rockot): Share this duplicated code with the extension updater.
161 // See http://crbug.com/371398.
162 std::string new_query_string
= old_query
+ authuser_param
;
163 url::Component
new_query(0, new_query_string
.length());
164 url::Replacements
<char> replacements
;
165 replacements
.SetQuery(new_query_string
.c_str(), new_query
);
166 *url
= url
->ReplaceComponents(replacements
);
171 namespace extensions
{
174 GURL
WebstoreInstaller::GetWebstoreInstallURL(
175 const std::string
& extension_id
,
176 InstallSource source
) {
177 std::string install_source
;
179 case INSTALL_SOURCE_INLINE
:
180 install_source
= kInlineInstallSource
;
182 case INSTALL_SOURCE_APP_LAUNCHER
:
183 install_source
= kAppLauncherInstallSource
;
185 case INSTALL_SOURCE_OTHER
:
186 install_source
= kDefaultInstallSource
;
189 base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
190 if (cmd_line
->HasSwitch(switches::kAppsGalleryDownloadURL
)) {
191 std::string download_url
=
192 cmd_line
->GetSwitchValueASCII(switches::kAppsGalleryDownloadURL
);
193 return GURL(base::StringPrintf(download_url
.c_str(),
194 extension_id
.c_str()));
196 std::vector
<std::string
> params
;
197 params
.push_back("id=" + extension_id
);
198 if (!install_source
.empty())
199 params
.push_back("installsource=" + install_source
);
200 params
.push_back("uc");
201 std::string url_string
= extension_urls::GetWebstoreUpdateUrl().spec();
203 GURL
url(url_string
+ "?response=redirect&" +
204 update_client::UpdateQueryParams::Get(
205 update_client::UpdateQueryParams::CRX
) +
206 "&x=" + net::EscapeQueryParamValue(JoinString(params
, '&'), true));
207 DCHECK(url
.is_valid());
212 void WebstoreInstaller::Delegate::OnExtensionDownloadStarted(
213 const std::string
& id
,
214 content::DownloadItem
* item
) {
217 void WebstoreInstaller::Delegate::OnExtensionDownloadProgress(
218 const std::string
& id
,
219 content::DownloadItem
* item
) {
222 WebstoreInstaller::Approval::Approval()
224 use_app_installed_bubble(false),
225 skip_post_install_ui(false),
226 skip_install_dialog(false),
227 enable_launcher(false),
228 manifest_check_level(MANIFEST_CHECK_LEVEL_STRICT
),
229 is_ephemeral(false) {
232 scoped_ptr
<WebstoreInstaller::Approval
>
233 WebstoreInstaller::Approval::CreateWithInstallPrompt(Profile
* profile
) {
234 scoped_ptr
<Approval
> result(new Approval());
235 result
->profile
= profile
;
236 return result
.Pass();
239 scoped_ptr
<WebstoreInstaller::Approval
>
240 WebstoreInstaller::Approval::CreateForSharedModule(Profile
* profile
) {
241 scoped_ptr
<Approval
> result(new Approval());
242 result
->profile
= profile
;
243 result
->skip_install_dialog
= true;
244 result
->skip_post_install_ui
= true;
245 result
->manifest_check_level
= MANIFEST_CHECK_LEVEL_NONE
;
246 return result
.Pass();
249 scoped_ptr
<WebstoreInstaller::Approval
>
250 WebstoreInstaller::Approval::CreateWithNoInstallPrompt(
252 const std::string
& extension_id
,
253 scoped_ptr
<base::DictionaryValue
> parsed_manifest
,
254 bool strict_manifest_check
) {
255 scoped_ptr
<Approval
> result(new Approval());
256 result
->extension_id
= extension_id
;
257 result
->profile
= profile
;
258 result
->manifest
= scoped_ptr
<Manifest
>(
259 new Manifest(Manifest::INVALID_LOCATION
,
260 scoped_ptr
<base::DictionaryValue
>(
261 parsed_manifest
->DeepCopy())));
262 result
->skip_install_dialog
= true;
263 result
->manifest_check_level
= strict_manifest_check
?
264 MANIFEST_CHECK_LEVEL_STRICT
: MANIFEST_CHECK_LEVEL_LOOSE
;
265 return result
.Pass();
268 WebstoreInstaller::Approval::~Approval() {}
270 const WebstoreInstaller::Approval
* WebstoreInstaller::GetAssociatedApproval(
271 const DownloadItem
& download
) {
272 return static_cast<const Approval
*>(download
.GetUserData(kApprovalKey
));
275 WebstoreInstaller::WebstoreInstaller(Profile
* profile
,
277 content::WebContents
* web_contents
,
278 const std::string
& id
,
279 scoped_ptr
<Approval
> approval
,
280 InstallSource source
)
281 : content::WebContentsObserver(web_contents
),
282 extension_registry_observer_(this),
286 install_source_(source
),
287 download_item_(NULL
),
288 approval_(approval
.release()),
290 download_started_(false) {
291 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
292 DCHECK(web_contents
);
295 extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR
,
296 content::Source
<CrxInstaller
>(NULL
));
297 extension_registry_observer_
.Add(ExtensionRegistry::Get(profile
));
300 void WebstoreInstaller::Start() {
301 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
302 AddRef(); // Balanced in ReportSuccess and ReportFailure.
304 if (!crx_file::id_util::IdIsValid(id_
)) {
305 ReportFailure(kInvalidIdError
, FAILURE_REASON_OTHER
);
309 ExtensionService
* extension_service
=
310 ExtensionSystem::Get(profile_
)->extension_service();
311 if (approval_
.get() && approval_
->dummy_extension
.get()) {
312 extension_service
->shared_module_service()->CheckImports(
313 approval_
->dummy_extension
.get(), &pending_modules_
, &pending_modules_
);
314 // Do not check the return value of CheckImports, the CRX installer
315 // will report appropriate error messages and fail to install if there
316 // is an import error.
319 // Add the extension main module into the list.
320 SharedModuleInfo::ImportInfo info
;
321 info
.extension_id
= id_
;
322 pending_modules_
.push_back(info
);
324 total_modules_
= pending_modules_
.size();
326 std::set
<std::string
> ids
;
327 std::list
<SharedModuleInfo::ImportInfo
>::const_iterator i
;
328 for (i
= pending_modules_
.begin(); i
!= pending_modules_
.end(); ++i
) {
329 ids
.insert(i
->extension_id
);
331 ExtensionSystem::Get(profile_
)->install_verifier()->AddProvisional(ids
);
334 if (!approval_
->manifest
->value()->GetString(manifest_keys::kName
, &name
)) {
337 extensions::InstallTracker
* tracker
=
338 extensions::InstallTrackerFactory::GetForBrowserContext(profile_
);
339 extensions::InstallObserver::ExtensionInstallParams
params(
342 approval_
->installing_icon
,
343 approval_
->manifest
->is_app(),
344 approval_
->manifest
->is_platform_app());
345 params
.is_ephemeral
= approval_
->is_ephemeral
;
346 tracker
->OnBeginExtensionInstall(params
);
348 tracker
->OnBeginExtensionDownload(id_
);
350 // TODO(crbug.com/305343): Query manifest of dependencies before
351 // downloading & installing those dependencies.
352 DownloadNextPendingModule();
355 void WebstoreInstaller::Observe(int type
,
356 const content::NotificationSource
& source
,
357 const content::NotificationDetails
& details
) {
359 case extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR
: {
360 CrxInstaller
* crx_installer
= content::Source
<CrxInstaller
>(source
).ptr();
361 CHECK(crx_installer
);
362 if (crx_installer
!= crx_installer_
.get())
365 // TODO(rdevlin.cronin): Continue removing std::string errors and
366 // replacing with base::string16. See crbug.com/71980.
367 const extensions::CrxInstallError
* error
=
368 content::Details
<const extensions::CrxInstallError
>(details
).ptr();
369 const std::string utf8_error
= base::UTF16ToUTF8(error
->message());
370 crx_installer_
= NULL
;
371 // ReportFailure releases a reference to this object so it must be the
372 // last operation in this method.
373 ReportFailure(utf8_error
, FAILURE_REASON_OTHER
);
382 void WebstoreInstaller::OnExtensionInstalled(
383 content::BrowserContext
* browser_context
,
384 const Extension
* extension
,
386 CHECK(profile_
->IsSameProfile(Profile::FromBrowserContext(browser_context
)));
387 if (pending_modules_
.empty())
389 SharedModuleInfo::ImportInfo info
= pending_modules_
.front();
390 if (extension
->id() != info
.extension_id
)
392 pending_modules_
.pop_front();
394 // Clean up local state from the current download.
395 if (download_item_
) {
396 download_item_
->RemoveObserver(this);
397 download_item_
->Remove();
398 download_item_
= NULL
;
400 crx_installer_
= NULL
;
402 if (pending_modules_
.empty()) {
403 CHECK_EQ(extension
->id(), id_
);
406 const Version
version_required(info
.minimum_version
);
407 if (version_required
.IsValid() &&
408 extension
->version()->CompareTo(version_required
) < 0) {
409 // It should not happen, CrxInstaller will make sure the version is
410 // equal or newer than version_required.
411 ReportFailure(kDependencyNotFoundError
,
412 FAILURE_REASON_DEPENDENCY_NOT_FOUND
);
413 } else if (!SharedModuleInfo::IsSharedModule(extension
)) {
414 // It should not happen, CrxInstaller will make sure it is a shared
416 ReportFailure(kDependencyNotSharedModuleError
,
417 FAILURE_REASON_DEPENDENCY_NOT_SHARED_MODULE
);
419 DownloadNextPendingModule();
424 void WebstoreInstaller::InvalidateDelegate() {
428 void WebstoreInstaller::SetDownloadDirectoryForTests(
429 base::FilePath
* directory
) {
430 g_download_directory_for_tests
= directory
;
433 WebstoreInstaller::~WebstoreInstaller() {
434 if (download_item_
) {
435 download_item_
->RemoveObserver(this);
436 download_item_
= NULL
;
440 void WebstoreInstaller::OnDownloadStarted(
441 const std::string
& extension_id
,
443 content::DownloadInterruptReason interrupt_reason
) {
445 DCHECK_NE(content::DOWNLOAD_INTERRUPT_REASON_NONE
, interrupt_reason
);
446 ReportFailure(content::DownloadInterruptReasonToString(interrupt_reason
),
447 FAILURE_REASON_OTHER
);
452 for (const auto& module
: pending_modules_
) {
453 if (extension_id
== module
.extension_id
) {
459 // If this extension is not pending, it means another installer has
460 // installed this extension and triggered OnExtensionInstalled(). In this
461 // case, either it was the main module and success has already been
462 // reported, or it was a dependency and either failed (ie. wrong version) or
463 // the next download was triggered. In any case, the only thing that needs
464 // to be done is to stop this download.
469 DCHECK_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE
, interrupt_reason
);
470 DCHECK(!pending_modules_
.empty());
471 download_item_
= item
;
472 download_item_
->AddObserver(this);
473 if (pending_modules_
.size() > 1) {
474 // We are downloading a shared module. We need create an approval for it.
475 scoped_ptr
<Approval
> approval
= Approval::CreateForSharedModule(profile_
);
476 const SharedModuleInfo::ImportInfo
& info
= pending_modules_
.front();
477 approval
->extension_id
= info
.extension_id
;
478 const Version
version_required(info
.minimum_version
);
480 if (version_required
.IsValid()) {
481 approval
->minimum_version
.reset(
482 new Version(version_required
));
484 download_item_
->SetUserData(kApprovalKey
, approval
.release());
486 // It is for the main module of the extension. We should use the provided
489 download_item_
->SetUserData(kApprovalKey
, approval_
.release());
492 if (!download_started_
) {
494 delegate_
->OnExtensionDownloadStarted(id_
, download_item_
);
495 download_started_
= true;
499 void WebstoreInstaller::OnDownloadUpdated(DownloadItem
* download
) {
500 CHECK_EQ(download_item_
, download
);
502 switch (download
->GetState()) {
503 case DownloadItem::CANCELLED
:
504 ReportFailure(kDownloadCanceledError
, FAILURE_REASON_CANCELLED
);
506 case DownloadItem::INTERRUPTED
:
507 RecordInterrupt(download
);
508 ReportFailure(kDownloadInterruptedError
, FAILURE_REASON_OTHER
);
510 case DownloadItem::COMPLETE
:
511 // Wait for other notifications if the download is really an extension.
512 if (!download_crx_util::IsExtensionDownload(*download
)) {
513 ReportFailure(kInvalidDownloadError
, FAILURE_REASON_OTHER
);
515 if (crx_installer_
.get())
516 return; // DownloadItemImpl calls the observer twice, ignore it.
517 StartCrxInstaller(*download
);
519 if (pending_modules_
.size() == 1) {
520 // The download is the last module - the extension main module.
522 delegate_
->OnExtensionDownloadProgress(id_
, download
);
523 extensions::InstallTracker
* tracker
=
524 extensions::InstallTrackerFactory::GetForBrowserContext(profile_
);
525 tracker
->OnDownloadProgress(id_
, 100);
528 // Stop the progress timer if it's running.
529 download_progress_timer_
.Stop();
531 case DownloadItem::IN_PROGRESS
: {
532 if (delegate_
&& pending_modules_
.size() == 1) {
533 // Only report download progress for the main module to |delegrate_|.
534 delegate_
->OnExtensionDownloadProgress(id_
, download
);
536 UpdateDownloadProgress();
540 // Continue listening if the download is not in one of the above states.
545 void WebstoreInstaller::OnDownloadDestroyed(DownloadItem
* download
) {
546 CHECK_EQ(download_item_
, download
);
547 download_item_
->RemoveObserver(this);
548 download_item_
= NULL
;
551 void WebstoreInstaller::DownloadNextPendingModule() {
552 CHECK(!pending_modules_
.empty());
553 if (pending_modules_
.size() == 1) {
554 DCHECK_EQ(id_
, pending_modules_
.front().extension_id
);
555 DownloadCrx(id_
, install_source_
);
557 DownloadCrx(pending_modules_
.front().extension_id
, INSTALL_SOURCE_OTHER
);
561 void WebstoreInstaller::DownloadCrx(
562 const std::string
& extension_id
,
563 InstallSource source
) {
564 download_url_
= GetWebstoreInstallURL(extension_id
, source
);
565 MaybeAppendAuthUserParameter(approval_
->authuser
, &download_url_
);
567 base::FilePath user_data_dir
;
568 PathService::Get(chrome::DIR_USER_DATA
, &user_data_dir
);
569 base::FilePath download_path
= user_data_dir
.Append(kWebstoreDownloadFolder
);
571 base::FilePath
download_directory(g_download_directory_for_tests
?
572 *g_download_directory_for_tests
: download_path
);
574 #if defined(OS_CHROMEOS)
575 // Do not use drive for extension downloads.
576 if (drive::util::IsUnderDriveMountPoint(download_directory
)) {
577 download_directory
= DownloadPrefs::FromBrowserContext(
578 profile_
)->GetDefaultDownloadDirectoryForProfile();
582 BrowserThread::PostTask(
583 BrowserThread::FILE, FROM_HERE
,
584 base::Bind(&GetDownloadFilePath
, download_directory
, extension_id
,
585 base::Bind(&WebstoreInstaller::StartDownload
, this, extension_id
)));
588 // http://crbug.com/165634
589 // http://crbug.com/126013
590 // The current working theory is that one of the many pointers dereferenced in
591 // here is occasionally deleted before all of its referers are nullified,
592 // probably in a callback race. After this comment is released, the crash
593 // reports should narrow down exactly which pointer it is. Collapsing all the
594 // early-returns into a single branch makes it hard to see exactly which pointer
596 void WebstoreInstaller::StartDownload(const std::string
& extension_id
,
597 const base::FilePath
& file
) {
598 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
601 ReportFailure(kDownloadDirectoryError
, FAILURE_REASON_OTHER
);
605 DownloadManager
* download_manager
=
606 BrowserContext::GetDownloadManager(profile_
);
607 if (!download_manager
) {
608 ReportFailure(kDownloadDirectoryError
, FAILURE_REASON_OTHER
);
612 content::WebContents
* contents
= web_contents();
614 ReportFailure(kDownloadDirectoryError
, FAILURE_REASON_OTHER
);
617 if (!contents
->GetRenderProcessHost()) {
618 ReportFailure(kDownloadDirectoryError
, FAILURE_REASON_OTHER
);
621 if (!contents
->GetRenderViewHost()) {
622 ReportFailure(kDownloadDirectoryError
, FAILURE_REASON_OTHER
);
626 content::NavigationController
& controller
= contents
->GetController();
627 if (!controller
.GetBrowserContext()) {
628 ReportFailure(kDownloadDirectoryError
, FAILURE_REASON_OTHER
);
631 if (!controller
.GetBrowserContext()->GetResourceContext()) {
632 ReportFailure(kDownloadDirectoryError
, FAILURE_REASON_OTHER
);
636 // The download url for the given extension is contained in |download_url_|.
637 // We will navigate the current tab to this url to start the download. The
638 // download system will then pass the crx to the CrxInstaller.
639 RecordDownloadSource(DOWNLOAD_INITIATED_BY_WEBSTORE_INSTALLER
);
640 int render_process_host_id
= contents
->GetRenderProcessHost()->GetID();
641 int render_view_host_routing_id
=
642 contents
->GetRenderViewHost()->GetRoutingID();
643 content::ResourceContext
* resource_context
=
644 controller
.GetBrowserContext()->GetResourceContext();
645 scoped_ptr
<DownloadUrlParameters
> params(new DownloadUrlParameters(
647 render_process_host_id
,
648 render_view_host_routing_id
,
650 params
->set_file_path(file
);
651 if (controller
.GetVisibleEntry())
652 params
->set_referrer(content::Referrer::SanitizeForRequest(
653 download_url_
, content::Referrer(controller
.GetVisibleEntry()->GetURL(),
654 blink::WebReferrerPolicyDefault
)));
655 params
->set_callback(base::Bind(&WebstoreInstaller::OnDownloadStarted
,
658 download_manager
->DownloadUrl(params
.Pass());
661 void WebstoreInstaller::UpdateDownloadProgress() {
662 // If the download has gone away, or isn't in progress (in which case we can't
663 // give a good progress estimate), stop any running timers and return.
664 if (!download_item_
||
665 download_item_
->GetState() != DownloadItem::IN_PROGRESS
) {
666 download_progress_timer_
.Stop();
670 int percent
= download_item_
->PercentComplete();
671 // Only report progress if percent is more than 0 or we have finished
672 // downloading at least one of the pending modules.
673 int finished_modules
= total_modules_
- pending_modules_
.size();
674 if (finished_modules
> 0 && percent
< 0)
677 percent
= (percent
+ (finished_modules
* 100)) / total_modules_
;
678 extensions::InstallTracker
* tracker
=
679 extensions::InstallTrackerFactory::GetForBrowserContext(profile_
);
680 tracker
->OnDownloadProgress(id_
, percent
);
683 // If there's enough time remaining on the download to warrant an update,
684 // set the timer (overwriting any current timers). Otherwise, stop the
686 base::TimeDelta time_remaining
;
687 if (download_item_
->TimeRemaining(&time_remaining
) &&
689 base::TimeDelta::FromSeconds(kTimeRemainingMinutesThreshold
)) {
690 download_progress_timer_
.Start(
692 base::TimeDelta::FromSeconds(kTimeRemainingMinutesThreshold
),
694 &WebstoreInstaller::UpdateDownloadProgress
);
696 download_progress_timer_
.Stop();
700 void WebstoreInstaller::StartCrxInstaller(const DownloadItem
& download
) {
701 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
702 DCHECK(!crx_installer_
.get());
704 // The clock may be backward, e.g. daylight savings time just happenned.
705 if (download
.GetEndTime() >= download
.GetStartTime()) {
706 UMA_HISTOGRAM_TIMES("Extensions.WebstoreDownload.FileDownload",
707 download
.GetEndTime() - download
.GetStartTime());
709 ExtensionService
* service
= ExtensionSystem::Get(profile_
)->
713 const Approval
* approval
= GetAssociatedApproval(download
);
716 crx_installer_
= download_crx_util::CreateCrxInstaller(profile_
, download
);
718 crx_installer_
->set_expected_id(approval
->extension_id
);
719 crx_installer_
->set_is_gallery_install(true);
720 crx_installer_
->set_allow_silent_install(true);
722 crx_installer_
->InstallCrx(download
.GetFullPath());
725 void WebstoreInstaller::ReportFailure(const std::string
& error
,
726 FailureReason reason
) {
728 delegate_
->OnExtensionInstallFailure(id_
, error
, reason
);
732 extensions::InstallTracker
* tracker
=
733 extensions::InstallTrackerFactory::GetForBrowserContext(profile_
);
734 tracker
->OnInstallFailure(id_
);
736 Release(); // Balanced in Start().
739 void WebstoreInstaller::ReportSuccess() {
741 delegate_
->OnExtensionInstallSuccess(id_
);
745 Release(); // Balanced in Start().
748 void WebstoreInstaller::RecordInterrupt(const DownloadItem
* download
) const {
749 UMA_HISTOGRAM_SPARSE_SLOWLY("Extensions.WebstoreDownload.InterruptReason",
750 download
->GetLastReason());
752 // Use logarithmic bin sizes up to 1 TB.
753 const int kNumBuckets
= 30;
754 const int64 kMaxSizeKb
= 1 << kNumBuckets
;
755 UMA_HISTOGRAM_CUSTOM_COUNTS(
756 "Extensions.WebstoreDownload.InterruptReceivedKBytes",
757 download
->GetReceivedBytes() / 1024,
761 int64 total_bytes
= download
->GetTotalBytes();
762 if (total_bytes
>= 0) {
763 UMA_HISTOGRAM_CUSTOM_COUNTS(
764 "Extensions.WebstoreDownload.InterruptTotalKBytes",
770 UMA_HISTOGRAM_BOOLEAN(
771 "Extensions.WebstoreDownload.InterruptTotalSizeUnknown",
775 } // namespace extensions