| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/webui/instant_ui.h" | 5 #include "chrome/browser/ui/webui/instant_ui.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/time.h" | 10 #include "chrome/browser/instant/instant_service.h" |
| 11 #include "chrome/browser/instant/instant_controller.h" | 11 #include "chrome/browser/instant/instant_service_factory.h" |
| 12 #include "chrome/browser/prefs/pref_registry_syncable.h" | 12 #include "chrome/browser/prefs/pref_registry_syncable.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser.h" | |
| 15 #include "chrome/browser/ui/browser_finder.h" | |
| 16 #include "chrome/browser/ui/browser_instant_controller.h" | |
| 17 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 19 #include "content/public/browser/web_ui.h" | 16 #include "content/public/browser/web_ui.h" |
| 20 #include "content/public/browser/web_ui_data_source.h" | 17 #include "content/public/browser/web_ui_data_source.h" |
| 21 #include "content/public/browser/web_ui_message_handler.h" | 18 #include "content/public/browser/web_ui_message_handler.h" |
| 22 #include "grit/browser_resources.h" | 19 #include "grit/browser_resources.h" |
| 23 | 20 |
| 24 namespace { | 21 namespace { |
| 25 | 22 |
| 26 content::WebUIDataSource* CreateInstantHTMLSource() { | 23 content::WebUIDataSource* CreateInstantHTMLSource() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 44 // This class receives JavaScript messages from the renderer. | 41 // This class receives JavaScript messages from the renderer. |
| 45 // Note that the WebUI infrastructure runs on the UI thread, therefore all of | 42 // Note that the WebUI infrastructure runs on the UI thread, therefore all of |
| 46 // this class's methods are expected to run on the UI thread. | 43 // this class's methods are expected to run on the UI thread. |
| 47 class InstantUIMessageHandler | 44 class InstantUIMessageHandler |
| 48 : public content::WebUIMessageHandler, | 45 : public content::WebUIMessageHandler, |
| 49 public base::SupportsWeakPtr<InstantUIMessageHandler> { | 46 public base::SupportsWeakPtr<InstantUIMessageHandler> { |
| 50 public: | 47 public: |
| 51 InstantUIMessageHandler(); | 48 InstantUIMessageHandler(); |
| 52 virtual ~InstantUIMessageHandler(); | 49 virtual ~InstantUIMessageHandler(); |
| 53 | 50 |
| 54 // WebUIMessageHandler implementation. | 51 private: |
| 52 // Overridden from content::WebUIMessageHandler: |
| 55 virtual void RegisterMessages() OVERRIDE; | 53 virtual void RegisterMessages() OVERRIDE; |
| 56 | 54 |
| 57 private: | |
| 58 void GetPreferenceValue(const base::ListValue* args); | 55 void GetPreferenceValue(const base::ListValue* args); |
| 59 void SetPreferenceValue(const base::ListValue* args); | 56 void SetPreferenceValue(const base::ListValue* args); |
| 60 void GetDebugInfo(const base::ListValue* value); | 57 void GetDebugInfo(const base::ListValue* value); |
| 61 | 58 |
| 62 DISALLOW_COPY_AND_ASSIGN(InstantUIMessageHandler); | 59 DISALLOW_COPY_AND_ASSIGN(InstantUIMessageHandler); |
| 63 }; | 60 }; |
| 64 | 61 |
| 65 InstantUIMessageHandler::InstantUIMessageHandler() {} | 62 InstantUIMessageHandler::InstantUIMessageHandler() { |
| 63 } |
| 66 | 64 |
| 67 InstantUIMessageHandler::~InstantUIMessageHandler() {} | 65 InstantUIMessageHandler::~InstantUIMessageHandler() { |
| 66 } |
| 68 | 67 |
| 69 void InstantUIMessageHandler::RegisterMessages() { | 68 void InstantUIMessageHandler::RegisterMessages() { |
| 70 web_ui()->RegisterMessageCallback( | 69 web_ui()->RegisterMessageCallback( |
| 71 "getPreferenceValue", | 70 "getPreferenceValue", |
| 72 base::Bind(&InstantUIMessageHandler::GetPreferenceValue, | 71 base::Bind(&InstantUIMessageHandler::GetPreferenceValue, |
| 73 base::Unretained(this))); | 72 base::Unretained(this))); |
| 74 web_ui()->RegisterMessageCallback( | 73 web_ui()->RegisterMessageCallback( |
| 75 "setPreferenceValue", | 74 "setPreferenceValue", |
| 76 base::Bind(&InstantUIMessageHandler::SetPreferenceValue, | 75 base::Bind(&InstantUIMessageHandler::SetPreferenceValue, |
| 77 base::Unretained(this))); | 76 base::Unretained(this))); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 101 if (pref_name == prefs::kInstantUIZeroSuggestUrlPrefix) { | 100 if (pref_name == prefs::kInstantUIZeroSuggestUrlPrefix) { |
| 102 std::string value; | 101 std::string value; |
| 103 if (!args->GetString(1, &value)) | 102 if (!args->GetString(1, &value)) |
| 104 return; | 103 return; |
| 105 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | 104 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); |
| 106 prefs->SetString(pref_name.c_str(), value); | 105 prefs->SetString(pref_name.c_str(), value); |
| 107 } | 106 } |
| 108 } | 107 } |
| 109 | 108 |
| 110 void InstantUIMessageHandler::GetDebugInfo(const base::ListValue* args) { | 109 void InstantUIMessageHandler::GetDebugInfo(const base::ListValue* args) { |
| 111 #if !defined(OS_ANDROID) | 110 InstantService* service = |
| 112 typedef std::pair<int64, std::string> DebugEvent; | 111 InstantServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
| 113 | 112 if (!service) |
| 114 if (!web_ui()->GetWebContents()) | |
| 115 return; | |
| 116 Browser* browser = chrome::FindBrowserWithWebContents( | |
| 117 web_ui()->GetWebContents()); | |
| 118 if (!browser || !browser->instant_controller()) | |
| 119 return; | 113 return; |
| 120 | 114 |
| 121 InstantController* instant = browser->instant_controller()->instant(); | 115 typedef std::list<std::pair<int64, std::string> > DebugEventList; |
| 122 const std::list<DebugEvent>& events = instant->debug_events(); | 116 const DebugEventList& events = service->debug_events(); |
| 123 | 117 |
| 124 base::DictionaryValue data; | 118 base::DictionaryValue data; |
| 125 base::ListValue* entries = new base::ListValue(); | 119 base::ListValue* entries = new base::ListValue(); |
| 126 for (std::list<DebugEvent>::const_iterator it = events.begin(); | 120 for (DebugEventList::const_iterator it = events.begin(); it != events.end(); |
| 127 it != events.end(); ++it) { | 121 ++it) { |
| 128 base::DictionaryValue* entry = new base::DictionaryValue(); | 122 base::DictionaryValue* entry = new base::DictionaryValue(); |
| 129 entry->SetString("time", FormatTime(it->first)); | 123 entry->SetString("time", FormatTime(it->first)); |
| 130 entry->SetString("text", it->second); | 124 entry->SetString("text", it->second); |
| 131 entries->Append(entry); | 125 entries->Append(entry); |
| 132 } | 126 } |
| 133 data.Set("entries", entries); | 127 data.Set("entries", entries); |
| 134 | 128 |
| 135 web_ui()->CallJavascriptFunction("instantConfig.getDebugInfoResult", data); | 129 web_ui()->CallJavascriptFunction("instantConfig.getDebugInfoResult", data); |
| 136 #endif | |
| 137 } | 130 } |
| 138 | 131 |
| 139 } // namespace | 132 } // namespace |
| 140 | 133 |
| 141 //////////////////////////////////////////////////////////////////////////////// | 134 //////////////////////////////////////////////////////////////////////////////// |
| 142 // InstantUI | 135 // InstantUI |
| 143 | 136 |
| 144 InstantUI::InstantUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 137 InstantUI::InstantUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| 145 web_ui->AddMessageHandler(new InstantUIMessageHandler()); | 138 web_ui->AddMessageHandler(new InstantUIMessageHandler()); |
| 146 | 139 |
| 147 // Set up the chrome://instant/ source. | 140 // Set up the chrome://instant/ source. |
| 148 Profile* profile = Profile::FromWebUI(web_ui); | 141 Profile* profile = Profile::FromWebUI(web_ui); |
| 149 content::WebUIDataSource::Add(profile, CreateInstantHTMLSource()); | 142 content::WebUIDataSource::Add(profile, CreateInstantHTMLSource()); |
| 150 } | 143 } |
| 151 | 144 |
| 145 InstantUI::~InstantUI() { |
| 146 } |
| 147 |
| 152 // static | 148 // static |
| 153 void InstantUI::RegisterUserPrefs(PrefRegistrySyncable* registry) { | 149 void InstantUI::RegisterUserPrefs(PrefRegistrySyncable* registry) { |
| 154 registry->RegisterStringPref(prefs::kInstantUIZeroSuggestUrlPrefix, "", | 150 registry->RegisterStringPref(prefs::kInstantUIZeroSuggestUrlPrefix, "", |
| 155 PrefRegistrySyncable::UNSYNCABLE_PREF); | 151 PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 156 } | 152 } |
| OLD | NEW |