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