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/ui/web_applications/web_app_ui.h" | 5 #include "chrome/browser/ui/web_applications/web_app_ui.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/string16.h" | 11 #include "base/string16.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/extensions/tab_helper.h" | 13 #include "chrome/browser/extensions/tab_helper.h" |
14 #include "chrome/browser/favicon/favicon_download_helper.h" | |
15 #include "chrome/browser/favicon/favicon_download_helper_delegate.h" | |
16 #include "chrome/browser/favicon/favicon_tab_helper.h" | 14 #include "chrome/browser/favicon/favicon_tab_helper.h" |
17 #include "chrome/browser/favicon/favicon_util.h" | 15 #include "chrome/browser/favicon/favicon_util.h" |
18 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/web_applications/web_app.h" | 17 #include "chrome/browser/web_applications/web_app.h" |
20 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
21 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
22 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
24 #include "content/public/browser/notification_registrar.h" | 22 #include "content/public/browser/notification_registrar.h" |
25 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
26 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 25 #include "googleurl/src/gurl.h" |
| 26 #include "third_party/skia/include/core/SkBitmap.h" |
27 | 27 |
28 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 28 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
29 #include "base/environment.h" | 29 #include "base/environment.h" |
30 #endif | 30 #endif |
31 | 31 |
32 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
33 #include "base/win/shortcut.h" | 33 #include "base/win/shortcut.h" |
34 #include "base/win/windows_version.h" | 34 #include "base/win/windows_version.h" |
35 #endif | 35 #endif |
36 | 36 |
37 using content::BrowserThread; | 37 using content::BrowserThread; |
38 using content::NavigationController; | 38 using content::NavigationController; |
39 using content::WebContents; | 39 using content::WebContents; |
40 | 40 |
41 namespace { | 41 namespace { |
42 | 42 |
43 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
44 // UpdateShortcutWorker holds all context data needed for update shortcut. | 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 | 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 | 46 // updated. If there are such shortcuts, it schedules icon download and |
47 // update them when icons are downloaded. It observes TAB_CLOSING notification | 47 // update them when icons are downloaded. It observes TAB_CLOSING notification |
48 // and cancels all the work when the underlying tab is closing. | 48 // and cancels all the work when the underlying tab is closing. |
49 class UpdateShortcutWorker : public content::NotificationObserver, | 49 class UpdateShortcutWorker : public content::NotificationObserver { |
50 public FaviconDownloadHelperDelegate { | |
51 public: | 50 public: |
52 explicit UpdateShortcutWorker(WebContents* web_contents); | 51 explicit UpdateShortcutWorker(WebContents* web_contents); |
53 | 52 |
54 void Run(); | 53 void Run(); |
55 | 54 |
56 private: | 55 private: |
57 // Overridden from content::NotificationObserver: | 56 // Overridden from content::NotificationObserver: |
58 virtual void Observe(int type, | 57 virtual void Observe(int type, |
59 const content::NotificationSource& source, | 58 const content::NotificationSource& source, |
60 const content::NotificationDetails& details); | 59 const content::NotificationDetails& details); |
61 | 60 |
62 // Downloads icon via the FaviconTabHelper. | 61 // Downloads icon via the FaviconTabHelper. |
63 void DownloadIcon(); | 62 void DownloadIcon(); |
64 | 63 |
65 // FaviconDownloadHelperDelegate override. | 64 // Favicon download callback. |
66 virtual void OnDidDownloadFavicon( | 65 void DidDownloadFavicon( |
67 int id, | 66 int id, |
68 const GURL& image_url, | 67 const GURL& image_url, |
69 bool errored, | 68 bool errored, |
70 int requested_size, | 69 int requested_size, |
71 const std::vector<SkBitmap>& bitmaps) OVERRIDE; | 70 const std::vector<SkBitmap>& bitmaps); |
72 | 71 |
73 // Checks if shortcuts exists on desktop, start menu and quick launch. | 72 // Checks if shortcuts exists on desktop, start menu and quick launch. |
74 void CheckExistingShortcuts(); | 73 void CheckExistingShortcuts(); |
75 | 74 |
76 // Update shortcut files and icons. | 75 // Update shortcut files and icons. |
77 void UpdateShortcuts(); | 76 void UpdateShortcuts(); |
78 void UpdateShortcutsOnFileThread(); | 77 void UpdateShortcutsOnFileThread(); |
79 | 78 |
80 // Callback after shortcuts are updated. | 79 // Callback after shortcuts are updated. |
81 void OnShortcutsUpdated(bool); | 80 void OnShortcutsUpdated(bool); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 DeleteMe(); // We are done if underlying WebContents is gone. | 149 DeleteMe(); // We are done if underlying WebContents is gone. |
151 return; | 150 return; |
152 } | 151 } |
153 | 152 |
154 if (unprocessed_icons_.empty()) { | 153 if (unprocessed_icons_.empty()) { |
155 // No app icon. Just use the favicon from WebContents. | 154 // No app icon. Just use the favicon from WebContents. |
156 UpdateShortcuts(); | 155 UpdateShortcuts(); |
157 return; | 156 return; |
158 } | 157 } |
159 | 158 |
160 scoped_ptr<FaviconDownloadHelper> download_helper( | 159 web_contents_->DownloadFavicon( |
161 new FaviconDownloadHelper(web_contents_, this)); | 160 unprocessed_icons_.back().url, |
162 | 161 std::max(unprocessed_icons_.back().width, |
163 download_helper->DownloadFavicon(unprocessed_icons_.back().url, | 162 unprocessed_icons_.back().height), |
164 std::max(unprocessed_icons_.back().width, | 163 base::Bind(&UpdateShortcutWorker::DidDownloadFavicon, |
165 unprocessed_icons_.back().height)); | 164 base::Unretained(this))); |
166 unprocessed_icons_.pop_back(); | 165 unprocessed_icons_.pop_back(); |
167 } | 166 } |
168 | 167 |
169 void UpdateShortcutWorker::OnDidDownloadFavicon( | 168 void UpdateShortcutWorker::DidDownloadFavicon( |
170 int id, | 169 int id, |
171 const GURL& image_url, | 170 const GURL& image_url, |
172 bool errored, | 171 bool errored, |
173 int requested_size, | 172 int requested_size, |
174 const std::vector<SkBitmap>& bitmaps) { | 173 const std::vector<SkBitmap>& bitmaps) { |
175 if (web_contents_ == NULL) { | |
176 DeleteMe(); // We are done if underlying WebContents is gone. | |
177 return; | |
178 } | |
179 | |
180 std::vector<ui::ScaleFactor> scale_factors; | 174 std::vector<ui::ScaleFactor> scale_factors; |
181 scale_factors.push_back(ui::SCALE_FACTOR_100P); | 175 scale_factors.push_back(ui::SCALE_FACTOR_100P); |
182 | 176 |
183 size_t closest_index = | 177 size_t closest_index = |
184 FaviconUtil::SelectBestFaviconFromBitmaps(bitmaps, | 178 FaviconUtil::SelectBestFaviconFromBitmaps(bitmaps, |
185 scale_factors, | 179 scale_factors, |
186 requested_size); | 180 requested_size); |
187 | 181 |
188 if (!errored && !bitmaps.empty() && !bitmaps[closest_index].isNull()) { | 182 if (!errored && !bitmaps.empty() && !bitmaps[closest_index].isNull()) { |
189 // Update icon with download image and update shortcut. | 183 // Update icon with download image and update shortcut. |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 shortcut_info->extension_id = app.id(); | 354 shortcut_info->extension_id = app.id(); |
361 shortcut_info->is_platform_app = app.is_platform_app(); | 355 shortcut_info->is_platform_app = app.is_platform_app(); |
362 shortcut_info->url = GURL(app.launch_web_url()); | 356 shortcut_info->url = GURL(app.launch_web_url()); |
363 shortcut_info->title = UTF8ToUTF16(app.name()); | 357 shortcut_info->title = UTF8ToUTF16(app.name()); |
364 shortcut_info->description = UTF8ToUTF16(app.description()); | 358 shortcut_info->description = UTF8ToUTF16(app.description()); |
365 shortcut_info->extension_path = app.path(); | 359 shortcut_info->extension_path = app.path(); |
366 shortcut_info->profile_path = profile->GetPath(); | 360 shortcut_info->profile_path = profile->GetPath(); |
367 } | 361 } |
368 | 362 |
369 } // namespace web_app | 363 } // namespace web_app |
OLD | NEW |