summaryrefslogtreecommitdiffstats
path: root/chrome/browser/apps/ephemeral_app_throttle.cc
blob: 655d36face600d82809069a9f912376bc4fe8a6d (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
// 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_throttle.h"

#include "base/command_line.h"
#include "chrome/browser/apps/ephemeral_app_launcher.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_constants.h"
#include "components/navigation_interception/intercept_navigation_resource_throttle.h"
#include "components/navigation_interception/navigation_params.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_throttle.h"
#include "content/public/browser/web_contents.h"
#include "net/url_request/url_request.h"

using content::BrowserThread;
using content::WebContents;
using extensions::Extension;

namespace {

bool LaunchEphemeralApp(
    const std::string& app_id,
    content::WebContents* source,
    const navigation_interception::NavigationParams& params) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  // Redirect top-level navigations only.
  if (source->IsSubframe())
    return false;

  Profile* profile =
      Profile::FromBrowserContext(source->GetBrowserContext());
  if (!profile)
    return false;

  ExtensionService* service =
      extensions::ExtensionSystem::Get(profile)->extension_service();
  DCHECK(service);
  const Extension* extension = service->GetInstalledExtension(app_id);

  // Only launch apps.
  if (extension && !extension->is_app())
    return false;

  // The EphemeralAppLauncher will handle launching of an existing app or
  // installing and launching a new ephemeral app.
  scoped_refptr<EphemeralAppLauncher> installer =
      EphemeralAppLauncher::CreateForLink(app_id, source);
  installer->Start();
  return true;
}

}  // namespace

// static
content::ResourceThrottle*
EphemeralAppThrottle::MaybeCreateThrottleForLaunch(
    net::URLRequest* request,
    ProfileIOData* profile_io_data) {
  if (!CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kEnableLinkableEphemeralApps))
    return NULL;

  if (request->method() != "GET" || !request->url().SchemeIsHTTPOrHTTPS())
    return NULL;

  // Not supported for incognito profiles.
  if (profile_io_data->is_incognito())
    return NULL;

  // Only watch for links in Google search results.
  if (request->referrer().find("https://www.google.com") == std::string::npos)
    return NULL;

  // Crudely watch for links to Chrome Web Store detail pages and assume that
  // the app ID will be after the last slash of the URL. We cannot even
  // differentiate between apps and extensions, so attempt to launch both.
  // This is obviously for demonstration purposes only and will be implemented
  // properly in production code - for example, links to ephemeral apps could
  // have a new scheme.
  if (request->url().spec().find(
          extension_urls::GetWebstoreItemDetailURLPrefix()) != 0)
    return NULL;

  std::string app_id(request->url().ExtractFileName());
  if (!Extension::IdIsValid(app_id))
    return NULL;

  return new navigation_interception::InterceptNavigationResourceThrottle(
      request,
      base::Bind(&LaunchEphemeralApp, app_id));
}