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

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: move favicon_downloader* to c/b/extensions 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 // This will create the hosted app on icon download completion.
379 CreateHostedApp();
313 break; 380 break;
314 } 381 }
315 case UPDATE_SHORTCUT: { 382 case UPDATE_SHORTCUT: {
316 web_app::UpdateShortcutForTabContents(web_contents()); 383 web_app::UpdateShortcutForTabContents(web_contents());
317 break; 384 break;
318 } 385 }
319 default: 386 default:
320 NOTREACHED(); 387 NOTREACHED();
321 break; 388 break;
322 } 389 }
323 390
324 // The hosted app action will be cleared once the installation completes or 391 // The hosted app action will be cleared once the installation completes or
325 // fails. 392 // fails.
326 if (pending_web_app_action_ != CREATE_HOSTED_APP) 393 if (pending_web_app_action_ != CREATE_HOSTED_APP)
327 pending_web_app_action_ = NONE; 394 pending_web_app_action_ = NONE;
328 #endif 395 #endif
329 } 396 }
330 397
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( 398 void TabHelper::OnInlineWebstoreInstall(
365 int install_id, 399 int install_id,
366 int return_route_id, 400 int return_route_id,
367 const std::string& webstore_item_id, 401 const std::string& webstore_item_id,
368 const GURL& requestor_url) { 402 const GURL& requestor_url) {
369 WebstoreStandaloneInstaller::Callback callback = 403 WebstoreStandaloneInstaller::Callback callback =
370 base::Bind(&TabHelper::OnInlineInstallComplete, base::Unretained(this), 404 base::Bind(&TabHelper::OnInlineInstallComplete, base::Unretained(this),
371 install_id, return_route_id); 405 install_id, return_route_id);
372 scoped_refptr<WebstoreInlineInstaller> installer( 406 scoped_refptr<WebstoreInlineInstaller> installer(
373 webstore_inline_installer_factory_->CreateInstaller( 407 webstore_inline_installer_factory_->CreateInstaller(
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 GetApplicationInfo(entry->GetPageID()); 575 GetApplicationInfo(entry->GetPageID());
542 else 576 else
543 pending_web_app_action_ = NONE; 577 pending_web_app_action_ = NONE;
544 } 578 }
545 break; 579 break;
546 } 580 }
547 case chrome::NOTIFICATION_CRX_INSTALLER_DONE: { 581 case chrome::NOTIFICATION_CRX_INSTALLER_DONE: {
548 if (pending_web_app_action_ != CREATE_HOSTED_APP) 582 if (pending_web_app_action_ != CREATE_HOSTED_APP)
549 return; 583 return;
550 584
585 favicon_downloader_.reset();
551 pending_web_app_action_ = NONE; 586 pending_web_app_action_ = NONE;
552 587
553 const Extension* extension = 588 const Extension* extension =
554 content::Details<const Extension>(details).ptr(); 589 content::Details<const Extension>(details).ptr();
555 if (!extension || !extension->from_bookmark()) 590 if (!extension || !extension->from_bookmark())
556 return; 591 return;
557 592
558 // If enabled, launch the app launcher and highlight the new app. 593 // If enabled, launch the app launcher and highlight the new app.
559 // Otherwise, open the chrome://apps page in a new foreground tab. 594 // Otherwise, open the chrome://apps page in a new foreground tab.
560 if (IsAppLauncherEnabled()) { 595 if (IsAppLauncherEnabled()) {
(...skipping 16 matching lines...) Expand all
577 browser->OpenURL( 612 browser->OpenURL(
578 content::OpenURLParams(GURL(chrome::kChromeUIAppsURL), 613 content::OpenURLParams(GURL(chrome::kChromeUIAppsURL),
579 content::Referrer(), 614 content::Referrer(),
580 NEW_FOREGROUND_TAB, 615 NEW_FOREGROUND_TAB,
581 content::PAGE_TRANSITION_LINK, 616 content::PAGE_TRANSITION_LINK,
582 false)); 617 false));
583 } 618 }
584 #endif 619 #endif
585 } 620 }
586 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: { 621 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: {
587 if (pending_web_app_action_ == CREATE_HOSTED_APP) 622 if (pending_web_app_action_ == CREATE_HOSTED_APP) {
623 favicon_downloader_.reset();
588 pending_web_app_action_ = NONE; 624 pending_web_app_action_ = NONE;
625 }
589 break; 626 break;
590 } 627 }
591 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { 628 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
592 if (script_bubble_controller_) { 629 if (script_bubble_controller_) {
593 script_bubble_controller_->OnExtensionUnloaded( 630 script_bubble_controller_->OnExtensionUnloaded(
594 content::Details<extensions::UnloadedExtensionInfo>( 631 content::Details<extensions::UnloadedExtensionInfo>(
595 details)->extension->id()); 632 details)->extension->id());
596 break; 633 break;
597 } 634 }
598 } 635 }
599 } 636 }
600 } 637 }
601 638
602 void TabHelper::SetTabId(RenderViewHost* render_view_host) { 639 void TabHelper::SetTabId(RenderViewHost* render_view_host) {
603 render_view_host->Send( 640 render_view_host->Send(
604 new ExtensionMsg_SetTabId(render_view_host->GetRoutingID(), 641 new ExtensionMsg_SetTabId(render_view_host->GetRoutingID(),
605 SessionID::IdForTab(web_contents()))); 642 SessionID::IdForTab(web_contents())));
606 } 643 }
607 644
608 } // namespace extensions 645 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698