| 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, bool launch_now) { |
| 31 Profile* profile = host->GetProfile(); | 36 Profile* profile = host->GetProfile(); |
| 32 DCHECK(profile); | 37 DCHECK(profile); |
| 33 | 38 |
| 34 const std::string& app_id = host->GetAppId(); | 39 const std::string& app_id = host->GetAppId(); |
| 35 if (!extensions::Extension::IdIsValid(app_id)) { | 40 if (!extensions::Extension::IdIsValid(app_id)) { |
| 36 LOG(ERROR) << "Bad app ID from app shim launch message."; | 41 LOG(ERROR) << "Bad app ID from app shim launch message."; |
| 37 return false; | 42 return false; |
| 38 } | 43 } |
| 39 | 44 |
| 40 if (!LaunchApp(profile, app_id)) | 45 if (!LaunchApp(profile, app_id, launch_now)) |
| 41 return false; | 46 return false; |
| 42 | 47 |
| 43 // The first host to claim this (profile, app_id) becomes the main host. | 48 // The first host to claim this (profile, app_id) becomes the main host. |
| 44 // For any others, we launch the app but return false. | 49 // For any others, we focus the app and return false. |
| 45 if (!hosts_.insert(make_pair(make_pair(profile, app_id), host)).second) | 50 if (!hosts_.insert(make_pair(make_pair(profile, app_id), host)).second) { |
| 51 OnShimFocus(host); |
| 46 return false; | 52 return false; |
| 53 } |
| 47 | 54 |
| 48 if (!registrar_.IsRegistered( | 55 if (!registrar_.IsRegistered( |
| 49 this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 56 this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 50 content::Source<Profile>(profile))) { | 57 content::Source<Profile>(profile))) { |
| 51 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 58 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 52 content::Source<Profile>(profile)); | 59 content::Source<Profile>(profile)); |
| 53 } | 60 } |
| 54 | 61 |
| 55 return true; | 62 return true; |
| 56 } | 63 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 74 registry->GetShellWindowsForApp(host->GetAppId()); | 81 registry->GetShellWindowsForApp(host->GetAppId()); |
| 75 std::set<gfx::NativeWindow> native_windows; | 82 std::set<gfx::NativeWindow> native_windows; |
| 76 for (extensions::ShellWindowRegistry::const_iterator i = windows.begin(); | 83 for (extensions::ShellWindowRegistry::const_iterator i = windows.begin(); |
| 77 i != windows.end(); ++i) { | 84 i != windows.end(); ++i) { |
| 78 native_windows.insert((*i)->GetNativeWindow()); | 85 native_windows.insert((*i)->GetNativeWindow()); |
| 79 } | 86 } |
| 80 ui::FocusWindowSet(native_windows); | 87 ui::FocusWindowSet(native_windows); |
| 81 } | 88 } |
| 82 | 89 |
| 83 bool ExtensionAppShimHandler::LaunchApp(Profile* profile, | 90 bool ExtensionAppShimHandler::LaunchApp(Profile* profile, |
| 84 const std::string& app_id) { | 91 const std::string& app_id, |
| 92 bool launch_now) { |
| 85 extensions::ExtensionSystem* extension_system = | 93 extensions::ExtensionSystem* extension_system = |
| 86 extensions::ExtensionSystem::Get(profile); | 94 extensions::ExtensionSystem::Get(profile); |
| 87 ExtensionServiceInterface* extension_service = | 95 ExtensionServiceInterface* extension_service = |
| 88 extension_system->extension_service(); | 96 extension_system->extension_service(); |
| 89 const extensions::Extension* extension = | 97 const extensions::Extension* extension = |
| 90 extension_service->GetExtensionById(app_id, false); | 98 extension_service->GetExtensionById(app_id, false); |
| 91 if (!extension) { | 99 if (!extension) { |
| 92 LOG(ERROR) << "Attempted to launch nonexistent app with id '" | 100 LOG(ERROR) << "Attempted to launch nonexistent app with id '" |
| 93 << app_id << "'."; | 101 << app_id << "'."; |
| 94 return false; | 102 return false; |
| 95 } | 103 } |
| 104 |
| 105 if (!launch_now) |
| 106 return true; |
| 107 |
| 96 // TODO(jeremya): Handle the case that launching the app fails. Probably we | 108 // 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 | 109 // 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 | 110 // exists/was created' and time out with failure if we don't see that sign of |
| 99 // life within a certain window. | 111 // life within a certain window. |
| 100 chrome::OpenApplication(chrome::AppLaunchParams( | 112 chrome::OpenApplication(chrome::AppLaunchParams( |
| 101 profile, extension, NEW_FOREGROUND_TAB)); | 113 profile, extension, NEW_FOREGROUND_TAB)); |
| 102 return true; | 114 return true; |
| 103 } | 115 } |
| 104 | 116 |
| 105 void ExtensionAppShimHandler::Observe( | 117 void ExtensionAppShimHandler::Observe( |
| 106 int type, | 118 int type, |
| 107 const content::NotificationSource& source, | 119 const content::NotificationSource& source, |
| 108 const content::NotificationDetails& details) { | 120 const content::NotificationDetails& details) { |
| 109 Profile* profile = content::Source<Profile>(source).ptr(); | 121 Profile* profile = content::Source<Profile>(source).ptr(); |
| 122 extensions::ExtensionHost* extension_host = |
| 123 content::Details<extensions::ExtensionHost>(details).ptr(); |
| 110 switch (type) { | 124 switch (type) { |
| 111 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | 125 case chrome::NOTIFICATION_EXTENSION_HOST_CREATED: |
| 112 extensions::ExtensionHost* extension_host = | 126 StartShim(profile, extension_host->extension()); |
| 113 content::Details<extensions::ExtensionHost>(details).ptr(); | 127 break; |
| 128 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: |
| 114 CloseShim(profile, extension_host->extension_id()); | 129 CloseShim(profile, extension_host->extension_id()); |
| 115 break; | 130 break; |
| 116 } | |
| 117 default: | 131 default: |
| 118 NOTREACHED(); // Unexpected notification. | 132 NOTREACHED(); // Unexpected notification. |
| 119 break; | 133 break; |
| 120 } | 134 } |
| 121 } | 135 } |
| 122 | 136 |
| 137 void ExtensionAppShimHandler::StartShim( |
| 138 Profile* profile, |
| 139 const extensions::Extension* extension) { |
| 140 if (!extension->is_platform_app()) |
| 141 return; |
| 142 |
| 143 if (hosts_.count(make_pair(profile, extension->id()))) |
| 144 return; |
| 145 |
| 146 web_app::MaybeLaunchShortcut( |
| 147 web_app::ShortcutInfoForExtensionAndProfile(extension, profile)); |
| 148 } |
| 149 |
| 123 void ExtensionAppShimHandler::CloseShim(Profile* profile, | 150 void ExtensionAppShimHandler::CloseShim(Profile* profile, |
| 124 const std::string& app_id) { | 151 const std::string& app_id) { |
| 125 HostMap::const_iterator it = hosts_.find(make_pair(profile, app_id)); | 152 HostMap::const_iterator it = hosts_.find(make_pair(profile, app_id)); |
| 126 if (it != hosts_.end()) | 153 if (it != hosts_.end()) |
| 127 it->second->OnAppClosed(); | 154 it->second->OnAppClosed(); |
| 128 } | 155 } |
| 129 | 156 |
| 130 } // namespace apps | 157 } // namespace apps |
| OLD | NEW |