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

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: mass refactor: pull code into FaviconDownloader class 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"
20 #include "chrome/browser/extensions/image_loader.h" 21 #include "chrome/browser/extensions/image_loader.h"
21 #include "chrome/browser/extensions/page_action_controller.h" 22 #include "chrome/browser/extensions/page_action_controller.h"
22 #include "chrome/browser/extensions/script_badge_controller.h" 23 #include "chrome/browser/extensions/script_badge_controller.h"
23 #include "chrome/browser/extensions/script_bubble_controller.h" 24 #include "chrome/browser/extensions/script_bubble_controller.h"
24 #include "chrome/browser/extensions/script_executor.h" 25 #include "chrome/browser/extensions/script_executor.h"
25 #include "chrome/browser/extensions/webstore_inline_installer.h" 26 #include "chrome/browser/extensions/webstore_inline_installer.h"
26 #include "chrome/browser/extensions/webstore_inline_installer_factory.h" 27 #include "chrome/browser/extensions/webstore_inline_installer_factory.h"
28 #include "chrome/browser/favicon/favicon_downloader.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"
30 #include "chrome/browser/shell_integration.h" 32 #include "chrome/browser/shell_integration.h"
31 #include "chrome/browser/ui/app_list/app_list_service.h" 33 #include "chrome/browser/ui/app_list/app_list_service.h"
32 #include "chrome/browser/ui/app_list/app_list_util.h" 34 #include "chrome/browser/ui/app_list/app_list_util.h"
33 #include "chrome/browser/ui/browser_commands.h" 35 #include "chrome/browser/ui/browser_commands.h"
34 #include "chrome/browser/ui/browser_dialogs.h" 36 #include "chrome/browser/ui/browser_dialogs.h"
35 #include "chrome/browser/ui/browser_finder.h" 37 #include "chrome/browser/ui/browser_finder.h"
36 #include "chrome/browser/ui/host_desktop.h" 38 #include "chrome/browser/ui/host_desktop.h"
(...skipping 235 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 favicon_downloader_.reset(
286 new FaviconDownloader(web_contents(),
287 base::Bind(&TabHelper::FinishCreateHostedApp,
288 base::Unretained(this))));
289
290 // Add urls from the WebApplicationInfo.
291 std::vector<GURL> web_app_info_icon_urls;
292 for (std::vector<WebApplicationInfo::IconInfo>::const_iterator it =
293 web_app_info_.icons.begin();
294 it != web_app_info_.icons.end(); ++it) {
295 if (it->url.is_valid())
296 favicon_downloader_->AddExtraFaviconUrl(it->url);
297 }
298 favicon_downloader_->Start();
299 }
300
301 void TabHelper::FinishCreateHostedApp(
302 bool success,
303 const std::map<GURL, std::vector<SkBitmap> >& bitmaps) {
304 // The tab has navigated away during the icon download. Cancel the hosted app
305 // creation.
306 if (!success) {
307 favicon_downloader_.reset();
308 return;
309 }
310
311 WebApplicationInfo install_info(web_app_info_);
312 if (install_info.app_url.is_empty())
313 install_info.app_url = web_contents()->GetURL();
314
315 if (install_info.title.empty())
316 install_info.title = web_contents()->GetTitle();
317 if (install_info.title.empty())
318 install_info.title = UTF8ToUTF16(install_info.app_url.spec());
319
320 install_info.urls.push_back(install_info.app_url);
321 install_info.is_bookmark_app = true;
322
323 // Add the downloaded icons.
324 for (FaviconDownloader::FaviconMap::const_iterator map_it = bitmaps.begin();
325 map_it != bitmaps.end(); ++map_it) {
326 for (std::vector<SkBitmap>::const_iterator bitmap_it =
327 map_it->second.begin();
328 bitmap_it != map_it->second.end(); ++bitmap_it) {
329 if (!bitmap_it->empty()) {
330 WebApplicationInfo::IconInfo icon_info;
331 icon_info.data = *bitmap_it;
332 icon_info.width = icon_info.data.width();
333 icon_info.height = icon_info.data.height();
334 install_info.icons.push_back(icon_info);
335 }
336 }
337 }
338
339 Profile* profile =
340 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
341 scoped_refptr<extensions::CrxInstaller> installer(
342 extensions::CrxInstaller::CreateSilent(profile->GetExtensionService()));
343 installer->set_error_on_unsupported_requirements(true);
344 installer->InstallWebApp(install_info);
345 }
346
282 void TabHelper::DidCloneToNewWebContents(WebContents* old_web_contents, 347 void TabHelper::DidCloneToNewWebContents(WebContents* old_web_contents,
283 WebContents* new_web_contents) { 348 WebContents* new_web_contents) {
284 // When the WebContents that this is attached to is cloned, give the new clone 349 // When the WebContents that this is attached to is cloned, give the new clone
285 // a TabHelper and copy state over. 350 // a TabHelper and copy state over.
286 CreateForWebContents(new_web_contents); 351 CreateForWebContents(new_web_contents);
287 TabHelper* new_helper = FromWebContents(new_web_contents); 352 TabHelper* new_helper = FromWebContents(new_web_contents);
288 353
289 new_helper->SetExtensionApp(extension_app()); 354 new_helper->SetExtensionApp(extension_app());
290 new_helper->extension_app_icon_ = extension_app_icon_; 355 new_helper->extension_app_icon_ = extension_app_icon_;
291 } 356 }
(...skipping 10 matching lines...) Expand all
302 return; 367 return;
303 368
304 switch (pending_web_app_action_) { 369 switch (pending_web_app_action_) {
305 case CREATE_SHORTCUT: { 370 case CREATE_SHORTCUT: {
306 chrome::ShowCreateWebAppShortcutsDialog( 371 chrome::ShowCreateWebAppShortcutsDialog(
307 web_contents()->GetView()->GetTopLevelNativeWindow(), 372 web_contents()->GetView()->GetTopLevelNativeWindow(),
308 web_contents()); 373 web_contents());
309 break; 374 break;
310 } 375 }
311 case CREATE_HOSTED_APP: { 376 case CREATE_HOSTED_APP: {
312 CreateHostedApp(info); 377 // This will create the hosted app on icon download completion.
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);
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();
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