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