• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "athena/extensions/public/extensions_delegate.h"
6 
7 #include "athena/activity/public/activity_factory.h"
8 #include "athena/extensions/chrome/athena_chrome_app_window_client.h"
9 #include "base/macros.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/extensions/application_launch.h"
15 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "extensions/browser/extension_system.h"
18 #include "extensions/common/constants.h"
19 #include "extensions/common/extension_set.h"
20 #include "extensions/common/extension_urls.h"
21 #include "net/base/url_util.h"
22 
23 namespace athena {
24 namespace {
25 
26 class ChromeExtensionsDelegate : public ExtensionsDelegate {
27  public:
ChromeExtensionsDelegate(content::BrowserContext * context)28   explicit ChromeExtensionsDelegate(content::BrowserContext* context)
29       : extension_service_(
30             extensions::ExtensionSystem::Get(context)->extension_service()) {
31     extensions::AppWindowClient::Set(&app_window_client_);
32   }
33 
~ChromeExtensionsDelegate()34   virtual ~ChromeExtensionsDelegate() {
35     extensions::AppWindowClient::Set(NULL);
36   }
37 
38  private:
39   // ExtensionsDelegate:
GetBrowserContext() const40   virtual content::BrowserContext* GetBrowserContext() const OVERRIDE {
41     return extension_service_->GetBrowserContext();
42   }
GetInstalledExtensions()43   virtual const extensions::ExtensionSet& GetInstalledExtensions() OVERRIDE {
44     return *extension_service_->extensions();
45   }
LaunchApp(const std::string & app_id)46   virtual bool LaunchApp(const std::string& app_id) OVERRIDE {
47     // Check Running apps
48     content::BrowserContext* context = GetBrowserContext();
49     const extensions::Extension* extension =
50         extensions::ExtensionRegistry::Get(context)->GetExtensionById(
51             app_id, extensions::ExtensionRegistry::EVERYTHING);
52     DCHECK(extension);
53     if (!extension)
54       return false;
55 
56     // TODO(oshima): Support installation/enabling process.
57     if (!extensions::util::IsAppLaunchableWithoutEnabling(app_id, context))
58       return false;
59 
60     int event_flags = 0;
61     AppLaunchParams params(Profile::FromBrowserContext(context),
62                            extension,
63                            event_flags,
64                            chrome::HOST_DESKTOP_TYPE_ASH);
65     // TODO(oshima): rename HOST_DESTOP_TYPE_ASH to non native desktop.
66 
67     if (app_id == extensions::kWebStoreAppId) {
68       std::string source_value =
69           std::string(extension_urls::kLaunchSourceAppList);
70       // Set an override URL to include the source.
71       GURL extension_url =
72           extensions::AppLaunchInfo::GetFullLaunchURL(extension);
73       params.override_url = net::AppendQueryParameter(
74           extension_url, extension_urls::kWebstoreSourceField, source_value);
75     }
76     params.container = extensions::LAUNCH_CONTAINER_WINDOW;
77 
78     // V2 apps
79     if (CanLaunchViaEvent(extension)) {
80       OpenApplication(params);
81       return true;
82     }
83     LaunchV1App(params, extension);
84     return true;
85   }
86 
UnloadApp(const std::string & app_id)87   virtual bool UnloadApp(const std::string& app_id) OVERRIDE {
88     // TODO(skuhne): Implement using extension service.
89     return false;
90   }
91 
LaunchV1App(const AppLaunchParams & params,const extensions::Extension * extension)92   void LaunchV1App(const AppLaunchParams& params,
93                    const extensions::Extension* extension) {
94     // TODO(oshima): Just activate if the app is already running.
95     const GURL url_input = params.override_url;
96 
97     DCHECK(!url_input.is_empty() || extension);
98     GURL url = UrlForExtension(extension, url_input);
99     athena::ActivityFactory::Get()->CreateWebActivity(
100         GetBrowserContext(), base::UTF8ToUTF16(extension->name()), url);
101   }
102 
103   // ExtensionService for the browser context this is created for.
104   ExtensionService* extension_service_;
105 
106   // Installed extensions.
107   extensions::ExtensionSet extensions_;
108 
109   AthenaChromeAppWindowClient app_window_client_;
110 
111   DISALLOW_COPY_AND_ASSIGN(ChromeExtensionsDelegate);
112 };
113 
114 }  // namespace
115 
116 // static
CreateExtensionsDelegateForChrome(content::BrowserContext * context)117 void ExtensionsDelegate::CreateExtensionsDelegateForChrome(
118     content::BrowserContext* context) {
119   new ChromeExtensionsDelegate(context);
120 }
121 
122 }  // namespace athena
123