OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "apps/app_shim/extension_app_shim_handler_mac.h" |
| 6 |
| 7 #include "apps/app_shim/app_shim_messages.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" |
| 10 #include "chrome/browser/extensions/extension_host.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/extension_system.h" |
| 13 #include "chrome/browser/extensions/shell_window_registry.h" |
| 14 #include "chrome/browser/ui/extensions/application_launch.h" |
| 15 #include "chrome/browser/ui/extensions/shell_window.h" |
| 16 #include "ui/base/cocoa/focus_window_set.h" |
| 17 |
| 18 namespace apps { |
| 19 |
| 20 ExtensionAppShimHandler::ExtensionAppShimHandler() {} |
| 21 |
| 22 ExtensionAppShimHandler::~ExtensionAppShimHandler() { |
| 23 for (HostMap::iterator it = hosts_.begin(); it != hosts_.end(); ) { |
| 24 // Increment the iterator first as OnAppClosed may call back to OnShimClose |
| 25 // and invalidate the iterator. |
| 26 it++->second->OnAppClosed(); |
| 27 } |
| 28 } |
| 29 |
| 30 bool ExtensionAppShimHandler::OnShimLaunch(Host* host) { |
| 31 Profile* profile = host->GetProfile(); |
| 32 DCHECK(profile); |
| 33 |
| 34 const std::string& app_id = host->GetAppId(); |
| 35 if (!extensions::Extension::IdIsValid(app_id)) { |
| 36 LOG(ERROR) << "Bad app ID from app shim launch message."; |
| 37 return false; |
| 38 } |
| 39 |
| 40 if (!LaunchApp(profile, app_id)) |
| 41 return false; |
| 42 |
| 43 // The first host to claim this (profile, app_id) becomes the main host. |
| 44 // For any others, we launch the app but return false. |
| 45 if (!hosts_.insert(make_pair(make_pair(profile, app_id), host)).second) |
| 46 return false; |
| 47 |
| 48 if (!registrar_.IsRegistered( |
| 49 this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 50 content::Source<Profile>(profile))) { |
| 51 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 52 content::Source<Profile>(profile)); |
| 53 } |
| 54 |
| 55 return true; |
| 56 } |
| 57 |
| 58 void ExtensionAppShimHandler::OnShimClose(Host* host) { |
| 59 HostMap::iterator it = hosts_.find(make_pair(host->GetProfile(), |
| 60 host->GetAppId())); |
| 61 // Any hosts other than the main host will still call OnShimClose, so ignore |
| 62 // them. |
| 63 if (it != hosts_.end() && it->second == host) |
| 64 hosts_.erase(it); |
| 65 } |
| 66 |
| 67 void ExtensionAppShimHandler::OnShimFocus(Host* host) { |
| 68 if (!host->GetProfile()) |
| 69 return; |
| 70 |
| 71 extensions::ShellWindowRegistry* registry = |
| 72 extensions::ShellWindowRegistry::Get(host->GetProfile()); |
| 73 const extensions::ShellWindowRegistry::ShellWindowList windows = |
| 74 registry->GetShellWindowsForApp(host->GetAppId()); |
| 75 std::set<gfx::NativeWindow> native_windows; |
| 76 for (extensions::ShellWindowRegistry::const_iterator i = windows.begin(); |
| 77 i != windows.end(); ++i) { |
| 78 native_windows.insert((*i)->GetNativeWindow()); |
| 79 } |
| 80 ui::FocusWindowSet(native_windows); |
| 81 } |
| 82 |
| 83 bool ExtensionAppShimHandler::LaunchApp(Profile* profile, |
| 84 const std::string& app_id) { |
| 85 extensions::ExtensionSystem* extension_system = |
| 86 extensions::ExtensionSystem::Get(profile); |
| 87 ExtensionServiceInterface* extension_service = |
| 88 extension_system->extension_service(); |
| 89 const extensions::Extension* extension = |
| 90 extension_service->GetExtensionById(app_id, false); |
| 91 if (!extension) { |
| 92 LOG(ERROR) << "Attempted to launch nonexistent app with id '" |
| 93 << app_id << "'."; |
| 94 return false; |
| 95 } |
| 96 // TODO(jeremya): Handle the case that launching the app fails. Probably we |
| 97 // need to watch for 'app successfully launched' or at least 'background page |
| 98 // exists/was created' and time out with failure if we don't see that sign of |
| 99 // life within a certain window. |
| 100 chrome::OpenApplication(chrome::AppLaunchParams( |
| 101 profile, extension, NEW_FOREGROUND_TAB)); |
| 102 return true; |
| 103 } |
| 104 |
| 105 void ExtensionAppShimHandler::Observe( |
| 106 int type, |
| 107 const content::NotificationSource& source, |
| 108 const content::NotificationDetails& details) { |
| 109 Profile* profile = content::Source<Profile>(source).ptr(); |
| 110 switch (type) { |
| 111 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { |
| 112 extensions::ExtensionHost* extension_host = |
| 113 content::Details<extensions::ExtensionHost>(details).ptr(); |
| 114 CloseShim(profile, extension_host->extension_id()); |
| 115 break; |
| 116 } |
| 117 default: |
| 118 NOTREACHED(); // Unexpected notification. |
| 119 break; |
| 120 } |
| 121 } |
| 122 |
| 123 void ExtensionAppShimHandler::CloseShim(Profile* profile, |
| 124 const std::string& app_id) { |
| 125 HostMap::const_iterator it = hosts_.find(make_pair(profile, app_id)); |
| 126 if (it != hosts_.end()) |
| 127 it->second->OnAppClosed(); |
| 128 } |
| 129 |
| 130 } // namespace apps |
OLD | NEW |