summaryrefslogtreecommitdiffstats
path: root/apps/app_shim/extension_app_shim_handler_mac.cc
blob: 396184b72221b35a6da796e0221d5fde1168a463 (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
// 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 "apps/app_shim/extension_app_shim_handler_mac.h"

#include "apps/app_shim/app_shim_messages.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/shell_window_registry.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/browser/ui/extensions/native_app_window.h"
#include "chrome/browser/ui/extensions/shell_window.h"
#include "chrome/browser/ui/web_applications/web_app_ui.h"
#include "chrome/browser/web_applications/web_app_mac.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "ui/base/cocoa/focus_window_set.h"

namespace apps {

ExtensionAppShimHandler::ExtensionAppShimHandler() {
  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_CREATED,
                 content::NotificationService::AllBrowserContextsAndSources());
}

ExtensionAppShimHandler::~ExtensionAppShimHandler() {
  for (HostMap::iterator it = hosts_.begin(); it != hosts_.end(); ) {
    // Increment the iterator first as OnAppClosed may call back to OnShimClose
    // and invalidate the iterator.
    it++->second->OnAppClosed();
  }
}

bool ExtensionAppShimHandler::OnShimLaunch(Host* host,
                                           AppShimLaunchType launch_type) {
  Profile* profile = host->GetProfile();
  DCHECK(profile);

  const std::string& app_id = host->GetAppId();
  if (!extensions::Extension::IdIsValid(app_id)) {
    LOG(ERROR) << "Bad app ID from app shim launch message.";
    return false;
  }

  if (!LaunchApp(profile, app_id, launch_type))
    return false;

  // The first host to claim this (profile, app_id) becomes the main host.
  // For any others, we focus the app and return false.
  if (!hosts_.insert(make_pair(make_pair(profile, app_id), host)).second) {
    OnShimFocus(host);
    return false;
  }

  if (!registrar_.IsRegistered(
      this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
      content::Source<Profile>(profile))) {
    registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
                   content::Source<Profile>(profile));
  }

  return true;
}

void ExtensionAppShimHandler::OnShimClose(Host* host) {
  HostMap::iterator it = hosts_.find(make_pair(host->GetProfile(),
                                               host->GetAppId()));
  // Any hosts other than the main host will still call OnShimClose, so ignore
  // them.
  if (it != hosts_.end() && it->second == host)
    hosts_.erase(it);
}

void ExtensionAppShimHandler::OnShimFocus(Host* host) {
  if (!host->GetProfile())
    return;

  extensions::ShellWindowRegistry* registry =
      extensions::ShellWindowRegistry::Get(host->GetProfile());
  const extensions::ShellWindowRegistry::ShellWindowList windows =
      registry->GetShellWindowsForApp(host->GetAppId());
  std::set<gfx::NativeWindow> native_windows;
  for (extensions::ShellWindowRegistry::const_iterator it = windows.begin();
       it != windows.end(); ++it) {
    native_windows.insert((*it)->GetNativeWindow());
  }
  ui::FocusWindowSet(native_windows);
}

void ExtensionAppShimHandler::OnShimQuit(Host* host) {
  if (!host->GetProfile())
    return;

  extensions::ShellWindowRegistry::ShellWindowList windows =
      extensions::ShellWindowRegistry::Get(host->GetProfile())->
          GetShellWindowsForApp(host->GetAppId());
  for (extensions::ShellWindowRegistry::const_iterator it = windows.begin();
       it != windows.end(); ++it) {
    (*it)->GetBaseWindow()->Close();
  }
}

bool ExtensionAppShimHandler::LaunchApp(Profile* profile,
                                        const std::string& app_id,
                                        AppShimLaunchType launch_type) {
  extensions::ExtensionSystem* extension_system =
      extensions::ExtensionSystem::Get(profile);
  ExtensionServiceInterface* extension_service =
      extension_system->extension_service();
  const extensions::Extension* extension =
      extension_service->GetExtensionById(app_id, false);
  if (!extension) {
    LOG(ERROR) << "Attempted to launch nonexistent app with id '"
               << app_id << "'.";
    return false;
  }

  if (launch_type == APP_SHIM_LAUNCH_REGISTER_ONLY)
    return true;

  // TODO(jeremya): Handle the case that launching the app fails. Probably we
  // need to watch for 'app successfully launched' or at least 'background page
  // exists/was created' and time out with failure if we don't see that sign of
  // life within a certain window.
  chrome::OpenApplication(chrome::AppLaunchParams(
      profile, extension, NEW_FOREGROUND_TAB));
  return true;
}

void ExtensionAppShimHandler::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  Profile* profile = content::Source<Profile>(source).ptr();
  extensions::ExtensionHost* extension_host =
      content::Details<extensions::ExtensionHost>(details).ptr();
  switch (type) {
    case chrome::NOTIFICATION_EXTENSION_HOST_CREATED:
      StartShim(profile, extension_host->extension());
      break;
    case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED:
      CloseShim(profile, extension_host->extension_id());
      break;
    default:
      NOTREACHED();  // Unexpected notification.
      break;
  }
}

void ExtensionAppShimHandler::StartShim(
    Profile* profile,
    const extensions::Extension* extension) {
  if (!extension->is_platform_app())
    return;

  if (hosts_.count(make_pair(profile, extension->id())))
    return;

  web_app::MaybeLaunchShortcut(
      web_app::ShortcutInfoForExtensionAndProfile(extension, profile));
}

void ExtensionAppShimHandler::CloseShim(Profile* profile,
                                        const std::string& app_id) {
  HostMap::const_iterator it = hosts_.find(make_pair(profile, app_id));
  if (it != hosts_.end())
    it->second->OnAppClosed();
}

}  // namespace apps