OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/api/system_display/system_display_api.h" | 5 #include "extensions/browser/api/system_display/system_display_api.h" |
6 | 6 |
7 #include <map> | |
7 #include <memory> | 8 #include <memory> |
9 #include <set> | |
8 #include <string> | 10 #include <string> |
9 | 11 |
12 #include "base/macros.h" | |
13 #include "base/memory/ptr_util.h" | |
10 #include "build/build_config.h" | 14 #include "build/build_config.h" |
15 #include "content/public/browser/web_contents.h" | |
16 #include "content/public/browser/web_contents_observer.h" | |
11 #include "extensions/browser/api/system_display/display_info_provider.h" | 17 #include "extensions/browser/api/system_display/display_info_provider.h" |
12 #include "extensions/common/api/system_display.h" | 18 #include "extensions/common/api/system_display.h" |
13 | 19 |
14 #if defined(OS_CHROMEOS) | 20 #if defined(OS_CHROMEOS) |
15 #include "extensions/common/manifest_handlers/kiosk_mode_info.h" | 21 #include "extensions/common/manifest_handlers/kiosk_mode_info.h" |
16 #endif | 22 #endif |
17 | 23 |
18 namespace extensions { | 24 namespace extensions { |
19 | 25 |
20 namespace display = api::system_display; | 26 namespace display = api::system_display; |
21 | 27 |
22 const char SystemDisplayFunction::kCrosOnlyError[] = | 28 const char SystemDisplayFunction::kCrosOnlyError[] = |
23 "Function available only on ChromeOS."; | 29 "Function available only on ChromeOS."; |
24 const char SystemDisplayFunction::kKioskOnlyError[] = | 30 const char SystemDisplayFunction::kKioskOnlyError[] = |
25 "Only kiosk enabled extensions are allowed to use this function."; | 31 "Only kiosk enabled extensions are allowed to use this function."; |
26 | 32 |
33 namespace { | |
34 | |
35 class OverscanTracker; | |
36 | |
37 // Singleton class to track overscan calibration overlays. An observer is | |
38 // created per WebContents which tracks any calbiration overlays by id. | |
39 // If the render frame is deleted (e.g. the tab is closed) before the overlay | |
40 // calibraiton is completed, the observer will call the overscan complete | |
41 // method to remove the overlay. When all observers are removed, the singleton | |
42 // tracker will delete itself. | |
43 class OverscanTracker { | |
44 public: | |
45 static void AddDisplay(content::WebContents* web_contents, | |
46 const std::string& id); | |
47 static void RemoveDisplay(content::WebContents* web_contents, | |
48 const std::string& id); | |
49 static void RemoveObserver(content::WebContents* web_contents); | |
50 | |
51 OverscanTracker() {} | |
52 ~OverscanTracker() {} | |
53 | |
54 private: | |
55 class OverscanWebObserver; | |
56 | |
57 OverscanWebObserver* GetObserver(content::WebContents* web_contents, | |
58 bool create); | |
59 bool RemoveObserverImpl(content::WebContents* web_contents); | |
60 | |
61 using ObserverMap = | |
62 std::map<content::WebContents*, std::unique_ptr<OverscanWebObserver>>; | |
63 ObserverMap observers_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(OverscanTracker); | |
66 }; | |
67 | |
68 class OverscanTracker::OverscanWebObserver | |
69 : public content::WebContentsObserver { | |
70 public: | |
71 explicit OverscanWebObserver(content::WebContents* web_contents) | |
72 : content::WebContentsObserver(web_contents) {} | |
73 ~OverscanWebObserver() override {} | |
74 | |
75 // WebContentsObserver | |
76 void RenderFrameDeleted( | |
77 content::RenderFrameHost* render_frame_host) override { | |
78 for (const std::string& id : display_ids_) { | |
79 // Reset any uncomitted calibraiton changes and complete calibration to | |
80 // hide the overlay. | |
81 DisplayInfoProvider::Get()->OverscanCalibrationReset(id); | |
82 DisplayInfoProvider::Get()->OverscanCalibrationComplete(id); | |
83 } | |
84 OverscanTracker::RemoveObserver(web_contents()); // Deletes this. | |
85 } | |
86 | |
87 void AddDisplay(const std::string& id) { display_ids_.insert(id); } | |
88 | |
89 void RemoveDisplay(const std::string& id) { | |
90 display_ids_.erase(id); | |
91 if (display_ids_.empty()) | |
92 OverscanTracker::RemoveObserver(web_contents()); // Deletes this. | |
93 } | |
94 | |
95 private: | |
96 std::set<std::string> display_ids_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(OverscanWebObserver); | |
99 }; | |
100 | |
101 static OverscanTracker* s_overscan_tracker = nullptr; | |
asargent_no_longer_on_chrome
2016/12/08 22:03:25
FYI, in many other areas of extensions code, we've
stevenjb
2016/12/08 22:05:52
I tend to use g_ for statics that might be referen
| |
102 | |
103 // static | |
104 void OverscanTracker::AddDisplay(content::WebContents* web_contents, | |
105 const std::string& id) { | |
106 if (!s_overscan_tracker) | |
107 s_overscan_tracker = new OverscanTracker; | |
108 s_overscan_tracker->GetObserver(web_contents, true)->AddDisplay(id); | |
109 } | |
110 | |
111 // static | |
112 void OverscanTracker::RemoveDisplay(content::WebContents* web_contents, | |
113 const std::string& id) { | |
114 if (!s_overscan_tracker) | |
115 return; | |
116 OverscanWebObserver* observer = | |
117 s_overscan_tracker->GetObserver(web_contents, false); | |
118 if (observer) | |
119 observer->RemoveDisplay(id); | |
120 } | |
121 | |
122 // static | |
123 void OverscanTracker::RemoveObserver(content::WebContents* web_contents) { | |
124 if (!s_overscan_tracker) | |
125 return; | |
126 if (s_overscan_tracker->RemoveObserverImpl(web_contents)) { | |
127 delete s_overscan_tracker; | |
128 s_overscan_tracker = nullptr; | |
129 } | |
130 } | |
131 | |
132 OverscanTracker::OverscanWebObserver* OverscanTracker::GetObserver( | |
133 content::WebContents* web_contents, | |
134 bool create) { | |
135 ObserverMap::iterator iter = observers_.find(web_contents); | |
136 if (iter != observers_.end()) | |
137 return iter->second.get(); | |
138 if (!create) | |
139 return nullptr; | |
140 auto owned_observer = base::MakeUnique<OverscanWebObserver>(web_contents); | |
141 auto observer_ptr = owned_observer.get(); | |
142 observers_[web_contents] = std::move(owned_observer); | |
143 return observer_ptr; | |
144 } | |
145 | |
146 bool OverscanTracker::RemoveObserverImpl(content::WebContents* web_contents) { | |
147 observers_.erase(web_contents); | |
148 return observers_.empty(); | |
149 } | |
150 | |
151 } // namespace | |
152 | |
27 bool SystemDisplayFunction::PreRunValidation(std::string* error) { | 153 bool SystemDisplayFunction::PreRunValidation(std::string* error) { |
28 if (!UIThreadExtensionFunction::PreRunValidation(error)) | 154 if (!UIThreadExtensionFunction::PreRunValidation(error)) |
29 return false; | 155 return false; |
30 | 156 |
31 #if !defined(OS_CHROMEOS) | 157 #if !defined(OS_CHROMEOS) |
32 *error = kCrosOnlyError; | 158 *error = kCrosOnlyError; |
33 return false; | 159 return false; |
34 #else | 160 #else |
35 if (!ShouldRestrictToKioskAndWebUI()) | 161 if (!ShouldRestrictToKioskAndWebUI()) |
36 return true; | 162 return true; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 DisplayInfoProvider::Get()->EnableUnifiedDesktop(params->enabled); | 219 DisplayInfoProvider::Get()->EnableUnifiedDesktop(params->enabled); |
94 return RespondNow(NoArguments()); | 220 return RespondNow(NoArguments()); |
95 } | 221 } |
96 | 222 |
97 ExtensionFunction::ResponseAction | 223 ExtensionFunction::ResponseAction |
98 SystemDisplayOverscanCalibrationStartFunction::Run() { | 224 SystemDisplayOverscanCalibrationStartFunction::Run() { |
99 std::unique_ptr<display::OverscanCalibrationStart::Params> params( | 225 std::unique_ptr<display::OverscanCalibrationStart::Params> params( |
100 display::OverscanCalibrationStart::Params::Create(*args_)); | 226 display::OverscanCalibrationStart::Params::Create(*args_)); |
101 if (!DisplayInfoProvider::Get()->OverscanCalibrationStart(params->id)) | 227 if (!DisplayInfoProvider::Get()->OverscanCalibrationStart(params->id)) |
102 return RespondNow(Error("Invalid display ID: " + params->id)); | 228 return RespondNow(Error("Invalid display ID: " + params->id)); |
229 OverscanTracker::AddDisplay(GetSenderWebContents(), params->id); | |
103 return RespondNow(NoArguments()); | 230 return RespondNow(NoArguments()); |
104 } | 231 } |
105 | 232 |
106 ExtensionFunction::ResponseAction | 233 ExtensionFunction::ResponseAction |
107 SystemDisplayOverscanCalibrationAdjustFunction::Run() { | 234 SystemDisplayOverscanCalibrationAdjustFunction::Run() { |
108 std::unique_ptr<display::OverscanCalibrationAdjust::Params> params( | 235 std::unique_ptr<display::OverscanCalibrationAdjust::Params> params( |
109 display::OverscanCalibrationAdjust::Params::Create(*args_)); | 236 display::OverscanCalibrationAdjust::Params::Create(*args_)); |
110 if (!params) | 237 if (!params) |
111 return RespondNow(Error("Invalid parameters")); | 238 return RespondNow(Error("Invalid parameters")); |
112 if (!DisplayInfoProvider::Get()->OverscanCalibrationAdjust(params->id, | 239 if (!DisplayInfoProvider::Get()->OverscanCalibrationAdjust(params->id, |
(...skipping 15 matching lines...) Expand all Loading... | |
128 } | 255 } |
129 | 256 |
130 ExtensionFunction::ResponseAction | 257 ExtensionFunction::ResponseAction |
131 SystemDisplayOverscanCalibrationCompleteFunction::Run() { | 258 SystemDisplayOverscanCalibrationCompleteFunction::Run() { |
132 std::unique_ptr<display::OverscanCalibrationComplete::Params> params( | 259 std::unique_ptr<display::OverscanCalibrationComplete::Params> params( |
133 display::OverscanCalibrationComplete::Params::Create(*args_)); | 260 display::OverscanCalibrationComplete::Params::Create(*args_)); |
134 if (!DisplayInfoProvider::Get()->OverscanCalibrationComplete(params->id)) { | 261 if (!DisplayInfoProvider::Get()->OverscanCalibrationComplete(params->id)) { |
135 return RespondNow( | 262 return RespondNow( |
136 Error("Calibration not started for display ID: " + params->id)); | 263 Error("Calibration not started for display ID: " + params->id)); |
137 } | 264 } |
265 OverscanTracker::RemoveDisplay(GetSenderWebContents(), params->id); | |
138 return RespondNow(NoArguments()); | 266 return RespondNow(NoArguments()); |
139 } | 267 } |
140 | 268 |
141 ExtensionFunction::ResponseAction | 269 ExtensionFunction::ResponseAction |
142 SystemDisplayTouchCalibrationStartFunction::Run() { | 270 SystemDisplayTouchCalibrationStartFunction::Run() { |
143 std::unique_ptr<display::TouchCalibrationStart::Params> params( | 271 std::unique_ptr<display::TouchCalibrationStart::Params> params( |
144 display::TouchCalibrationStart::Params::Create(*args_)); | 272 display::TouchCalibrationStart::Params::Create(*args_)); |
145 if (!DisplayInfoProvider::Get()->IsTouchCalibrationActive(params->id)) { | 273 if (!DisplayInfoProvider::Get()->IsTouchCalibrationActive(params->id)) { |
146 return RespondNow(Error( | 274 return RespondNow(Error( |
147 "Another touch calibration is already active.")); | 275 "Another touch calibration is already active.")); |
(...skipping 26 matching lines...) Expand all Loading... | |
174 if (!DisplayInfoProvider::Get()->IsTouchCalibrationActive(params->id)) { | 302 if (!DisplayInfoProvider::Get()->IsTouchCalibrationActive(params->id)) { |
175 return RespondNow(Error( | 303 return RespondNow(Error( |
176 "Another touch calibration is already active.")); | 304 "Another touch calibration is already active.")); |
177 } | 305 } |
178 if (!DisplayInfoProvider::Get()->TouchCalibrationReset(params->id)) | 306 if (!DisplayInfoProvider::Get()->TouchCalibrationReset(params->id)) |
179 return RespondNow(Error("Invalid display ID: " + params->id)); | 307 return RespondNow(Error("Invalid display ID: " + params->id)); |
180 return RespondNow(NoArguments()); | 308 return RespondNow(NoArguments()); |
181 } | 309 } |
182 | 310 |
183 } // namespace extensions | 311 } // namespace extensions |
OLD | NEW |