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

Side by Side Diff: chrome/browser/extensions/tab_helper.cc

Issue 64853004: Use high resolution icons where possible for streamlined hosted app icons. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@browser_experiment_create_app_from_page
Patch Set: add RenderViewImpl test to ensure FaviconTabHelper is not sent an empty vector Created 7 years 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/extensions/tab_helper.h" 5 #include "chrome/browser/extensions/tab_helper.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h" 10 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/activity_log/activity_log.h" 11 #include "chrome/browser/extensions/activity_log/activity_log.h"
11 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" 12 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
12 #include "chrome/browser/extensions/api/declarative_content/content_rules_regist ry.h" 13 #include "chrome/browser/extensions/api/declarative_content/content_rules_regist ry.h"
13 #include "chrome/browser/extensions/crx_installer.h" 14 #include "chrome/browser/extensions/crx_installer.h"
14 #include "chrome/browser/extensions/error_console/error_console.h" 15 #include "chrome/browser/extensions/error_console/error_console.h"
15 #include "chrome/browser/extensions/extension_action.h" 16 #include "chrome/browser/extensions/extension_action.h"
16 #include "chrome/browser/extensions/extension_action_manager.h" 17 #include "chrome/browser/extensions/extension_action_manager.h"
17 #include "chrome/browser/extensions/extension_service.h" 18 #include "chrome/browser/extensions/extension_service.h"
18 #include "chrome/browser/extensions/extension_system.h" 19 #include "chrome/browser/extensions/extension_system.h"
19 #include "chrome/browser/extensions/extension_tab_util.h" 20 #include "chrome/browser/extensions/extension_tab_util.h"
21 #include "chrome/browser/extensions/favicon_downloader.h"
20 #include "chrome/browser/extensions/image_loader.h" 22 #include "chrome/browser/extensions/image_loader.h"
21 #include "chrome/browser/extensions/page_action_controller.h" 23 #include "chrome/browser/extensions/page_action_controller.h"
22 #include "chrome/browser/extensions/script_badge_controller.h" 24 #include "chrome/browser/extensions/script_badge_controller.h"
23 #include "chrome/browser/extensions/script_bubble_controller.h" 25 #include "chrome/browser/extensions/script_bubble_controller.h"
24 #include "chrome/browser/extensions/script_executor.h" 26 #include "chrome/browser/extensions/script_executor.h"
25 #include "chrome/browser/extensions/webstore_inline_installer.h" 27 #include "chrome/browser/extensions/webstore_inline_installer.h"
26 #include "chrome/browser/extensions/webstore_inline_installer_factory.h" 28 #include "chrome/browser/extensions/webstore_inline_installer_factory.h"
27 #include "chrome/browser/profiles/profile.h" 29 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/browser/sessions/session_id.h" 30 #include "chrome/browser/sessions/session_id.h"
29 #include "chrome/browser/sessions/session_tab_helper.h" 31 #include "chrome/browser/sessions/session_tab_helper.h"
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 OnContentScriptsExecuting) 274 OnContentScriptsExecuting)
273 IPC_MESSAGE_HANDLER(ExtensionHostMsg_OnWatchedPageChange, 275 IPC_MESSAGE_HANDLER(ExtensionHostMsg_OnWatchedPageChange,
274 OnWatchedPageChange) 276 OnWatchedPageChange)
275 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DetailedConsoleMessageAdded, 277 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DetailedConsoleMessageAdded,
276 OnDetailedConsoleMessageAdded) 278 OnDetailedConsoleMessageAdded)
277 IPC_MESSAGE_UNHANDLED(handled = false) 279 IPC_MESSAGE_UNHANDLED(handled = false)
278 IPC_END_MESSAGE_MAP() 280 IPC_END_MESSAGE_MAP()
279 return handled; 281 return handled;
280 } 282 }
281 283
284 void TabHelper::CreateHostedApp() {
285 // Add urls from the WebApplicationInfo.
286 std::vector<GURL> web_app_info_icon_urls;
287 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it =
288 web_app_info_.icons.begin();
289 it != web_app_info_.icons.end(); ++it) {
290 if (it->url.is_valid())
291 web_app_info_icon_urls.push_back(it->url);
292 }
293
294 favicon_downloader_.reset(
295 new FaviconDownloader(web_contents(),
296 web_app_info_icon_urls,
297 base::Bind(&TabHelper::FinishCreateHostedApp,
298 base::Unretained(this))));
299 favicon_downloader_->Start();
300 }
301
302 void TabHelper::FinishCreateHostedApp(
303 bool success,
304 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) {
305 // The tab has navigated away during the icon download. Cancel the hosted app
306 // creation.
307 if (!success) {
308 favicon_downloader_.reset();
309 return;
310 }
311
312 WebApplicationInfo install_info(web_app_info_);
313 if (install_info.app_url.is_empty())
314 install_info.app_url = web_contents()->GetURL();
315
316 if (install_info.title.empty())
317 install_info.title = web_contents()->GetTitle();
318 if (install_info.title.empty())
319 install_info.title = UTF8ToUTF16(install_info.app_url.spec());
320
321 install_info.urls.push_back(install_info.app_url);
322 install_info.is_bookmark_app = true;
323
324 // Add the downloaded icons.
325 for (FaviconDownloader::FaviconMap::const_iterator map_it = bitmaps.begin();
326 map_it != bitmaps.end(); ++map_it) {
327 for (std::vector<SkBitmap>::const_iterator bitmap_it =
328 map_it->second.begin();
329 bitmap_it != map_it->second.end(); ++bitmap_it) {
330 if (!bitmap_it->empty()) {
331 WebApplicationInfo::IconInfo icon_info;
332 icon_info.data = *bitmap_it;
333 icon_info.width = icon_info.data.width();
334 icon_info.height = icon_info.data.height();
335 install_info.icons.push_back(icon_info);
336 }
337 }
338 }
339
340 Profile* profile =
341 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
342 scoped_refptr<extensions::CrxInstaller> installer(
343 extensions::CrxInstaller::CreateSilent(profile->GetExtensionService()));
344 installer->set_error_on_unsupported_requirements(true);
345 installer->InstallWebApp(install_info);
346 }
347
282 void TabHelper::DidCloneToNewWebContents(WebContents* old_web_contents, 348 void TabHelper::DidCloneToNewWebContents(WebContents* old_web_contents,
283 WebContents* new_web_contents) { 349 WebContents* new_web_contents) {
284 // When the WebContents that this is attached to is cloned, give the new clone 350 // When the WebContents that this is attached to is cloned, give the new clone
285 // a TabHelper and copy state over. 351 // a TabHelper and copy state over.
286 CreateForWebContents(new_web_contents); 352 CreateForWebContents(new_web_contents);
287 TabHelper* new_helper = FromWebContents(new_web_contents); 353 TabHelper* new_helper = FromWebContents(new_web_contents);
288 354
289 new_helper->SetExtensionApp(extension_app()); 355 new_helper->SetExtensionApp(extension_app());
290 new_helper->extension_app_icon_ = extension_app_icon_; 356 new_helper->extension_app_icon_ = extension_app_icon_;
291 } 357 }
(...skipping 10 matching lines...) Expand all
302 return; 368 return;
303 369
304 switch (pending_web_app_action_) { 370 switch (pending_web_app_action_) {
305 case CREATE_SHORTCUT: { 371 case CREATE_SHORTCUT: {
306 chrome::ShowCreateWebAppShortcutsDialog( 372 chrome::ShowCreateWebAppShortcutsDialog(
307 web_contents()->GetView()->GetTopLevelNativeWindow(), 373 web_contents()->GetView()->GetTopLevelNativeWindow(),
308 web_contents()); 374 web_contents());
309 break; 375 break;
310 } 376 }
311 case CREATE_HOSTED_APP: { 377 case CREATE_HOSTED_APP: {
312 CreateHostedApp(info); 378 CreateHostedApp();
313 break; 379 break;
314 } 380 }
315 case UPDATE_SHORTCUT: { 381 case UPDATE_SHORTCUT: {
316 web_app::UpdateShortcutForTabContents(web_contents()); 382 web_app::UpdateShortcutForTabContents(web_contents());
317 break; 383 break;
318 } 384 }
319 default: 385 default:
320 NOTREACHED(); 386 NOTREACHED();
321 break; 387 break;
322 } 388 }
323 389
324 // The hosted app action will be cleared once the installation completes or 390 // The hosted app action will be cleared once the installation completes or
325 // fails. 391 // fails.
326 if (pending_web_app_action_ != CREATE_HOSTED_APP) 392 if (pending_web_app_action_ != CREATE_HOSTED_APP)
327 pending_web_app_action_ = NONE; 393 pending_web_app_action_ = NONE;
328 #endif 394 #endif
329 } 395 }
330 396
331 void TabHelper::CreateHostedApp(const WebApplicationInfo& info) {
332 ShellIntegration::ShortcutInfo shortcut_info;
333 web_app::GetShortcutInfoForTab(web_contents(), &shortcut_info);
benwells 2013/12/12 21:17:57 Is GetShortcutInfoForTab still used?
calamity 2013/12/13 03:11:55 Yes, in the create applications shortcut dialog =(
334 WebApplicationInfo web_app_info;
335
336 web_app_info.is_bookmark_app = true;
337 web_app_info.app_url = shortcut_info.url;
338 web_app_info.title = shortcut_info.title;
339 web_app_info.urls.push_back(web_app_info.app_url);
340
341 // TODO(calamity): this should attempt to download the best icon that it can
342 // from |info.icons| rather than just using the favicon as it scales up badly.
343 // Fix this once |info.icons| gets populated commonly.
344
345 // Get the smallest icon in the icon family (should have only 1).
346 const gfx::Image* icon = shortcut_info.favicon.GetBest(0, 0);
347 SkBitmap bitmap = icon ? icon->AsBitmap() : SkBitmap();
348
349 if (!icon->IsEmpty()) {
350 WebApplicationInfo::IconInfo icon_info;
351 icon_info.data = bitmap;
352 icon_info.width = icon_info.data.width();
353 icon_info.height = icon_info.data.height();
354 web_app_info.icons.push_back(icon_info);
355 }
356
357 ExtensionService* service = profile_->GetExtensionService();
358 scoped_refptr<extensions::CrxInstaller> installer(
359 extensions::CrxInstaller::CreateSilent(service));
360 installer->set_error_on_unsupported_requirements(true);
361 installer->InstallWebApp(web_app_info);
362 }
363
364 void TabHelper::OnInlineWebstoreInstall( 397 void TabHelper::OnInlineWebstoreInstall(
365 int install_id, 398 int install_id,
366 int return_route_id, 399 int return_route_id,
367 const std::string& webstore_item_id, 400 const std::string& webstore_item_id,
368 const GURL& requestor_url) { 401 const GURL& requestor_url) {
369 WebstoreStandaloneInstaller::Callback callback = 402 WebstoreStandaloneInstaller::Callback callback =
370 base::Bind(&TabHelper::OnInlineInstallComplete, base::Unretained(this), 403 base::Bind(&TabHelper::OnInlineInstallComplete, base::Unretained(this),
371 install_id, return_route_id); 404 install_id, return_route_id);
372 scoped_refptr<WebstoreInlineInstaller> installer( 405 scoped_refptr<WebstoreInlineInstaller> installer(
373 webstore_inline_installer_factory_->CreateInstaller( 406 webstore_inline_installer_factory_->CreateInstaller(
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 GetApplicationInfo(entry->GetPageID()); 574 GetApplicationInfo(entry->GetPageID());
542 else 575 else
543 pending_web_app_action_ = NONE; 576 pending_web_app_action_ = NONE;
544 } 577 }
545 break; 578 break;
546 } 579 }
547 case chrome::NOTIFICATION_CRX_INSTALLER_DONE: { 580 case chrome::NOTIFICATION_CRX_INSTALLER_DONE: {
548 if (pending_web_app_action_ != CREATE_HOSTED_APP) 581 if (pending_web_app_action_ != CREATE_HOSTED_APP)
549 return; 582 return;
550 583
584 favicon_downloader_.reset();
benwells 2013/12/12 21:17:57 Can the downloader be removed once it is finished,
calamity 2013/12/13 03:11:55 Done.
551 pending_web_app_action_ = NONE; 585 pending_web_app_action_ = NONE;
552 586
553 const Extension* extension = 587 const Extension* extension =
554 content::Details<const Extension>(details).ptr(); 588 content::Details<const Extension>(details).ptr();
555 if (!extension || !extension->from_bookmark()) 589 if (!extension || !extension->from_bookmark())
556 return; 590 return;
557 591
558 // If enabled, launch the app launcher and highlight the new app. 592 // If enabled, launch the app launcher and highlight the new app.
559 // Otherwise, open the chrome://apps page in a new foreground tab. 593 // Otherwise, open the chrome://apps page in a new foreground tab.
560 if (IsAppLauncherEnabled()) { 594 if (IsAppLauncherEnabled()) {
(...skipping 16 matching lines...) Expand all
577 browser->OpenURL( 611 browser->OpenURL(
578 content::OpenURLParams(GURL(chrome::kChromeUIAppsURL), 612 content::OpenURLParams(GURL(chrome::kChromeUIAppsURL),
579 content::Referrer(), 613 content::Referrer(),
580 NEW_FOREGROUND_TAB, 614 NEW_FOREGROUND_TAB,
581 content::PAGE_TRANSITION_LINK, 615 content::PAGE_TRANSITION_LINK,
582 false)); 616 false));
583 } 617 }
584 #endif 618 #endif
585 } 619 }
586 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: { 620 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: {
587 if (pending_web_app_action_ == CREATE_HOSTED_APP) 621 if (pending_web_app_action_ == CREATE_HOSTED_APP) {
622 favicon_downloader_.reset();
588 pending_web_app_action_ = NONE; 623 pending_web_app_action_ = NONE;
624 }
589 break; 625 break;
590 } 626 }
591 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { 627 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
592 if (script_bubble_controller_) { 628 if (script_bubble_controller_) {
593 script_bubble_controller_->OnExtensionUnloaded( 629 script_bubble_controller_->OnExtensionUnloaded(
594 content::Details<extensions::UnloadedExtensionInfo>( 630 content::Details<extensions::UnloadedExtensionInfo>(
595 details)->extension->id()); 631 details)->extension->id());
596 break; 632 break;
597 } 633 }
598 } 634 }
599 } 635 }
600 } 636 }
601 637
602 void TabHelper::SetTabId(RenderViewHost* render_view_host) { 638 void TabHelper::SetTabId(RenderViewHost* render_view_host) {
603 render_view_host->Send( 639 render_view_host->Send(
604 new ExtensionMsg_SetTabId(render_view_host->GetRoutingID(), 640 new ExtensionMsg_SetTabId(render_view_host->GetRoutingID(),
605 SessionID::IdForTab(web_contents()))); 641 SessionID::IdForTab(web_contents())));
606 } 642 }
607 643
608 } // namespace extensions 644 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698