1 // Copyright 2014 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/api/inline_install_private/inline_install_private_api.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/browser/extensions/webstore_install_with_prompt.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/extensions/api/inline_install_private.h"
12 #include "chrome/common/extensions/api/webstore/webstore_api_constants.h"
13 #include "components/crx_file/id_util.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "extensions/browser/extension_registry.h"
17 namespace extensions
{
21 class Installer
: public WebstoreInstallWithPrompt
{
23 Installer(const std::string
& id
,
24 const GURL
& requestor_url
,
26 const Callback
& callback
);
28 friend class base::RefCountedThreadSafe
<Installer
>;
29 ~Installer() override
;
31 // Needed so that we send the right referrer value in requests to the
33 const GURL
& GetRequestorURL() const override
{ return requestor_url_
; }
35 scoped_refptr
<ExtensionInstallPrompt::Prompt
> CreateInstallPrompt() const
38 void OnManifestParsed() override
;
43 Installer::Installer(const std::string
& id
,
44 const GURL
& requestor_url
,
46 const Callback
& callback
) :
47 WebstoreInstallWithPrompt(id
, profile
, callback
),
48 requestor_url_(requestor_url
) {
49 set_show_post_install_ui(false);
52 Installer::~Installer() {
55 scoped_refptr
<ExtensionInstallPrompt::Prompt
>
56 Installer::CreateInstallPrompt() const {
57 scoped_refptr
<ExtensionInstallPrompt::Prompt
> prompt(
58 new ExtensionInstallPrompt::Prompt(
59 ExtensionInstallPrompt::INLINE_INSTALL_PROMPT
));
60 prompt
->SetWebstoreData(localized_user_count(),
68 void Installer::OnManifestParsed() {
69 if (manifest() == nullptr) {
70 CompleteInstall(webstore_install::INVALID_MANIFEST
, std::string());
74 Manifest
parsed_manifest(Manifest::INTERNAL
,
75 make_scoped_ptr(manifest()->DeepCopy()));
77 std::string manifest_error
;
78 std::vector
<InstallWarning
> warnings
;
80 if (!parsed_manifest
.is_platform_app()) {
81 CompleteInstall(webstore_install::NOT_PERMITTED
, std::string());
85 ProceedWithInstallPrompt();
91 InlineInstallPrivateInstallFunction::
92 InlineInstallPrivateInstallFunction() {
95 InlineInstallPrivateInstallFunction::
96 ~InlineInstallPrivateInstallFunction() {
99 ExtensionFunction::ResponseAction
100 InlineInstallPrivateInstallFunction::Run() {
101 typedef api::inline_install_private::Install::Params Params
;
102 scoped_ptr
<Params
> params(Params::Create(*args_
));
105 return RespondNow(CreateResponse("Must be called with a user gesture",
106 webstore_install::NOT_PERMITTED
));
108 content::WebContents
* web_contents
= GetAssociatedWebContents();
110 return RespondNow(CreateResponse("Must be called from a foreground page",
111 webstore_install::NOT_PERMITTED
));
113 ExtensionRegistry
* registry
= ExtensionRegistry::Get(browser_context());
114 if (registry
->GetExtensionById(params
->id
, ExtensionRegistry::EVERYTHING
))
115 return RespondNow(CreateResponse("Already installed",
116 webstore_install::OTHER_ERROR
));
118 scoped_refptr
<Installer
> installer
=
122 Profile::FromBrowserContext(browser_context()),
124 &InlineInstallPrivateInstallFunction::InstallerCallback
,
126 installer
->BeginInstall();
128 return RespondLater();
131 void InlineInstallPrivateInstallFunction::InstallerCallback(
133 const std::string
& error
,
134 webstore_install::Result result
) {
136 Respond(CreateResponse(success
? std::string() : error
, result
));
139 ExtensionFunction::ResponseValue
140 InlineInstallPrivateInstallFunction::CreateResponse(
141 const std::string
& error
, webstore_install::Result result
) {
142 return ArgumentList(api::inline_install_private::Install::Results::Create(
144 api::webstore::kInstallResultCodes
[result
]));
147 } // namespace extensions