OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/dom_ui/app_launcher_handler.h" | 5 #include "chrome/browser/dom_ui/app_launcher_handler.h" |
6 | 6 |
7 #include "app/animation.h" | 7 #include "app/animation.h" |
8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/app_launched_animation.h" | 12 #include "chrome/browser/app_launched_animation.h" |
13 #include "chrome/browser/browser.h" | 13 #include "chrome/browser/browser.h" |
14 #include "chrome/browser/browser_list.h" | 14 #include "chrome/browser/browser_list.h" |
15 #include "chrome/browser/extensions/extension_prefs.h" | |
16 #include "chrome/browser/extensions/extensions_service.h" | 15 #include "chrome/browser/extensions/extensions_service.h" |
17 #include "chrome/browser/platform_util.h" | 16 #include "chrome/browser/platform_util.h" |
18 #include "chrome/browser/tab_contents/tab_contents.h" | 17 #include "chrome/browser/tab_contents/tab_contents.h" |
19 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
20 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
21 #include "chrome/common/extensions/extension_constants.h" | 20 #include "chrome/common/extensions/extension_constants.h" |
22 #include "chrome/common/extensions/extension_icon_set.h" | 21 #include "chrome/common/extensions/extension_icon_set.h" |
23 #include "chrome/common/extensions/extension_resource.h" | 22 #include "chrome/common/extensions/extension_resource.h" |
24 #include "chrome/common/notification_service.h" | 23 #include "chrome/common/notification_service.h" |
25 #include "chrome/common/notification_type.h" | 24 #include "chrome/common/notification_type.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 DOMMessageHandler* AppLauncherHandler::Attach(DOMUI* dom_ui) { | 61 DOMMessageHandler* AppLauncherHandler::Attach(DOMUI* dom_ui) { |
63 // TODO(arv): Add initialization code to the Apps store etc. | 62 // TODO(arv): Add initialization code to the Apps store etc. |
64 return DOMMessageHandler::Attach(dom_ui); | 63 return DOMMessageHandler::Attach(dom_ui); |
65 } | 64 } |
66 | 65 |
67 void AppLauncherHandler::RegisterMessages() { | 66 void AppLauncherHandler::RegisterMessages() { |
68 dom_ui_->RegisterMessageCallback("getApps", | 67 dom_ui_->RegisterMessageCallback("getApps", |
69 NewCallback(this, &AppLauncherHandler::HandleGetApps)); | 68 NewCallback(this, &AppLauncherHandler::HandleGetApps)); |
70 dom_ui_->RegisterMessageCallback("launchApp", | 69 dom_ui_->RegisterMessageCallback("launchApp", |
71 NewCallback(this, &AppLauncherHandler::HandleLaunchApp)); | 70 NewCallback(this, &AppLauncherHandler::HandleLaunchApp)); |
72 dom_ui_->RegisterMessageCallback("setLaunchType", | |
73 NewCallback(this, &AppLauncherHandler::HandleSetLaunchType)); | |
74 dom_ui_->RegisterMessageCallback("uninstallApp", | 71 dom_ui_->RegisterMessageCallback("uninstallApp", |
75 NewCallback(this, &AppLauncherHandler::HandleUninstallApp)); | 72 NewCallback(this, &AppLauncherHandler::HandleUninstallApp)); |
76 } | 73 } |
77 | 74 |
78 void AppLauncherHandler::Observe(NotificationType type, | 75 void AppLauncherHandler::Observe(NotificationType type, |
79 const NotificationSource& source, | 76 const NotificationSource& source, |
80 const NotificationDetails& details) { | 77 const NotificationDetails& details) { |
81 switch (type.value) { | 78 switch (type.value) { |
82 case NotificationType::EXTENSION_LOADED: | 79 case NotificationType::EXTENSION_LOADED: |
83 case NotificationType::EXTENSION_UNLOADED: | 80 case NotificationType::EXTENSION_UNLOADED: |
84 if (dom_ui_->tab_contents()) | 81 if (dom_ui_->tab_contents()) |
85 HandleGetApps(NULL); | 82 HandleGetApps(NULL); |
86 break; | 83 break; |
87 case NotificationType::PREF_CHANGED: { | |
88 if (!dom_ui_->tab_contents()) | |
89 break; | |
90 | 84 |
91 DictionaryValue dictionary; | |
92 FillAppDictionary(&dictionary); | |
93 dom_ui_->CallJavascriptFunction(L"appsPrefChangeCallback", dictionary); | |
94 break; | |
95 } | |
96 default: | 85 default: |
97 NOTREACHED(); | 86 NOTREACHED(); |
98 } | 87 } |
99 } | 88 } |
100 | 89 |
101 // static | 90 // static |
102 void AppLauncherHandler::CreateAppInfo(Extension* extension, | 91 void AppLauncherHandler::CreateAppInfo(Extension* extension, |
103 ExtensionPrefs* extension_prefs, | |
104 DictionaryValue* value) { | 92 DictionaryValue* value) { |
105 value->Clear(); | 93 value->Clear(); |
106 value->SetString("id", extension->id()); | 94 value->SetString("id", extension->id()); |
107 value->SetString("name", extension->name()); | 95 value->SetString("name", extension->name()); |
108 value->SetString("description", extension->description()); | 96 value->SetString("description", extension->description()); |
109 value->SetString("launch_url", extension->GetFullLaunchURL().spec()); | 97 value->SetString("launch_url", extension->GetFullLaunchURL().spec()); |
110 value->SetString("options_url", extension->options_url().spec()); | 98 value->SetString("options_url", extension->options_url().spec()); |
| 99 |
111 value->SetString("icon_big", GetIconURL( | 100 value->SetString("icon_big", GetIconURL( |
112 extension, Extension::EXTENSION_ICON_LARGE, | 101 extension, Extension::EXTENSION_ICON_LARGE, |
113 "chrome://theme/IDR_APP_DEFAULT_ICON")); | 102 "chrome://theme/IDR_APP_DEFAULT_ICON")); |
114 value->SetString("icon_small", GetIconURL( | 103 value->SetString("icon_small", GetIconURL( |
115 extension, Extension::EXTENSION_ICON_BITTY, | 104 extension, Extension::EXTENSION_ICON_BITTY, |
116 std::string("chrome://favicon/") + extension->GetFullLaunchURL().spec())); | 105 std::string("chrome://favicon/") + extension->GetFullLaunchURL().spec())); |
117 value->SetInteger("launch_container", extension->launch_container()); | |
118 value->SetInteger("launch_type", | |
119 extension_prefs->GetLaunchType(extension->id())); | |
120 } | 106 } |
121 | 107 |
122 void AppLauncherHandler::FillAppDictionary(DictionaryValue* dictionary) { | 108 void AppLauncherHandler::HandleGetApps(const ListValue* args) { |
| 109 bool show_debug_link = CommandLine::ForCurrentProcess()->HasSwitch( |
| 110 switches::kAppsDebug); |
| 111 |
| 112 DictionaryValue dictionary; |
| 113 dictionary.SetBoolean("showDebugLink", show_debug_link); |
| 114 |
123 ListValue* list = new ListValue(); | 115 ListValue* list = new ListValue(); |
124 const ExtensionList* extensions = extensions_service_->extensions(); | 116 const ExtensionList* extensions = extensions_service_->extensions(); |
125 for (ExtensionList::const_iterator it = extensions->begin(); | 117 for (ExtensionList::const_iterator it = extensions->begin(); |
126 it != extensions->end(); ++it) { | 118 it != extensions->end(); ++it) { |
127 // Don't include the WebStore component app. The WebStore launcher | 119 // Don't include the WebStore component app. The WebStore launcher |
128 // gets special treatment in ntp/apps.js. | 120 // gets special treatment in ntp/apps.js. |
129 if ((*it)->is_app() && (*it)->id() != extension_misc::kWebStoreAppId) { | 121 if ((*it)->is_app() && (*it)->id() != extension_misc::kWebStoreAppId) { |
130 DictionaryValue* app_info = new DictionaryValue(); | 122 DictionaryValue* app_info = new DictionaryValue(); |
131 CreateAppInfo(*it, extensions_service_->extension_prefs(), app_info); | 123 CreateAppInfo(*it, app_info); |
132 list->Append(app_info); | 124 list->Append(app_info); |
133 } | 125 } |
134 } | 126 } |
135 dictionary->Set("apps", list); | |
136 } | |
137 | 127 |
138 void AppLauncherHandler::HandleGetApps(const ListValue* args) { | 128 dictionary.Set("apps", list); |
139 DictionaryValue dictionary; | |
140 FillAppDictionary(&dictionary); | |
141 dom_ui_->CallJavascriptFunction(L"getAppsCallback", dictionary); | 129 dom_ui_->CallJavascriptFunction(L"getAppsCallback", dictionary); |
142 | 130 |
143 // First time we get here we set up the observer so that we can tell update | 131 // First time we get here we set up the observer so that we can tell update |
144 // the apps as they change. | 132 // the apps as they change. |
145 if (registrar_.IsEmpty()) { | 133 if (registrar_.IsEmpty()) { |
146 registrar_.Add(this, NotificationType::EXTENSION_LOADED, | 134 registrar_.Add(this, NotificationType::EXTENSION_LOADED, |
147 NotificationService::AllSources()); | 135 NotificationService::AllSources()); |
148 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, | 136 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, |
149 NotificationService::AllSources()); | 137 NotificationService::AllSources()); |
150 } | 138 } |
151 if (pref_change_registrar_.IsEmpty()) { | |
152 pref_change_registrar_.Init( | |
153 extensions_service_->extension_prefs()->pref_service()); | |
154 pref_change_registrar_.Add(ExtensionPrefs::kExtensionsPref, this); | |
155 } | |
156 } | 139 } |
157 | 140 |
158 void AppLauncherHandler::HandleLaunchApp(const ListValue* args) { | 141 void AppLauncherHandler::HandleLaunchApp(const ListValue* args) { |
159 std::string extension_id; | 142 std::string extension_id; |
160 int left = 0; | 143 int left = 0; |
161 int top = 0; | 144 int top = 0; |
162 int width = 0; | 145 int width = 0; |
163 int height = 0; | 146 int height = 0; |
164 | 147 |
165 if (!args->GetString(0, &extension_id) || | 148 if (!args->GetString(0, &extension_id) || |
166 !ExtractInt(args, 1, &left) || | 149 !ExtractInt(args, 1, &left) || |
167 !ExtractInt(args, 2, &top) || | 150 !ExtractInt(args, 2, &top) || |
168 !ExtractInt(args, 3, &width) || | 151 !ExtractInt(args, 3, &width) || |
169 !ExtractInt(args, 4, &height)) { | 152 !ExtractInt(args, 4, &height)) { |
170 NOTREACHED(); | 153 NOTREACHED(); |
171 return; | 154 return; |
172 } | 155 } |
173 | 156 |
174 // The rect we get from the client is relative to the browser client viewport. | 157 // The rect we get from the client is relative to the browser client viewport. |
175 // Offset the rect by the tab contents bounds. | 158 // Offset the rect by the tab contents bounds. |
176 gfx::Rect rect(left, top, width, height); | 159 gfx::Rect rect(left, top, width, height); |
177 gfx::Rect tab_contents_bounds; | 160 gfx::Rect tab_contents_bounds; |
178 dom_ui_->tab_contents()->GetContainerBounds(&tab_contents_bounds); | 161 dom_ui_->tab_contents()->GetContainerBounds(&tab_contents_bounds); |
179 rect.Offset(tab_contents_bounds.origin()); | 162 rect.Offset(tab_contents_bounds.origin()); |
180 | 163 |
181 Extension* extension = | 164 Extension* extension = |
182 extensions_service_->GetExtensionById(extension_id, false); | 165 extensions_service_->GetExtensionById(extension_id, false); |
183 DCHECK(extension); | 166 DCHECK(extension); |
184 Profile* profile = extensions_service_->profile(); | 167 Profile* profile = extensions_service_->profile(); |
| 168 extension_misc::LaunchContainer container = extension->launch_container(); |
185 | 169 |
186 // To give a more "launchy" experience when using the NTP launcher, we close | 170 // To give a more "launchy" experience when using the NTP launcher, we close |
187 // it automatically. | 171 // it automatically. |
188 Browser* browser = BrowserList::GetLastActive(); | 172 Browser* browser = BrowserList::GetLastActive(); |
189 TabContents* old_contents = NULL; | 173 TabContents* old_contents = NULL; |
190 if (browser) | 174 if (browser) |
191 old_contents = browser->GetSelectedTabContents(); | 175 old_contents = browser->GetSelectedTabContents(); |
192 | 176 |
193 AnimateAppIcon(extension, rect); | 177 AnimateAppIcon(extension, rect); |
194 Browser::OpenApplication(profile, extension, extension->launch_container()); | 178 Browser::OpenApplication(profile, extension, container); |
195 | 179 |
196 if (old_contents && | 180 if (old_contents && |
197 old_contents->GetURL().GetOrigin() == | 181 old_contents->GetURL().GetOrigin() == |
198 GURL(chrome::kChromeUINewTabURL).GetOrigin()) { | 182 GURL(chrome::kChromeUINewTabURL).GetOrigin()) { |
199 browser->CloseTabContents(old_contents); | 183 browser->CloseTabContents(old_contents); |
200 } | 184 } |
201 } | 185 } |
202 | 186 |
203 void AppLauncherHandler::HandleSetLaunchType(const ListValue* args) { | |
204 std::string extension_id; | |
205 int launch_type; | |
206 if (!args->GetString(0, &extension_id) || | |
207 !ExtractInt(args, 1, &launch_type)) { | |
208 NOTREACHED(); | |
209 return; | |
210 } | |
211 | |
212 Extension* extension = | |
213 extensions_service_->GetExtensionById(extension_id, false); | |
214 DCHECK(extension); | |
215 | |
216 extensions_service_->extension_prefs()->SetLaunchType( | |
217 extension_id, | |
218 static_cast<ExtensionPrefs::LaunchType>(launch_type)); | |
219 } | |
220 | |
221 void AppLauncherHandler::AnimateAppIcon(Extension* extension, | 187 void AppLauncherHandler::AnimateAppIcon(Extension* extension, |
222 const gfx::Rect& rect) { | 188 const gfx::Rect& rect) { |
223 // We make this check for the case of minimized windows, unit tests, etc. | 189 // We make this check for the case of minimized windows, unit tests, etc. |
224 if (platform_util::IsVisible(dom_ui_->tab_contents()->GetNativeView()) && | 190 if (platform_util::IsVisible(dom_ui_->tab_contents()->GetNativeView()) && |
225 Animation::ShouldRenderRichAnimation()) { | 191 Animation::ShouldRenderRichAnimation()) { |
226 #if defined(OS_WIN) | 192 #if defined(OS_WIN) |
227 AppLaunchedAnimation::Show(extension, rect); | 193 AppLaunchedAnimation::Show(extension, rect); |
228 #else | 194 #else |
229 NOTIMPLEMENTED(); | 195 NOTIMPLEMENTED(); |
230 #endif | 196 #endif |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 return; | 228 return; |
263 | 229 |
264 extensions_service_->UninstallExtension(extension_id_prompting_, | 230 extensions_service_->UninstallExtension(extension_id_prompting_, |
265 false /* external_uninstall */); | 231 false /* external_uninstall */); |
266 extension_id_prompting_ = ""; | 232 extension_id_prompting_ = ""; |
267 } | 233 } |
268 | 234 |
269 void AppLauncherHandler::InstallUIAbort() { | 235 void AppLauncherHandler::InstallUIAbort() { |
270 extension_id_prompting_ = ""; | 236 extension_id_prompting_ = ""; |
271 } | 237 } |
OLD | NEW |