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/intents/web_intent_picker_controller.h" | 5 #include "chrome/browser/ui/intents/web_intent_picker_controller.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/md5.h" | 11 #include "base/md5.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "chrome/browser/download/download_util.h" | 15 #include "chrome/browser/download/download_util.h" |
16 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
17 #include "chrome/browser/extensions/platform_app_launcher.h" | 17 #include "chrome/browser/extensions/platform_app_launcher.h" |
18 #include "chrome/browser/extensions/webstore_installer.h" | 18 #include "chrome/browser/extensions/webstore_installer.h" |
19 #include "chrome/browser/favicon/favicon_service.h" | |
20 #include "chrome/browser/favicon/favicon_service_factory.h" | |
21 #include "chrome/browser/intents/cws_intents_registry_factory.h" | 19 #include "chrome/browser/intents/cws_intents_registry_factory.h" |
22 #include "chrome/browser/intents/default_web_intent_service.h" | 20 #include "chrome/browser/intents/default_web_intent_service.h" |
23 #include "chrome/browser/intents/intent_service_host.h" | 21 #include "chrome/browser/intents/intent_service_host.h" |
24 #include "chrome/browser/intents/native_services.h" | 22 #include "chrome/browser/intents/native_services.h" |
25 #include "chrome/browser/intents/web_intents_registry_factory.h" | 23 #include "chrome/browser/intents/web_intents_registry_factory.h" |
26 #include "chrome/browser/intents/web_intents_reporting.h" | 24 #include "chrome/browser/intents/web_intents_reporting.h" |
27 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
28 #include "chrome/browser/tab_contents/tab_util.h" | 26 #include "chrome/browser/tab_contents/tab_util.h" |
29 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
30 #include "chrome/browser/ui/browser_finder.h" | 28 #include "chrome/browser/ui/browser_finder.h" |
31 #include "chrome/browser/ui/browser_list.h" | 29 #include "chrome/browser/ui/browser_list.h" |
32 #include "chrome/browser/ui/browser_navigator.h" | 30 #include "chrome/browser/ui/browser_navigator.h" |
33 #include "chrome/browser/ui/browser_tabstrip.h" | 31 #include "chrome/browser/ui/browser_tabstrip.h" |
34 #include "chrome/browser/ui/browser_window.h" | 32 #include "chrome/browser/ui/browser_window.h" |
35 #include "chrome/browser/ui/constrained_window_tab_helper.h" | 33 #include "chrome/browser/ui/constrained_window_tab_helper.h" |
| 34 #include "chrome/browser/ui/intents/web_intent_icon_loader.h" |
36 #include "chrome/browser/ui/intents/web_intent_picker.h" | 35 #include "chrome/browser/ui/intents/web_intent_picker.h" |
37 #include "chrome/browser/ui/intents/web_intent_picker_model.h" | 36 #include "chrome/browser/ui/intents/web_intent_picker_model.h" |
38 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 37 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
39 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 38 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
40 #include "chrome/browser/webdata/web_data_service.h" | 39 #include "chrome/browser/webdata/web_data_service.h" |
41 #include "chrome/common/url_constants.h" | 40 #include "chrome/common/url_constants.h" |
42 #include "content/public/browser/browser_thread.h" | 41 #include "content/public/browser/browser_thread.h" |
43 #include "content/public/browser/download_manager.h" | 42 #include "content/public/browser/download_manager.h" |
44 #include "content/public/browser/web_contents.h" | 43 #include "content/public/browser/web_contents.h" |
45 #include "content/public/browser/web_contents_observer.h" | 44 #include "content/public/browser/web_contents_observer.h" |
(...skipping 12 matching lines...) Expand all Loading... |
58 DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebIntentPickerController) | 57 DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebIntentPickerController) |
59 | 58 |
60 namespace { | 59 namespace { |
61 | 60 |
62 // Maximum amount of time to delay displaying dialog while waiting for data. | 61 // Maximum amount of time to delay displaying dialog while waiting for data. |
63 const int kMaxHiddenSetupTimeMs = 200; | 62 const int kMaxHiddenSetupTimeMs = 200; |
64 | 63 |
65 // Minimum amount of time to show waiting dialog, if it is shown. | 64 // Minimum amount of time to show waiting dialog, if it is shown. |
66 const int kMinThrobberDisplayTimeMs = 800; | 65 const int kMinThrobberDisplayTimeMs = 800; |
67 | 66 |
68 | |
69 // Gets the favicon service for the specified profile. | |
70 FaviconService* GetFaviconService(Profile* profile) { | |
71 return FaviconServiceFactory::GetForProfile(profile, | |
72 Profile::EXPLICIT_ACCESS); | |
73 } | |
74 | |
75 // Gets the web intents registry for the specified profile. | 67 // Gets the web intents registry for the specified profile. |
76 WebIntentsRegistry* GetWebIntentsRegistry(Profile* profile) { | 68 WebIntentsRegistry* GetWebIntentsRegistry(Profile* profile) { |
77 return WebIntentsRegistryFactory::GetForProfile(profile); | 69 return WebIntentsRegistryFactory::GetForProfile(profile); |
78 } | 70 } |
79 | 71 |
80 // Gets the Chrome web store intents registry for the specified profile. | 72 // Gets the Chrome web store intents registry for the specified profile. |
81 CWSIntentsRegistry* GetCWSIntentsRegistry(Profile* profile) { | 73 CWSIntentsRegistry* GetCWSIntentsRegistry(Profile* profile) { |
82 return CWSIntentsRegistryFactory::GetForProfile(profile); | 74 return CWSIntentsRegistryFactory::GetForProfile(profile); |
83 } | 75 } |
84 | 76 |
(...skipping 21 matching lines...) Expand all Loading... |
106 URLFetcherTrampoline::~URLFetcherTrampoline() { | 98 URLFetcherTrampoline::~URLFetcherTrampoline() { |
107 } | 99 } |
108 | 100 |
109 void URLFetcherTrampoline::OnURLFetchComplete( | 101 void URLFetcherTrampoline::OnURLFetchComplete( |
110 const net::URLFetcher* source) { | 102 const net::URLFetcher* source) { |
111 DCHECK(!callback_.is_null()); | 103 DCHECK(!callback_.is_null()); |
112 callback_.Run(source); | 104 callback_.Run(source); |
113 delete source; | 105 delete source; |
114 delete this; | 106 delete this; |
115 } | 107 } |
116 | |
117 class SourceWindowObserver : content::WebContentsObserver { | 108 class SourceWindowObserver : content::WebContentsObserver { |
118 public: | 109 public: |
119 SourceWindowObserver(content::WebContents* web_contents, | 110 SourceWindowObserver(content::WebContents* web_contents, |
120 base::WeakPtr<WebIntentPickerController> controller) | 111 base::WeakPtr<WebIntentPickerController> controller) |
121 : content::WebContentsObserver(web_contents), | 112 : content::WebContentsObserver(web_contents), |
122 controller_(controller) {} | 113 controller_(controller) {} |
123 virtual ~SourceWindowObserver() {} | 114 virtual ~SourceWindowObserver() {} |
124 | 115 |
125 // Implement WebContentsObserver | 116 // Implement WebContentsObserver |
126 virtual void WebContentsDestroyed(content::WebContents* web_contents) { | 117 virtual void WebContentsDestroyed(content::WebContents* web_contents) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 picker_model_(new WebIntentPickerModel()), | 171 picker_model_(new WebIntentPickerModel()), |
181 uma_reporter_(new UMAReporter()), | 172 uma_reporter_(new UMAReporter()), |
182 pending_async_count_(0), | 173 pending_async_count_(0), |
183 pending_registry_calls_count_(0), | 174 pending_registry_calls_count_(0), |
184 pending_cws_request_(false), | 175 pending_cws_request_(false), |
185 picker_shown_(false), | 176 picker_shown_(false), |
186 window_disposition_source_(NULL), | 177 window_disposition_source_(NULL), |
187 source_intents_dispatcher_(NULL), | 178 source_intents_dispatcher_(NULL), |
188 intents_dispatcher_(NULL), | 179 intents_dispatcher_(NULL), |
189 service_tab_(NULL), | 180 service_tab_(NULL), |
| 181 icon_loader_(NULL), |
190 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 182 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), |
191 ALLOW_THIS_IN_INITIALIZER_LIST(timer_factory_(this)), | 183 ALLOW_THIS_IN_INITIALIZER_LIST(timer_factory_(this)), |
192 ALLOW_THIS_IN_INITIALIZER_LIST(dispatcher_factory_(this)) { | 184 ALLOW_THIS_IN_INITIALIZER_LIST(dispatcher_factory_(this)) { |
193 native_services_.reset(new web_intents::NativeServiceFactory()); | 185 native_services_.reset(new web_intents::NativeServiceFactory()); |
194 #if defined(TOOLKIT_VIEWS) | 186 #if defined(TOOLKIT_VIEWS) |
195 cancelled_ = true; | 187 cancelled_ = true; |
196 #endif | 188 #endif |
| 189 icon_loader_.reset( |
| 190 new web_intents::IconLoader(profile_, picker_model_.get())); |
197 } | 191 } |
198 | 192 |
199 WebIntentPickerController::~WebIntentPickerController() { | 193 WebIntentPickerController::~WebIntentPickerController() { |
200 } | 194 } |
201 | 195 |
202 // TODO(gbillock): combine this with ShowDialog. | 196 // TODO(gbillock): combine this with ShowDialog. |
203 void WebIntentPickerController::SetIntentsDispatcher( | 197 void WebIntentPickerController::SetIntentsDispatcher( |
204 content::WebIntentsDispatcher* intents_dispatcher) { | 198 content::WebIntentsDispatcher* intents_dispatcher) { |
205 // TODO(gbillock): This is to account for multiple dispatches in the same tab. | 199 // TODO(gbillock): This is to account for multiple dispatches in the same tab. |
206 // That is currently not a well-handled case, and this is a band-aid. | 200 // That is currently not a well-handled case, and this is a band-aid. |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 } | 600 } |
607 } | 601 } |
608 service_tab_ = NULL; | 602 service_tab_ = NULL; |
609 } | 603 } |
610 | 604 |
611 intents_dispatcher_ = NULL; | 605 intents_dispatcher_ = NULL; |
612 } | 606 } |
613 | 607 |
614 void WebIntentPickerController::AddServiceToModel( | 608 void WebIntentPickerController::AddServiceToModel( |
615 const webkit_glue::WebIntentServiceData& service) { | 609 const webkit_glue::WebIntentServiceData& service) { |
616 FaviconService* favicon_service = GetFaviconService(profile_); | |
617 | 610 |
618 picker_model_->AddInstalledService( | 611 picker_model_->AddInstalledService( |
619 service.title, | 612 service.title, |
620 service.service_url, | 613 service.service_url, |
621 service.disposition); | 614 service.disposition); |
622 | 615 |
623 pending_async_count_++; | 616 icon_loader_->LoadFavicon(service.service_url); |
624 FaviconService::Handle handle = favicon_service->GetFaviconImageForURL( | |
625 FaviconService::FaviconForURLParams( | |
626 profile_, | |
627 service.service_url, | |
628 history::FAVICON, | |
629 gfx::kFaviconSize, | |
630 &favicon_consumer_), | |
631 base::Bind( | |
632 &WebIntentPickerController::OnFaviconDataAvailable, | |
633 weak_ptr_factory_.GetWeakPtr())); | |
634 favicon_consumer_.SetClientData( | |
635 favicon_service, handle, picker_model_->GetInstalledServiceCount() - 1); | |
636 } | 617 } |
637 | 618 |
638 void WebIntentPickerController::OnWebIntentServicesAvailable( | 619 void WebIntentPickerController::OnWebIntentServicesAvailable( |
639 const std::vector<webkit_glue::WebIntentServiceData>& services) { | 620 const std::vector<webkit_glue::WebIntentServiceData>& services) { |
640 for (size_t i = 0; i < services.size(); ++i) | 621 for (size_t i = 0; i < services.size(); ++i) |
641 AddServiceToModel(services[i]); | 622 AddServiceToModel(services[i]); |
642 | 623 |
643 RegistryCallsCompleted(); | 624 RegistryCallsCompleted(); |
644 AsyncOperationFinished(); | 625 AsyncOperationFinished(); |
645 } | 626 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 if (default_service != NULL) { | 671 if (default_service != NULL) { |
691 InvokeService(*default_service); | 672 InvokeService(*default_service); |
692 return; | 673 return; |
693 } | 674 } |
694 } | 675 } |
695 | 676 |
696 OnPickerEvent(kPickerEventRegistryDataComplete); | 677 OnPickerEvent(kPickerEventRegistryDataComplete); |
697 OnIntentDataArrived(); | 678 OnIntentDataArrived(); |
698 } | 679 } |
699 | 680 |
700 void WebIntentPickerController::OnFaviconDataAvailable( | |
701 FaviconService::Handle handle, | |
702 const history::FaviconImageResult& image_result) { | |
703 size_t index = favicon_consumer_.GetClientDataForCurrentRequest(); | |
704 if (!image_result.image.IsEmpty()) { | |
705 picker_model_->UpdateFaviconAt(index, image_result.image); | |
706 return; | |
707 } | |
708 | |
709 AsyncOperationFinished(); | |
710 } | |
711 | |
712 void WebIntentPickerController::OnCWSIntentServicesAvailable( | 681 void WebIntentPickerController::OnCWSIntentServicesAvailable( |
713 const CWSIntentsRegistry::IntentExtensionList& extensions) { | 682 const CWSIntentsRegistry::IntentExtensionList& extensions) { |
714 ExtensionServiceInterface* extension_service = | 683 ExtensionServiceInterface* extension_service = |
715 profile_->GetExtensionService(); | 684 profile_->GetExtensionService(); |
716 | 685 |
717 std::vector<WebIntentPickerModel::SuggestedExtension> suggestions; | 686 std::vector<WebIntentPickerModel::SuggestedExtension> suggestions; |
718 for (size_t i = 0; i < extensions.size(); ++i) { | 687 for (size_t i = 0; i < extensions.size(); ++i) { |
719 const CWSIntentsRegistry::IntentExtensionInfo& info = extensions[i]; | 688 const CWSIntentsRegistry::IntentExtensionInfo& info = extensions[i]; |
720 | 689 |
721 // Do not include suggestions for already installed extensions. | 690 // Do not include suggestions for already installed extensions. |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 // Abandon all callbacks. | 774 // Abandon all callbacks. |
806 weak_ptr_factory_.InvalidateWeakPtrs(); | 775 weak_ptr_factory_.InvalidateWeakPtrs(); |
807 timer_factory_.InvalidateWeakPtrs(); | 776 timer_factory_.InvalidateWeakPtrs(); |
808 | 777 |
809 // Reset state associated with callbacks. | 778 // Reset state associated with callbacks. |
810 pending_async_count_ = 0; | 779 pending_async_count_ = 0; |
811 pending_registry_calls_count_ = 0; | 780 pending_registry_calls_count_ = 0; |
812 pending_cws_request_ = false; | 781 pending_cws_request_ = false; |
813 | 782 |
814 // Reset picker. | 783 // Reset picker. |
| 784 icon_loader_.reset(); |
815 picker_model_.reset(new WebIntentPickerModel()); | 785 picker_model_.reset(new WebIntentPickerModel()); |
| 786 icon_loader_.reset( |
| 787 new web_intents::IconLoader(profile_, picker_model_.get())); |
| 788 |
816 picker_shown_ = false; | 789 picker_shown_ = false; |
817 | 790 |
818 DCHECK(web_contents_); | 791 DCHECK(web_contents_); |
819 ConstrainedWindowTabHelper* constrained_window_tab_helper = | 792 ConstrainedWindowTabHelper* constrained_window_tab_helper = |
820 ConstrainedWindowTabHelper::FromWebContents(web_contents_); | 793 ConstrainedWindowTabHelper::FromWebContents(web_contents_); |
821 constrained_window_tab_helper->BlockTabContent(false); | 794 constrained_window_tab_helper->BlockTabContent(false); |
822 } | 795 } |
823 | 796 |
824 // static | 797 // static |
825 void WebIntentPickerController::DecodeExtensionIconAndResize( | 798 void WebIntentPickerController::DecodeExtensionIconAndResize( |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1112 content::DownloadManager* download_manager = | 1085 content::DownloadManager* download_manager = |
1113 content::BrowserContext::GetDownloadManager(profile); | 1086 content::BrowserContext::GetDownloadManager(profile); |
1114 if (!download_manager) | 1087 if (!download_manager) |
1115 return; | 1088 return; |
1116 content::DownloadItem* item = | 1089 content::DownloadItem* item = |
1117 download_manager->GetDownload(download_id_.local()); | 1090 download_manager->GetDownload(download_id_.local()); |
1118 if (item) | 1091 if (item) |
1119 item->Cancel(true); | 1092 item->Cancel(true); |
1120 download_id_ = content::DownloadId(); | 1093 download_id_ = content::DownloadId(); |
1121 } | 1094 } |
OLD | NEW |