Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(349)

Side by Side Diff: chrome/browser/dom_ui/app_launcher_handler.cc

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

Powered by Google App Engine
This is Rietveld 408576698