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/bind.h" | |
tapted
2013/05/23 02:29:39
unused?
jackhou1
2013/05/23 06:10:19
Done.
| |
9 #include "base/files/file_path.h" | |
10 #include "base/logging.h" | |
11 #include "base/stl_util.h" | |
12 #include "chrome/browser/extensions/extension_host.h" | |
13 #include "chrome/browser/extensions/extension_service.h" | |
14 #include "chrome/browser/extensions/extension_system.h" | |
15 #include "chrome/browser/extensions/shell_window_registry.h" | |
16 #include "chrome/browser/ui/extensions/application_launch.h" | |
17 #include "chrome/browser/ui/extensions/shell_window.h" | |
18 #include "ui/base/cocoa/focus_window_set.h" | |
19 | |
20 namespace apps { | |
21 | |
22 ExtensionAppShimHandler::ExtensionAppShimHandler() {} | |
23 | |
24 ExtensionAppShimHandler::~ExtensionAppShimHandler() { | |
25 STLDeleteContainerPairSecondPointers(hosts_.begin(), hosts_.end()); | |
tapted
2013/05/23 02:29:39
This should probably send all the shim hosts a clo
jackhou1
2013/05/23 06:10:19
Turns out we can't have a map of ObserverLists bec
| |
26 } | |
27 | |
28 bool ExtensionAppShimHandler::OnShimLaunch(Host* host) { | |
29 Profile* profile = host->GetProfile(); | |
30 DCHECK(profile); | |
31 | |
32 const std::string& app_id = host->GetAppId(); | |
33 if (!extensions::Extension::IdIsValid(app_id)) { | |
34 LOG(ERROR) << "Bad app ID from app shim launch message."; | |
35 return false; | |
36 } | |
37 | |
38 if (!LaunchApp(profile, app_id)) | |
39 return false; | |
40 | |
41 HostMap::mapped_type observer_list = | |
42 FindOrInsertObserverList(profile, app_id); | |
43 if (!observer_list->HasObserver(host)) | |
tapted
2013/05/23 02:29:39
This should always be true? A particular Host shou
jackhou1
2013/05/23 06:10:19
Done.
| |
44 observer_list->AddObserver(host); | |
45 | |
46 bool already_registered = registrar_.IsRegistered( | |
47 this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
48 content::Source<Profile>(profile)); | |
49 if (!already_registered) | |
tapted
2013/05/23 02:29:39
This needs curlies for a multi-line body. It shoul
jackhou1
2013/05/23 06:10:19
Do you mean move this code into FindOrInsertObserv
| |
50 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
51 content::Source<Profile>(profile)); | |
52 | |
53 return true; | |
54 } | |
55 | |
56 void ExtensionAppShimHandler::OnShimClose(Host* host) { | |
57 HostMap::mapped_type observer_list = | |
58 FindObserverList(host->GetProfile(), host->GetAppId()); | |
59 DCHECK(observer_list); | |
60 observer_list->RemoveObserver(host); | |
61 if (!observer_list->size()) | |
tapted
2013/05/23 02:29:39
nit: size() == 0 tends to be preferred when there
jackhou1
2013/05/23 06:10:19
Done.
| |
62 RemoveObserverList(host->GetProfile(), host->GetAppId()); | |
63 } | |
64 | |
65 void ExtensionAppShimHandler::OnShimFocus(Host* host) { | |
66 if (!host->GetProfile()) | |
67 return; | |
68 | |
69 extensions::ShellWindowRegistry* registry = | |
70 extensions::ShellWindowRegistry::Get(host->GetProfile()); | |
71 const extensions::ShellWindowRegistry::ShellWindowSet windows = | |
72 registry->GetShellWindowsForApp(host->GetAppId()); | |
73 std::set<gfx::NativeWindow> native_windows; | |
74 for (extensions::ShellWindowRegistry::const_iterator i = windows.begin(); | |
75 i != windows.end(); ++i) { | |
76 native_windows.insert((*i)->GetNativeWindow()); | |
77 } | |
78 ui::FocusWindowSet(native_windows); | |
79 } | |
80 | |
81 ObserverList<apps::AppShimHandler::Host>* | |
82 ExtensionAppShimHandler::FindObserverList(Profile* profile, | |
83 const std::string& app_id) { | |
tapted
2013/05/23 02:29:39
nit: indenting
You could also use HostMap::const_
jackhou1
2013/05/23 06:10:19
Done.
| |
84 HostMap::iterator it = hosts_.find(make_pair(profile, app_id)); | |
85 return it == hosts_.end() ? NULL : it->second; | |
86 } | |
87 | |
88 ObserverList<apps::AppShimHandler::Host>* | |
89 ExtensionAppShimHandler::FindOrInsertObserverList(Profile* profile, | |
90 const std::string& app_id) { | |
91 HostMap::mapped_type& value = hosts_[make_pair(profile, app_id)]; | |
92 if (!value) | |
93 value = new ObserverList<apps::AppShimHandler::Host>(); | |
94 return value; | |
95 } | |
96 | |
97 bool ExtensionAppShimHandler::RemoveObserverList(Profile* profile, | |
98 const std::string& app_id) { | |
99 return hosts_.erase(make_pair(profile, app_id)); | |
100 } | |
101 | |
102 bool ExtensionAppShimHandler::LaunchApp(Profile* profile, | |
103 const std::string& app_id) { | |
104 extensions::ExtensionSystem* extension_system = | |
105 extensions::ExtensionSystem::Get(profile); | |
106 ExtensionServiceInterface* extension_service = | |
107 extension_system->extension_service(); | |
108 const extensions::Extension* extension = | |
109 extension_service->GetExtensionById(app_id, false); | |
110 if (!extension) { | |
111 LOG(ERROR) << "Attempted to launch nonexistent app with id '" | |
112 << app_id << "'."; | |
113 return false; | |
114 } | |
115 // TODO(jeremya): Handle the case that launching the app fails. Probably we | |
116 // need to watch for 'app successfully launched' or at least 'background page | |
117 // exists/was created' and time out with failure if we don't see that sign of | |
118 // life within a certain window. | |
119 chrome::OpenApplication(chrome::AppLaunchParams( | |
120 profile, extension, NEW_FOREGROUND_TAB)); | |
121 return true; | |
122 } | |
123 | |
124 void ExtensionAppShimHandler::Observe( | |
125 int type, | |
126 const content::NotificationSource& source, | |
127 const content::NotificationDetails& details) { | |
128 Profile* profile = content::Source<Profile>(source).ptr(); | |
129 switch (type) { | |
130 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | |
131 extensions::ExtensionHost* extension_host = | |
132 content::Details<extensions::ExtensionHost>(details).ptr(); | |
133 Close(profile, extension_host->extension_id()); | |
134 break; | |
135 } | |
136 default: | |
137 NOTREACHED(); // Unexpected notification. | |
138 break; | |
139 } | |
140 } | |
141 | |
142 void ExtensionAppShimHandler::Close(Profile* profile, | |
143 const std::string& app_id) { | |
144 HostMap::mapped_type observer_list = FindObserverList(profile, app_id); | |
145 if (!observer_list) | |
146 return; | |
147 | |
148 FOR_EACH_OBSERVER(apps::AppShimHandler::Host, | |
149 *observer_list, | |
150 OnAppClosed()); | |
151 } | |
152 | |
153 } // namespace apps | |
OLD | NEW |