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/supports_user_data.h" | |
8 #include "chrome/browser/instant/instant_controller.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 }; | |
sreeram
2013/02/01 20:36:53
How about we get rid of this class, and just make
samarth
2013/02/04 20:43:32
As we discussed, this doesn't work because SetUser
| |
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_.Load(GURL(instant_url_), profile, active_tab, | |
55 base::Bind(&InstantOverlay::HandleStalePage, | |
56 base::Unretained(this))); | |
sreeram
2013/02/01 20:36:53
You need to split this into two methods:
loader_
samarth
2013/02/04 20:43:32
Done.
| |
57 SetContents(loader_.contents()); | |
58 contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this)); | |
59 } | |
60 | |
61 scoped_ptr<content::WebContents> InstantOverlay::ReleaseContents() { | |
62 contents()->RemoveUserData(&kUserDataKey); | |
63 SetContents(NULL); | |
64 return loader_.ReleaseContents(); | |
65 } | |
66 | |
67 void InstantOverlay::DidNavigate( | |
68 const history::HistoryAddPageArgs& add_page_args) { | |
69 last_navigation_ = add_page_args; | |
70 } | |
71 | |
72 bool InstantOverlay::IsUsingLocalPreview() const { | |
73 return instant_url_ == InstantController::kLocalOmniboxPopupURL; | |
74 } | |
75 | |
76 void InstantOverlay::Update(const string16& text, | |
77 size_t selection_start, | |
78 size_t selection_end, | |
79 bool verbatim) { | |
80 last_navigation_ = history::HistoryAddPageArgs(); | |
81 InstantPage::Update(text, selection_start, selection_end, verbatim); | |
82 } | |
83 | |
84 void InstantOverlay::OnSwappedContents() { | |
85 contents()->RemoveUserData(&kUserDataKey); | |
86 SetContents(loader_.contents()); | |
87 contents()->SetUserData(&kUserDataKey, new InstantOverlayUserData(this)); | |
88 instant_controller()->SwappedOverlayContents(); | |
89 } | |
90 | |
91 void InstantOverlay::OnFocus() { | |
92 // The preview is getting focus. Equivalent to it being clicked. | |
93 bool tmp = is_pointer_down_from_activate_; | |
sreeram
2013/02/01 20:36:53
Use base::AutoReset<> here.
samarth
2013/02/04 20:43:32
Done.
| |
94 is_pointer_down_from_activate_ = true; | |
95 instant_controller()->FocusedOverlayContents(); | |
96 is_pointer_down_from_activate_ = tmp; | |
97 } | |
98 | |
99 void InstantOverlay::OnMouseDown() { | |
100 is_pointer_down_from_activate_ = true; | |
101 } | |
102 | |
103 void InstantOverlay::OnMouseUp() { | |
104 if (is_pointer_down_from_activate_) { | |
105 is_pointer_down_from_activate_ = false; | |
106 instant_controller()->CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); | |
107 } | |
108 } | |
109 | |
110 content::WebContents* InstantOverlay::OpenURLFromTab( | |
111 content::WebContents* source, | |
112 const content::OpenURLParams& params) { | |
113 content::WebContents* overlay = contents(); | |
sreeram
2013/02/01 20:36:53
Add a comment on top of this line to say why we ha
samarth
2013/02/04 20:43:32
Done.
| |
114 if (instant_controller()->CommitIfPossible(INSTANT_COMMIT_NAVIGATED)) { | |
115 // Allow navigation to continue if we are able to commit the overlay. At | |
116 // this point, the overlay's delegate should be the tab strip, which will | |
117 // be able to handle the navigation. | |
sreeram
2013/02/01 20:36:53
To be safe:
DCHECK_NE(this, overlay->GetDelegate()
samarth
2013/02/04 20:43:32
Done. (Actually compared it to loader_ which is t
| |
118 return overlay->GetDelegate()->OpenURLFromTab(source, params); | |
119 } | |
120 return NULL; | |
121 } | |
122 | |
123 void InstantOverlay::HandleStalePage() { | |
124 is_stale_ = true; | |
125 instant_controller()->ReloadOverlayIfStale(); | |
126 } | |
OLD | NEW |