OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 <string> |
| 6 |
| 7 #include "ppapi/cpp/instance.h" |
| 8 #include "ppapi/cpp/module.h" |
| 9 #include "ppapi/cpp/url_loader.h" |
| 10 #include "ppapi/cpp/url_request_info.h" |
| 11 #include "ppapi/cpp/url_response_info.h" |
| 12 #include "ppapi/cpp/var.h" |
| 13 #include "ppapi/utility/completion_callback_factory.h" |
| 14 |
| 15 class PnaclUrlLoaderInstance : public pp::Instance { |
| 16 public: |
| 17 explicit PnaclUrlLoaderInstance(PP_Instance instance) |
| 18 : pp::Instance(instance), loader_(this), factory_(this) {} |
| 19 |
| 20 void HandleMessage(const pp::Var& var_message) override { |
| 21 if (var_message.is_string()) { |
| 22 command_ = var_message.AsString(); |
| 23 pp::URLRequestInfo request(this); |
| 24 request.SetMethod("GET"); |
| 25 if (command_.find("Other") != std::string::npos) |
| 26 request.SetURL("https://www.example.com/echo"); |
| 27 else |
| 28 request.SetURL("/echo"); |
| 29 if (command_.find("CORS") != std::string::npos) |
| 30 request.SetAllowCrossOriginRequests(true); |
| 31 if (command_.find("Credentials") != std::string::npos) |
| 32 request.SetAllowCredentials(true); |
| 33 loader_.Open(request, |
| 34 factory_.NewCallback(&PnaclUrlLoaderInstance::OnOpen)); |
| 35 return; |
| 36 } |
| 37 } |
| 38 |
| 39 private: |
| 40 void OnOpen(int32_t result) { PostMessage(pp::Var("OnOpen" + command_)); } |
| 41 |
| 42 pp::URLLoader loader_; |
| 43 pp::CompletionCallbackFactory<PnaclUrlLoaderInstance> factory_; |
| 44 std::string command_; |
| 45 }; |
| 46 |
| 47 class PnaclUrlLoaderModule : public pp::Module { |
| 48 public: |
| 49 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 50 return new PnaclUrlLoaderInstance(instance); |
| 51 } |
| 52 }; |
| 53 |
| 54 namespace pp { |
| 55 |
| 56 __attribute__((visibility("default"))) Module* CreateModule() { |
| 57 return new PnaclUrlLoaderModule(); |
| 58 } |
| 59 |
| 60 } // namespace pp |
OLD | NEW |