OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 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 "ios/chrome/browser/ui/webui/omaha_ui.h" |
| 6 |
| 7 #include "base/memory/weak_ptr.h" |
| 8 #include "base/values.h" |
| 9 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 10 #include "ios/chrome/browser/chrome_url_constants.h" |
| 11 #include "ios/chrome/browser/omaha/omaha_service.h" |
| 12 #include "ios/chrome/grit/ios_resources.h" |
| 13 #include "ios/web/public/web_ui_ios_data_source.h" |
| 14 #include "ios/web/public/webui/web_ui_ios.h" |
| 15 #include "ios/web/public/webui/web_ui_ios_message_handler.h" |
| 16 |
| 17 using web::WebUIIOSMessageHandler; |
| 18 |
| 19 namespace { |
| 20 |
| 21 web::WebUIIOSDataSource* CreateOmahaUIHTMLSource() { |
| 22 web::WebUIIOSDataSource* source = |
| 23 web::WebUIIOSDataSource::Create(kChromeUIOmahaHost); |
| 24 |
| 25 source->SetJsonPath("strings.js"); |
| 26 source->AddResourcePath("omaha.js", IDR_IOS_OMAHA_JS); |
| 27 source->SetDefaultResource(IDR_IOS_OMAHA_HTML); |
| 28 return source; |
| 29 } |
| 30 |
| 31 // OmahaDOMHandler |
| 32 |
| 33 // The handler for Javascript messages for the chrome://omaha/ page. |
| 34 class OmahaDOMHandler : public WebUIIOSMessageHandler { |
| 35 public: |
| 36 OmahaDOMHandler(); |
| 37 ~OmahaDOMHandler() override; |
| 38 |
| 39 // WebUIIOSMessageHandler implementation. |
| 40 void RegisterMessages() override; |
| 41 |
| 42 private: |
| 43 // Asynchronously fetches the debug information. Called from JS. |
| 44 void HandleRequestDebugInformation(const base::ListValue* args); |
| 45 |
| 46 // Called when the debug information have been computed. |
| 47 void OnDebugInformationAvailable(base::DictionaryValue* debug_information); |
| 48 |
| 49 // WeakPtr factory needed because this object might be deleted before |
| 50 // receiving the callbacks from the OmahaService. |
| 51 base::WeakPtrFactory<OmahaDOMHandler> weak_ptr_factory_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(OmahaDOMHandler); |
| 54 }; |
| 55 |
| 56 OmahaDOMHandler::OmahaDOMHandler() : weak_ptr_factory_(this) {} |
| 57 |
| 58 OmahaDOMHandler::~OmahaDOMHandler() {} |
| 59 |
| 60 void OmahaDOMHandler::RegisterMessages() { |
| 61 web_ui()->RegisterMessageCallback( |
| 62 "requestOmahaDebugInformation", |
| 63 base::Bind(&OmahaDOMHandler::HandleRequestDebugInformation, |
| 64 base::Unretained(this))); |
| 65 } |
| 66 |
| 67 void OmahaDOMHandler::HandleRequestDebugInformation( |
| 68 const base::ListValue* args) { |
| 69 OmahaService::GetDebugInformation( |
| 70 base::Bind(&OmahaDOMHandler::OnDebugInformationAvailable, |
| 71 weak_ptr_factory_.GetWeakPtr())); |
| 72 } |
| 73 |
| 74 void OmahaDOMHandler::OnDebugInformationAvailable( |
| 75 base::DictionaryValue* debug_information) { |
| 76 web_ui()->CallJavascriptFunction("updateOmahaDebugInformation", |
| 77 *debug_information); |
| 78 } |
| 79 |
| 80 } // namespace |
| 81 |
| 82 // OmahaUI |
| 83 OmahaUI::OmahaUI(web::WebUIIOS* web_ui) : WebUIIOSController(web_ui) { |
| 84 web_ui->AddMessageHandler(new OmahaDOMHandler()); |
| 85 |
| 86 // Set up the chrome://omaha/ source. |
| 87 ios::ChromeBrowserState* browser_state = |
| 88 ios::ChromeBrowserState::FromWebUIIOS(web_ui); |
| 89 web::WebUIIOSDataSource::Add(browser_state, CreateOmahaUIHTMLSource()); |
| 90 } |
| 91 |
| 92 OmahaUI::~OmahaUI() {} |
OLD | NEW |