| OLD | NEW |
| 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 "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 #include "content/public/browser/render_view_host.h" | 56 #include "content/public/browser/render_view_host.h" |
| 57 #include "content/public/browser/render_widget_host_view.h" | 57 #include "content/public/browser/render_widget_host_view.h" |
| 58 #include "content/public/browser/web_contents.h" | 58 #include "content/public/browser/web_contents.h" |
| 59 #include "content/public/browser/web_contents_view.h" | 59 #include "content/public/browser/web_contents_view.h" |
| 60 #include "content/public/common/frame_navigate_params.h" | 60 #include "content/public/common/frame_navigate_params.h" |
| 61 #include "extensions/browser/extension_error.h" | 61 #include "extensions/browser/extension_error.h" |
| 62 #include "extensions/common/extension.h" | 62 #include "extensions/common/extension.h" |
| 63 #include "extensions/common/extension_resource.h" | 63 #include "extensions/common/extension_resource.h" |
| 64 #include "extensions/common/extension_urls.h" | 64 #include "extensions/common/extension_urls.h" |
| 65 #include "extensions/common/feature_switch.h" | 65 #include "extensions/common/feature_switch.h" |
| 66 #include "skia/ext/image_operations.h" |
| 66 #include "ui/gfx/image/image.h" | 67 #include "ui/gfx/image/image.h" |
| 67 | 68 |
| 68 using content::NavigationController; | 69 using content::NavigationController; |
| 69 using content::NavigationEntry; | 70 using content::NavigationEntry; |
| 70 using content::RenderViewHost; | 71 using content::RenderViewHost; |
| 71 using content::WebContents; | 72 using content::WebContents; |
| 72 | 73 |
| 73 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::TabHelper); | 74 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::TabHelper); |
| 74 | 75 |
| 75 namespace extensions { | 76 namespace extensions { |
| 76 | 77 |
| 77 TabHelper::ScriptExecutionObserver::ScriptExecutionObserver( | 78 TabHelper::ScriptExecutionObserver::ScriptExecutionObserver( |
| 78 TabHelper* tab_helper) | 79 TabHelper* tab_helper) |
| 79 : tab_helper_(tab_helper) { | 80 : tab_helper_(tab_helper) { |
| 80 tab_helper_->AddScriptExecutionObserver(this); | 81 tab_helper_->AddScriptExecutionObserver(this); |
| 81 } | 82 } |
| 82 | 83 |
| 83 TabHelper::ScriptExecutionObserver::ScriptExecutionObserver() | 84 TabHelper::ScriptExecutionObserver::ScriptExecutionObserver() |
| 84 : tab_helper_(NULL) { | 85 : tab_helper_(NULL) { |
| 85 } | 86 } |
| 86 | 87 |
| 87 TabHelper::ScriptExecutionObserver::~ScriptExecutionObserver() { | 88 TabHelper::ScriptExecutionObserver::~ScriptExecutionObserver() { |
| 88 if (tab_helper_) | 89 if (tab_helper_) |
| 89 tab_helper_->RemoveScriptExecutionObserver(this); | 90 tab_helper_->RemoveScriptExecutionObserver(this); |
| 90 } | 91 } |
| 91 | 92 |
| 93 // static |
| 94 std::vector<SkBitmap> TabHelper::ConstrainBitmapsToSizes( |
| 95 const std::vector<SkBitmap>& bitmaps, |
| 96 const std::set<int>& sizes) { |
| 97 std::vector<SkBitmap> output_bitmaps; |
| 98 std::map<int, SkBitmap> ordered_bitmaps; |
| 99 for (std::vector<SkBitmap>::const_iterator it = bitmaps.begin(); |
| 100 it != bitmaps.end(); ++it) { |
| 101 DCHECK(it->width() == it->height()); |
| 102 ordered_bitmaps[it->width()] = *it; |
| 103 } |
| 104 |
| 105 std::set<int>::const_iterator sizes_it = sizes.begin(); |
| 106 std::map<int, SkBitmap>::const_iterator bitmaps_it = ordered_bitmaps.begin(); |
| 107 while (sizes_it != sizes.end() && bitmaps_it != ordered_bitmaps.end()) { |
| 108 int size = *sizes_it; |
| 109 // Find the closest not-smaller bitmap. |
| 110 bitmaps_it = ordered_bitmaps.lower_bound(size); |
| 111 ++sizes_it; |
| 112 // Ensure the bitmap is valid and smaller than the next allowed size. |
| 113 if (bitmaps_it != ordered_bitmaps.end() && |
| 114 (sizes_it == sizes.end() || bitmaps_it->second.width() < *sizes_it)) { |
| 115 // Resize the bitmap if it does not exactly match the desired size. |
| 116 output_bitmaps.push_back(bitmaps_it->second.width() == size |
| 117 ? bitmaps_it->second |
| 118 : skia::ImageOperations::Resize( |
| 119 bitmaps_it->second, skia::ImageOperations::RESIZE_LANCZOS3, |
| 120 size, size)); |
| 121 } |
| 122 } |
| 123 return output_bitmaps; |
| 124 } |
| 125 |
| 92 TabHelper::TabHelper(content::WebContents* web_contents) | 126 TabHelper::TabHelper(content::WebContents* web_contents) |
| 93 : content::WebContentsObserver(web_contents), | 127 : content::WebContentsObserver(web_contents), |
| 94 extension_app_(NULL), | 128 extension_app_(NULL), |
| 95 extension_function_dispatcher_( | 129 extension_function_dispatcher_( |
| 96 Profile::FromBrowserContext(web_contents->GetBrowserContext()), this), | 130 Profile::FromBrowserContext(web_contents->GetBrowserContext()), this), |
| 97 pending_web_app_action_(NONE), | 131 pending_web_app_action_(NONE), |
| 98 script_executor_(new ScriptExecutor(web_contents, | 132 script_executor_(new ScriptExecutor(web_contents, |
| 99 &script_execution_observers_)), | 133 &script_execution_observers_)), |
| 100 image_loader_ptr_factory_(this), | 134 image_loader_ptr_factory_(this), |
| 101 webstore_inline_installer_factory_(new WebstoreInlineInstallerFactory()) { | 135 webstore_inline_installer_factory_(new WebstoreInlineInstallerFactory()) { |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 install_info.app_url = web_contents()->GetURL(); | 351 install_info.app_url = web_contents()->GetURL(); |
| 318 | 352 |
| 319 if (install_info.title.empty()) | 353 if (install_info.title.empty()) |
| 320 install_info.title = web_contents()->GetTitle(); | 354 install_info.title = web_contents()->GetTitle(); |
| 321 if (install_info.title.empty()) | 355 if (install_info.title.empty()) |
| 322 install_info.title = base::UTF8ToUTF16(install_info.app_url.spec()); | 356 install_info.title = base::UTF8ToUTF16(install_info.app_url.spec()); |
| 323 | 357 |
| 324 install_info.urls.push_back(install_info.app_url); | 358 install_info.urls.push_back(install_info.app_url); |
| 325 install_info.is_bookmark_app = true; | 359 install_info.is_bookmark_app = true; |
| 326 | 360 |
| 327 // Add the downloaded icons. | 361 // Add the downloaded icons. Extensions only allow certain icon sizes. First |
| 362 // populate icons that match the allowed sizes exactly and then downscale |
| 363 // remaining icons to the closest allowed size that doesn't yet have an icon. |
| 364 std::set<int> allowed_sizes( |
| 365 extension_misc::kExtensionIconSizes, |
| 366 extension_misc::kExtensionIconSizes + |
| 367 extension_misc::kNumExtensionIconSizes); |
| 368 std::vector<SkBitmap> downloaded_icons; |
| 328 for (FaviconDownloader::FaviconMap::const_iterator map_it = bitmaps.begin(); | 369 for (FaviconDownloader::FaviconMap::const_iterator map_it = bitmaps.begin(); |
| 329 map_it != bitmaps.end(); ++map_it) { | 370 map_it != bitmaps.end(); ++map_it) { |
| 330 for (std::vector<SkBitmap>::const_iterator bitmap_it = | 371 for (std::vector<SkBitmap>::const_iterator bitmap_it = |
| 331 map_it->second.begin(); | 372 map_it->second.begin(); |
| 332 bitmap_it != map_it->second.end(); ++bitmap_it) { | 373 bitmap_it != map_it->second.end(); ++bitmap_it) { |
| 333 if (!bitmap_it->empty()) { | 374 if (bitmap_it->empty() || bitmap_it->width() != bitmap_it->height()) |
| 334 WebApplicationInfo::IconInfo icon_info; | 375 continue; |
| 335 icon_info.data = *bitmap_it; | 376 |
| 336 icon_info.width = icon_info.data.width(); | 377 downloaded_icons.push_back(*bitmap_it); |
| 337 icon_info.height = icon_info.data.height(); | |
| 338 install_info.icons.push_back(icon_info); | |
| 339 } | |
| 340 } | 378 } |
| 341 } | 379 } |
| 342 | 380 |
| 381 // If there are icons that don't match the accepted icon sizes, find the |
| 382 // closest bigger icon to the accepted sizes and resize the icon to it. An |
| 383 // icon will be resized and used for at most one size. |
| 384 std::vector<SkBitmap> resized_bitmaps( |
| 385 TabHelper::ConstrainBitmapsToSizes(downloaded_icons, |
| 386 allowed_sizes)); |
| 387 for (std::vector<SkBitmap>::const_iterator resized_bitmaps_it = |
| 388 resized_bitmaps.begin(); |
| 389 resized_bitmaps_it != resized_bitmaps.end(); ++resized_bitmaps_it) { |
| 390 WebApplicationInfo::IconInfo icon_info; |
| 391 icon_info.data = *resized_bitmaps_it; |
| 392 icon_info.width = icon_info.data.width(); |
| 393 icon_info.height = icon_info.data.height(); |
| 394 install_info.icons.push_back(icon_info); |
| 395 } |
| 396 |
| 343 Profile* profile = | 397 Profile* profile = |
| 344 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 398 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 345 scoped_refptr<extensions::CrxInstaller> installer( | 399 scoped_refptr<extensions::CrxInstaller> installer( |
| 346 extensions::CrxInstaller::CreateSilent(profile->GetExtensionService())); | 400 extensions::CrxInstaller::CreateSilent(profile->GetExtensionService())); |
| 347 installer->set_error_on_unsupported_requirements(true); | 401 installer->set_error_on_unsupported_requirements(true); |
| 348 installer->InstallWebApp(install_info); | 402 installer->InstallWebApp(install_info); |
| 349 favicon_downloader_.reset(); | 403 favicon_downloader_.reset(); |
| 350 } | 404 } |
| 351 | 405 |
| 352 void TabHelper::DidCloneToNewWebContents(WebContents* old_web_contents, | 406 void TabHelper::DidCloneToNewWebContents(WebContents* old_web_contents, |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 } | 690 } |
| 637 } | 691 } |
| 638 | 692 |
| 639 void TabHelper::SetTabId(RenderViewHost* render_view_host) { | 693 void TabHelper::SetTabId(RenderViewHost* render_view_host) { |
| 640 render_view_host->Send( | 694 render_view_host->Send( |
| 641 new ExtensionMsg_SetTabId(render_view_host->GetRoutingID(), | 695 new ExtensionMsg_SetTabId(render_view_host->GetRoutingID(), |
| 642 SessionID::IdForTab(web_contents()))); | 696 SessionID::IdForTab(web_contents()))); |
| 643 } | 697 } |
| 644 | 698 |
| 645 } // namespace extensions | 699 } // namespace extensions |
| OLD | NEW |