blob: 6c63f2c934e8d90ccfe3a5f5462f9bb8bd3c8985 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include "chrome/browser/history_tab_ui.h"
6
7#include "base/string_util.h"
8#include "chrome/app/theme/theme_resources.h"
9#include "chrome/browser/history_model.h"
10#include "chrome/browser/history_view.h"
11#include "chrome/browser/user_metrics.h"
12#include "chrome/common/l10n_util.h"
13#include "chrome/common/resource_bundle.h"
14#include "chrome/views/checkbox.h"
15#include "net/base/escape.h"
16
17#include "generated_resources.h"
18
19// State key used to identify search text.
20static const wchar_t kSearchTextKey[] = L"st";
21
22// State key used for whether the search is for starred pages only.
23static const wchar_t kStarredOnlyKey[] = L"starred_only";
24
25// HistoryTabUIFactory ------------------------------------------------------
26
27class HistoryTabUIFactory : public NativeUIFactory {
28 public:
29 HistoryTabUIFactory() {}
30 virtual ~HistoryTabUIFactory() {}
31
32 virtual NativeUI* CreateNativeUIForURL(const GURL& url,
33 NativeUIContents* contents) {
34 HistoryTabUI* tab_ui = new HistoryTabUI(contents);
35 tab_ui->Init();
36 return tab_ui;
37 }
38
39 private:
40 DISALLOW_EVIL_CONSTRUCTORS(HistoryTabUIFactory);
41};
42
43// HistoryTabUI -------------------------------------------------------------
44
45HistoryTabUI::HistoryTabUI(NativeUIContents* contents)
46#pragma warning(suppress: 4355) // Okay to pass "this" here.
47 : searchable_container_(this),
48 contents_(contents) {
49}
50
51void HistoryTabUI::Init() {
52 model_ = CreateModel();
53 searchable_container_.SetContents(CreateHistoryView());
54}
55
56const std::wstring HistoryTabUI::GetTitle() const {
57 return l10n_util::GetString(IDS_HISTORY_TITLE);
58}
59
60const int HistoryTabUI::GetFavIconID() const {
61 return IDR_HISTORY_FAVICON;
62}
63
64const int HistoryTabUI::GetSectionIconID() const {
65 return IDR_HISTORY_SECTION;
66}
67
68const std::wstring HistoryTabUI::GetSearchButtonText() const {
69 return l10n_util::GetString(IDS_HISTORY_SEARCH_BUTTON);
70}
71
[email protected]c2dacc92008-10-16 23:51:3872views::View* HistoryTabUI::GetView() {
initial.commit09911bf2008-07-26 23:55:2973 return &searchable_container_;
74}
75
76void HistoryTabUI::WillBecomeVisible(NativeUIContents* parent) {
77 UserMetrics::RecordAction(L"Destination_History", parent->profile());
78}
79
80void HistoryTabUI::WillBecomeInvisible(NativeUIContents* parent) {
81}
82
83void HistoryTabUI::Navigate(const PageState& state) {
84 std::wstring search_text;
85 state.GetProperty(kSearchTextKey, &search_text);
86 // Make sure a query starts on navigation, that way if history has changed
87 // since we last issued the query we'll show the right thing.
88 if (model_->GetSearchText() == search_text)
89 model_->Refresh();
90 else
91 model_->SetSearchText(search_text);
92 searchable_container_.GetSearchField()->SetText(search_text);
93
94 ChangedModel();
95}
96
97bool HistoryTabUI::SetInitialFocus() {
98 searchable_container_.GetSearchField()->RequestFocus();
99 return true;
100}
101
102// static
103GURL HistoryTabUI::GetURL() {
104 std::string spec(NativeUIContents::GetScheme());
105 spec.append("://history");
106 return GURL(spec);
107}
108
109// static
110NativeUIFactory* HistoryTabUI::GetNativeUIFactory() {
111 return new HistoryTabUIFactory();
112}
113
114// static
115const GURL HistoryTabUI::GetHistoryURLWithSearchText(
116 const std::wstring& text) {
117 return GURL(GetURL().spec() + "/params?" + WideToUTF8(kSearchTextKey) + "=" +
118 EscapeQueryParamValue(WideToUTF8(text)));
119}
120
121BaseHistoryModel* HistoryTabUI::CreateModel() {
122 return new HistoryModel(contents_->profile(), std::wstring());
123}
124
125HistoryView* HistoryTabUI::CreateHistoryView() {
126 return new HistoryView(&searchable_container_, model_, contents_);
127}
128
129void HistoryTabUI::ChangedModel() {
130 HistoryView* history_view =
131 static_cast<HistoryView*>(searchable_container_.GetContents());
132 history_view->SetShowDeleteControls(model_->GetSearchText().empty());
133 if (!model_->GetSearchText().empty())
134 UserMetrics::RecordAction(L"History_Search", contents_->profile());
135}
136
137void HistoryTabUI::DoSearch(const std::wstring& text) {
138 if (model_->GetSearchText() == text)
139 return;
140
141 model_->SetSearchText(text);
142
143 // Update the page state.
144 PageState* page_state = contents_->page_state().Copy();
145 page_state->SetProperty(kSearchTextKey, text);
146 contents_->SetPageState(page_state);
147
148 ChangedModel();
149}
license.botbf09a502008-08-24 00:55:55150