| 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/views/omnibox/omnibox_view_views.h" | 5 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/property_bag.h" | |
| 9 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_input.h" | 11 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_match.h" | 12 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 14 #include "chrome/browser/bookmarks/bookmark_node_data.h" | 13 #include "chrome/browser/bookmarks/bookmark_node_data.h" |
| 15 #include "chrome/browser/command_updater.h" | 14 #include "chrome/browser/command_updater.h" |
| 16 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" | 15 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" |
| 17 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" | 16 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
| 18 #include "chrome/browser/ui/search/search.h" | 17 #include "chrome/browser/ui/search/search.h" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 // Stores omnibox state for each tab. | 153 // Stores omnibox state for each tab. |
| 155 struct ViewState { | 154 struct ViewState { |
| 156 explicit ViewState(const gfx::SelectionModel& selection_model) | 155 explicit ViewState(const gfx::SelectionModel& selection_model) |
| 157 : selection_model(selection_model) { | 156 : selection_model(selection_model) { |
| 158 } | 157 } |
| 159 | 158 |
| 160 // SelectionModel of selected text. | 159 // SelectionModel of selected text. |
| 161 gfx::SelectionModel selection_model; | 160 gfx::SelectionModel selection_model; |
| 162 }; | 161 }; |
| 163 | 162 |
| 164 struct AutocompleteEditState { | 163 const char kAutocompleteEditStateKey[] = "AutocompleteEditState"; |
| 164 |
| 165 struct AutocompleteEditState : public base::SupportsUserData::Data { |
| 165 AutocompleteEditState(const OmniboxEditModel::State& model_state, | 166 AutocompleteEditState(const OmniboxEditModel::State& model_state, |
| 166 const ViewState& view_state) | 167 const ViewState& view_state) |
| 167 : model_state(model_state), | 168 : model_state(model_state), |
| 168 view_state(view_state) { | 169 view_state(view_state) { |
| 169 } | 170 } |
| 171 virtual ~AutocompleteEditState() {} |
| 170 | 172 |
| 171 const OmniboxEditModel::State model_state; | 173 const OmniboxEditModel::State model_state; |
| 172 const ViewState view_state; | 174 const ViewState view_state; |
| 173 }; | 175 }; |
| 174 | 176 |
| 175 // Returns a lazily initialized property bag accessor for saving our state in a | |
| 176 // WebContents. | |
| 177 base::PropertyAccessor<AutocompleteEditState>* GetStateAccessor() { | |
| 178 CR_DEFINE_STATIC_LOCAL( | |
| 179 base::PropertyAccessor<AutocompleteEditState>, state, ()); | |
| 180 return &state; | |
| 181 } | |
| 182 | |
| 183 // A convenience method for applying URL styles. | 177 // A convenience method for applying URL styles. |
| 184 void ApplyURLStyle(views::Textfield* textfield, | 178 void ApplyURLStyle(views::Textfield* textfield, |
| 185 size_t start, | 179 size_t start, |
| 186 size_t end, | 180 size_t end, |
| 187 SkColor color, | 181 SkColor color, |
| 188 bool diagonal_strike) { | 182 bool diagonal_strike) { |
| 189 gfx::StyleRange style; | 183 gfx::StyleRange style; |
| 190 style.foreground = color; | 184 style.foreground = color; |
| 191 style.range = ui::Range(start, end); | 185 style.range = ui::Range(start, end); |
| 192 style.diagonal_strike = diagonal_strike; | 186 style.diagonal_strike = diagonal_strike; |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 // also important. | 468 // also important. |
| 475 if (textfield_->IsIMEComposing()) { | 469 if (textfield_->IsIMEComposing()) { |
| 476 textfield_->GetTextInputClient()->ConfirmCompositionText(); | 470 textfield_->GetTextInputClient()->ConfirmCompositionText(); |
| 477 textfield_->GetInputMethod()->CancelComposition(textfield_); | 471 textfield_->GetInputMethod()->CancelComposition(textfield_); |
| 478 } | 472 } |
| 479 | 473 |
| 480 // NOTE: GetStateForTabSwitch may affect GetSelection, so order is important. | 474 // NOTE: GetStateForTabSwitch may affect GetSelection, so order is important. |
| 481 OmniboxEditModel::State model_state = model()->GetStateForTabSwitch(); | 475 OmniboxEditModel::State model_state = model()->GetStateForTabSwitch(); |
| 482 gfx::SelectionModel selection; | 476 gfx::SelectionModel selection; |
| 483 textfield_->GetSelectionModel(&selection); | 477 textfield_->GetSelectionModel(&selection); |
| 484 GetStateAccessor()->SetProperty( | 478 tab->SetUserData( |
| 485 tab->GetPropertyBag(), | 479 kAutocompleteEditStateKey, |
| 486 AutocompleteEditState(model_state, ViewState(selection))); | 480 new AutocompleteEditState(model_state, ViewState(selection))); |
| 487 } | 481 } |
| 488 | 482 |
| 489 void OmniboxViewViews::Update(const WebContents* contents) { | 483 void OmniboxViewViews::Update(const WebContents* contents) { |
| 490 // NOTE: We're getting the URL text here from the ToolbarModel. | 484 // NOTE: We're getting the URL text here from the ToolbarModel. |
| 491 bool visibly_changed_permanent_text = | 485 bool visibly_changed_permanent_text = |
| 492 model()->UpdatePermanentText(toolbar_model()->GetText()); | 486 model()->UpdatePermanentText(toolbar_model()->GetText()); |
| 493 ToolbarModel::SecurityLevel security_level = | 487 ToolbarModel::SecurityLevel security_level = |
| 494 toolbar_model()->GetSecurityLevel(); | 488 toolbar_model()->GetSecurityLevel(); |
| 495 bool changed_security_level = (security_level != security_level_); | 489 bool changed_security_level = (security_level != security_level_); |
| 496 security_level_ = security_level; | 490 security_level_ = security_level; |
| 497 | 491 |
| 498 // TODO(oshima): Copied from gtk implementation which is | 492 // TODO(oshima): Copied from gtk implementation which is |
| 499 // slightly different from WIN impl. Find out the correct implementation | 493 // slightly different from WIN impl. Find out the correct implementation |
| 500 // for views-implementation. | 494 // for views-implementation. |
| 501 if (contents) { | 495 if (contents) { |
| 502 RevertAll(); | 496 RevertAll(); |
| 503 const AutocompleteEditState* state = | 497 const AutocompleteEditState* state = static_cast<AutocompleteEditState*>( |
| 504 GetStateAccessor()->GetProperty(contents->GetPropertyBag()); | 498 contents->GetUserData(&kAutocompleteEditStateKey)); |
| 505 if (state) { | 499 if (state) { |
| 506 model()->RestoreState(state->model_state); | 500 model()->RestoreState(state->model_state); |
| 507 | 501 |
| 508 // Move the marks for the cursor and the other end of the selection to | 502 // Move the marks for the cursor and the other end of the selection to |
| 509 // the previously-saved offsets (but preserve PRIMARY). | 503 // the previously-saved offsets (but preserve PRIMARY). |
| 510 textfield_->SelectSelectionModel(state->view_state.selection_model); | 504 textfield_->SelectSelectionModel(state->view_state.selection_model); |
| 511 // We do not carry over the current edit history to another tab. | 505 // We do not carry over the current edit history to another tab. |
| 512 // TODO(oshima): consider saving/restoring edit history. | 506 // TODO(oshima): consider saving/restoring edit history. |
| 513 textfield_->ClearEditHistory(); | 507 textfield_->ClearEditHistory(); |
| 514 } | 508 } |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 899 const ui::Range& range) { | 893 const ui::Range& range) { |
| 900 if (text != GetText()) | 894 if (text != GetText()) |
| 901 textfield_->SetText(text); | 895 textfield_->SetText(text); |
| 902 textfield_->SelectRange(range); | 896 textfield_->SelectRange(range); |
| 903 } | 897 } |
| 904 | 898 |
| 905 string16 OmniboxViewViews::GetSelectedText() const { | 899 string16 OmniboxViewViews::GetSelectedText() const { |
| 906 // TODO(oshima): Support instant, IME. | 900 // TODO(oshima): Support instant, IME. |
| 907 return textfield_->GetSelectedText(); | 901 return textfield_->GetSelectedText(); |
| 908 } | 902 } |
| OLD | NEW |