OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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. |
| 4 |
| 5 #include "chrome/browser/instant/instant_overlay.h" |
| 6 |
| 7 #include "base/auto_reset.h" |
| 8 #include "base/supports_user_data.h" |
| 9 #include "content/public/browser/web_contents.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 int kUserDataKey; |
| 14 |
| 15 class InstantOverlayUserData : public base::SupportsUserData::Data { |
| 16 public: |
| 17 explicit InstantOverlayUserData(InstantOverlay* overlay) |
| 18 : overlay_(overlay) {} |
| 19 |
| 20 virtual InstantOverlay* overlay() const { return overlay_; } |
| 21 |
| 22 private: |
| 23 ~InstantOverlayUserData() {} |
| 24 |
| 25 InstantOverlay* const overlay_; |
| 26 |
| 27 DISALLOW_COPY_AND_ASSIGN(InstantOverlayUserData); |
| 28 }; |
| 29 |
| 30 } // namespace |
| 31 |
| 32 // static |
| 33 InstantOverlay* InstantOverlay::FromWebContents( |
| 34 const content::WebContents* web_contents) { |
| 35 InstantOverlayUserData* data = static_cast<InstantOverlayUserData*>( |
| 36 web_contents->GetUserData(&kUserDataKey)); |
| 37 return data ? data->overlay() : NULL; |
| 38 } |
| 39 |
| 40 InstantOverlay::InstantOverlay(InstantController* controller, |
| 41 const std::string& instant_url) |
| 42 : InstantPage(controller), |
| 43 loader_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 44 instant_url_(instant_url), |
| 45 is_stale_(false), |
| 46 is_pointer_down_from_activate_(false) { |
| 47 } |
| 48 |
| 49 InstantOverlay::~InstantOverlay() { |
| 50 } |
| 51 |
| 52 void InstantOverlay::InitContents(Profile* profile, |
| 53 const content::WebContents* active_tab) { |
| 54 loader_.Init(GURL(instant_url_), profile, active_tab, |
| 55 base::Bind(&InstantOverlay::HandleStalePage, |
| 56 base::Unretained(this))); |
| 57 SetContents(loader_.contents()); |
| 58 contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this)); |
| 59 loader_.Load(); |
| 60 } |
| 61 |
| 62 scoped_ptr<content::WebContents> InstantOverlay::ReleaseContents() { |
| 63 contents()->RemoveUserData(&kUserDataKey); |
| 64 SetContents(NULL); |
| 65 return loader_.ReleaseContents(); |
| 66 } |
| 67 |
| 68 void InstantOverlay::DidNavigate( |
| 69 const history::HistoryAddPageArgs& add_page_args) { |
| 70 last_navigation_ = add_page_args; |
| 71 } |
| 72 |
| 73 bool InstantOverlay::IsUsingLocalPreview() const { |
| 74 return instant_url_ == InstantController::kLocalOmniboxPopupURL; |
| 75 } |
| 76 |
| 77 void InstantOverlay::Update(const string16& text, |
| 78 size_t selection_start, |
| 79 size_t selection_end, |
| 80 bool verbatim) { |
| 81 last_navigation_ = history::HistoryAddPageArgs(); |
| 82 InstantPage::Update(text, selection_start, selection_end, verbatim); |
| 83 } |
| 84 |
| 85 bool InstantOverlay::ShouldProcessRenderViewCreated() { |
| 86 return true; |
| 87 } |
| 88 |
| 89 bool InstantOverlay::ShouldProcessRenderViewGone() { |
| 90 return true; |
| 91 } |
| 92 |
| 93 bool InstantOverlay::ShouldProcessAboutToNavigateMainFrame() { |
| 94 return true; |
| 95 } |
| 96 |
| 97 bool InstantOverlay::ShouldProcessSetSuggestions() { |
| 98 return true; |
| 99 } |
| 100 |
| 101 bool InstantOverlay::ShouldProcessShowInstantPreview() { |
| 102 return true; |
| 103 } |
| 104 |
| 105 bool InstantOverlay::ShouldProcessNavigateToURL() { |
| 106 return true; |
| 107 } |
| 108 |
| 109 void InstantOverlay::OnSwappedContents() { |
| 110 contents()->RemoveUserData(&kUserDataKey); |
| 111 SetContents(loader_.contents()); |
| 112 contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this)); |
| 113 instant_controller()->SwappedOverlayContents(); |
| 114 } |
| 115 |
| 116 void InstantOverlay::OnFocus() { |
| 117 // The preview is getting focus. Equivalent to it being clicked. |
| 118 base::AutoReset<bool> reset(&is_pointer_down_from_activate_, true); |
| 119 instant_controller()->FocusedOverlayContents(); |
| 120 } |
| 121 |
| 122 void InstantOverlay::OnMouseDown() { |
| 123 is_pointer_down_from_activate_ = true; |
| 124 } |
| 125 |
| 126 void InstantOverlay::OnMouseUp() { |
| 127 if (is_pointer_down_from_activate_) { |
| 128 is_pointer_down_from_activate_ = false; |
| 129 instant_controller()->CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); |
| 130 } |
| 131 } |
| 132 |
| 133 content::WebContents* InstantOverlay::OpenURLFromTab( |
| 134 content::WebContents* source, |
| 135 const content::OpenURLParams& params) { |
| 136 // We will allow the navigate to continue if we are able to commit the |
| 137 // overlay. |
| 138 // |
| 139 // First, cache the overlay contents since committing it will cause the |
| 140 // contents to be released (and be set to NULL). |
| 141 content::WebContents* overlay = contents(); |
| 142 if (instant_controller()->CommitIfPossible(INSTANT_COMMIT_NAVIGATED)) { |
| 143 // If the commit was successful, the overlay's delegate should be the tab |
| 144 // strip, which will be able to handle the navigation. |
| 145 DCHECK_NE(&loader_, overlay->GetDelegate()); |
| 146 return overlay->GetDelegate()->OpenURLFromTab(source, params); |
| 147 } |
| 148 return NULL; |
| 149 } |
| 150 |
| 151 void InstantOverlay::HandleStalePage() { |
| 152 is_stale_ = true; |
| 153 instant_controller()->ReloadOverlayIfStale(); |
| 154 } |
OLD | NEW |