OLD | NEW |
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 "chrome/browser/instant/instant_loader.h" | 5 #include "chrome/browser/instant/instant_loader.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 // Header and value set for all loads. | 51 // Header and value set for all loads. |
52 const char kPreviewHeader[] = "X-Purpose:"; | 52 const char kPreviewHeader[] = "X-Purpose:"; |
53 const char kPreviewHeaderValue[] = "preview"; | 53 const char kPreviewHeaderValue[] = "preview"; |
54 | 54 |
55 } // namespace | 55 } // namespace |
56 | 56 |
57 // FrameLoadObserver is responsible for determining if the page supports | 57 // FrameLoadObserver is responsible for determining if the page supports |
58 // instant after it has loaded. | 58 // instant after it has loaded. |
59 class InstantLoader::FrameLoadObserver : public NotificationObserver { | 59 class InstantLoader::FrameLoadObserver : public NotificationObserver { |
60 public: | 60 public: |
61 FrameLoadObserver(TabContents* tab_contents, | 61 FrameLoadObserver(InstantLoader* loader, |
| 62 TabContents* tab_contents, |
62 const string16& text, | 63 const string16& text, |
63 bool verbatim) | 64 bool verbatim) |
64 : tab_contents_(tab_contents), | 65 : loader_(loader), |
| 66 tab_contents_(tab_contents), |
65 text_(text), | 67 text_(text), |
66 verbatim_(verbatim), | 68 verbatim_(verbatim), |
67 unique_id_(tab_contents_->controller().pending_entry()->unique_id()) { | 69 unique_id_(tab_contents_->controller().pending_entry()->unique_id()) { |
68 registrar_.Add(this, NotificationType::LOAD_COMPLETED_MAIN_FRAME, | 70 registrar_.Add(this, NotificationType::LOAD_COMPLETED_MAIN_FRAME, |
69 Source<TabContents>(tab_contents_)); | 71 Source<TabContents>(tab_contents_)); |
70 } | 72 } |
71 | 73 |
72 // Sets the text to send to the page. | 74 // Sets the text to send to the page. |
73 void set_text(const string16& text) { text_ = text; } | 75 void set_text(const string16& text) { text_ = text; } |
74 | 76 |
75 // Sets whether verbatim results are obtained rather than predictive. | 77 // Sets whether verbatim results are obtained rather than predictive. |
76 void set_verbatim(bool verbatim) { verbatim_ = verbatim; } | 78 void set_verbatim(bool verbatim) { verbatim_ = verbatim; } |
77 | 79 |
78 // NotificationObserver: | 80 // NotificationObserver: |
79 virtual void Observe(NotificationType type, | 81 virtual void Observe(NotificationType type, |
80 const NotificationSource& source, | 82 const NotificationSource& source, |
81 const NotificationDetails& details) { | 83 const NotificationDetails& details) { |
82 switch (type.value) { | 84 switch (type.value) { |
83 case NotificationType::LOAD_COMPLETED_MAIN_FRAME: { | 85 case NotificationType::LOAD_COMPLETED_MAIN_FRAME: { |
84 int page_id = *(Details<int>(details).ptr()); | 86 int page_id = *(Details<int>(details).ptr()); |
85 NavigationEntry* active_entry = | 87 NavigationEntry* active_entry = |
86 tab_contents_->controller().GetActiveEntry(); | 88 tab_contents_->controller().GetActiveEntry(); |
87 if (!active_entry || active_entry->page_id() != page_id || | 89 if (!active_entry || active_entry->page_id() != page_id || |
88 active_entry->unique_id() != unique_id_) { | 90 active_entry->unique_id() != unique_id_) { |
89 return; | 91 return; |
90 } | 92 } |
| 93 loader_->SendBoundsToPage(true); |
91 tab_contents_->render_view_host()->DetermineIfPageSupportsInstant( | 94 tab_contents_->render_view_host()->DetermineIfPageSupportsInstant( |
92 text_, verbatim_); | 95 text_, verbatim_); |
93 break; | 96 break; |
94 } | 97 } |
95 default: | 98 default: |
96 NOTREACHED(); | 99 NOTREACHED(); |
97 break; | 100 break; |
98 } | 101 } |
99 } | 102 } |
100 | 103 |
101 private: | 104 private: |
| 105 InstantLoader* loader_; |
| 106 |
102 // The TabContents we're listening for changes on. | 107 // The TabContents we're listening for changes on. |
103 TabContents* tab_contents_; | 108 TabContents* tab_contents_; |
104 | 109 |
105 // Text to send down to the page. | 110 // Text to send down to the page. |
106 string16 text_; | 111 string16 text_; |
107 | 112 |
108 // Whether verbatim results are obtained. | 113 // Whether verbatim results are obtained. |
109 bool verbatim_; | 114 bool verbatim_; |
110 | 115 |
111 // unique_id of the NavigationEntry we're waiting on. | 116 // unique_id of the NavigationEntry we're waiting on. |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 else | 384 else |
380 loader_->SetCompleteSuggestedText(UTF8ToUTF16(suggestions[0])); | 385 loader_->SetCompleteSuggestedText(UTF8ToUTF16(suggestions[0])); |
381 } | 386 } |
382 | 387 |
383 virtual void OnInstantSupportDetermined(int32 page_id, bool result) { | 388 virtual void OnInstantSupportDetermined(int32 page_id, bool result) { |
384 TabContents* source = loader_->preview_contents()->tab_contents(); | 389 TabContents* source = loader_->preview_contents()->tab_contents(); |
385 if (!source->controller().GetActiveEntry() || | 390 if (!source->controller().GetActiveEntry() || |
386 page_id != source->controller().GetActiveEntry()->page_id()) | 391 page_id != source->controller().GetActiveEntry()->page_id()) |
387 return; | 392 return; |
388 | 393 |
389 if (result) { | 394 if (result) |
390 gfx::Rect bounds = loader_->GetOmniboxBoundsInTermsOfPreview(); | |
391 loader_->last_omnibox_bounds_ = loader_->omnibox_bounds_; | |
392 loader_->PageFinishedLoading(); | 395 loader_->PageFinishedLoading(); |
393 } else { | 396 else |
394 loader_->PageDoesntSupportInstant(user_typed_before_load_); | 397 loader_->PageDoesntSupportInstant(user_typed_before_load_); |
395 } | |
396 } | 398 } |
397 | 399 |
398 private: | 400 private: |
399 typedef std::vector<scoped_refptr<history::HistoryAddPageArgs> > | 401 typedef std::vector<scoped_refptr<history::HistoryAddPageArgs> > |
400 AddPageVector; | 402 AddPageVector; |
401 | 403 |
402 void CommitFromMouseReleaseIfNecessary() { | 404 void CommitFromMouseReleaseIfNecessary() { |
403 bool was_down = is_mouse_down_from_activate_; | 405 bool was_down = is_mouse_down_from_activate_; |
404 is_mouse_down_from_activate_ = false; | 406 is_mouse_down_from_activate_ = false; |
405 if (was_down && loader_->ShouldCommitInstantOnMouseUp()) | 407 if (was_down && loader_->ShouldCommitInstantOnMouseUp()) |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 // TODO(sky): having to use a replaceable url is a bit of a hack here. | 555 // TODO(sky): having to use a replaceable url is a bit of a hack here. |
554 GURL instant_url( | 556 GURL instant_url( |
555 template_url->instant_url()->ReplaceSearchTerms( | 557 template_url->instant_url()->ReplaceSearchTerms( |
556 *template_url, std::wstring(), -1, std::wstring())); | 558 *template_url, std::wstring(), -1, std::wstring())); |
557 CommandLine* cl = CommandLine::ForCurrentProcess(); | 559 CommandLine* cl = CommandLine::ForCurrentProcess(); |
558 if (cl->HasSwitch(switches::kInstantURL)) | 560 if (cl->HasSwitch(switches::kInstantURL)) |
559 instant_url = GURL(cl->GetSwitchValueASCII(switches::kInstantURL)); | 561 instant_url = GURL(cl->GetSwitchValueASCII(switches::kInstantURL)); |
560 preview_contents_->controller().LoadURL( | 562 preview_contents_->controller().LoadURL( |
561 instant_url, GURL(), transition_type); | 563 instant_url, GURL(), transition_type); |
562 frame_load_observer_.reset( | 564 frame_load_observer_.reset( |
563 new FrameLoadObserver(preview_contents()->tab_contents(), | 565 new FrameLoadObserver(this, |
564 user_text_, verbatim)); | 566 preview_contents()->tab_contents(), |
| 567 user_text_, |
| 568 verbatim)); |
565 } | 569 } |
566 } else { | 570 } else { |
567 DCHECK(template_url_id_ == 0); | 571 DCHECK(template_url_id_ == 0); |
568 frame_load_observer_.reset(NULL); | 572 frame_load_observer_.reset(NULL); |
569 preview_contents_->controller().LoadURL(url_, GURL(), transition_type); | 573 preview_contents_->controller().LoadURL(url_, GURL(), transition_type); |
570 } | 574 } |
571 } | 575 } |
572 | 576 |
573 void InstantLoader::SetOmniboxBounds(const gfx::Rect& bounds) { | 577 void InstantLoader::SetOmniboxBounds(const gfx::Rect& bounds) { |
574 if (omnibox_bounds_ == bounds) | 578 if (omnibox_bounds_ == bounds) |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 delegate_->AddToBlacklist(this, load_details->entry->url()); | 714 delegate_->AddToBlacklist(this, load_details->entry->url()); |
711 } | 715 } |
712 return; | 716 return; |
713 } | 717 } |
714 | 718 |
715 NOTREACHED() << "Got a notification we didn't register for."; | 719 NOTREACHED() << "Got a notification we didn't register for."; |
716 } | 720 } |
717 | 721 |
718 void InstantLoader::PageFinishedLoading() { | 722 void InstantLoader::PageFinishedLoading() { |
719 frame_load_observer_.reset(); | 723 frame_load_observer_.reset(); |
| 724 |
| 725 // Send the bounds of the omnibox down now. |
| 726 SendBoundsToPage(false); |
| 727 |
720 // Wait for the user input before showing, this way the page should be up to | 728 // Wait for the user input before showing, this way the page should be up to |
721 // date by the time we show it. | 729 // date by the time we show it. |
722 } | 730 } |
723 | 731 |
724 // TODO(tonyg): This method only fires when the omnibox bounds change. It also | 732 // TODO(tonyg): This method only fires when the omnibox bounds change. It also |
725 // needs to fire when the preview bounds change (e.g. open/close info bar). | 733 // needs to fire when the preview bounds change (e.g. open/close info bar). |
726 gfx::Rect InstantLoader::GetOmniboxBoundsInTermsOfPreview() { | 734 gfx::Rect InstantLoader::GetOmniboxBoundsInTermsOfPreview() { |
727 gfx::Rect preview_bounds(delegate_->GetInstantBounds()); | 735 gfx::Rect preview_bounds(delegate_->GetInstantBounds()); |
728 gfx::Rect intersection(omnibox_bounds_.Intersect(preview_bounds)); | 736 gfx::Rect intersection(omnibox_bounds_.Intersect(preview_bounds)); |
729 | 737 |
(...skipping 13 matching lines...) Expand all Loading... |
743 return intersection; | 751 return intersection; |
744 } | 752 } |
745 | 753 |
746 void InstantLoader::PageDoesntSupportInstant(bool needs_reload) { | 754 void InstantLoader::PageDoesntSupportInstant(bool needs_reload) { |
747 frame_load_observer_.reset(NULL); | 755 frame_load_observer_.reset(NULL); |
748 | 756 |
749 delegate_->InstantLoaderDoesntSupportInstant(this); | 757 delegate_->InstantLoaderDoesntSupportInstant(this); |
750 } | 758 } |
751 | 759 |
752 void InstantLoader::ProcessBoundsChange() { | 760 void InstantLoader::ProcessBoundsChange() { |
| 761 SendBoundsToPage(false); |
| 762 } |
| 763 |
| 764 void InstantLoader::SendBoundsToPage(bool force_if_waiting) { |
753 if (last_omnibox_bounds_ == omnibox_bounds_) | 765 if (last_omnibox_bounds_ == omnibox_bounds_) |
754 return; | 766 return; |
755 | 767 |
756 last_omnibox_bounds_ = omnibox_bounds_; | |
757 if (preview_contents_.get() && is_showing_instant() && | 768 if (preview_contents_.get() && is_showing_instant() && |
758 !is_waiting_for_load()) { | 769 (force_if_waiting || !is_waiting_for_load())) { |
| 770 last_omnibox_bounds_ = omnibox_bounds_; |
759 preview_contents_->render_view_host()->SearchBoxResize( | 771 preview_contents_->render_view_host()->SearchBoxResize( |
760 GetOmniboxBoundsInTermsOfPreview()); | 772 GetOmniboxBoundsInTermsOfPreview()); |
761 } | 773 } |
762 } | 774 } |
763 | 775 |
764 void InstantLoader::CreatePreviewContents(TabContentsWrapper* tab_contents) { | 776 void InstantLoader::CreatePreviewContents(TabContentsWrapper* tab_contents) { |
765 TabContents* new_contents = | 777 TabContents* new_contents = |
766 new TabContents( | 778 new TabContents( |
767 tab_contents->profile(), NULL, MSG_ROUTING_NONE, NULL, NULL); | 779 tab_contents->profile(), NULL, MSG_ROUTING_NONE, NULL, NULL); |
768 preview_contents_.reset(new TabContentsWrapper(new_contents)); | 780 preview_contents_.reset(new TabContentsWrapper(new_contents)); |
(...skipping 25 matching lines...) Expand all Loading... |
794 Source<NavigationController>(&preview_contents_->controller())); | 806 Source<NavigationController>(&preview_contents_->controller())); |
795 #endif | 807 #endif |
796 | 808 |
797 registrar_.Add( | 809 registrar_.Add( |
798 this, | 810 this, |
799 NotificationType::NAV_ENTRY_COMMITTED, | 811 NotificationType::NAV_ENTRY_COMMITTED, |
800 Source<NavigationController>(&preview_contents_->controller())); | 812 Source<NavigationController>(&preview_contents_->controller())); |
801 | 813 |
802 preview_contents_->tab_contents()->ShowContents(); | 814 preview_contents_->tab_contents()->ShowContents(); |
803 } | 815 } |
OLD | NEW |