summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorsky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-24 17:00:13 +0000
committersky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-24 17:00:13 +0000
commit3aac77aeb279763357f07b9f0f15e6ad9c8fceb0 (patch)
tree69d82f95a53cb1a7d7242dc360184edabcd16eb1 /chrome
parent8f5359cde5640fc63cd84605fd9197d97af0ed18 (diff)
downloadchromium_src-3aac77aeb279763357f07b9f0f15e6ad9c8fceb0.zip
chromium_src-3aac77aeb279763357f07b9f0f15e6ad9c8fceb0.tar.gz
chromium_src-3aac77aeb279763357f07b9f0f15e6ad9c8fceb0.tar.bz2
Adds support for sending script when the user accepts instant
results. Also tweaks the delegate interface to be a bit more sensical. BUG=54833 TEST=see bug Review URL: http://codereview.chromium.org/3440025 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60479 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/browser.cc9
-rw-r--r--chrome/browser/browser.h2
-rw-r--r--chrome/browser/tab_contents/match_preview.cc103
-rw-r--r--chrome/browser/tab_contents/match_preview.h16
-rw-r--r--chrome/browser/tab_contents/match_preview_delegate.h7
-rw-r--r--chrome/browser/views/location_bar/location_bar_view.cc2
6 files changed, 98 insertions, 41 deletions
diff --git a/chrome/browser/browser.cc b/chrome/browser/browser.cc
index dea3cb9..a326149 100644
--- a/chrome/browser/browser.cc
+++ b/chrome/browser/browser.cc
@@ -1254,7 +1254,7 @@ void Browser::OpenCurrentURL() {
// TODO(sky): support other dispositions.
if (open_disposition == CURRENT_TAB && match_preview() &&
match_preview()->is_active()) {
- match_preview()->CommitCurrentPreview();
+ match_preview()->CommitCurrentPreview(MatchPreview::COMMIT_PRESSED_ENTER);
return;
}
@@ -3222,17 +3222,16 @@ void Browser::HideMatchPreview() {
window_->HideMatchPreview();
}
-void Browser::CommitMatchPreview() {
+void Browser::CommitMatchPreview(TabContents* preview_contents) {
TabContents* tab_contents = match_preview_->tab_contents();
int index = tabstrip_model_->GetIndexOfTabContents(tab_contents);
DCHECK_NE(-1, index);
- scoped_ptr<TabContents> preview_contents(
- match_preview()->ReleasePreviewContents(true));
preview_contents->controller().CopyStateFromAndPrune(
tab_contents->controller());
// TabStripModel takes ownership of preview_contents.
tabstrip_model_->ReplaceTabContentsAt(
- index, preview_contents.release(),
+ index,
+ preview_contents,
TabStripModelObserver::REPLACE_MATCH_PREVIEW);
}
diff --git a/chrome/browser/browser.h b/chrome/browser/browser.h
index 9b4bdc3..ecfef28 100644
--- a/chrome/browser/browser.h
+++ b/chrome/browser/browser.h
@@ -771,7 +771,7 @@ class Browser : public TabStripModelDelegate,
// Overriden from MatchPreviewDelegate:
virtual void ShowMatchPreview();
virtual void HideMatchPreview();
- virtual void CommitMatchPreview();
+ virtual void CommitMatchPreview(TabContents* preview_contents);
virtual void SetSuggestedText(const string16& text);
virtual gfx::Rect GetMatchPreviewBounds();
diff --git a/chrome/browser/tab_contents/match_preview.cc b/chrome/browser/tab_contents/match_preview.cc
index 1ddd4e9..deb9a9c 100644
--- a/chrome/browser/tab_contents/match_preview.cc
+++ b/chrome/browser/tab_contents/match_preview.cc
@@ -36,26 +36,60 @@
namespace {
+// Script sent as the user is typing and the provider supports instant.
+// Params:
+// . the text the user typed.
+// TODO: add support for the 2nd and 3rd params.
const char kUserInputScript[] =
- "if (window.chrome.userInput) window.chrome.userInput(\"$1\");";
+ "if (window.chrome.userInput) window.chrome.userInput(\"$1\", 0, 0);";
+
+// Script sent when the page is committed and the provider supports instant.
+// Params:
+// . the text the user typed.
+// . boolean indicating if the user pressed enter to accept the text.
const char kUserDoneScript[] =
- "if (window.chrome.userWantsQuery) window.chrome.userWantsQuery(\"$1\");";
+ "if (window.chrome.userWantsQuery) "
+ "window.chrome.userWantsQuery(\"$1\", $2);";
+
+// Script sent when the bounds of the omnibox changes and the provider supports
+// instant. The params are the bounds relative to the origin of the preview
+// (x, y, width, height).
const char kSetOmniboxBoundsScript[] =
"if (window.chrome.setOmniboxDimensions) "
"window.chrome.setOmniboxDimensions($1, $2, $3, $4);";
-// Sends the user input script to |tab_contents|. |text| is the text the user
-// input into the omnibox.
-void SendUserInputScript(TabContents* tab_contents,
- const string16& text,
- bool done) {
+// Escapes quotes in the |text| so that it be passed to JavaScript as a quoted
+// string.
+string16 EscapeUserText(const string16& text) {
string16 escaped_text(text);
ReplaceSubstringsAfterOffset(&escaped_text, 0L, ASCIIToUTF16("\""),
ASCIIToUTF16("\\\""));
- string16 script = ReplaceStringPlaceholders(
- ASCIIToUTF16(done ? kUserDoneScript : kUserInputScript),
- escaped_text,
- NULL);
+ return escaped_text;
+}
+
+// Sends the script for when the user commits the preview. |pressed_enter| is
+// true if the user pressed enter to commit.
+void SendDoneScript(TabContents* tab_contents,
+ const string16& text,
+ bool pressed_enter) {
+ std::vector<string16> params;
+ params.push_back(EscapeUserText(text));
+ params.push_back(pressed_enter ? ASCIIToUTF16("true") :
+ ASCIIToUTF16("false"));
+ string16 script = ReplaceStringPlaceholders(ASCIIToUTF16(kUserDoneScript),
+ params,
+ NULL);
+ tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
+ std::wstring(),
+ UTF16ToWide(script));
+}
+
+// Sends the user input script to |tab_contents|. |text| is the text the user
+// input into the omnibox.
+void SendUserInputScript(TabContents* tab_contents, const string16& text) {
+ string16 script = ReplaceStringPlaceholders(ASCIIToUTF16(kUserInputScript),
+ EscapeUserText(text),
+ NULL);
tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(
std::wstring(),
UTF16ToWide(script));
@@ -89,7 +123,7 @@ class MatchPreview::FrameLoadObserver : public NotificationObserver {
tab_contents_(match_preview->preview_contents()),
unique_id_(tab_contents_->controller().pending_entry()->unique_id()),
text_(text),
- send_done_(false) {
+ pressed_enter_(false) {
registrar_.Add(this, NotificationType::LOAD_COMPLETED_MAIN_FRAME,
Source<TabContents>(tab_contents_));
registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED,
@@ -101,9 +135,9 @@ class MatchPreview::FrameLoadObserver : public NotificationObserver {
// Invoked when the MatchPreview releases ownership of the TabContents and
// the page hasn't finished loading.
- void DetachFromPreview() {
+ void DetachFromPreview(bool pressed_enter) {
match_preview_ = NULL;
- send_done_ = true;
+ pressed_enter_ = pressed_enter;
}
// NotificationObserver:
@@ -126,10 +160,12 @@ class MatchPreview::FrameLoadObserver : public NotificationObserver {
SendOmniboxBoundsScript(tab_contents_, bounds);
}
- SendUserInputScript(tab_contents_, text_, send_done_);
+ SendUserInputScript(tab_contents_, text_);
if (match_preview_)
match_preview_->PageFinishedLoading();
+ else
+ SendDoneScript(tab_contents_, text_, pressed_enter_);
delete this;
return;
@@ -158,8 +194,8 @@ class MatchPreview::FrameLoadObserver : public NotificationObserver {
// Text to send down to the page.
string16 text_;
- // Passed to SendScript.
- bool send_done_;
+ // Passed to SendDoneScript.
+ bool pressed_enter_;
// Registers and unregisters us for notifications.
NotificationRegistrar registrar_;
@@ -469,7 +505,7 @@ void MatchPreview::Update(TabContents* tab_contents,
frame_load_observer_->set_text(user_text_);
return;
}
- SendUserInputScript(preview_contents_.get(), user_text_, false);
+ SendUserInputScript(preview_contents_.get(), user_text_);
if (complete_suggested_text_.size() > user_text_.size() &&
!complete_suggested_text_.compare(0, user_text_.size(), user_text_)) {
*suggested_text = complete_suggested_text_.substr(user_text_.size());
@@ -511,32 +547,39 @@ void MatchPreview::DestroyPreviewContents() {
}
delegate_->HideMatchPreview();
- delete ReleasePreviewContents(false);
+ delete ReleasePreviewContents(COMMIT_DESTROY);
}
-void MatchPreview::CommitCurrentPreview() {
+void MatchPreview::CommitCurrentPreview(CommitType type) {
DCHECK(preview_contents_.get());
- delegate_->CommitMatchPreview();
+ delegate_->CommitMatchPreview(ReleasePreviewContents(type));
}
-TabContents* MatchPreview::ReleasePreviewContents(bool commit_history) {
- omnibox_bounds_ = gfx::Rect();
- template_url_id_ = 0;
- url_ = GURL();
- user_text_.clear();
- complete_suggested_text_.clear();
+TabContents* MatchPreview::ReleasePreviewContents(CommitType type) {
+ if (!preview_contents_.get())
+ return NULL;
+
if (frame_load_observer_.get()) {
- frame_load_observer_->DetachFromPreview();
+ frame_load_observer_->DetachFromPreview(type == COMMIT_PRESSED_ENTER);
// FrameLoadObserver will delete itself either when the TabContents is
// deleted, or when the page finishes loading.
FrameLoadObserver* unused ALLOW_UNUSED = frame_load_observer_.release();
+ } else if (type != COMMIT_DESTROY && is_showing_instant()) {
+ SendDoneScript(preview_contents_.get(),
+ user_text_,
+ type == COMMIT_PRESSED_ENTER);
}
+ omnibox_bounds_ = gfx::Rect();
+ template_url_id_ = 0;
+ url_ = GURL();
+ user_text_.clear();
+ complete_suggested_text_.clear();
if (preview_contents_.get()) {
- if (commit_history)
+ if (type != COMMIT_DESTROY)
preview_tab_contents_delegate_->CommitHistory();
// Destroy the paint observer.
+ // RenderWidgetHostView may be null during shutdown.
if (preview_contents_->GetRenderWidgetHostView()) {
- // RenderWidgetHostView may be null during shutdown.
preview_contents_->GetRenderWidgetHostView()->GetRenderWidgetHost()->
set_paint_observer(NULL);
}
diff --git a/chrome/browser/tab_contents/match_preview.h b/chrome/browser/tab_contents/match_preview.h
index 25bb8b7..d871abb 100644
--- a/chrome/browser/tab_contents/match_preview.h
+++ b/chrome/browser/tab_contents/match_preview.h
@@ -29,6 +29,18 @@ class TabContents;
// being invoked on the delegate.
class MatchPreview {
public:
+ enum CommitType {
+ // The commit is the result of the user pressing enter.
+ COMMIT_PRESSED_ENTER,
+
+ // The commit is the result of focus being lost. This typically corresponds
+ // to a mouse click event.
+ COMMIT_FOCUS_LOST,
+
+ // Used internally.
+ COMMIT_DESTROY
+ };
+
explicit MatchPreview(MatchPreviewDelegate* delegate);
~MatchPreview();
@@ -55,12 +67,12 @@ class MatchPreview {
// Invoked when the user does some gesture that should trigger making the
// current previewed page the permanent page.
- void CommitCurrentPreview();
+ void CommitCurrentPreview(CommitType type);
// Releases the preview TabContents passing ownership to the caller. This is
// intended to be called when the preview TabContents is committed. This does
// not notify the delegate.
- TabContents* ReleasePreviewContents(bool commit_history);
+ TabContents* ReleasePreviewContents(CommitType type);
// TabContents the match is being shown for.
TabContents* tab_contents() const { return tab_contents_; }
diff --git a/chrome/browser/tab_contents/match_preview_delegate.h b/chrome/browser/tab_contents/match_preview_delegate.h
index 5839198..8506cef 100644
--- a/chrome/browser/tab_contents/match_preview_delegate.h
+++ b/chrome/browser/tab_contents/match_preview_delegate.h
@@ -8,6 +8,8 @@
#include "base/string16.h"
+class TabContents;
+
namespace gfx {
class Rect;
}
@@ -23,8 +25,9 @@ class MatchPreviewDelegate {
virtual void HideMatchPreview() = 0;
// Invoked when the user does something that should result in the preview
- // TabContents becoming the active TabContents.
- virtual void CommitMatchPreview() = 0;
+ // TabContents becoming the active TabContents. The delegate takes ownership
+ // of the supplied TabContents.
+ virtual void CommitMatchPreview(TabContents* preview_contents) = 0;
// Invoked when the suggested text is to change to |text|.
virtual void SetSuggestedText(const string16& text) = 0;
diff --git a/chrome/browser/views/location_bar/location_bar_view.cc b/chrome/browser/views/location_bar/location_bar_view.cc
index d6730ca..a55281a 100644
--- a/chrome/browser/views/location_bar/location_bar_view.cc
+++ b/chrome/browser/views/location_bar/location_bar_view.cc
@@ -737,7 +737,7 @@ void LocationBarView::OnAutocompleteLosingFocus(
return;
if (ShouldCommitMatchPreviewOnFocusLoss(view_gaining_focus))
- match_preview->CommitCurrentPreview();
+ match_preview->CommitCurrentPreview(MatchPreview::COMMIT_FOCUS_LOST);
else
match_preview->DestroyPreviewContents();
}