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 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 void InstantOverlay::OnSwappedContents() { |
| 86 contents()->RemoveUserData(&kUserDataKey); |
| 87 SetContents(loader_.contents()); |
| 88 contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this)); |
| 89 instant_controller()->SwappedOverlayContents(); |
| 90 } |
| 91 |
| 92 void InstantOverlay::OnFocus() { |
| 93 // The preview is getting focus. Equivalent to it being clicked. |
| 94 base::AutoReset<bool> reset(&is_pointer_down_from_activate_, true); |
| 95 instant_controller()->FocusedOverlayContents(); |
| 96 } |
| 97 |
| 98 void InstantOverlay::OnMouseDown() { |
| 99 is_pointer_down_from_activate_ = true; |
| 100 } |
| 101 |
| 102 void InstantOverlay::OnMouseUp() { |
| 103 if (is_pointer_down_from_activate_) { |
| 104 is_pointer_down_from_activate_ = false; |
| 105 instant_controller()->CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); |
| 106 } |
| 107 } |
| 108 |
| 109 content::WebContents* InstantOverlay::OpenURLFromTab( |
| 110 content::WebContents* source, |
| 111 const content::OpenURLParams& params) { |
| 112 // We will allow the navigate to continue if we are able to commit the |
| 113 // overlay. |
| 114 // |
| 115 // First, cache the overlay contents since committing it will cause the |
| 116 // contents to be released (and be set to NULL). |
| 117 content::WebContents* overlay = contents(); |
| 118 if (instant_controller()->CommitIfPossible(INSTANT_COMMIT_NAVIGATED)) { |
| 119 // If the commit was successful, the overlay's delegate should be the tab |
| 120 // strip, which will be able to handle the navigation. |
| 121 DCHECK_NE(&loader_, overlay->GetDelegate()); |
| 122 return overlay->GetDelegate()->OpenURLFromTab(source, params); |
| 123 } |
| 124 return NULL; |
| 125 } |
| 126 |
| 127 void InstantOverlay::HandleStalePage() { |
| 128 is_stale_ = true; |
| 129 instant_controller()->ReloadOverlayIfStale(); |
| 130 } |
OLD | NEW |