blob: b1e7b5d55867b07043dffb0b15686a6ee55fbe07 [file] [log] [blame]
[email protected]29679dea2012-03-10 03:20:281// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]655b2b1a2011-10-13 17:13:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_
6#define CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/compiler_specific.h"
[email protected]98e4e522011-10-25 13:00:1613#include "base/memory/ref_counted.h"
[email protected]21a5ad62012-04-03 04:48:4514#include "base/memory/scoped_ptr.h"
15#include "base/values.h"
[email protected]8adbe3112012-03-27 05:42:2216#include "content/public/browser/download_id.h"
17#include "content/public/browser/download_item.h"
[email protected]6c2381d2011-10-19 02:52:5318#include "content/public/browser/notification_observer.h"
19#include "content/public/browser/notification_registrar.h"
[email protected]8adbe3112012-03-27 05:42:2220#include "net/base/net_errors.h"
[email protected]98e4e522011-10-25 13:00:1621#include "googleurl/src/gurl.h"
[email protected]655b2b1a2011-10-13 17:13:0622
[email protected]f66a50a2011-11-02 23:53:4623class FilePath;
[email protected]655b2b1a2011-10-13 17:13:0624class Profile;
25
[email protected]cdcb1dee2012-01-04 00:46:2026namespace content {
27class NavigationController;
28}
29
[email protected]655b2b1a2011-10-13 17:13:0630// Downloads and installs extensions from the web store.
[email protected]98e4e522011-10-25 13:00:1631class WebstoreInstaller : public content::NotificationObserver,
[email protected]8adbe3112012-03-27 05:42:2232 public content::DownloadItem::Observer,
[email protected]98e4e522011-10-25 13:00:1633 public base::RefCounted<WebstoreInstaller> {
[email protected]655b2b1a2011-10-13 17:13:0634 public:
35 enum Flag {
36 FLAG_NONE = 0,
37
[email protected]99dfecd2011-10-18 01:11:5038 // Inline installs trigger slightly different behavior (install source
39 // is different, download referrers are the item's page in the gallery).
40 FLAG_INLINE_INSTALL = 1 << 0
[email protected]655b2b1a2011-10-13 17:13:0641 };
42
43 class Delegate {
44 public:
45 virtual void OnExtensionInstallSuccess(const std::string& id) = 0;
46 virtual void OnExtensionInstallFailure(const std::string& id,
47 const std::string& error) = 0;
48 };
49
[email protected]21a5ad62012-04-03 04:48:4550 // If added to the WebstoreInstaller, an Approval indicates that the user has
51 // already approved the installation and that the CrxInstaller can bypass its
52 // install prompt.
53 struct Approval : public content::DownloadItem::ExternalData {
54 Approval();
55 virtual ~Approval();
56
57 // The extension id that was approved for installation.
58 std::string extension_id;
59
60 // The profile the extension should be installed into.
61 Profile* profile;
62
63 // The expected manifest, before localization.
64 scoped_ptr<base::DictionaryValue> parsed_manifest;
65
66 // Whether to use a bubble notification when an app is installed, instead of
67 // the default behavior of transitioning to the new tab page.
68 bool use_app_installed_bubble;
69
70 // Whether to skip the post install UI like the extension installed bubble.
71 bool skip_post_install_ui;
72 };
73
74 // Gets the Approval associated with the |download|, or NULL if there's none.
75 // Note that the Approval is owned by |download|.
76 static const Approval* GetAssociatedApproval(
77 const content::DownloadItem& download);
78
[email protected]98e4e522011-10-25 13:00:1679 // Creates a WebstoreInstaller for downloading and installing the extension
80 // with the given |id| from the Chrome Web Store. If |delegate| is not NULL,
81 // it will be notified when the install succeeds or fails. The installer will
82 // use the specified |controller| to download the extension. Only one
[email protected]21a5ad62012-04-03 04:48:4583 // WebstoreInstaller can use a specific controller at any given time. This
84 // also associates the |approval| with this install.
[email protected]98e4e522011-10-25 13:00:1685 // Note: the delegate should stay alive until being called back.
86 WebstoreInstaller(Profile* profile,
87 Delegate* delegate,
[email protected]cdcb1dee2012-01-04 00:46:2088 content::NavigationController* controller,
[email protected]98e4e522011-10-25 13:00:1689 const std::string& id,
[email protected]21a5ad62012-04-03 04:48:4590 scoped_ptr<Approval> approval,
[email protected]98e4e522011-10-25 13:00:1691 int flags);
[email protected]655b2b1a2011-10-13 17:13:0692
[email protected]98e4e522011-10-25 13:00:1693 // Starts downloading and installing the extension.
94 void Start();
[email protected]655b2b1a2011-10-13 17:13:0695
[email protected]6c2381d2011-10-19 02:52:5396 // content::NotificationObserver
[email protected]655b2b1a2011-10-13 17:13:0697 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:5398 const content::NotificationSource& source,
99 const content::NotificationDetails& details) OVERRIDE;
[email protected]655b2b1a2011-10-13 17:13:06100
[email protected]9c265d02011-12-29 22:13:43101 // Instead of using the default download directory, use |directory| instead.
102 // This does *not* transfer ownership of |directory|.
103 static void SetDownloadDirectoryForTests(FilePath* directory);
104
[email protected]655b2b1a2011-10-13 17:13:06105 private:
[email protected]5f2a4752012-04-27 22:18:58106 friend class base::RefCounted<WebstoreInstaller>;
107 virtual ~WebstoreInstaller();
108
[email protected]8adbe3112012-03-27 05:42:22109 // DownloadManager::DownloadUrl callback.
110 void OnDownloadStarted(content::DownloadId id, net::Error error);
111
112 // DownloadItem::Observer implementation:
113 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE;
114 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE;
115
[email protected]f66a50a2011-11-02 23:53:46116 // Starts downloading the extension to |file_path|.
[email protected]29679dea2012-03-10 03:20:28117 void StartDownload(const FilePath& file_path);
[email protected]f66a50a2011-11-02 23:53:46118
[email protected]655b2b1a2011-10-13 17:13:06119 // Reports an install |error| to the delegate for the given extension if this
120 // managed its installation. This also removes the associated PendingInstall.
[email protected]98e4e522011-10-25 13:00:16121 void ReportFailure(const std::string& error);
[email protected]655b2b1a2011-10-13 17:13:06122
123 // Reports a successful install to the delegate for the given extension if
124 // this managed its installation. This also removes the associated
125 // PendingInstall.
[email protected]98e4e522011-10-25 13:00:16126 void ReportSuccess();
[email protected]655b2b1a2011-10-13 17:13:06127
[email protected]6c2381d2011-10-19 02:52:53128 content::NotificationRegistrar registrar_;
[email protected]655b2b1a2011-10-13 17:13:06129 Profile* profile_;
[email protected]98e4e522011-10-25 13:00:16130 Delegate* delegate_;
[email protected]cdcb1dee2012-01-04 00:46:20131 content::NavigationController* controller_;
[email protected]98e4e522011-10-25 13:00:16132 std::string id_;
[email protected]8adbe3112012-03-27 05:42:22133 // The DownloadItem is owned by the DownloadManager and is valid from when
134 // OnDownloadStarted is called (with no error) until the DownloadItem
135 // transitions to state REMOVING.
136 content::DownloadItem* download_item_;
[email protected]98e4e522011-10-25 13:00:16137 int flags_;
[email protected]21a5ad62012-04-03 04:48:45138 scoped_ptr<Approval> approval_;
[email protected]98e4e522011-10-25 13:00:16139 GURL download_url_;
[email protected]655b2b1a2011-10-13 17:13:06140};
141
142#endif // CHROME_BROWSER_EXTENSIONS_WEBSTORE_INSTALLER_H_