| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/webui/history_ui.h" | 5 #include "chrome/browser/ui/webui/history_ui.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <set> | 7 #include <set> |
| 9 | 8 |
| 10 #include "base/bind.h" | 9 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 12 #include "base/callback_old.h" | 11 #include "base/callback_old.h" |
| 13 #include "base/i18n/time_formatting.h" | 12 #include "base/i18n/time_formatting.h" |
| 14 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
| 15 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 16 #include "base/string16.h" | 15 #include "base/string16.h" |
| 17 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
| 18 #include "base/string_piece.h" | 17 #include "base/string_piece.h" |
| 19 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 20 #include "base/time.h" | 19 #include "base/time.h" |
| 21 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 22 #include "base/values.h" | 21 #include "base/values.h" |
| 23 #include "chrome/browser/bookmarks/bookmark_model.h" | 22 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 23 #include "chrome/browser/history/history_notifications.h" |
| 24 #include "chrome/browser/history/history_types.h" | 24 #include "chrome/browser/history/history_types.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
| 27 #include "chrome/browser/ui/browser_list.h" | 27 #include "chrome/browser/ui/browser_list.h" |
| 28 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 28 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 29 #include "chrome/browser/ui/webui/favicon_source.h" | 29 #include "chrome/browser/ui/webui/favicon_source.h" |
| 30 #include "chrome/common/chrome_notification_types.h" | 30 #include "chrome/common/chrome_notification_types.h" |
| 31 #include "chrome/common/time_format.h" | 31 #include "chrome/common/time_format.h" |
| 32 #include "chrome/common/url_constants.h" | 32 #include "chrome/common/url_constants.h" |
| 33 #include "content/browser/tab_contents/tab_contents.h" | 33 #include "content/browser/tab_contents/tab_contents.h" |
| 34 #include "content/browser/tab_contents/tab_contents_delegate.h" | 34 #include "content/browser/tab_contents/tab_contents_delegate.h" |
| 35 #include "content/browser/user_metrics.h" | 35 #include "content/browser/user_metrics.h" |
| 36 #include "content/public/browser/notification_details.h" |
| 36 #include "content/public/browser/notification_source.h" | 37 #include "content/public/browser/notification_source.h" |
| 37 #include "grit/browser_resources.h" | 38 #include "grit/browser_resources.h" |
| 38 #include "grit/chromium_strings.h" | 39 #include "grit/chromium_strings.h" |
| 39 #include "grit/generated_resources.h" | 40 #include "grit/generated_resources.h" |
| 40 #include "grit/locale_settings.h" | 41 #include "grit/locale_settings.h" |
| 41 #include "grit/theme_resources.h" | 42 #include "grit/theme_resources.h" |
| 42 #include "grit/theme_resources_standard.h" | 43 #include "grit/theme_resources_standard.h" |
| 43 #include "net/base/escape.h" | 44 #include "net/base/escape.h" |
| 44 #include "ui/base/l10n/l10n_util.h" | 45 #include "ui/base/l10n/l10n_util.h" |
| 45 #include "ui/base/resource/resource_bundle.h" | 46 #include "ui/base/resource/resource_bundle.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 66 int request_id) OVERRIDE; | 67 int request_id) OVERRIDE; |
| 67 | 68 |
| 68 virtual std::string GetMimeType(const std::string& path) const OVERRIDE; | 69 virtual std::string GetMimeType(const std::string& path) const OVERRIDE; |
| 69 | 70 |
| 70 private: | 71 private: |
| 71 ~HistoryUIHTMLSource(); | 72 ~HistoryUIHTMLSource(); |
| 72 | 73 |
| 73 DISALLOW_COPY_AND_ASSIGN(HistoryUIHTMLSource); | 74 DISALLOW_COPY_AND_ASSIGN(HistoryUIHTMLSource); |
| 74 }; | 75 }; |
| 75 | 76 |
| 76 | |
| 77 HistoryUIHTMLSource::HistoryUIHTMLSource() | 77 HistoryUIHTMLSource::HistoryUIHTMLSource() |
| 78 : ChromeWebUIDataSource(chrome::kChromeUIHistoryHost) { | 78 : ChromeWebUIDataSource(chrome::kChromeUIHistoryHost) { |
| 79 AddLocalizedString("loading", IDS_HISTORY_LOADING); | 79 AddLocalizedString("loading", IDS_HISTORY_LOADING); |
| 80 AddLocalizedString("title", IDS_HISTORY_TITLE); | 80 AddLocalizedString("title", IDS_HISTORY_TITLE); |
| 81 AddLocalizedString("newest", IDS_HISTORY_NEWEST); | 81 AddLocalizedString("newest", IDS_HISTORY_NEWEST); |
| 82 AddLocalizedString("newer", IDS_HISTORY_NEWER); | 82 AddLocalizedString("newer", IDS_HISTORY_NEWER); |
| 83 AddLocalizedString("older", IDS_HISTORY_OLDER); | 83 AddLocalizedString("older", IDS_HISTORY_OLDER); |
| 84 AddLocalizedString("searchresultsfor", IDS_HISTORY_SEARCHRESULTSFOR); | 84 AddLocalizedString("searchresultsfor", IDS_HISTORY_SEARCHRESULTSFOR); |
| 85 AddLocalizedString("history", IDS_HISTORY_BROWSERESULTS); | 85 AddLocalizedString("history", IDS_HISTORY_BROWSERESULTS); |
| 86 AddLocalizedString("cont", IDS_HISTORY_CONTINUED); | 86 AddLocalizedString("cont", IDS_HISTORY_CONTINUED); |
| 87 AddLocalizedString("searchbutton", IDS_HISTORY_SEARCH_BUTTON); | 87 AddLocalizedString("searchbutton", IDS_HISTORY_SEARCH_BUTTON); |
| 88 AddLocalizedString("noresults", IDS_HISTORY_NO_RESULTS); | 88 AddLocalizedString("noresults", IDS_HISTORY_NO_RESULTS); |
| 89 AddLocalizedString("noitems", IDS_HISTORY_NO_ITEMS); | 89 AddLocalizedString("noitems", IDS_HISTORY_NO_ITEMS); |
| 90 AddLocalizedString("edithistory", IDS_HISTORY_START_EDITING_HISTORY); | 90 AddLocalizedString("edithistory", IDS_HISTORY_START_EDITING_HISTORY); |
| 91 AddLocalizedString("doneediting", IDS_HISTORY_STOP_EDITING_HISTORY); | 91 AddLocalizedString("doneediting", IDS_HISTORY_STOP_EDITING_HISTORY); |
| 92 AddLocalizedString("removeselected", IDS_HISTORY_REMOVE_SELECTED_ITEMS); | 92 AddLocalizedString("removeselected", IDS_HISTORY_REMOVE_SELECTED_ITEMS); |
| 93 AddLocalizedString("clearallhistory", | 93 AddLocalizedString("clearallhistory", |
| 94 IDS_HISTORY_OPEN_CLEAR_BROWSING_DATA_DIALOG); | 94 IDS_HISTORY_OPEN_CLEAR_BROWSING_DATA_DIALOG); |
| 95 AddLocalizedString("deletewarning", | 95 AddLocalizedString("deletewarning", |
| 96 IDS_HISTORY_DELETE_PRIOR_VISITS_WARNING); | 96 IDS_HISTORY_DELETE_PRIOR_VISITS_WARNING); |
| 97 AddLocalizedString("actionMenuDescription", |
| 98 IDS_HISTORY_ACTION_MENU_DESCRIPTION); |
| 99 AddLocalizedString("removeFromHistory", IDS_HISTORY_REMOVE_PAGE); |
| 100 AddLocalizedString("moreFromSite", IDS_HISTORY_MORE_FROM_SITE); |
| 97 } | 101 } |
| 98 | 102 |
| 99 HistoryUIHTMLSource::~HistoryUIHTMLSource() { | 103 HistoryUIHTMLSource::~HistoryUIHTMLSource() { |
| 100 } | 104 } |
| 101 | 105 |
| 102 void HistoryUIHTMLSource::StartDataRequest(const std::string& path, | 106 void HistoryUIHTMLSource::StartDataRequest(const std::string& path, |
| 103 bool is_incognito, | 107 bool is_incognito, |
| 104 int request_id) { | 108 int request_id) { |
| 105 if (path == kStringsJsFile) { | 109 if (path == kStringsJsFile) { |
| 106 SendLocalizedStringsAsJSON(request_id); | 110 SendLocalizedStringsAsJSON(request_id); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 } | 216 } |
| 213 | 217 |
| 214 void BrowsingHistoryHandler::HandleRemoveURLsOnOneDay(const ListValue* args) { | 218 void BrowsingHistoryHandler::HandleRemoveURLsOnOneDay(const ListValue* args) { |
| 215 if (cancelable_delete_consumer_.HasPendingRequests()) { | 219 if (cancelable_delete_consumer_.HasPendingRequests()) { |
| 216 web_ui_->CallJavascriptFunction("deleteFailed"); | 220 web_ui_->CallJavascriptFunction("deleteFailed"); |
| 217 return; | 221 return; |
| 218 } | 222 } |
| 219 | 223 |
| 220 // Get day to delete data from. | 224 // Get day to delete data from. |
| 221 int visit_time = 0; | 225 int visit_time = 0; |
| 222 ExtractIntegerValue(args, &visit_time); | 226 if (!ExtractIntegerValue(args, &visit_time)) { |
| 227 LOG(ERROR) << "Unable to extract integer argument."; |
| 228 web_ui_->CallJavascriptFunction("deleteFailed"); |
| 229 return; |
| 230 } |
| 223 base::Time::Exploded exploded; | 231 base::Time::Exploded exploded; |
| 224 base::Time::FromTimeT( | 232 base::Time::FromTimeT( |
| 225 static_cast<time_t>(visit_time)).LocalExplode(&exploded); | 233 static_cast<time_t>(visit_time)).LocalExplode(&exploded); |
| 226 exploded.hour = exploded.minute = exploded.second = exploded.millisecond = 0; | 234 exploded.hour = exploded.minute = exploded.second = exploded.millisecond = 0; |
| 227 base::Time begin_time = base::Time::FromLocalExploded(exploded); | 235 base::Time begin_time = base::Time::FromLocalExploded(exploded); |
| 228 base::Time end_time = begin_time + base::TimeDelta::FromDays(1); | 236 base::Time end_time = begin_time + base::TimeDelta::FromDays(1); |
| 229 | 237 |
| 230 // Get URLs. | 238 // Get URLs. |
| 231 std::set<GURL> urls; | 239 DCHECK(urls_to_be_deleted_.empty()); |
| 232 for (ListValue::const_iterator v = args->begin() + 1; | 240 for (ListValue::const_iterator v = args->begin() + 1; |
| 233 v != args->end(); ++v) { | 241 v != args->end(); ++v) { |
| 234 if ((*v)->GetType() != Value::TYPE_STRING) | 242 if ((*v)->GetType() != Value::TYPE_STRING) |
| 235 continue; | 243 continue; |
| 236 const StringValue* string_value = static_cast<const StringValue*>(*v); | 244 const StringValue* string_value = static_cast<const StringValue*>(*v); |
| 237 string16 string16_value; | 245 string16 string16_value; |
| 238 if (!string_value->GetAsString(&string16_value)) | 246 if (!string_value->GetAsString(&string16_value)) |
| 239 continue; | 247 continue; |
| 240 urls.insert(GURL(string16_value)); | 248 |
| 249 urls_to_be_deleted_.insert(GURL(string16_value)); |
| 241 } | 250 } |
| 242 | 251 |
| 243 HistoryService* hs = | 252 HistoryService* hs = |
| 244 Profile::FromWebUI(web_ui_)->GetHistoryService(Profile::EXPLICIT_ACCESS); | 253 Profile::FromWebUI(web_ui_)->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 245 hs->ExpireHistoryBetween( | 254 hs->ExpireHistoryBetween( |
| 246 urls, begin_time, end_time, &cancelable_delete_consumer_, | 255 urls_to_be_deleted_, begin_time, end_time, &cancelable_delete_consumer_, |
| 247 base::Bind(&BrowsingHistoryHandler::RemoveComplete, | 256 base::Bind(&BrowsingHistoryHandler::RemoveComplete, |
| 248 base::Unretained(this))); | 257 base::Unretained(this))); |
| 249 } | 258 } |
| 250 | 259 |
| 251 void BrowsingHistoryHandler::HandleClearBrowsingData(const ListValue* args) { | 260 void BrowsingHistoryHandler::HandleClearBrowsingData(const ListValue* args) { |
| 252 // TODO(beng): This is an improper direct dependency on Browser. Route this | 261 // TODO(beng): This is an improper direct dependency on Browser. Route this |
| 253 // through some sort of delegate. | 262 // through some sort of delegate. |
| 254 Profile* profile = Profile::FromWebUI(web_ui_); | 263 Profile* profile = Profile::FromWebUI(web_ui_); |
| 255 Browser* browser = BrowserList::FindBrowserWithProfile(profile); | 264 Browser* browser = BrowserList::FindBrowserWithProfile(profile); |
| 256 if (browser) | 265 if (browser) |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 } | 315 } |
| 307 | 316 |
| 308 DictionaryValue info_value; | 317 DictionaryValue info_value; |
| 309 info_value.SetString("term", search_text_); | 318 info_value.SetString("term", search_text_); |
| 310 info_value.SetBoolean("finished", results->reached_beginning()); | 319 info_value.SetBoolean("finished", results->reached_beginning()); |
| 311 | 320 |
| 312 web_ui_->CallJavascriptFunction("historyResult", info_value, results_value); | 321 web_ui_->CallJavascriptFunction("historyResult", info_value, results_value); |
| 313 } | 322 } |
| 314 | 323 |
| 315 void BrowsingHistoryHandler::RemoveComplete() { | 324 void BrowsingHistoryHandler::RemoveComplete() { |
| 316 // Some Visits were deleted from history. Reload the list. | 325 urls_to_be_deleted_.clear(); |
| 326 |
| 327 // Notify the page that the deletion request succeeded. |
| 317 web_ui_->CallJavascriptFunction("deleteComplete"); | 328 web_ui_->CallJavascriptFunction("deleteComplete"); |
| 318 } | 329 } |
| 319 | 330 |
| 320 void BrowsingHistoryHandler::ExtractSearchHistoryArguments( | 331 void BrowsingHistoryHandler::ExtractSearchHistoryArguments( |
| 321 const ListValue* args, | 332 const ListValue* args, |
| 322 int* month, | 333 int* month, |
| 323 string16* query) { | 334 string16* query) { |
| 324 CHECK(args->GetSize() == 2); | 335 *month = 0; |
| 325 query->clear(); | 336 Value* list_member; |
| 326 CHECK(args->GetString(0, query)); | |
| 327 | 337 |
| 328 string16 string16_value; | 338 // Get search string. |
| 329 CHECK(args->GetString(1, &string16_value)); | 339 if (args->Get(0, &list_member) && |
| 330 *month = 0; | 340 list_member->GetType() == Value::TYPE_STRING) { |
| 331 base::StringToInt(string16_value, month); | 341 const StringValue* string_value = |
| 342 static_cast<const StringValue*>(list_member); |
| 343 string_value->GetAsString(query); |
| 344 } |
| 345 |
| 346 // Get search month. |
| 347 if (args->Get(1, &list_member) && |
| 348 list_member->GetType() == Value::TYPE_STRING) { |
| 349 const StringValue* string_value = |
| 350 static_cast<const StringValue*>(list_member); |
| 351 string16 string16_value; |
| 352 if (string_value->GetAsString(&string16_value)) |
| 353 base::StringToInt(string16_value, month); |
| 354 } |
| 332 } | 355 } |
| 333 | 356 |
| 334 history::QueryOptions BrowsingHistoryHandler::CreateMonthQueryOptions( | 357 history::QueryOptions BrowsingHistoryHandler::CreateMonthQueryOptions( |
| 335 int month) { | 358 int month) { |
| 336 history::QueryOptions options; | 359 history::QueryOptions options; |
| 337 | 360 |
| 338 // Configure the begin point of the search to the start of the | 361 // Configure the begin point of the search to the start of the |
| 339 // current month. | 362 // current month. |
| 340 base::Time::Exploded exploded; | 363 base::Time::Exploded exploded; |
| 341 base::Time::Now().LocalMidnight().LocalExplode(&exploded); | 364 base::Time::Now().LocalMidnight().LocalExplode(&exploded); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 } | 398 } |
| 376 | 399 |
| 377 void BrowsingHistoryHandler::Observe( | 400 void BrowsingHistoryHandler::Observe( |
| 378 int type, | 401 int type, |
| 379 const content::NotificationSource& source, | 402 const content::NotificationSource& source, |
| 380 const content::NotificationDetails& details) { | 403 const content::NotificationDetails& details) { |
| 381 if (type != chrome::NOTIFICATION_HISTORY_URLS_DELETED) { | 404 if (type != chrome::NOTIFICATION_HISTORY_URLS_DELETED) { |
| 382 NOTREACHED(); | 405 NOTREACHED(); |
| 383 return; | 406 return; |
| 384 } | 407 } |
| 385 | 408 history::URLsDeletedDetails* deletedDetails = |
| 386 // Some URLs were deleted from history. Reload the list. | 409 content::Details<history::URLsDeletedDetails>(details).ptr(); |
| 387 web_ui_->CallJavascriptFunction("historyDeleted"); | 410 if (deletedDetails->urls != urls_to_be_deleted_) { |
| 411 // Notify the page that someone else deleted from the history. |
| 412 web_ui_->CallJavascriptFunction("historyDeleted"); |
| 413 } |
| 388 } | 414 } |
| 389 | 415 |
| 390 //////////////////////////////////////////////////////////////////////////////// | 416 //////////////////////////////////////////////////////////////////////////////// |
| 391 // | 417 // |
| 392 // HistoryUI | 418 // HistoryUI |
| 393 // | 419 // |
| 394 //////////////////////////////////////////////////////////////////////////////// | 420 //////////////////////////////////////////////////////////////////////////////// |
| 395 | 421 |
| 396 HistoryUI::HistoryUI(TabContents* contents) : ChromeWebUI(contents) { | 422 HistoryUI::HistoryUI(TabContents* contents) : ChromeWebUI(contents) { |
| 397 AddMessageHandler((new BrowsingHistoryHandler())->Attach(this)); | 423 AddMessageHandler((new BrowsingHistoryHandler())->Attach(this)); |
| 398 | 424 |
| 399 HistoryUIHTMLSource* html_source = new HistoryUIHTMLSource(); | 425 HistoryUIHTMLSource* html_source = new HistoryUIHTMLSource(); |
| 400 | 426 |
| 401 // Set up the chrome://history/ source. | 427 // Set up the chrome://history/ source. |
| 402 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); | 428 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); |
| 403 profile->GetChromeURLDataManager()->AddDataSource(html_source); | 429 profile->GetChromeURLDataManager()->AddDataSource(html_source); |
| 404 } | 430 } |
| 405 | 431 |
| 406 // static | 432 // static |
| 407 const GURL HistoryUI::GetHistoryURLWithSearchText(const string16& text) { | 433 const GURL HistoryUI::GetHistoryURLWithSearchText(const string16& text) { |
| 408 return GURL(std::string(chrome::kChromeUIHistoryURL) + "#q=" + | 434 return GURL(std::string(chrome::kChromeUIHistoryURL) + "#q=" + |
| 409 net::EscapeQueryParamValue(UTF16ToUTF8(text), true)); | 435 net::EscapeQueryParamValue(UTF16ToUTF8(text), true)); |
| 410 } | 436 } |
| 411 | 437 |
| 412 // static | 438 // static |
| 413 RefCountedMemory* HistoryUI::GetFaviconResourceBytes() { | 439 RefCountedMemory* HistoryUI::GetFaviconResourceBytes() { |
| 414 return ResourceBundle::GetSharedInstance(). | 440 return ResourceBundle::GetSharedInstance(). |
| 415 LoadDataResourceBytes(IDR_HISTORY_FAVICON); | 441 LoadDataResourceBytes(IDR_HISTORY_FAVICON); |
| 416 } | 442 } |
| OLD | NEW |