| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "apps/app_shim/extension_app_shim_handler_mac.h" | 5 #include "apps/app_shim/extension_app_shim_handler_mac.h" |
| 6 | 6 |
| 7 #include "apps/app_shim/app_shim_messages.h" | 7 #include "apps/app_shim/app_shim_messages.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "chrome/browser/extensions/extension_host.h" | 10 #include "chrome/browser/extensions/extension_host.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/extension_system.h" | 12 #include "chrome/browser/extensions/extension_system.h" |
| 13 #include "chrome/browser/extensions/shell_window_registry.h" | 13 #include "chrome/browser/extensions/shell_window_registry.h" |
| 14 #include "chrome/browser/ui/extensions/application_launch.h" | 14 #include "chrome/browser/ui/extensions/application_launch.h" |
| 15 #include "chrome/browser/ui/extensions/shell_window.h" | 15 #include "chrome/browser/ui/extensions/shell_window.h" |
| 16 #include "chrome/browser/ui/web_applications/web_app_ui.h" |
| 17 #include "chrome/browser/web_applications/web_app_mac.h" |
| 16 #include "ui/base/cocoa/focus_window_set.h" | 18 #include "ui/base/cocoa/focus_window_set.h" |
| 17 | 19 |
| 18 namespace apps { | 20 namespace apps { |
| 19 | 21 |
| 20 ExtensionAppShimHandler::ExtensionAppShimHandler() {} | 22 ExtensionAppShimHandler::ExtensionAppShimHandler() { |
| 23 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_CREATED, |
| 24 content::NotificationService::AllBrowserContextsAndSources()); |
| 25 } |
| 21 | 26 |
| 22 ExtensionAppShimHandler::~ExtensionAppShimHandler() { | 27 ExtensionAppShimHandler::~ExtensionAppShimHandler() { |
| 23 for (HostMap::iterator it = hosts_.begin(); it != hosts_.end(); ) { | 28 for (HostMap::iterator it = hosts_.begin(); it != hosts_.end(); ) { |
| 24 // Increment the iterator first as OnAppClosed may call back to OnShimClose | 29 // Increment the iterator first as OnAppClosed may call back to OnShimClose |
| 25 // and invalidate the iterator. | 30 // and invalidate the iterator. |
| 26 it++->second->OnAppClosed(); | 31 it++->second->OnAppClosed(); |
| 27 } | 32 } |
| 28 } | 33 } |
| 29 | 34 |
| 30 bool ExtensionAppShimHandler::OnShimLaunch(Host* host) { | 35 bool ExtensionAppShimHandler::OnShimLaunch(Host* host, |
| 36 AppShimLaunchType launch_type) { |
| 31 Profile* profile = host->GetProfile(); | 37 Profile* profile = host->GetProfile(); |
| 32 DCHECK(profile); | 38 DCHECK(profile); |
| 33 | 39 |
| 34 const std::string& app_id = host->GetAppId(); | 40 const std::string& app_id = host->GetAppId(); |
| 35 if (!extensions::Extension::IdIsValid(app_id)) { | 41 if (!extensions::Extension::IdIsValid(app_id)) { |
| 36 LOG(ERROR) << "Bad app ID from app shim launch message."; | 42 LOG(ERROR) << "Bad app ID from app shim launch message."; |
| 37 return false; | 43 return false; |
| 38 } | 44 } |
| 39 | 45 |
| 40 if (!LaunchApp(profile, app_id)) | 46 if (!LaunchApp(profile, app_id, launch_type)) |
| 41 return false; | 47 return false; |
| 42 | 48 |
| 43 // The first host to claim this (profile, app_id) becomes the main host. | 49 // The first host to claim this (profile, app_id) becomes the main host. |
| 44 // For any others, we launch the app but return false. | 50 // For any others, we focus the app and return false. |
| 45 if (!hosts_.insert(make_pair(make_pair(profile, app_id), host)).second) | 51 if (!hosts_.insert(make_pair(make_pair(profile, app_id), host)).second) { |
| 52 OnShimFocus(host); |
| 46 return false; | 53 return false; |
| 54 } |
| 47 | 55 |
| 48 if (!registrar_.IsRegistered( | 56 if (!registrar_.IsRegistered( |
| 49 this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 57 this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 50 content::Source<Profile>(profile))) { | 58 content::Source<Profile>(profile))) { |
| 51 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 59 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 52 content::Source<Profile>(profile)); | 60 content::Source<Profile>(profile)); |
| 53 } | 61 } |
| 54 | 62 |
| 55 return true; | 63 return true; |
| 56 } | 64 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 74 registry->GetShellWindowsForApp(host->GetAppId()); | 82 registry->GetShellWindowsForApp(host->GetAppId()); |
| 75 std::set<gfx::NativeWindow> native_windows; | 83 std::set<gfx::NativeWindow> native_windows; |
| 76 for (extensions::ShellWindowRegistry::const_iterator i = windows.begin(); | 84 for (extensions::ShellWindowRegistry::const_iterator i = windows.begin(); |
| 77 i != windows.end(); ++i) { | 85 i != windows.end(); ++i) { |
| 78 native_windows.insert((*i)->GetNativeWindow()); | 86 native_windows.insert((*i)->GetNativeWindow()); |
| 79 } | 87 } |
| 80 ui::FocusWindowSet(native_windows); | 88 ui::FocusWindowSet(native_windows); |
| 81 } | 89 } |
| 82 | 90 |
| 83 bool ExtensionAppShimHandler::LaunchApp(Profile* profile, | 91 bool ExtensionAppShimHandler::LaunchApp(Profile* profile, |
| 84 const std::string& app_id) { | 92 const std::string& app_id, |
| 93 AppShimLaunchType launch_type) { |
| 85 extensions::ExtensionSystem* extension_system = | 94 extensions::ExtensionSystem* extension_system = |
| 86 extensions::ExtensionSystem::Get(profile); | 95 extensions::ExtensionSystem::Get(profile); |
| 87 ExtensionServiceInterface* extension_service = | 96 ExtensionServiceInterface* extension_service = |
| 88 extension_system->extension_service(); | 97 extension_system->extension_service(); |
| 89 const extensions::Extension* extension = | 98 const extensions::Extension* extension = |
| 90 extension_service->GetExtensionById(app_id, false); | 99 extension_service->GetExtensionById(app_id, false); |
| 91 if (!extension) { | 100 if (!extension) { |
| 92 LOG(ERROR) << "Attempted to launch nonexistent app with id '" | 101 LOG(ERROR) << "Attempted to launch nonexistent app with id '" |
| 93 << app_id << "'."; | 102 << app_id << "'."; |
| 94 return false; | 103 return false; |
| 95 } | 104 } |
| 105 |
| 106 if (launch_type == REGISTER_WITHOUT_LAUNCH) |
| 107 return true; |
| 108 |
| 96 // TODO(jeremya): Handle the case that launching the app fails. Probably we | 109 // 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 | 110 // 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 | 111 // exists/was created' and time out with failure if we don't see that sign of |
| 99 // life within a certain window. | 112 // life within a certain window. |
| 100 chrome::OpenApplication(chrome::AppLaunchParams( | 113 chrome::OpenApplication(chrome::AppLaunchParams( |
| 101 profile, extension, NEW_FOREGROUND_TAB)); | 114 profile, extension, NEW_FOREGROUND_TAB)); |
| 102 return true; | 115 return true; |
| 103 } | 116 } |
| 104 | 117 |
| 105 void ExtensionAppShimHandler::Observe( | 118 void ExtensionAppShimHandler::Observe( |
| 106 int type, | 119 int type, |
| 107 const content::NotificationSource& source, | 120 const content::NotificationSource& source, |
| 108 const content::NotificationDetails& details) { | 121 const content::NotificationDetails& details) { |
| 109 Profile* profile = content::Source<Profile>(source).ptr(); | 122 Profile* profile = content::Source<Profile>(source).ptr(); |
| 123 extensions::ExtensionHost* extension_host = |
| 124 content::Details<extensions::ExtensionHost>(details).ptr(); |
| 110 switch (type) { | 125 switch (type) { |
| 111 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | 126 case chrome::NOTIFICATION_EXTENSION_HOST_CREATED: |
| 112 extensions::ExtensionHost* extension_host = | 127 StartShim(profile, extension_host->extension()); |
| 113 content::Details<extensions::ExtensionHost>(details).ptr(); | 128 break; |
| 129 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: |
| 114 CloseShim(profile, extension_host->extension_id()); | 130 CloseShim(profile, extension_host->extension_id()); |
| 115 break; | 131 break; |
| 116 } | |
| 117 default: | 132 default: |
| 118 NOTREACHED(); // Unexpected notification. | 133 NOTREACHED(); // Unexpected notification. |
| 119 break; | 134 break; |
| 120 } | 135 } |
| 121 } | 136 } |
| 122 | 137 |
| 138 void ExtensionAppShimHandler::StartShim( |
| 139 Profile* profile, |
| 140 const extensions::Extension* extension) { |
| 141 if (!extension->is_platform_app()) |
| 142 return; |
| 143 |
| 144 if (hosts_.count(make_pair(profile, extension->id()))) |
| 145 return; |
| 146 |
| 147 web_app::MaybeLaunchShortcut( |
| 148 web_app::ShortcutInfoForExtensionAndProfile(extension, profile)); |
| 149 } |
| 150 |
| 123 void ExtensionAppShimHandler::CloseShim(Profile* profile, | 151 void ExtensionAppShimHandler::CloseShim(Profile* profile, |
| 124 const std::string& app_id) { | 152 const std::string& app_id) { |
| 125 HostMap::const_iterator it = hosts_.find(make_pair(profile, app_id)); | 153 HostMap::const_iterator it = hosts_.find(make_pair(profile, app_id)); |
| 126 if (it != hosts_.end()) | 154 if (it != hosts_.end()) |
| 127 it->second->OnAppClosed(); | 155 it->second->OnAppClosed(); |
| 128 } | 156 } |
| 129 | 157 |
| 130 } // namespace apps | 158 } // namespace apps |
| OLD | NEW |