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/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "base/win/windows_version.h" | 12 #include "base/win/windows_version.h" |
13 #include "chrome/browser/extensions/extension_tab_helper.h" | 13 #include "chrome/browser/extensions/extension_tab_helper.h" |
14 #include "chrome/browser/favicon/favicon_tab_helper.h" | 14 #include "chrome/browser/favicon/favicon_tab_helper.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 16 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
17 #include "chrome/browser/web_applications/web_app.h" | 17 #include "chrome/browser/web_applications/web_app.h" |
18 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
19 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
22 #include "content/public/browser/notification_registrar.h" | 22 #include "content/public/browser/notification_registrar.h" |
23 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
24 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
25 | 25 |
26 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 26 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
27 #include "base/environment.h" | 27 #include "base/environment.h" |
28 #endif | 28 #endif |
29 | 29 |
30 using content::BrowserThread; | 30 using content::BrowserThread; |
31 using content::NavigationController; | 31 using content::NavigationController; |
32 using content::WebContents; | 32 using content::WebContents; |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
37 // UpdateShortcutWorker holds all context data needed for update shortcut. | 37 // UpdateShortcutWorker holds all context data needed for update shortcut. |
38 // It schedules a pre-update check to find all shortcuts that needs to be | 38 // It schedules a pre-update check to find all shortcuts that needs to be |
39 // updated. If there are such shortcuts, it schedules icon download and | 39 // updated. If there are such shortcuts, it schedules icon download and |
40 // update them when icons are downloaded. It observes TAB_CLOSING notification | 40 // update them when icons are downloaded. It observes TAB_CLOSING notification |
41 // and cancels all the work when the underlying tab is closing. | 41 // and cancels all the work when the underlying tab is closing. |
42 class UpdateShortcutWorker : public content::NotificationObserver { | 42 class UpdateShortcutWorker : public content::NotificationObserver { |
43 public: | 43 public: |
44 explicit UpdateShortcutWorker(TabContentsWrapper* tab_contents); | 44 explicit UpdateShortcutWorker(TabContents* tab_contents); |
45 | 45 |
46 void Run(); | 46 void Run(); |
47 | 47 |
48 private: | 48 private: |
49 // Overridden from content::NotificationObserver: | 49 // Overridden from content::NotificationObserver: |
50 virtual void Observe(int type, | 50 virtual void Observe(int type, |
51 const content::NotificationSource& source, | 51 const content::NotificationSource& source, |
52 const content::NotificationDetails& details); | 52 const content::NotificationDetails& details); |
53 | 53 |
54 // Downloads icon via TabContentsWrapper. | 54 // Downloads icon via TabContents. |
55 void DownloadIcon(); | 55 void DownloadIcon(); |
56 | 56 |
57 // Callback when icon downloaded. | 57 // Callback when icon downloaded. |
58 void OnIconDownloaded(int download_id, bool errored, const SkBitmap& image); | 58 void OnIconDownloaded(int download_id, bool errored, const SkBitmap& image); |
59 | 59 |
60 // Checks if shortcuts exists on desktop, start menu and quick launch. | 60 // Checks if shortcuts exists on desktop, start menu and quick launch. |
61 void CheckExistingShortcuts(); | 61 void CheckExistingShortcuts(); |
62 | 62 |
63 // Update shortcut files and icons. | 63 // Update shortcut files and icons. |
64 void UpdateShortcuts(); | 64 void UpdateShortcuts(); |
65 void UpdateShortcutsOnFileThread(); | 65 void UpdateShortcutsOnFileThread(); |
66 | 66 |
67 // Callback after shortcuts are updated. | 67 // Callback after shortcuts are updated. |
68 void OnShortcutsUpdated(bool); | 68 void OnShortcutsUpdated(bool); |
69 | 69 |
70 // Deletes the worker on UI thread where it gets created. | 70 // Deletes the worker on UI thread where it gets created. |
71 void DeleteMe(); | 71 void DeleteMe(); |
72 void DeleteMeOnUIThread(); | 72 void DeleteMeOnUIThread(); |
73 | 73 |
74 content::NotificationRegistrar registrar_; | 74 content::NotificationRegistrar registrar_; |
75 | 75 |
76 // Underlying TabContentsWrapper whose shortcuts will be updated. | 76 // Underlying TabContents whose shortcuts will be updated. |
77 TabContentsWrapper* tab_contents_; | 77 TabContents* tab_contents_; |
78 | 78 |
79 // Icons info from tab_contents_'s web app data. | 79 // Icons info from tab_contents_'s web app data. |
80 web_app::IconInfoList unprocessed_icons_; | 80 web_app::IconInfoList unprocessed_icons_; |
81 | 81 |
82 // Cached shortcut data from the tab_contents_. | 82 // Cached shortcut data from the tab_contents_. |
83 ShellIntegration::ShortcutInfo shortcut_info_; | 83 ShellIntegration::ShortcutInfo shortcut_info_; |
84 | 84 |
85 // Our copy of profile path. | 85 // Our copy of profile path. |
86 FilePath profile_path_; | 86 FilePath profile_path_; |
87 | 87 |
88 // File name of shortcut/ico file based on app title. | 88 // File name of shortcut/ico file based on app title. |
89 FilePath file_name_; | 89 FilePath file_name_; |
90 | 90 |
91 // Existing shortcuts. | 91 // Existing shortcuts. |
92 std::vector<FilePath> shortcut_files_; | 92 std::vector<FilePath> shortcut_files_; |
93 | 93 |
94 DISALLOW_COPY_AND_ASSIGN(UpdateShortcutWorker); | 94 DISALLOW_COPY_AND_ASSIGN(UpdateShortcutWorker); |
95 }; | 95 }; |
96 | 96 |
97 UpdateShortcutWorker::UpdateShortcutWorker(TabContentsWrapper* tab_contents) | 97 UpdateShortcutWorker::UpdateShortcutWorker(TabContents* tab_contents) |
98 : tab_contents_(tab_contents), | 98 : tab_contents_(tab_contents), |
99 profile_path_(tab_contents->profile()->GetPath()) { | 99 profile_path_(tab_contents->profile()->GetPath()) { |
100 web_app::GetShortcutInfoForTab(tab_contents_, &shortcut_info_); | 100 web_app::GetShortcutInfoForTab(tab_contents_, &shortcut_info_); |
101 web_app::GetIconsInfo(tab_contents_->extension_tab_helper()->web_app_info(), | 101 web_app::GetIconsInfo(tab_contents_->extension_tab_helper()->web_app_info(), |
102 &unprocessed_icons_); | 102 &unprocessed_icons_); |
103 file_name_ = web_app::internals::GetSanitizedFileName(shortcut_info_.title); | 103 file_name_ = web_app::internals::GetSanitizedFileName(shortcut_info_.title); |
104 | 104 |
105 registrar_.Add( | 105 registrar_.Add( |
106 this, | 106 this, |
107 chrome::NOTIFICATION_TAB_CLOSING, | 107 chrome::NOTIFICATION_TAB_CLOSING, |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 void UpdateShortcutWorker::DeleteMeOnUIThread() { | 290 void UpdateShortcutWorker::DeleteMeOnUIThread() { |
291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
292 delete this; | 292 delete this; |
293 } | 293 } |
294 #endif // defined(OS_WIN) | 294 #endif // defined(OS_WIN) |
295 | 295 |
296 } // namespace | 296 } // namespace |
297 | 297 |
298 namespace web_app { | 298 namespace web_app { |
299 | 299 |
300 void GetShortcutInfoForTab(TabContentsWrapper* tab_contents_wrapper, | 300 void GetShortcutInfoForTab(TabContents* tab_contents, |
301 ShellIntegration::ShortcutInfo* info) { | 301 ShellIntegration::ShortcutInfo* info) { |
302 DCHECK(info); // Must provide a valid info. | 302 DCHECK(info); // Must provide a valid info. |
303 const WebContents* web_contents = tab_contents_wrapper->web_contents(); | 303 const WebContents* web_contents = tab_contents->web_contents(); |
304 | 304 |
305 const WebApplicationInfo& app_info = | 305 const WebApplicationInfo& app_info = |
306 tab_contents_wrapper->extension_tab_helper()->web_app_info(); | 306 tab_contents->extension_tab_helper()->web_app_info(); |
307 | 307 |
308 info->url = app_info.app_url.is_empty() ? web_contents->GetURL() : | 308 info->url = app_info.app_url.is_empty() ? web_contents->GetURL() : |
309 app_info.app_url; | 309 app_info.app_url; |
310 info->title = app_info.title.empty() ? | 310 info->title = app_info.title.empty() ? |
311 (web_contents->GetTitle().empty() ? UTF8ToUTF16(info->url.spec()) : | 311 (web_contents->GetTitle().empty() ? UTF8ToUTF16(info->url.spec()) : |
312 web_contents->GetTitle()) : | 312 web_contents->GetTitle()) : |
313 app_info.title; | 313 app_info.title; |
314 info->description = app_info.description; | 314 info->description = app_info.description; |
315 info->favicon = | 315 info->favicon = |
316 gfx::Image(tab_contents_wrapper->favicon_tab_helper()->GetFavicon()); | 316 gfx::Image(tab_contents->favicon_tab_helper()->GetFavicon()); |
317 } | 317 } |
318 | 318 |
319 void UpdateShortcutForTabContents(TabContentsWrapper* tab_contents) { | 319 void UpdateShortcutForTabContents(TabContents* tab_contents) { |
320 #if defined(OS_WIN) | 320 #if defined(OS_WIN) |
321 // UpdateShortcutWorker will delete itself when it's done. | 321 // UpdateShortcutWorker will delete itself when it's done. |
322 UpdateShortcutWorker* worker = new UpdateShortcutWorker(tab_contents); | 322 UpdateShortcutWorker* worker = new UpdateShortcutWorker(tab_contents); |
323 worker->Run(); | 323 worker->Run(); |
324 #endif // defined(OS_WIN) | 324 #endif // defined(OS_WIN) |
325 } | 325 } |
326 | 326 |
327 } // namespace web_app | 327 } // namespace web_app |
OLD | NEW |