summaryrefslogtreecommitdiffstats
path: root/chrome/browser/dom_ui
diff options
context:
space:
mode:
authortim@chromium.org <tim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-05 01:18:27 +0000
committertim@chromium.org <tim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-05 01:18:27 +0000
commit132c8565a63ad57d680b6b8d9beaa28786a46ea8 (patch)
tree428425605547842abd6a7ab851bd27132f11efde /chrome/browser/dom_ui
parentf902600776feea6570c876f2349bb4a8746ea95b (diff)
downloadchromium_src-132c8565a63ad57d680b6b8d9beaa28786a46ea8.zip
chromium_src-132c8565a63ad57d680b6b8d9beaa28786a46ea8.tar.gz
chromium_src-132c8565a63ad57d680b6b8d9beaa28786a46ea8.tar.bz2
Add files to browser/sync and tweak includes.
Create browser/sync/glue and /engine. Create sync watchlist and add a few folks. No GYP change here so no build changes should occur. chrome.gyp CL is coming shortly, as well as live_sync tests. Review URL: http://codereview.chromium.org/160598 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@22454 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/dom_ui')
-rw-r--r--chrome/browser/dom_ui/chrome_url_data_manager.cc12
-rw-r--r--chrome/browser/dom_ui/dom_ui_factory.cc2
-rw-r--r--chrome/browser/dom_ui/new_tab_page_sync_handler.cc200
-rw-r--r--chrome/browser/dom_ui/new_tab_page_sync_handler.h61
-rw-r--r--chrome/browser/dom_ui/new_tab_ui.cc6
5 files changed, 271 insertions, 10 deletions
diff --git a/chrome/browser/dom_ui/chrome_url_data_manager.cc b/chrome/browser/dom_ui/chrome_url_data_manager.cc
index 36fde4d..dfc08ce 100644
--- a/chrome/browser/dom_ui/chrome_url_data_manager.cc
+++ b/chrome/browser/dom_ui/chrome_url_data_manager.cc
@@ -12,6 +12,12 @@
#include "base/thread.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
+#ifdef CHROME_PERSONALIZATION
+// TODO(timsteele): Remove all CHROME_PERSONALIZATION code in this file.
+// It is only temporarily needed to configure some personalization data sources
+// that will go away soon.
+#include "chrome/browser/sync/personalization.h"
+#endif
#include "chrome/common/chrome_paths.h"
#include "chrome/common/ref_counted_util.h"
#include "chrome/common/url_constants.h"
@@ -21,12 +27,6 @@
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_file_job.h"
#include "net/url_request/url_request_job.h"
-#ifdef CHROME_PERSONALIZATION
-// TODO(timsteele): Remove all CHROME_PERSONALIZATION code in this file.
-// It is only temporarily needed to configure some personalization data sources
-// that will go away soon.
-#include "chrome/personalization/personalization.h"
-#endif
#include "grit/locale_settings.h"
diff --git a/chrome/browser/dom_ui/dom_ui_factory.cc b/chrome/browser/dom_ui/dom_ui_factory.cc
index d58dc48..af214c6 100644
--- a/chrome/browser/dom_ui/dom_ui_factory.cc
+++ b/chrome/browser/dom_ui/dom_ui_factory.cc
@@ -17,7 +17,7 @@
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/url_constants.h"
#ifdef CHROME_PERSONALIZATION
-#include "chrome/personalization/personalization.h"
+#include "chrome/browser/sync/personalization.h"
#endif
#include "googleurl/src/gurl.h"
diff --git a/chrome/browser/dom_ui/new_tab_page_sync_handler.cc b/chrome/browser/dom_ui/new_tab_page_sync_handler.cc
new file mode 100644
index 0000000..3a30e03a
--- /dev/null
+++ b/chrome/browser/dom_ui/new_tab_page_sync_handler.cc
@@ -0,0 +1,200 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifdef CHROME_PERSONALIZATION
+
+#include "chrome/browser/dom_ui/new_tab_page_sync_handler.h"
+
+#include "base/json_writer.h"
+#include "base/string_util.h"
+#include "base/values.h"
+#include "chrome/browser/profile.h"
+#include "chrome/browser/renderer_host/render_view_host.h"
+#include "chrome/browser/sync/personalization.h"
+#include "chrome/browser/sync/personalization_strings.h"
+#include "chrome/browser/tab_contents/tab_contents.h"
+#include "chrome/common/pref_names.h"
+#include "chrome/common/pref_service.h"
+#include "grit/browser_resources.h"
+#include "net/base/cookie_monster.h"
+#include "net/url_request/url_request_context.h"
+
+// XPath expression for finding our p13n iframe.
+static const wchar_t* kP13nIframeXpath = L"//iframe[@id='p13n']";
+
+namespace Personalization {
+
+// A simple pair of fields that identify a set of Google cookies, used to
+// filter from a larger set.
+struct GoogleCookieFilter {
+ // The generalized, fully qualified URL of pages where
+ // cookies with id |cookie_id| are obtained / accessed.
+ const char* url;
+ // The id of the cookie this filter is selecting,
+ // with name/value delimiter (i.e '=').
+ const char* cookie_id;
+};
+
+// Filters to select Google GAIA cookies.
+static const GoogleCookieFilter kGAIACookieFilters[] = {
+ { "http://.google.com/", "SID=" }, // Gmail.
+ // Add filters here for other interesting cookies that should result in
+ // showing the promotions (e.g ASIDAS for dasher accounts).
+};
+
+bool IsGoogleGAIACookieInstalled() {
+ for (size_t i = 0; i < arraysize(kGAIACookieFilters); ++i) {
+ URLRequestContext* context = Profile::GetDefaultRequestContext();
+ net::CookieStore* store = context->cookie_store();
+ GURL url(kGAIACookieFilters[i].url);
+ net::CookieOptions options;
+ options.set_include_httponly(); // The SID cookie might be httponly.
+ std::string cookies = store->GetCookiesWithOptions(url, options);
+ std::vector<std::string> cookie_list;
+ SplitString(cookies, ';', &cookie_list);
+ for (std::vector<std::string>::iterator current = cookie_list.begin();
+ current != cookie_list.end();
+ ++current) {
+ size_t position =
+ current->find(kGAIACookieFilters[i].cookie_id);
+ if (0 == position)
+ return true;
+ }
+ }
+ return false;
+}
+
+} // namespace Personalization
+
+NewTabPageSyncHandler::NewTabPageSyncHandler() : sync_service_(NULL),
+ waiting_for_initial_page_load_(true) {
+}
+
+NewTabPageSyncHandler::~NewTabPageSyncHandler() {
+ sync_service_->RemoveObserver(this);
+}
+
+DOMMessageHandler* NewTabPageSyncHandler::Attach(DOMUI* dom_ui) {
+ Profile* p = dom_ui->GetProfile();
+ sync_service_ = p->GetProfilePersonalization()->sync_service();
+ DCHECK(sync_service_);
+ sync_service_->AddObserver(this);
+ return DOMMessageHandler::Attach(dom_ui);
+}
+
+void NewTabPageSyncHandler::RegisterMessages() {
+ dom_ui_->RegisterMessageCallback("GetSyncMessage",
+ NewCallback(this, &NewTabPageSyncHandler::HandleGetSyncMessage));
+ dom_ui_->RegisterMessageCallback("SyncLinkClicked",
+ NewCallback(this, &NewTabPageSyncHandler::HandleSyncLinkClicked));
+ dom_ui_->RegisterMessageCallback("ResizeP13N",
+ NewCallback(this, &NewTabPageSyncHandler::HandleResizeP13N));
+}
+
+void NewTabPageSyncHandler::HandleResizeP13N(const Value* value) {
+ // We just want to call back in to the new tab page on behalf of our
+ // same-origin-policy crippled iframe, to tell it to resize the container.
+ dom_ui_->CallJavascriptFunction(L"resizeP13N", *value);
+}
+
+void NewTabPageSyncHandler::BuildAndSendSyncStatus() {
+ DCHECK(!waiting_for_initial_page_load_);
+
+ if (!sync_service_->IsSyncEnabledByUser() &&
+ !sync_service_->SetupInProgress()) {
+ // Clear the page status, without showing the promotion or sync ui.
+ // TODO(timsteele): This is fine, but if the page is refreshed or another
+ // NTP is opened, we could end up showing the promo again. Not sure this is
+ // desired if the user already signed up once and disabled.
+ FundamentalValue value(0);
+ dom_ui_->CallJavascriptFunction(L"resizeP13N", value);
+ return;
+ }
+
+ // There are currently three supported "sync statuses" for the NTP, from
+ // the users perspective:
+ // "Synced to foo@gmail.com", when we are successfully authenticated and
+ // connected to a sync server.
+ // "Sync error", when we can't authenticate or establish a connection with
+ // the sync server (appropriate information appended to
+ // message).
+ // "Authenticating", when credentials are in flight.
+ SyncStatusUIHelper::MessageType type(SyncStatusUIHelper::PRE_SYNCED);
+ std::wstring status_msg;
+ std::wstring link_text;
+ type = SyncStatusUIHelper::GetLabels(sync_service_, &status_msg, &link_text);
+ SendSyncMessageToPage(type, WideToUTF8(status_msg), WideToUTF8(link_text));
+}
+
+void NewTabPageSyncHandler::HandleGetSyncMessage(const Value* value) {
+ waiting_for_initial_page_load_ = false;
+
+ if (!sync_service_->IsSyncEnabledByUser() &&
+ !sync_service_->SetupInProgress()) {
+ if (Personalization::IsGoogleGAIACookieInstalled()) {
+ // Sync has not been enabled, and the user has logged in to GAIA.
+ SendSyncMessageToPage(SyncStatusUIHelper::PRE_SYNCED, kSyncPromotionMsg,
+ kStartNowLinkText);
+ }
+ return;
+ }
+
+ BuildAndSendSyncStatus();
+}
+
+void NewTabPageSyncHandler::HandleSyncLinkClicked(const Value* value) {
+ DCHECK(!waiting_for_initial_page_load_);
+ if (sync_service_->IsSyncEnabledByUser()) {
+ // User clicked 'Login again' link to re-authenticate.
+ sync_service_->ShowLoginDialog();
+ } else {
+ // User clicked "Start now" link to begin syncing.
+ sync_service_->EnableForUser();
+ }
+}
+
+void NewTabPageSyncHandler::OnStateChanged() {
+ // Don't do anything if the page has not yet loaded.
+ if (waiting_for_initial_page_load_)
+ return;
+ BuildAndSendSyncStatus();
+}
+
+void NewTabPageSyncHandler::SendSyncMessageToPage(
+ SyncStatusUIHelper::MessageType type, std::string msg,
+ const std::string& linktext) {
+ DictionaryValue value;
+ std::string title, msgtype;
+ switch (type) {
+ case SyncStatusUIHelper::PRE_SYNCED:
+ title = kSyncSectionTitle;
+ msgtype = "presynced";
+ break;
+ case SyncStatusUIHelper::SYNCED:
+ msgtype = "synced";
+ msg = msg.substr(0, msg.find(WideToUTF8(kLastSyncedLabel)));
+ break;
+ case SyncStatusUIHelper::SYNC_ERROR:
+ title = kSyncErrorSectionTitle;
+ msgtype = "error";
+ break;
+ }
+
+ value.SetString(L"title", title);
+ value.SetString(L"msg", msg);
+ value.SetString(L"msgtype", msgtype);
+ if (!linktext.empty())
+ value.SetString(L"linktext", linktext);
+ else
+ value.SetBoolean(L"linktext", false);
+
+ std::string json;
+ JSONWriter::Write(&value, false, &json);
+ std::wstring javascript = std::wstring(L"renderSyncMessage") +
+ L"(" + UTF8ToWide(json) + L");";
+ RenderViewHost* rvh = dom_ui_->tab_contents()->render_view_host();
+ rvh->ExecuteJavascriptInWebFrame(kP13nIframeXpath, javascript);
+}
+
+#endif // CHROME_PERSONALIZATION
diff --git a/chrome/browser/dom_ui/new_tab_page_sync_handler.h b/chrome/browser/dom_ui/new_tab_page_sync_handler.h
new file mode 100644
index 0000000..2f50a2a
--- /dev/null
+++ b/chrome/browser/dom_ui/new_tab_page_sync_handler.h
@@ -0,0 +1,61 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifdef CHROME_PERSONALIZATION
+
+#ifndef CHROME_BROWSER_DOM_UI_NEW_TAB_PAGE_SYNC_HANDLER_H_
+#define CHROME_BROWSER_DOM_UI_NEW_TAB_PAGE_SYNC_HANDLER_H_
+
+#include <string>
+
+#include "chrome/browser/dom_ui/dom_ui.h"
+#include "chrome/browser/sync/profile_sync_service.h"
+#include "chrome/browser/sync/sync_status_ui_helper.h"
+
+class Value;
+
+// Sends sync-state changes to the New Tab Page for UI updating and forwards
+// link clicks on the page to the sync service.
+class NewTabPageSyncHandler : public DOMMessageHandler,
+ public ProfileSyncServiceObserver {
+ public:
+ NewTabPageSyncHandler();
+ virtual ~NewTabPageSyncHandler();
+
+ // DOMMessageHandler implementation.
+ virtual DOMMessageHandler* Attach(DOMUI* dom_ui);
+ virtual void RegisterMessages();
+
+ // Callback for "GetSyncMessage".
+ void HandleGetSyncMessage(const Value* value);
+ // Callback for "SyncLinkClicked".
+ void HandleSyncLinkClicked(const Value* value);
+ // Callback for "ResizeP13N"
+ void HandleResizeP13N(const Value* value);
+
+ // ProfileSyncServiceObserver
+ virtual void OnStateChanged();
+
+ private:
+ // Helper to invoke |renderSyncMessage| JS function on the new tab page.
+ void SendSyncMessageToPage(SyncStatusUIHelper::MessageType type,
+ std::string msg, const std::string& linktext);
+
+ // Helper to query the sync service and figure out what to send to
+ // the page, and send it via SendSyncMessageToPage.
+ // NOTE: precondition: sync must be enabled.
+ void BuildAndSendSyncStatus();
+
+ // Cached pointer to ProfileSyncService.
+ ProfileSyncService* sync_service_;
+
+ // Used to make sure we don't register ourselves twice if the user refreshes
+ // the new tab page.
+ bool waiting_for_initial_page_load_;
+
+ DISALLOW_COPY_AND_ASSIGN(NewTabPageSyncHandler);
+};
+
+#endif // CHROME_BROWSER_DOM_UI_NEW_TAB_PAGE_SYNC_HANDLER_H_
+#endif // CHROME_PERSONALIZATION
diff --git a/chrome/browser/dom_ui/new_tab_ui.cc b/chrome/browser/dom_ui/new_tab_ui.cc
index f8f4fda..d5463a5 100644
--- a/chrome/browser/dom_ui/new_tab_ui.cc
+++ b/chrome/browser/dom_ui/new_tab_ui.cc
@@ -36,6 +36,9 @@
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/sessions/tab_restore_service.h"
+#ifdef CHROME_PERSONALIZATION
+#include "chrome/browser/sync/personalization.h"
+#endif
#include "chrome/browser/user_data_manager.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/jstemplate_builder.h"
@@ -43,9 +46,6 @@
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "chrome/common/url_constants.h"
-#ifdef CHROME_PERSONALIZATION
-#include "chrome/personalization/personalization.h"
-#endif
#include "grit/browser_resources.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"