Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: extensions/browser/extension_function.h

Issue 2017113002: [Extensions] DCHECK that ExtensionFunctions respond (and only once) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/browser/api_test_utils.cc ('k') | extensions/browser/extension_function.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #ifndef EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ 5 #ifndef EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ 6 #define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <list> 10 #include <list>
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 do { \ 58 do { \
59 if (!(test)) { \ 59 if (!(test)) { \
60 this->bad_message_ = true; \ 60 this->bad_message_ = true; \
61 return ValidationFailure(this); \ 61 return ValidationFailure(this); \
62 } \ 62 } \
63 } while (0) 63 } while (0)
64 #else // NDEBUG 64 #else // NDEBUG
65 #define EXTENSION_FUNCTION_VALIDATE(test) CHECK(test) 65 #define EXTENSION_FUNCTION_VALIDATE(test) CHECK(test)
66 #endif // NDEBUG 66 #endif // NDEBUG
67 67
68 #ifdef NDEBUG
69 #define EXTENSION_FUNCTION_PRERUN_VALIDATE(test) \
70 do { \
71 if (!(test)) { \
72 this->bad_message_ = true; \
73 return false; \
74 } \
75 } while (0)
76 #else // NDEBUG
77 #define EXTENSION_FUNCTION_PRERUN_VALIDATE(test) CHECK(test)
78 #endif // NDEBUG
79
68 #define EXTENSION_FUNCTION_ERROR(error) \ 80 #define EXTENSION_FUNCTION_ERROR(error) \
69 do { \ 81 do { \
70 error_ = error; \ 82 error_ = error; \
71 this->bad_message_ = true; \ 83 this->bad_message_ = true; \
72 return ValidationFailure(this); \ 84 return ValidationFailure(this); \
73 } while (0) 85 } while (0)
74 86
75 // Declares a callable extension function with the given |name|. You must also 87 // Declares a callable extension function with the given |name|. You must also
76 // supply a unique |histogramvalue| used for histograms of extension function 88 // supply a unique |histogramvalue| used for histograms of extension function
77 // invocation (add new ones at the end of the enum in 89 // invocation (add new ones at the end of the enum in
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 162
151 // Helper class for tests to force all ExtensionFunction::user_gesture() 163 // Helper class for tests to force all ExtensionFunction::user_gesture()
152 // calls to return true as long as at least one instance of this class 164 // calls to return true as long as at least one instance of this class
153 // exists. 165 // exists.
154 class ScopedUserGestureForTests { 166 class ScopedUserGestureForTests {
155 public: 167 public:
156 ScopedUserGestureForTests(); 168 ScopedUserGestureForTests();
157 ~ScopedUserGestureForTests(); 169 ~ScopedUserGestureForTests();
158 }; 170 };
159 171
172 // Called before Run() in order to perform a common verification check so that
173 // APIs subclassing this don't have to roll their own RunSafe() variants.
174 // If this returns false, then Run() is never called, and the function
175 // responds immediately with an error (note that error must be non-empty in
176 // this case). If this returns true, execution continues on to Run().
177 virtual bool PreRunValidation(std::string* error);
178
179 // Runs the extension function if PreRunValidation() succeeds.
180 ResponseAction RunWithValidation();
181
160 // Runs the function and returns the action to take when the caller is ready 182 // Runs the function and returns the action to take when the caller is ready
161 // to respond. 183 // to respond.
162 // 184 //
163 // Typical return values might be: 185 // Typical return values might be:
164 // * RespondNow(NoArguments()) 186 // * RespondNow(NoArguments())
165 // * RespondNow(OneArgument(42)) 187 // * RespondNow(OneArgument(42))
166 // * RespondNow(ArgumentList(my_result.ToValue())) 188 // * RespondNow(ArgumentList(my_result.ToValue()))
167 // * RespondNow(Error("Warp core breach")) 189 // * RespondNow(Error("Warp core breach"))
168 // * RespondNow(Error("Warp core breach on *", GetURL())) 190 // * RespondNow(Error("Warp core breach on *", GetURL()))
169 // * RespondLater(), then later, 191 // * RespondLater(), then later,
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 return source_context_type_; 303 return source_context_type_;
282 } 304 }
283 305
284 void set_source_process_id(int source_process_id) { 306 void set_source_process_id(int source_process_id) {
285 source_process_id_ = source_process_id; 307 source_process_id_ = source_process_id;
286 } 308 }
287 int source_process_id() const { 309 int source_process_id() const {
288 return source_process_id_; 310 return source_process_id_;
289 } 311 }
290 312
313 // Sets did_respond_ to true so that the function won't DCHECK if it never
314 // sends a response. Typically, this shouldn't be used, even in testing. It's
315 // only for when you want to test functionality that doesn't exercise the
316 // Run() aspect of an extension function.
317 void ignore_did_respond_for_testing() { did_respond_ = true; }
318 // Same as above, but global. Yuck. Do not add any more uses of this.
319 static bool ignore_all_did_respond_for_testing_do_not_use;
320
291 protected: 321 protected:
292 friend struct ExtensionFunctionDeleteTraits; 322 friend struct ExtensionFunctionDeleteTraits;
293 323
294 // ResponseValues. 324 // ResponseValues.
295 // 325 //
296 // Success, no arguments to pass to caller. 326 // Success, no arguments to pass to caller.
297 ResponseValue NoArguments(); 327 ResponseValue NoArguments();
298 // Success, a single argument |arg| to pass to caller. TAKES OWNERSHIP - a 328 // Success, a single argument |arg| to pass to caller. TAKES OWNERSHIP - a
299 // raw pointer for convenience, since callers usually construct the argument 329 // raw pointer for convenience, since callers usually construct the argument
300 // to this by hand. 330 // to this by hand.
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 // The ID of the tab triggered this function call, or -1 if there is no tab. 462 // The ID of the tab triggered this function call, or -1 if there is no tab.
433 int source_tab_id_; 463 int source_tab_id_;
434 464
435 // The type of the JavaScript context where this call originated. 465 // The type of the JavaScript context where this call originated.
436 extensions::Feature::Context source_context_type_; 466 extensions::Feature::Context source_context_type_;
437 467
438 // The process ID of the page that triggered this function call, or -1 468 // The process ID of the page that triggered this function call, or -1
439 // if unknown. 469 // if unknown.
440 int source_process_id_; 470 int source_process_id_;
441 471
472 // Whether this function has responded.
473 bool did_respond_;
474
442 private: 475 private:
443 base::ElapsedTimer timer_; 476 base::ElapsedTimer timer_;
444 477
445 void OnRespondingLater(ResponseValue response); 478 void OnRespondingLater(ResponseValue response);
446 479
447 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction); 480 DISALLOW_COPY_AND_ASSIGN(ExtensionFunction);
448 }; 481 };
449 482
450 // Extension functions that run on the UI thread. Most functions fall into 483 // Extension functions that run on the UI thread. Most functions fall into
451 // this category. 484 // this category.
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 private: 715 private:
683 // If you're hitting a compile error here due to "final" - great! You're 716 // If you're hitting a compile error here due to "final" - great! You're
684 // doing the right thing, you just need to extend IOThreadExtensionFunction 717 // doing the right thing, you just need to extend IOThreadExtensionFunction
685 // instead of SyncIOExtensionFunction. 718 // instead of SyncIOExtensionFunction.
686 ResponseAction Run() final; 719 ResponseAction Run() final;
687 720
688 DISALLOW_COPY_AND_ASSIGN(SyncIOThreadExtensionFunction); 721 DISALLOW_COPY_AND_ASSIGN(SyncIOThreadExtensionFunction);
689 }; 722 };
690 723
691 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_ 724 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_H_
OLDNEW
« no previous file with comments | « extensions/browser/api_test_utils.cc ('k') | extensions/browser/extension_function.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698