Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / api / inline_install_private / inline_install_private_api.cc
blob566e6c7a3ed9cebbd6741cd71c0b11acc5afdf87
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"
7 #include "base/bind.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 {
19 namespace {
21 class Installer : public WebstoreInstallWithPrompt {
22 public:
23 Installer(const std::string& id,
24 const GURL& requestor_url,
25 Profile* profile,
26 const Callback& callback);
27 protected:
28 friend class base::RefCountedThreadSafe<Installer>;
29 ~Installer() override;
31 // Needed so that we send the right referrer value in requests to the
32 // webstore.
33 const GURL& GetRequestorURL() const override { return requestor_url_; }
35 scoped_refptr<ExtensionInstallPrompt::Prompt> CreateInstallPrompt() const
36 override;
38 void OnManifestParsed() override;
40 GURL requestor_url_;
43 Installer::Installer(const std::string& id,
44 const GURL& requestor_url,
45 Profile* profile,
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(),
61 show_user_count(),
62 average_rating(),
63 rating_count());
64 return prompt;
68 void Installer::OnManifestParsed() {
69 if (manifest() == nullptr) {
70 CompleteInstall(webstore_install::INVALID_MANIFEST, std::string());
71 return;
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());
82 return;
85 ProceedWithInstallPrompt();
89 } // namespace
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_));
104 if (!user_gesture())
105 return RespondNow(CreateResponse("Must be called with a user gesture",
106 webstore_install::NOT_PERMITTED));
108 content::WebContents* web_contents = GetAssociatedWebContents();
109 if (!web_contents)
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 =
119 new Installer(
120 params->id,
121 source_url_,
122 Profile::FromBrowserContext(browser_context()),
123 base::Bind(
124 &InlineInstallPrivateInstallFunction::InstallerCallback,
125 this));
126 installer->BeginInstall();
128 return RespondLater();
131 void InlineInstallPrivateInstallFunction::InstallerCallback(
132 bool success,
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(
143 error,
144 api::webstore::kInstallResultCodes[result]));
147 } // namespace extensions