OLD | NEW |
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 #include "components/dom_distiller/webui/dom_distiller_handler.h" | 5 #include "components/dom_distiller/webui/dom_distiller_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "components/dom_distiller/core/dom_distiller_service.h" |
| 10 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
9 #include "content/public/browser/web_ui.h" | 11 #include "content/public/browser/web_ui.h" |
| 12 #include "url/gurl.h" |
10 | 13 |
11 namespace dom_distiller { | 14 namespace dom_distiller { |
12 | 15 |
13 DomDistillerHandler::DomDistillerHandler() | 16 DomDistillerHandler::DomDistillerHandler(DomDistillerService* service, |
14 : weak_ptr_factory_(this) { | 17 const std::string& scheme) |
| 18 : weak_ptr_factory_(this), |
| 19 service_(service) { |
| 20 article_scheme_ = scheme; |
15 } | 21 } |
16 | 22 |
17 DomDistillerHandler::~DomDistillerHandler() {} | 23 DomDistillerHandler::~DomDistillerHandler() {} |
18 | 24 |
19 void DomDistillerHandler::RegisterMessages() { | 25 void DomDistillerHandler::RegisterMessages() { |
20 web_ui()->RegisterMessageCallback( | 26 web_ui()->RegisterMessageCallback( |
21 "requestEntries", | 27 "requestEntries", |
22 base::Bind(&DomDistillerHandler::HandleRequestEntries, | 28 base::Bind(&DomDistillerHandler::HandleRequestEntries, |
23 base::Unretained(this))); | 29 base::Unretained(this))); |
| 30 web_ui()->RegisterMessageCallback( |
| 31 "addArticle", |
| 32 base::Bind(&DomDistillerHandler::HandleAddArticle, |
| 33 base::Unretained(this))); |
| 34 web_ui()->RegisterMessageCallback( |
| 35 "selectArticle", |
| 36 base::Bind(&DomDistillerHandler::HandleSelectArticle, |
| 37 base::Unretained(this))); |
| 38 } |
| 39 |
| 40 void DomDistillerHandler::HandleAddArticle(const ListValue* args) { |
| 41 std::string url; |
| 42 args->GetString(0, &url); |
| 43 GURL gurl(url); |
| 44 if (gurl.is_valid()) |
| 45 service_->AddToList(gurl); |
| 46 else |
| 47 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); |
| 48 } |
| 49 |
| 50 void DomDistillerHandler::HandleSelectArticle(const ListValue* args) { |
| 51 std::string entry_id; |
| 52 args->GetString(0, &entry_id); |
| 53 |
| 54 // TODO(nyquist): Do something here. |
24 } | 55 } |
25 | 56 |
26 void DomDistillerHandler::HandleRequestEntries(const ListValue* args) { | 57 void DomDistillerHandler::HandleRequestEntries(const ListValue* args) { |
27 base::ListValue entries; | 58 base::ListValue entries; |
28 | 59 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries(); |
29 // Add some temporary placeholder entries. | 60 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin(); |
30 scoped_ptr<base::DictionaryValue> entry1(new base::DictionaryValue()); | 61 it != entries_specifics.end(); |
31 entry1->SetString("title", "Google"); | 62 ++it) { |
32 entry1->SetString("url", "http://www.google.com/"); | 63 const ArticleEntry& article = *it; |
33 entries.Append(entry1.release()); | 64 DCHECK(IsEntryValid(article)); |
34 scoped_ptr<base::DictionaryValue> entry2(new base::DictionaryValue()); | 65 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue()); |
35 entry2->SetString("title", "Chrome"); | 66 entry->SetString("entry_id", article.entry_id()); |
36 entry2->SetString("url", "http://www.chrome.com/"); | 67 std::string title = (!article.has_title() || article.title().empty()) ? |
37 entries.Append(entry2.release()); | 68 article.entry_id() : article.title(); |
38 | 69 entry->SetString("title", title); |
39 web_ui()->CallJavascriptFunction("onGotEntries", entries); | 70 entries.Append(entry.release()); |
| 71 } |
| 72 web_ui()->CallJavascriptFunction("domDistiller.onReceivedEntries", entries); |
40 } | 73 } |
41 | 74 |
42 } // namespace dom_distiller | 75 } // namespace dom_distiller |
OLD | NEW |