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_handler_mac.h" | |
tapted
2013/05/21 06:15:37
don't need this one, since the files "own" header
jackhou1
2013/05/22 23:54:44
Done.
| |
8 #include "apps/app_shim/app_shim_messages.h" | |
9 #include "base/bind.h" | |
10 #include "base/files/file_path.h" | |
11 #include "base/logging.h" | |
12 #include "base/stl_util.h" | |
13 #include "chrome/browser/browser_process.h" | |
14 #include "chrome/browser/extensions/extension_host.h" | |
15 #include "chrome/browser/extensions/extension_service.h" | |
16 #include "chrome/browser/extensions/extension_system.h" | |
17 #include "chrome/browser/extensions/shell_window_registry.h" | |
18 #include "chrome/browser/profiles/profile_manager.h" | |
19 #include "chrome/browser/ui/extensions/application_launch.h" | |
20 #include "chrome/browser/ui/extensions/shell_window.h" | |
21 #include "chrome/common/extensions/extension_constants.h" | |
22 #include "ipc/ipc_channel_proxy.h" | |
tapted
2013/05/21 06:15:37
should be able to drop this. And maybe some others
jackhou1
2013/05/22 23:54:44
Done.
| |
23 #include "ui/base/cocoa/focus_window_set.h" | |
24 | |
25 ExtensionAppShimHandler::ExtensionAppShimHandler() { | |
tapted
2013/05/21 06:15:37
nit: no blank line in function. Most common is cur
jackhou1
2013/05/22 23:54:44
Done.
| |
26 | |
27 } | |
28 | |
29 ExtensionAppShimHandler::~ExtensionAppShimHandler() { | |
30 STLDeleteContainerPairSecondPointers(hosts_.begin(), hosts_.end()); | |
31 } | |
32 | |
33 bool ExtensionAppShimHandler::OnShimLaunch(Host* host) { | |
34 Profile* profile = host->profile(); | |
35 if (!profile) | |
tapted
2013/05/21 06:15:37
DCHECK instead? This should never fail.
jackhou1
2013/05/22 23:54:44
Done.
| |
36 return false; | |
37 | |
38 const std::string& app_id = host->app_id(); | |
39 if (!extensions::Extension::IdIsValid(app_id)) { | |
40 LOG(ERROR) << "Bad app ID from app shim launch message."; | |
tapted
2013/05/21 06:15:37
I think this is OK for now.. But we might eventual
| |
41 return false; | |
42 } | |
43 | |
44 if (!LaunchApp(profile, app_id)) | |
45 return false; | |
46 | |
47 GetObserverList(profile, app_id, true)->AddObserver(host); | |
48 | |
49 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
tapted
2013/05/21 06:15:37
This should only be added once, in the constructor
jackhou1
2013/05/22 23:54:44
We don't know the profile at that point. Checking
tapted
2013/05/23 02:29:39
Ah, good point.
| |
50 content::Source<Profile>(profile)); | |
51 return true; | |
52 } | |
53 | |
54 void ExtensionAppShimHandler::OnShimClose(Host* host) { | |
55 HostMap::mapped_type observer_list = | |
56 GetObserverList(host->profile(), host->app_id(), false); | |
57 if (observer_list) | |
tapted
2013/05/21 06:15:37
DCHECK (and segfault in Release) if this should ne
jackhou1
2013/05/22 23:54:44
Done.
| |
58 observer_list->RemoveObserver(host); | |
59 } | |
tapted
2013/05/21 06:15:37
I think this should also remove the entry from hos
jackhou1
2013/05/22 23:54:44
Done.
| |
60 | |
61 void ExtensionAppShimHandler::OnShimFocus(Host* host) { | |
62 if (!host->profile()) | |
63 return; | |
64 | |
65 extensions::ShellWindowRegistry* registry = | |
66 extensions::ShellWindowRegistry::Get(host->profile()); | |
67 const std::set<ShellWindow*> windows = | |
tapted
2013/05/21 06:15:37
Should perhaps be ShellWindowRegistry::ShellWindow
jackhou1
2013/05/22 23:54:44
Done.
| |
68 registry->GetShellWindowsForApp(host->app_id()); | |
69 std::set<gfx::NativeWindow> native_windows; | |
70 for (std::set<ShellWindow*>::const_iterator i = windows.begin(); | |
tapted
2013/05/21 06:15:37
-> ShellWindowRegistry::const_iterator.
jackhou1
2013/05/22 23:54:44
Done.
| |
71 i != windows.end(); | |
72 ++i) { | |
tapted
2013/05/21 06:15:37
nit: put on line above.
jackhou1
2013/05/22 23:54:44
Done.
| |
73 native_windows.insert((*i)->GetNativeWindow()); | |
74 } | |
75 ui::FocusWindowSet(native_windows); | |
tapted
2013/05/21 06:15:37
something to think about in future: there is a ~5s
| |
76 } | |
77 | |
78 ObserverList<apps::AppShimHandler::Host>* | |
tapted
2013/05/21 06:15:37
the return type here should match the one in the h
jackhou1
2013/05/22 23:54:44
I can't seem to use HostMap::mapped_type here, mig
tapted
2013/05/23 02:29:39
I think you just need to resolve scope?
`Extensio
jackhou1
2013/05/23 06:10:19
Done.
| |
79 ExtensionAppShimHandler::GetObserverList(Profile* profile, | |
80 const std::string& app_id, | |
81 bool create_if_not_exist) { | |
82 if (!create_if_not_exist) { | |
tapted
2013/05/21 06:15:37
I think this should be two functions if there's no
jackhou1
2013/05/22 23:54:44
Done.
| |
83 HostMap::iterator it = hosts_.find(make_pair(profile, app_id)); | |
84 return it == hosts_.end() ? NULL : it->second; | |
85 } | |
86 | |
87 std::pair<HostMap::iterator, bool> result = | |
tapted
2013/05/21 06:15:37
Maybe
HostMap::mapped_type& value = hosts_[make_p
jackhou1
2013/05/22 23:54:44
Done.
| |
88 hosts_.insert(make_pair( | |
89 make_pair(profile, app_id), | |
90 static_cast<HostMap::mapped_type>(NULL))); | |
91 if (result.second) { | |
92 result.first->second = new ObserverList<apps::AppShimHandler::Host>(); | |
93 } | |
94 return result.first->second; | |
95 } | |
96 | |
97 bool ExtensionAppShimHandler::LaunchApp(Profile* profile, | |
98 const std::string& app_id) { | |
99 extensions::ExtensionSystem* extension_system = | |
100 extensions::ExtensionSystem::Get(profile); | |
101 ExtensionServiceInterface* extension_service = | |
102 extension_system->extension_service(); | |
103 const extensions::Extension* extension = | |
104 extension_service->GetExtensionById(app_id, false); | |
105 if (!extension) { | |
106 LOG(ERROR) << "Attempted to launch nonexistent app with id '" | |
107 << app_id << "'."; | |
108 return false; | |
109 } | |
110 // TODO(jeremya): Handle the case that launching the app fails. Probably we | |
111 // need to watch for 'app successfully launched' or at least 'background page | |
112 // exists/was created' and time out with failure if we don't see that sign of | |
113 // life within a certain window. | |
114 chrome::AppLaunchParams params(profile, | |
115 extension, | |
116 extension_misc::LAUNCH_NONE, | |
tapted
2013/05/21 06:15:37
interesting factoid. Using LAUNCH_NONE means OpenA
jackhou1
2013/05/22 23:54:44
Done.
| |
117 NEW_WINDOW); | |
118 chrome::OpenApplication(params); | |
119 return true; | |
120 } | |
121 | |
122 void ExtensionAppShimHandler::Observe( | |
123 int type, | |
124 const content::NotificationSource& source, | |
125 const content::NotificationDetails& details) { | |
126 DCHECK(CalledOnValidThread()); | |
127 Profile* profile = content::Source<Profile>(source).ptr(); | |
128 switch (type) { | |
129 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | |
130 extensions::ExtensionHost* extension_host = | |
131 content::Details<extensions::ExtensionHost>(details).ptr(); | |
132 Close(profile, extension_host->extension_id()); | |
133 break; | |
134 } | |
135 default: | |
136 NOTREACHED() << "Unexpected notification sent."; | |
tapted
2013/05/21 06:15:37
nit: drop the string literal, or make it a comment
jackhou1
2013/05/22 23:54:44
Done.
| |
137 break; | |
138 } | |
139 } | |
140 | |
141 void ExtensionAppShimHandler::Close(Profile* profile, | |
142 const std::string& app_id) { | |
143 DCHECK(CalledOnValidThread()); | |
144 | |
145 HostMap::mapped_type observer_list = GetObserverList(profile, app_id, false); | |
tapted
2013/05/21 06:15:37
nit: return early instead, `if (!observer_list)`.
jackhou1
2013/05/22 23:54:44
Done.
| |
146 if (observer_list) { | |
147 FOR_EACH_OBSERVER(apps::AppShimHandler::Host, | |
148 *observer_list, | |
149 OnAppClosed()); | |
150 } | |
151 } | |
OLD | NEW |