OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/favicon/favicon_tab_helper.h" | 5 #include "chrome/browser/favicon/favicon_tab_helper.h" |
6 | 6 |
7 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/scoped_observer.h" |
9 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_commands.h" | 13 #include "chrome/browser/ui/browser_commands.h" |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
14 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
15 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
| 17 #include "components/favicon/core/favicon_driver_observer.h" |
16 #include "components/favicon/core/favicon_handler.h" | 18 #include "components/favicon/core/favicon_handler.h" |
17 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
18 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
19 #include "content/public/browser/notification_types.h" | 21 #include "content/public/browser/notification_types.h" |
20 #include "content/public/browser/resource_dispatcher_host.h" | 22 #include "content/public/browser/resource_dispatcher_host.h" |
21 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 23 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
22 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
23 #include "net/test/spawned_test_server/spawned_test_server.h" | 25 #include "net/test/spawned_test_server/spawned_test_server.h" |
24 #include "net/url_request/url_request.h" | 26 #include "net/url_request/url_request.h" |
25 #include "url/url_constants.h" | 27 #include "url/url_constants.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 public: | 94 public: |
93 virtual ~FaviconTabHelperPendingTaskChecker() {} | 95 virtual ~FaviconTabHelperPendingTaskChecker() {} |
94 | 96 |
95 virtual bool HasPendingTasks() = 0; | 97 virtual bool HasPendingTasks() = 0; |
96 }; | 98 }; |
97 | 99 |
98 // Waits for the following the finish: | 100 // Waits for the following the finish: |
99 // - The pending navigation. | 101 // - The pending navigation. |
100 // - FaviconHandler's pending favicon database requests. | 102 // - FaviconHandler's pending favicon database requests. |
101 // - FaviconHandler's pending downloads. | 103 // - FaviconHandler's pending downloads. |
102 class PendingTaskWaiter : public content::NotificationObserver { | 104 class PendingTaskWaiter : public content::NotificationObserver, |
| 105 public favicon::FaviconDriverObserver { |
103 public: | 106 public: |
104 PendingTaskWaiter(content::WebContents* web_contents, | 107 PendingTaskWaiter(content::WebContents* web_contents, |
105 FaviconTabHelperPendingTaskChecker* checker) | 108 FaviconTabHelperPendingTaskChecker* checker) |
106 : checker_(checker), load_stopped_(false), weak_factory_(this) { | 109 : checker_(checker), |
107 registrar_.Add(this, chrome::NOTIFICATION_FAVICON_UPDATED, | 110 load_stopped_(false), |
108 content::Source<content::WebContents>(web_contents)); | 111 scoped_observer_(this), |
| 112 weak_factory_(this) { |
109 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, | 113 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, |
110 content::Source<content::NavigationController>( | 114 content::Source<content::NavigationController>( |
111 &web_contents->GetController())); | 115 &web_contents->GetController())); |
| 116 scoped_observer_.Add(FaviconTabHelper::FromWebContents(web_contents)); |
112 } | 117 } |
113 ~PendingTaskWaiter() override {} | 118 ~PendingTaskWaiter() override {} |
114 | 119 |
115 void Wait() { | 120 void Wait() { |
116 if (load_stopped_ && !checker_->HasPendingTasks()) | 121 if (load_stopped_ && !checker_->HasPendingTasks()) |
117 return; | 122 return; |
118 | 123 |
119 base::RunLoop run_loop; | 124 base::RunLoop run_loop; |
120 quit_closure_ = run_loop.QuitClosure(); | 125 quit_closure_ = run_loop.QuitClosure(); |
121 run_loop.Run(); | 126 run_loop.Run(); |
122 } | 127 } |
123 | 128 |
124 private: | 129 private: |
125 // content::NotificationObserver: | 130 // content::NotificationObserver: |
126 void Observe(int type, | 131 void Observe(int type, |
127 const content::NotificationSource& source, | 132 const content::NotificationSource& source, |
128 const content::NotificationDetails& details) override { | 133 const content::NotificationDetails& details) override { |
129 if (type == content::NOTIFICATION_LOAD_STOP) | 134 if (type == content::NOTIFICATION_LOAD_STOP) |
130 load_stopped_ = true; | 135 load_stopped_ = true; |
131 | 136 |
| 137 OnNotification(); |
| 138 } |
| 139 |
| 140 // favicon::Favicon |
| 141 void OnFaviconAvailable(const gfx::Image& image) override {} |
| 142 void OnFaviconUpdated(favicon::FaviconDriver* favicon_driver, |
| 143 bool icon_url_changed) override { |
| 144 OnNotification(); |
| 145 } |
| 146 |
| 147 void OnNotification() { |
132 if (!quit_closure_.is_null()) { | 148 if (!quit_closure_.is_null()) { |
133 // We stop waiting based on changes in state to FaviconHandler which occur | 149 // We stop waiting based on changes in state to FaviconHandler which occur |
134 // immediately after NOTIFICATION_FAVICON_UPDATED is sent. Post a task to | 150 // immediately after OnFaviconUpdated() is called. Post a task to check if |
135 // check if we can stop waiting. | 151 // we can stop waiting. |
136 base::MessageLoopForUI::current()->PostTask( | 152 base::MessageLoopForUI::current()->PostTask( |
137 FROM_HERE, base::Bind(&PendingTaskWaiter::EndLoopIfCanStopWaiting, | 153 FROM_HERE, base::Bind(&PendingTaskWaiter::EndLoopIfCanStopWaiting, |
138 weak_factory_.GetWeakPtr())); | 154 weak_factory_.GetWeakPtr())); |
139 } | 155 } |
140 } | 156 } |
141 | 157 |
142 void EndLoopIfCanStopWaiting() { | 158 void EndLoopIfCanStopWaiting() { |
143 if (!quit_closure_.is_null() && | 159 if (!quit_closure_.is_null() && |
144 load_stopped_ && | 160 load_stopped_ && |
145 !checker_->HasPendingTasks()) { | 161 !checker_->HasPendingTasks()) { |
146 quit_closure_.Run(); | 162 quit_closure_.Run(); |
147 } | 163 } |
148 } | 164 } |
149 | 165 |
150 FaviconTabHelperPendingTaskChecker* checker_; // Not owned. | 166 FaviconTabHelperPendingTaskChecker* checker_; // Not owned. |
151 bool load_stopped_; | 167 bool load_stopped_; |
152 base::Closure quit_closure_; | 168 base::Closure quit_closure_; |
153 content::NotificationRegistrar registrar_; | 169 content::NotificationRegistrar registrar_; |
| 170 ScopedObserver<FaviconTabHelper, PendingTaskWaiter> scoped_observer_; |
154 base::WeakPtrFactory<PendingTaskWaiter> weak_factory_; | 171 base::WeakPtrFactory<PendingTaskWaiter> weak_factory_; |
155 | 172 |
156 DISALLOW_COPY_AND_ASSIGN(PendingTaskWaiter); | 173 DISALLOW_COPY_AND_ASSIGN(PendingTaskWaiter); |
157 }; | 174 }; |
158 | 175 |
159 } // namespace | 176 } // namespace |
160 | 177 |
161 class FaviconTabHelperTest : public InProcessBrowserTest, | 178 class FaviconTabHelperTest : public InProcessBrowserTest, |
162 public FaviconTabHelperPendingTaskChecker { | 179 public FaviconTabHelperPendingTaskChecker { |
163 public: | 180 public: |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 | 236 |
220 // A reload ignoring the cache should refetch the favicon from the website. | 237 // A reload ignoring the cache should refetch the favicon from the website. |
221 { | 238 { |
222 PendingTaskWaiter waiter(web_contents(), this); | 239 PendingTaskWaiter waiter(web_contents(), this); |
223 chrome::ExecuteCommand(browser(), IDC_RELOAD_IGNORING_CACHE); | 240 chrome::ExecuteCommand(browser(), IDC_RELOAD_IGNORING_CACHE); |
224 waiter.Wait(); | 241 waiter.Wait(); |
225 } | 242 } |
226 ASSERT_TRUE(delegate->was_requested()); | 243 ASSERT_TRUE(delegate->was_requested()); |
227 EXPECT_TRUE(delegate->bypassed_cache()); | 244 EXPECT_TRUE(delegate->bypassed_cache()); |
228 } | 245 } |
OLD | NEW |