summaryrefslogtreecommitdiffstats
path: root/chrome/browser/apps/ephemeral_app_launcher.cc
blob: 9d37b760d9a08ab9a577020692efb80397cbae4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/apps/ephemeral_app_launcher.h"

#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_install_prompt.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/browser/ui/extensions/extension_enable_flow.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/permissions/permissions_data.h"

using content::WebContents;
using extensions::Extension;
using extensions::ExtensionSystem;
using extensions::WebstoreInstaller;

namespace {

const char kInvalidManifestError[] = "Invalid manifest";
const char kExtensionTypeError[] = "Ephemeral extensions are not permitted";
const char kLaunchAbortedError[] = "Launch aborted";

Profile* ProfileForWebContents(content::WebContents* contents) {
  if (!contents)
    return NULL;

  return Profile::FromBrowserContext(contents->GetBrowserContext());
}

gfx::NativeWindow NativeWindowForWebContents(content::WebContents* contents) {
  if (!contents)
    return NULL;

  return contents->GetView()->GetTopLevelNativeWindow();
}

}  // namespace

// static
scoped_refptr<EphemeralAppLauncher>
EphemeralAppLauncher::CreateForLauncher(
    const std::string& webstore_item_id,
    Profile* profile,
    gfx::NativeWindow parent_window,
    const Callback& callback) {
  scoped_refptr<EphemeralAppLauncher> installer =
      new EphemeralAppLauncher(webstore_item_id,
                               profile,
                               parent_window,
                               callback);
  installer->set_install_source(WebstoreInstaller::INSTALL_SOURCE_APP_LAUNCHER);
  return installer;
}

// static
scoped_refptr<EphemeralAppLauncher>
EphemeralAppLauncher::CreateForLink(
    const std::string& webstore_item_id,
    content::WebContents* web_contents) {
  scoped_refptr<EphemeralAppLauncher> installer =
      new EphemeralAppLauncher(webstore_item_id,
                               web_contents,
                               Callback());
  installer->set_install_source(WebstoreInstaller::INSTALL_SOURCE_OTHER);
  return installer;
}

void EphemeralAppLauncher::Start() {
  ExtensionService* extension_service =
      extensions::ExtensionSystem::Get(profile())->extension_service();
  DCHECK(extension_service);

  const Extension* extension = extension_service->GetInstalledExtension(id());
  if (extension) {
    if (extensions::util::IsAppLaunchableWithoutEnabling(extension->id(),
                                                         profile())) {
      LaunchApp(extension);
      InvokeCallback(std::string());
      return;
    }

    // The ephemeral app may have been updated and disabled as it requests
    // more permissions. In this case we should always prompt before
    // launching.
    extension_enable_flow_.reset(
        new ExtensionEnableFlow(profile(), extension->id(), this));
    if (web_contents())
      extension_enable_flow_->StartForWebContents(web_contents());
    else
      extension_enable_flow_->StartForNativeWindow(parent_window_);

    // Keep this object alive until the enable flow is complete.
    AddRef(); // Balanced in WebstoreStandaloneInstaller::CompleteInstall.
    return;
  }

  // Fetch the app from the webstore.
  StartObserving();
  BeginInstall();
}

EphemeralAppLauncher::EphemeralAppLauncher(
    const std::string& webstore_item_id,
    Profile* profile,
    gfx::NativeWindow parent_window,
    const Callback& callback)
        : WebstoreStandaloneInstaller(
              webstore_item_id,
              profile,
              callback),
          parent_window_(parent_window),
          dummy_web_contents_(
              WebContents::Create(WebContents::CreateParams(profile))) {
}

EphemeralAppLauncher::EphemeralAppLauncher(
    const std::string& webstore_item_id,
    content::WebContents* web_contents,
    const Callback& callback)
        : WebstoreStandaloneInstaller(
              webstore_item_id,
              ProfileForWebContents(web_contents),
              callback),
          content::WebContentsObserver(web_contents),
          parent_window_(NativeWindowForWebContents(web_contents)) {
}

EphemeralAppLauncher::~EphemeralAppLauncher() {}

void EphemeralAppLauncher::StartObserving() {
  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
                 content::Source<Profile>(profile()->GetOriginalProfile()));
}

void EphemeralAppLauncher::LaunchApp(const Extension* extension) const {
  DCHECK(extension);
  if (!extension->is_app()) {
    LOG(ERROR) << "Unable to launch extension " << extension->id()
               << ". It is not an app.";
    return;
  }

  AppLaunchParams params(profile(), extension, NEW_FOREGROUND_TAB);
  params.desktop_type =
      chrome::GetHostDesktopTypeForNativeWindow(parent_window_);
  OpenApplication(params);
}

bool EphemeralAppLauncher::CheckRequestorAlive() const {
  return dummy_web_contents_.get() != NULL || web_contents() != NULL;
}

const GURL& EphemeralAppLauncher::GetRequestorURL() const {
  return GURL::EmptyGURL();
}

bool EphemeralAppLauncher::ShouldShowPostInstallUI() const {
  return false;
}

bool EphemeralAppLauncher::ShouldShowAppInstalledBubble() const {
  return false;
}

WebContents* EphemeralAppLauncher::GetWebContents() const {
  return web_contents() ? web_contents() : dummy_web_contents_.get();
}

scoped_ptr<ExtensionInstallPrompt::Prompt>
EphemeralAppLauncher::CreateInstallPrompt() const {
  DCHECK(extension_.get() != NULL);

  // Skip the prompt by returning null if the app does not need to display
  // permission warnings.
  extensions::PermissionMessages permissions =
      extensions::PermissionsData::GetPermissionMessages(extension_.get());
  if (permissions.empty())
    return scoped_ptr<ExtensionInstallPrompt::Prompt>();

  return make_scoped_ptr(new ExtensionInstallPrompt::Prompt(
      ExtensionInstallPrompt::LAUNCH_PROMPT));
}

bool EphemeralAppLauncher::CheckInlineInstallPermitted(
    const base::DictionaryValue& webstore_data,
    std::string* error) const {
  *error = "";
  return true;
}

bool EphemeralAppLauncher::CheckRequestorPermitted(
    const base::DictionaryValue& webstore_data,
    std::string* error) const {
  *error = "";
  return true;
}

bool EphemeralAppLauncher::CheckInstallValid(
    const base::DictionaryValue& manifest,
    std::string* error) {
  extension_ = Extension::Create(
      base::FilePath(),
      extensions::Manifest::INTERNAL,
      manifest,
      Extension::REQUIRE_KEY |
          Extension::FROM_WEBSTORE |
          Extension::IS_EPHEMERAL,
      id(),
      error);
  if (!extension_.get()) {
    *error = kInvalidManifestError;
    return false;
  }

  if (!extension_->is_app()) {
    *error = kExtensionTypeError;
    return false;
  }

  return true;
}

scoped_ptr<ExtensionInstallPrompt>
EphemeralAppLauncher::CreateInstallUI() {
  if (web_contents())
    return make_scoped_ptr(new ExtensionInstallPrompt(web_contents()));

  return make_scoped_ptr(
      new ExtensionInstallPrompt(profile(), parent_window_, NULL));
}

scoped_ptr<WebstoreInstaller::Approval>
EphemeralAppLauncher::CreateApproval() const {
  scoped_ptr<WebstoreInstaller::Approval> approval =
      WebstoreStandaloneInstaller::CreateApproval();
  approval->is_ephemeral = true;
  return approval.Pass();
}

void EphemeralAppLauncher::CompleteInstall(const std::string& error) {
  if (!error.empty())
    WebstoreStandaloneInstaller::CompleteInstall(error);

  // If the installation succeeds, we reach this point as a result of
  // chrome::NOTIFICATION_EXTENSION_INSTALLED, but this is broadcasted before
  // ExtensionService has added the extension to its list of installed
  // extensions and is too early to launch the app. Instead, we will launch at
  // chrome::NOTIFICATION_EXTENSION_LOADED.
  // TODO(tmdiep): Refactor extensions/WebstoreInstaller or
  // WebstoreStandaloneInstaller to support this cleanly.
}

void EphemeralAppLauncher::WebContentsDestroyed(
    content::WebContents* web_contents) {
  AbortInstall();
}

void EphemeralAppLauncher::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  switch (type) {
    case chrome::NOTIFICATION_EXTENSION_LOADED: {
      const extensions::Extension* extension =
          content::Details<const extensions::Extension>(details).ptr();
      DCHECK(extension);
      if (extension->id() == id()) {
        LaunchApp(extension);
        WebstoreStandaloneInstaller::CompleteInstall(std::string());
      }
      break;
    }

    default:
      NOTREACHED();
  }
}

void EphemeralAppLauncher::ExtensionEnableFlowFinished() {
  ExtensionService* extension_service =
      extensions::ExtensionSystem::Get(profile())->extension_service();
  DCHECK(extension_service);

  const Extension* extension = extension_service->GetExtensionById(id(), false);
  if (extension) {
    LaunchApp(extension);
    WebstoreStandaloneInstaller::CompleteInstall(std::string());
  } else {
    WebstoreStandaloneInstaller::CompleteInstall(kLaunchAbortedError);
  }
}

void EphemeralAppLauncher::ExtensionEnableFlowAborted(bool user_initiated) {
  WebstoreStandaloneInstaller::CompleteInstall(kLaunchAbortedError);
}