Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(276)

Side by Side Diff: chrome/browser/dom_ui/new_tab_ui.cc

Issue 5088001: Add pyauto hook for getting and manipulating the data underneath the NTP. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/chrome/test/functional
Patch Set: ... Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #include "chrome/browser/dom_ui/new_tab_ui.h" 7 #include "chrome/browser/dom_ui/new_tab_ui.h"
8 8
9 #include <set> 9 #include <set>
10 10
(...skipping 10 matching lines...) Expand all
21 #include "chrome/browser/browser_thread.h" 21 #include "chrome/browser/browser_thread.h"
22 #include "chrome/browser/dom_ui/app_launcher_handler.h" 22 #include "chrome/browser/dom_ui/app_launcher_handler.h"
23 #include "chrome/browser/dom_ui/dom_ui_theme_source.h" 23 #include "chrome/browser/dom_ui/dom_ui_theme_source.h"
24 #include "chrome/browser/dom_ui/foreign_session_handler.h" 24 #include "chrome/browser/dom_ui/foreign_session_handler.h"
25 #include "chrome/browser/dom_ui/most_visited_handler.h" 25 #include "chrome/browser/dom_ui/most_visited_handler.h"
26 #include "chrome/browser/dom_ui/new_tab_page_sync_handler.h" 26 #include "chrome/browser/dom_ui/new_tab_page_sync_handler.h"
27 #include "chrome/browser/dom_ui/ntp_login_handler.h" 27 #include "chrome/browser/dom_ui/ntp_login_handler.h"
28 #include "chrome/browser/dom_ui/ntp_resource_cache.h" 28 #include "chrome/browser/dom_ui/ntp_resource_cache.h"
29 #include "chrome/browser/dom_ui/shown_sections_handler.h" 29 #include "chrome/browser/dom_ui/shown_sections_handler.h"
30 #include "chrome/browser/dom_ui/tips_handler.h" 30 #include "chrome/browser/dom_ui/tips_handler.h"
31 #include "chrome/browser/dom_ui/value_helper.h"
31 #include "chrome/browser/metrics/user_metrics.h" 32 #include "chrome/browser/metrics/user_metrics.h"
32 #include "chrome/browser/themes/browser_theme_provider.h" 33 #include "chrome/browser/themes/browser_theme_provider.h"
33 #include "chrome/browser/prefs/pref_service.h" 34 #include "chrome/browser/prefs/pref_service.h"
34 #include "chrome/browser/profile.h" 35 #include "chrome/browser/profile.h"
35 #include "chrome/browser/renderer_host/render_view_host.h" 36 #include "chrome/browser/renderer_host/render_view_host.h"
36 #include "chrome/browser/sessions/session_types.h" 37 #include "chrome/browser/sessions/session_types.h"
37 #include "chrome/browser/sessions/tab_restore_service.h" 38 #include "chrome/browser/sessions/tab_restore_service.h"
38 #include "chrome/browser/sessions/tab_restore_service_observer.h" 39 #include "chrome/browser/sessions/tab_restore_service_observer.h"
39 #include "chrome/browser/sync/profile_sync_service.h" 40 #include "chrome/browser/sync/profile_sync_service.h"
40 #include "chrome/browser/tab_contents/tab_contents.h" 41 #include "chrome/browser/tab_contents/tab_contents.h"
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 // Observer callback for TabRestoreServiceObserver. Sends data on 146 // Observer callback for TabRestoreServiceObserver. Sends data on
146 // recently closed tabs to the javascript side of this page to 147 // recently closed tabs to the javascript side of this page to
147 // display to the user. 148 // display to the user.
148 virtual void TabRestoreServiceChanged(TabRestoreService* service); 149 virtual void TabRestoreServiceChanged(TabRestoreService* service);
149 150
150 // Observer callback to notice when our associated TabRestoreService 151 // Observer callback to notice when our associated TabRestoreService
151 // is destroyed. 152 // is destroyed.
152 virtual void TabRestoreServiceDestroyed(TabRestoreService* service); 153 virtual void TabRestoreServiceDestroyed(TabRestoreService* service);
153 154
154 private: 155 private:
155 // Converts a closed tab to the value sent down to the NTP. Returns true on
156 // success, false if the value shouldn't be sent down.
157 bool TabToValue(const TabRestoreService::Tab& tab,
158 DictionaryValue* dictionary);
159
160 // Converts a closed window to the value sent down to the NTP. Returns true
161 // on success, false if the value shouldn't be sent down.
162 bool WindowToValue(const TabRestoreService::Window& window,
163 DictionaryValue* dictionary);
164
165 // Adds tab to unique_items list if it is not present. Returns false if
166 // tab was already in the list, true if it was absent. A tab is
167 // considered unique if no other tab shares both its title and its url.
168 bool EnsureTabIsUnique(const DictionaryValue* tab,
169 std::set<std::string>* unique_items);
170
171 // TabRestoreService that we are observing. 156 // TabRestoreService that we are observing.
172 TabRestoreService* tab_restore_service_; 157 TabRestoreService* tab_restore_service_;
173 158
174 DISALLOW_COPY_AND_ASSIGN(RecentlyClosedTabsHandler); 159 DISALLOW_COPY_AND_ASSIGN(RecentlyClosedTabsHandler);
175 }; 160 };
176 161
177 void RecentlyClosedTabsHandler::RegisterMessages() { 162 void RecentlyClosedTabsHandler::RegisterMessages() {
178 dom_ui_->RegisterMessageCallback("getRecentlyClosedTabs", 163 dom_ui_->RegisterMessageCallback("getRecentlyClosedTabs",
179 NewCallback(this, 164 NewCallback(this,
180 &RecentlyClosedTabsHandler::HandleGetRecentlyClosedTabs)); 165 &RecentlyClosedTabsHandler::HandleGetRecentlyClosedTabs));
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 tab_restore_service_->AddObserver(this); 200 tab_restore_service_->AddObserver(this);
216 } 201 }
217 } 202 }
218 203
219 if (tab_restore_service_) 204 if (tab_restore_service_)
220 TabRestoreServiceChanged(tab_restore_service_); 205 TabRestoreServiceChanged(tab_restore_service_);
221 } 206 }
222 207
223 void RecentlyClosedTabsHandler::TabRestoreServiceChanged( 208 void RecentlyClosedTabsHandler::TabRestoreServiceChanged(
224 TabRestoreService* service) { 209 TabRestoreService* service) {
225 const TabRestoreService::Entries& entries = service->entries();
226 ListValue list_value; 210 ListValue list_value;
227 std::set<std::string> unique_items; 211 NewTabUI::AddRecentlyClosedEntries(service->entries(), &list_value);
228 int added_count = 0;
229 const int max_count = 10;
230 212
231 // We filter the list of recently closed to only show 'interesting' entries,
232 // where an interesting entry is either a closed window or a closed tab
233 // whose selected navigation is not the new tab ui.
234 for (TabRestoreService::Entries::const_iterator it = entries.begin();
235 it != entries.end() && added_count < max_count; ++it) {
236 TabRestoreService::Entry* entry = *it;
237 DictionaryValue* value = new DictionaryValue();
238 if ((entry->type == TabRestoreService::TAB &&
239 TabToValue(*static_cast<TabRestoreService::Tab*>(entry), value) &&
240 EnsureTabIsUnique(value, &unique_items)) ||
241 (entry->type == TabRestoreService::WINDOW &&
242 WindowToValue(*static_cast<TabRestoreService::Window*>(entry),
243 value))) {
244 value->SetInteger("sessionId", entry->id);
245 list_value.Append(value);
246 added_count++;
247 } else {
248 delete value;
249 }
250 }
251 dom_ui_->CallJavascriptFunction(L"recentlyClosedTabs", list_value); 213 dom_ui_->CallJavascriptFunction(L"recentlyClosedTabs", list_value);
252 } 214 }
253 215
254 void RecentlyClosedTabsHandler::TabRestoreServiceDestroyed( 216 void RecentlyClosedTabsHandler::TabRestoreServiceDestroyed(
255 TabRestoreService* service) { 217 TabRestoreService* service) {
256 tab_restore_service_ = NULL; 218 tab_restore_service_ = NULL;
257 } 219 }
258 220
259 bool RecentlyClosedTabsHandler::TabToValue(
260 const TabRestoreService::Tab& tab,
261 DictionaryValue* dictionary) {
262 if (tab.navigations.empty())
263 return false;
264
265 const TabNavigation& current_navigation =
266 tab.navigations.at(tab.current_navigation_index);
267 if (current_navigation.virtual_url() == GURL(chrome::kChromeUINewTabURL))
268 return false;
269
270 NewTabUI::SetURLTitleAndDirection(dictionary, current_navigation.title(),
271 current_navigation.virtual_url());
272 dictionary->SetString("type", "tab");
273 dictionary->SetReal("timestamp", tab.timestamp.ToDoubleT());
274 return true;
275 }
276
277 bool RecentlyClosedTabsHandler::WindowToValue(
278 const TabRestoreService::Window& window,
279 DictionaryValue* dictionary) {
280 if (window.tabs.empty()) {
281 NOTREACHED();
282 return false;
283 }
284
285 ListValue* tab_values = new ListValue();
286 for (size_t i = 0; i < window.tabs.size(); ++i) {
287 DictionaryValue* tab_value = new DictionaryValue();
288 if (TabToValue(window.tabs[i], tab_value))
289 tab_values->Append(tab_value);
290 else
291 delete tab_value;
292 }
293 if (tab_values->GetSize() == 0) {
294 delete tab_values;
295 return false;
296 }
297
298 dictionary->SetString("type", "window");
299 dictionary->SetReal("timestamp", window.timestamp.ToDoubleT());
300 dictionary->Set("tabs", tab_values);
301 return true;
302 }
303
304 bool RecentlyClosedTabsHandler::EnsureTabIsUnique(
305 const DictionaryValue* tab,
306 std::set<std::string>* unique_items) {
307 DCHECK(unique_items);
308 std::string title;
309 std::string url;
310 if (tab->GetString("title", &title) &&
311 tab->GetString("url", &url)) {
312 // TODO(viettrungluu): this isn't obviously reliable, since different
313 // combinations of titles/urls may conceivably yield the same string.
314 std::string unique_key = title + url;
315 if (unique_items->find(unique_key) != unique_items->end())
316 return false;
317 else
318 unique_items->insert(unique_key);
319 }
320 return true;
321 }
322
323 221
324 /////////////////////////////////////////////////////////////////////////////// 222 ///////////////////////////////////////////////////////////////////////////////
325 // MetricsHandler 223 // MetricsHandler
326 224
327 // Let the page contents record UMA actions. Only use when you can't do it from 225 // Let the page contents record UMA actions. Only use when you can't do it from
328 // C++. For example, we currently use it to let the NTP log the postion of the 226 // C++. For example, we currently use it to let the NTP log the postion of the
329 // Most Visited or Bookmark the user clicked on, as we don't get that 227 // Most Visited or Bookmark the user clicked on, as we don't get that
330 // information through RequestOpenURL. You will need to update the metrics 228 // information through RequestOpenURL. You will need to update the metrics
331 // dashboard with the action names you use, as our processor won't catch that 229 // dashboard with the action names you use, as our processor won't catch that
332 // information (treat it as RecordComputedMetrics) 230 // information (treat it as RecordComputedMetrics)
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 direction = kRTLHtmlTextDirection; 496 direction = kRTLHtmlTextDirection;
599 } else { 497 } else {
600 base::i18n::WrapStringWithLTRFormatting(&title_to_set); 498 base::i18n::WrapStringWithLTRFormatting(&title_to_set);
601 } 499 }
602 } 500 }
603 } 501 }
604 dictionary->SetString("title", title_to_set); 502 dictionary->SetString("title", title_to_set);
605 dictionary->SetString("direction", direction); 503 dictionary->SetString("direction", direction);
606 } 504 }
607 505
506 namespace {
507
508 bool IsTabUnique(const DictionaryValue* tab,
509 std::set<std::string>* unique_items) {
510 DCHECK(unique_items);
511 std::string title;
512 std::string url;
513 if (tab->GetString("title", &title) &&
514 tab->GetString("url", &url)) {
515 // TODO(viettrungluu): this isn't obviously reliable, since different
516 // combinations of titles/urls may conceivably yield the same string.
517 std::string unique_key = title + url;
518 if (unique_items->find(unique_key) != unique_items->end())
519 return false;
520 else
521 unique_items->insert(unique_key);
522 }
523 return true;
524 }
525
526 } // namespace
527
528 // static
529 void NewTabUI::AddRecentlyClosedEntries(
530 const TabRestoreService::Entries& entries, ListValue* entry_list_value) {
531 const int max_count = 10;
532 int added_count = 0;
533 std::set<std::string> unique_items;
534 // We filter the list of recently closed to only show 'interesting' entries,
535 // where an interesting entry is either a closed window or a closed tab
536 // whose selected navigation is not the new tab ui.
537 for (TabRestoreService::Entries::const_iterator it = entries.begin();
538 it != entries.end() && added_count < max_count; ++it) {
539 TabRestoreService::Entry* entry = *it;
540 scoped_ptr<DictionaryValue> entry_dict(new DictionaryValue());
541 if ((entry->type == TabRestoreService::TAB &&
542 ValueHelper::TabToValue(
543 *static_cast<TabRestoreService::Tab*>(entry),
544 entry_dict.get()) &&
545 IsTabUnique(entry_dict.get(), &unique_items)) ||
546 (entry->type == TabRestoreService::WINDOW &&
547 ValueHelper::WindowToValue(
548 *static_cast<TabRestoreService::Window*>(entry),
549 entry_dict.get()))) {
550 entry_dict->SetInteger("sessionId", entry->id);
551 entry_list_value->Append(entry_dict.release());
552 added_count++;
553 }
554 }
555 }
556
608 /////////////////////////////////////////////////////////////////////////////// 557 ///////////////////////////////////////////////////////////////////////////////
609 // NewTabHTMLSource 558 // NewTabHTMLSource
610 559
611 bool NewTabUI::NewTabHTMLSource::first_run_ = true; 560 bool NewTabUI::NewTabHTMLSource::first_run_ = true;
612 561
613 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) 562 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile)
614 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), 563 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()),
615 profile_(profile) { 564 profile_(profile) {
616 } 565 }
617 566
(...skipping 13 matching lines...) Expand all
631 580
632 scoped_refptr<RefCountedBytes> html_bytes( 581 scoped_refptr<RefCountedBytes> html_bytes(
633 profile_->GetNTPResourceCache()->GetNewTabHTML(is_off_the_record)); 582 profile_->GetNTPResourceCache()->GetNewTabHTML(is_off_the_record));
634 583
635 SendResponse(request_id, html_bytes); 584 SendResponse(request_id, html_bytes);
636 } 585 }
637 586
638 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const { 587 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const {
639 return "text/html"; 588 return "text/html";
640 } 589 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698