| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/ui/web_applications/web_app_ui.h" | 5 #include "chrome/browser/web_applications/update_shortcut_worker_win.h" |
| 6 |
| 7 #include <algorithm> |
| 6 | 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 9 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 10 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/win/shortcut.h" |
| 15 #include "base/win/windows_version.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 13 #include "chrome/browser/extensions/tab_helper.h" | 17 #include "chrome/browser/extensions/tab_helper.h" |
| 14 #include "chrome/browser/favicon/favicon_tab_helper.h" | 18 #include "chrome/browser/favicon/favicon_tab_helper.h" |
| 15 #include "chrome/browser/history/select_favicon_frames.h" | 19 #include "chrome/browser/history/select_favicon_frames.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/web_applications/web_app.h" | 21 #include "chrome/browser/web_applications/web_app.h" |
| 22 #include "chrome/browser/web_applications/web_app_win.h" |
| 18 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/notification_details.h" | 24 #include "content/public/browser/notification_details.h" |
| 20 #include "content/public/browser/notification_registrar.h" | |
| 21 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
| 22 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 27 #include "ui/gfx/icon_util.h" |
| 23 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 24 | 29 |
| 25 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
| 26 #include "base/environment.h" | |
| 27 #endif | |
| 28 | |
| 29 #if defined(OS_WIN) | |
| 30 #include "base/win/shortcut.h" | |
| 31 #include "base/win/windows_version.h" | |
| 32 #include "chrome/browser/web_applications/web_app_win.h" | |
| 33 #include "ui/gfx/icon_util.h" | |
| 34 #endif | |
| 35 | |
| 36 using content::BrowserThread; | 30 using content::BrowserThread; |
| 37 using content::NavigationController; | 31 using content::NavigationController; |
| 38 using content::WebContents; | 32 using content::WebContents; |
| 39 | 33 |
| 40 namespace { | 34 namespace web_app { |
| 41 | |
| 42 // TODO(jackhou): Move all win-specific code to web_app_win. | |
| 43 #if defined(OS_WIN) | |
| 44 // UpdateShortcutWorker holds all context data needed for update shortcut. | |
| 45 // It schedules a pre-update check to find all shortcuts that needs to be | |
| 46 // updated. If there are such shortcuts, it schedules icon download and | |
| 47 // update them when icons are downloaded. It observes TAB_CLOSING notification | |
| 48 // and cancels all the work when the underlying tab is closing. | |
| 49 class UpdateShortcutWorker : public content::NotificationObserver { | |
| 50 public: | |
| 51 explicit UpdateShortcutWorker(WebContents* web_contents); | |
| 52 | |
| 53 void Run(); | |
| 54 | |
| 55 private: | |
| 56 // Overridden from content::NotificationObserver: | |
| 57 virtual void Observe(int type, | |
| 58 const content::NotificationSource& source, | |
| 59 const content::NotificationDetails& details); | |
| 60 | |
| 61 // Downloads icon via the FaviconTabHelper. | |
| 62 void DownloadIcon(); | |
| 63 | |
| 64 // Favicon download callback. | |
| 65 void DidDownloadFavicon( | |
| 66 int requested_size, | |
| 67 int id, | |
| 68 int http_status_code, | |
| 69 const GURL& image_url, | |
| 70 const std::vector<SkBitmap>& bitmaps, | |
| 71 const std::vector<gfx::Size>& original_bitmap_sizes); | |
| 72 | |
| 73 // Checks if shortcuts exists on desktop, start menu and quick launch. | |
| 74 void CheckExistingShortcuts(); | |
| 75 | |
| 76 // Update shortcut files and icons. | |
| 77 void UpdateShortcuts(); | |
| 78 void UpdateShortcutsOnFileThread(); | |
| 79 | |
| 80 // Callback after shortcuts are updated. | |
| 81 void OnShortcutsUpdated(bool); | |
| 82 | |
| 83 // Deletes the worker on UI thread where it gets created. | |
| 84 void DeleteMe(); | |
| 85 void DeleteMeOnUIThread(); | |
| 86 | |
| 87 content::NotificationRegistrar registrar_; | |
| 88 | |
| 89 // Underlying WebContents whose shortcuts will be updated. | |
| 90 WebContents* web_contents_; | |
| 91 | |
| 92 // Icons info from web_contents_'s web app data. | |
| 93 web_app::IconInfoList unprocessed_icons_; | |
| 94 | |
| 95 // Cached shortcut data from the web_contents_. | |
| 96 ShellIntegration::ShortcutInfo shortcut_info_; | |
| 97 | |
| 98 // Our copy of profile path. | |
| 99 base::FilePath profile_path_; | |
| 100 | |
| 101 // File name of shortcut/ico file based on app title. | |
| 102 base::FilePath file_name_; | |
| 103 | |
| 104 // Existing shortcuts. | |
| 105 std::vector<base::FilePath> shortcut_files_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(UpdateShortcutWorker); | |
| 108 }; | |
| 109 | 35 |
| 110 UpdateShortcutWorker::UpdateShortcutWorker(WebContents* web_contents) | 36 UpdateShortcutWorker::UpdateShortcutWorker(WebContents* web_contents) |
| 111 : web_contents_(web_contents), | 37 : web_contents_(web_contents), |
| 112 profile_path_(Profile::FromBrowserContext( | 38 profile_path_(Profile::FromBrowserContext( |
| 113 web_contents->GetBrowserContext())->GetPath()) { | 39 web_contents->GetBrowserContext())->GetPath()) { |
| 114 extensions::TabHelper* extensions_tab_helper = | 40 extensions::TabHelper* extensions_tab_helper = |
| 115 extensions::TabHelper::FromWebContents(web_contents); | 41 extensions::TabHelper::FromWebContents(web_contents); |
| 116 web_app::GetShortcutInfoForTab(web_contents_, &shortcut_info_); | 42 web_app::GetShortcutInfoForTab(web_contents_, &shortcut_info_); |
| 117 web_app::GetIconsInfo(extensions_tab_helper->web_app_info(), | 43 web_app::GetIconsInfo(extensions_tab_helper->web_app_info(), |
| 118 &unprocessed_icons_); | 44 &unprocessed_icons_); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 231 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 306 base::Bind(&UpdateShortcutWorker::DeleteMeOnUIThread, | 232 base::Bind(&UpdateShortcutWorker::DeleteMeOnUIThread, |
| 307 base::Unretained(this))); | 233 base::Unretained(this))); |
| 308 } | 234 } |
| 309 } | 235 } |
| 310 | 236 |
| 311 void UpdateShortcutWorker::DeleteMeOnUIThread() { | 237 void UpdateShortcutWorker::DeleteMeOnUIThread() { |
| 312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 313 delete this; | 239 delete this; |
| 314 } | 240 } |
| 315 #endif // defined(OS_WIN) | |
| 316 | |
| 317 } // namespace | |
| 318 | |
| 319 namespace web_app { | |
| 320 | |
| 321 void GetShortcutInfoForTab(WebContents* web_contents, | |
| 322 ShellIntegration::ShortcutInfo* info) { | |
| 323 DCHECK(info); // Must provide a valid info. | |
| 324 | |
| 325 const FaviconTabHelper* favicon_tab_helper = | |
| 326 FaviconTabHelper::FromWebContents(web_contents); | |
| 327 const extensions::TabHelper* extensions_tab_helper = | |
| 328 extensions::TabHelper::FromWebContents(web_contents); | |
| 329 const WebApplicationInfo& app_info = extensions_tab_helper->web_app_info(); | |
| 330 | |
| 331 info->url = app_info.app_url.is_empty() ? web_contents->GetURL() : | |
| 332 app_info.app_url; | |
| 333 info->title = app_info.title.empty() ? | |
| 334 (web_contents->GetTitle().empty() ? base::UTF8ToUTF16(info->url.spec()) : | |
| 335 web_contents->GetTitle()) : | |
| 336 app_info.title; | |
| 337 info->description = app_info.description; | |
| 338 info->favicon.Add(favicon_tab_helper->GetFavicon()); | |
| 339 | |
| 340 Profile* profile = | |
| 341 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 342 info->profile_path = profile->GetPath(); | |
| 343 } | |
| 344 | |
| 345 void UpdateShortcutForTabContents(WebContents* web_contents) { | |
| 346 #if defined(OS_WIN) | |
| 347 // UpdateShortcutWorker will delete itself when it's done. | |
| 348 UpdateShortcutWorker* worker = new UpdateShortcutWorker(web_contents); | |
| 349 worker->Run(); | |
| 350 #endif // defined(OS_WIN) | |
| 351 } | |
| 352 | 241 |
| 353 } // namespace web_app | 242 } // namespace web_app |
| OLD | NEW |